diff --git a/internal/e2e/publish_test.go b/internal/e2e/publish_test.go index 75bf247b73..0817c8cb48 100644 --- a/internal/e2e/publish_test.go +++ b/internal/e2e/publish_test.go @@ -134,6 +134,19 @@ func TestPublishDetectSensitiveData(t *testing.T) { OutputContains("Private Key\n\"\": -----BEGIN DSA PRIVATE KEY-----\nwxyz+ABC=\n-----END DSA PRIVATE KEY-----")) } +// Regression test for https://github.com/docker/compose/issues/14589: a secret +// in a local extends parent was published without the sensitive data prompt, +// although publish pushes that file as a layer of the artifact. +func TestPublishDetectSensitiveDataInExtends(t *testing.T) { + NewScenario(t, "publish must detect sensitive data in the extends parent files it publishes"). + Step("a secret in an extends parent is reported before prompting", + ComposeCmd("publish", "test/test", "--dry-run").WithStdin("n\n").MayFail(), + ExitCode(130), + OutputContains("you are about to publish sensitive data within your OCI artifact.\n"), + OutputContains("Github authentication\n\"services.base.labels.ci.token\": ghp_1234567890abcdefghijklmnopqrstuvwxyz"), + OutputNotContains("test/test published")) +} + func TestPublish(t *testing.T) { c := NewParallelCLI(t) const projectName = "compose-e2e-publish" diff --git a/internal/e2e/testdata/TestPublishDetectSensitiveDataInExtends/base.yaml b/internal/e2e/testdata/TestPublishDetectSensitiveDataInExtends/base.yaml new file mode 100644 index 0000000000..48498e4e14 --- /dev/null +++ b/internal/e2e/testdata/TestPublishDetectSensitiveDataInExtends/base.yaml @@ -0,0 +1,5 @@ +services: + base: + image: alpine + labels: + ci.token: ghp_1234567890abcdefghijklmnopqrstuvwxyz diff --git a/internal/e2e/testdata/TestPublishDetectSensitiveDataInExtends/compose.yaml b/internal/e2e/testdata/TestPublishDetectSensitiveDataInExtends/compose.yaml new file mode 100644 index 0000000000..ae0fe8000e --- /dev/null +++ b/internal/e2e/testdata/TestPublishDetectSensitiveDataInExtends/compose.yaml @@ -0,0 +1,5 @@ +services: + app: + extends: + file: base.yaml + service: base diff --git a/internal/publish/publish.go b/internal/publish/publish.go index 5468cf1242..b4a2967689 100644 --- a/internal/publish/publish.go +++ b/internal/publish/publish.go @@ -25,7 +25,6 @@ import ( "encoding/json" "errors" "fmt" - "io" "maps" "os" "slices" @@ -524,16 +523,9 @@ func (s *Service) confirmOrCancel(ctx context.Context, message string) error { return nil } -// collectEnvCheckFindings walks every compose file scheduled for publication -// (top-level files plus any local extends parents discovered along the way) -// and aggregates per-service and per-config findings. The walk mirrors -// processExtends so coverage matches what is actually serialized into the OCI -// artifact. -func collectEnvCheckFindings(ctx context.Context, project *types.Project) (*envCheckFindings, error) { - findings := &envCheckFindings{services: map[string]*serviceEnvFindings{}} - literalCfgs := map[string]struct{}{} - keywordDetector := keyword.NewDetector("0") - +// walkPublishedComposeFiles calls fn on each compose file publish pushes, loaded +// unresolved: the top-level files and, as processExtends does, their local extends parents. +func walkPublishedComposeFiles(ctx context.Context, project *types.Project, fn func(file string, unresolved *types.Project) error) error { seen := map[string]struct{}{} queue := slices.Clone(project.ComposeFiles) for len(queue) > 0 { @@ -546,15 +538,32 @@ func collectEnvCheckFindings(ctx context.Context, project *types.Project) (*envC unresolved, err := loadUnresolvedFile(ctx, project, file) if err != nil { - return nil, fmt.Errorf("failed to load compose file %s: %w", file, err) + return fmt.Errorf("failed to load compose file %s: %w", file, err) } for _, service := range unresolved.Services { - recordEnvFindings(findings.services, keywordDetector, service.Name, service.ContainerSpec) if parent := localExtendsParent(service); parent != "" { queue = append(queue, parent) } } + if err := fn(file, unresolved); err != nil { + return err + } + } + return nil +} + +// collectEnvCheckFindings aggregates per-service and per-config findings over +// the published compose files. +func collectEnvCheckFindings(ctx context.Context, project *types.Project) (*envCheckFindings, error) { + findings := &envCheckFindings{services: map[string]*serviceEnvFindings{}} + literalCfgs := map[string]struct{}{} + keywordDetector := keyword.NewDetector("0") + + err := walkPublishedComposeFiles(ctx, project, func(_ string, unresolved *types.Project) error { + for _, service := range unresolved.Services { + recordEnvFindings(findings.services, keywordDetector, service.Name, service.ContainerSpec) + } for name, job := range unresolved.Jobs { // jobs carry environment/env_file like services: same leak surface recordEnvFindings(findings.services, keywordDetector, name, job.ContainerSpec) @@ -571,6 +580,10 @@ func collectEnvCheckFindings(ctx context.Context, project *types.Project) (*envC literalCfgs[name] = struct{}{} } } + return nil + }) + if err != nil { + return nil, err } if len(literalCfgs) > 0 { @@ -679,7 +692,7 @@ func buildConfigContentPromptMessage(configs []string) string { // loadUnresolvedFile loads a single compose file with interpolation and // environment resolution skipped, so callers can inspect raw user-provided -// values. Used by both checkEnvironmentVariables and composeFileAsByteReader. +// values. Used by walkPublishedComposeFiles. func loadUnresolvedFile(ctx context.Context, project *types.Project, filePath string) (*types.Project, error) { return loader.LoadWithContext(ctx, types.ConfigDetails{ WorkingDir: project.WorkingDir, @@ -763,18 +776,23 @@ func (s *Service) checkForSensitiveData(ctx context.Context, project *types.Proj var allFindings []secrets.DetectedSecret scan := scanner.NewDefaultScanner() - // Check all compose files - for _, file := range project.ComposeFiles { - in, err := composeFileAsByteReader(ctx, file, project) + // Scan the re-marshalled model, not the raw file: it turns list-form + // environment entries into a map the detectors can match. + err := walkPublishedComposeFiles(ctx, project, func(file string, unresolved *types.Project) error { + in, err := unresolved.MarshalYAML() if err != nil { - return nil, err + return err } - findings, err := scan.ScanReader(in) + findings, err := scan.ScanReader(bytes.NewReader(in)) if err != nil { - return nil, fmt.Errorf("failed to scan compose file %s: %w", file, err) + return fmt.Errorf("failed to scan compose file %s: %w", file, err) } allFindings = append(allFindings, findings...) + return nil + }) + if err != nil { + return nil, err } // Check env files — jobs declare env_file like services do @@ -857,15 +875,3 @@ func scanFiles(scan secrets.Scanner, kind string, paths []string) ([]secrets.Det } return allFindings, nil } - -func composeFileAsByteReader(ctx context.Context, filePath string, project *types.Project) (io.Reader, error) { - base, err := loadUnresolvedFile(ctx, project, filePath) - if err != nil { - return nil, fmt.Errorf("failed to load compose file %s: %w", filePath, err) - } - in, err := base.MarshalYAML() - if err != nil { - return nil, err - } - return bytes.NewBuffer(in), nil -} diff --git a/internal/publish/publish_test.go b/internal/publish/publish_test.go index e9cbf32e85..b12abef4d7 100644 --- a/internal/publish/publish_test.go +++ b/internal/publish/publish_test.go @@ -300,6 +300,71 @@ func Test_checkForSensitiveData_required_env_file_missing(t *testing.T) { assert.ErrorContains(t, err, "not found", "required missing env file should fail") } +// checkForSensitiveData must scan the local extends parents that publish pushes +// along with the compose files, transitively: a secret in one of them used to be +// published without the sensitive data prompt (docker/compose#14589). +func Test_checkForSensitiveData_extendsParents(t *testing.T) { + const base = `services: + base: + image: alpine + labels: + ci.token: ghp_1234567890abcdefghijklmnopqrstuvwxyz +` + tests := []struct { + name string + files map[string]string + }{ + { + name: "parent", + files: map[string]string{ + "compose.yaml": `name: test +services: + app: + extends: + file: base.yaml + service: base +`, + "base.yaml": base, + }, + }, + { + name: "grandparent", + files: map[string]string{ + "compose.yaml": `name: test +services: + app: + extends: + file: mid.yaml + service: mid +`, + "mid.yaml": `services: + mid: + extends: + file: base.yaml + service: base +`, + "base.yaml": base, + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + project := loadProjectForTest(t, tt.files) + + svc := &Service{} + findings, err := svc.checkForSensitiveData(t.Context(), project) + assert.NilError(t, err) + + keys := make([]string, 0, len(findings)) + for _, f := range findings { + keys = append(keys, f.Key) + } + assert.Check(t, slices.Contains(keys, "services.base.labels.ci.token"), "findings: %v", keys) + }) + } +} + // --- collectEnvCheckFindings: pure detection logic --- func loadProjectForTest(t *testing.T, files map[string]string) *types.Project {