Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix scanner max buffer size in env collector #626

Merged
merged 2 commits into from
Jul 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions internal/command/env_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
const (
envStartFileName = ".env_start"
envEndFileName = ".env_end"

maxScannerBufferSizeInBytes = 1024 * 1024 * 1024
)

// EnvDumpCommand is a command that dumps the environment variables.
Expand Down Expand Up @@ -133,7 +135,18 @@ func (c *shellEnvCollector) readEnvFromFile(name string) (result []string, _ err
}
defer func() { _ = f.Close() }()

fileInfo, err := f.Stat()
if err != nil {
return nil, errors.WithMessagef(err, "failed to get the file info of the env file %q", name)
}

scannerBufferSizeInBytes := fileInfo.Size()
if scannerBufferSizeInBytes > maxScannerBufferSizeInBytes {
return nil, errors.Errorf("the env file %q is too big: %d bytes", name, scannerBufferSizeInBytes)
}

scanner := bufio.NewScanner(f)
scanner.Buffer(make([]byte, 4096), int(scannerBufferSizeInBytes)) // 4096 is taken from bufio as the initial buffer size
scanner.Split(splitNull)

for scanner.Scan() {
Expand Down
10 changes: 7 additions & 3 deletions internal/project/projectservice/project_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func TestProjectServiceServer_Load(t *testing.T) {
})
}

func TestProjectServiceServer_Load_ErrorWhileSending(t *testing.T) {
func TestProjectServiceServer_Load_ClientConnClosed(t *testing.T) {
t.Parallel()

temp := t.TempDir()
Expand All @@ -98,8 +98,10 @@ func TestProjectServiceServer_Load_ErrorWhileSending(t *testing.T) {
loadClient, err := client.Load(context.Background(), req)
require.NoError(t, err)

err = clientConn.Close()
require.NoError(t, err)
errc := make(chan error, 1)
go func() {
errc <- clientConn.Close()
}()

for {
_, err := loadClient.Recv()
Expand All @@ -108,6 +110,8 @@ func TestProjectServiceServer_Load_ErrorWhileSending(t *testing.T) {
break
}
}

require.NoError(t, <-errc)
}

func collectLoadEventTypes(client projectv1.ProjectService_LoadClient) ([]projectv1.LoadEventType, error) {
Expand Down
Loading