Skip to content

Commit 8e4ecdb

Browse files
committed
feat: copilot ifxes
Signed-off-by: Gustavo Carvalho <gustavo.carvalho@container-solutions.com>
1 parent baf7c2d commit 8e4ecdb

2 files changed

Lines changed: 44 additions & 10 deletions

File tree

main.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@ import (
2929
)
3030

3131
const (
32-
defaultCheckTimeoutSeconds = 300
33-
schemaVersionV1 = "v1"
34-
sourceCloudCustodian = "cloud-custodian"
35-
defaultRemotePolicyTimeout = 30 * time.Second
32+
defaultCheckTimeoutSeconds = 300
33+
schemaVersionV1 = "v1"
34+
sourceCloudCustodian = "cloud-custodian"
35+
defaultRemotePolicyTimeout = 30 * time.Second
36+
defaultMaxRemotePolicyBytes = 1 << 20 // 1 MiB
3637
)
3738

3839
var lookPath = exec.LookPath
@@ -261,9 +262,13 @@ func (e *CommandCustodianExecutor) Execute(ctx context.Context, req CustodianExe
261262
result.Err = fmt.Errorf("custodian execution failed: %w", err)
262263
result.Errors = append(result.Errors, result.Err.Error())
263264
}
264-
if runCtx.Err() != nil {
265-
result.Err = errors.Join(result.Err, runCtx.Err())
266-
result.Errors = append(result.Errors, runCtx.Err().Error())
265+
if runErr := runCtx.Err(); runErr != nil {
266+
// Avoid duplicating context timeout/cancel errors when cmd.Run already
267+
// returned an error that wraps the same context failure.
268+
if err == nil || !errors.Is(err, runErr) {
269+
result.Err = errors.Join(result.Err, runErr)
270+
result.Errors = append(result.Errors, runErr.Error())
271+
}
267272
}
268273
if resourcesErr != nil {
269274
result.Err = errors.Join(result.Err, resourcesErr)
@@ -595,11 +600,17 @@ func resolvePoliciesYAML(ctx context.Context, inlineYAML string, policiesPath st
595600
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
596601
return nil, fmt.Errorf("unexpected status code %d while fetching policies_path", resp.StatusCode)
597602
}
603+
if resp.ContentLength > defaultMaxRemotePolicyBytes {
604+
return nil, fmt.Errorf("policies_path response too large: content-length=%d exceeds max=%d bytes", resp.ContentLength, defaultMaxRemotePolicyBytes)
605+
}
598606

599-
content, err := io.ReadAll(resp.Body)
607+
content, err := io.ReadAll(io.LimitReader(resp.Body, defaultMaxRemotePolicyBytes+1))
600608
if err != nil {
601609
return nil, fmt.Errorf("failed to read policies_path response body: %w", err)
602610
}
611+
if len(content) > defaultMaxRemotePolicyBytes {
612+
return nil, fmt.Errorf("policies_path response too large: size=%d exceeds max=%d bytes", len(content), defaultMaxRemotePolicyBytes)
613+
}
603614
return content, nil
604615
default:
605616
return nil, fmt.Errorf("unsupported policies_path scheme: %s", parsedURL.Scheme)

main_test.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,23 @@ func TestResolvePoliciesYAML(t *testing.T) {
170170
}
171171
})
172172

173+
t.Run("http response too large", func(t *testing.T) {
174+
oversized := strings.Repeat("a", defaultMaxRemotePolicyBytes+1)
175+
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
176+
w.WriteHeader(http.StatusOK)
177+
_, _ = w.Write([]byte(oversized))
178+
}))
179+
defer srv.Close()
180+
181+
_, err := resolvePoliciesYAML(context.Background(), "", srv.URL)
182+
if err == nil {
183+
t.Fatalf("expected error for oversized response body")
184+
}
185+
if !strings.Contains(err.Error(), "too large") {
186+
t.Fatalf("expected oversized body error, got: %v", err)
187+
}
188+
})
189+
173190
t.Run("unsupported scheme", func(t *testing.T) {
174191
_, err := resolvePoliciesYAML(context.Background(), "", "s3://bucket/policies.yaml")
175192
if err == nil {
@@ -344,8 +361,14 @@ sleep 2
344361
if !strings.Contains(result.Error, "deadline exceeded") {
345362
t.Fatalf("expected deadline exceeded in error, got: %s", result.Error)
346363
}
347-
if len(result.Errors) == 0 {
348-
t.Fatalf("expected structured execution errors")
364+
deadlineMentions := 0
365+
for _, msg := range result.Errors {
366+
if strings.Contains(msg, "deadline exceeded") {
367+
deadlineMentions++
368+
}
369+
}
370+
if deadlineMentions > 1 {
371+
t.Fatalf("expected at most one deadline exceeded entry, got: %v", result.Errors)
349372
}
350373
})
351374
}

0 commit comments

Comments
 (0)