@@ -29,10 +29,11 @@ import (
2929)
3030
3131const (
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
3839var 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 )
0 commit comments