Repository navigation
Fix dynamic secret selection false positives in strict linting #80581
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
Changes from 2 commits
f0e277a
45f22a6
e6d6da4
1a530e7
3c4e3c9
db5c641
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -93,19 +93,7 @@ func overprovisionedSecretsDiagnostics(ctx context.Context, input Input) ([]Diag | |
| } | ||
|
|
||
| blocks, err := aw.ExecutionBlocks(ctx, input.Document) | ||
| failures := []error{err} | ||
| // engine.env and sandbox.agent.env are copied into the agent execution step | ||
| // environment but are not execution blocks. | ||
| if input.Document != nil { | ||
| if engine, ok := input.Document.Frontmatter["engine"].(map[string]any); ok { | ||
| failures = append(failures, walkExpressionScalars(ctx, engine["env"], "engine.env", visit)) | ||
| } | ||
| if sandbox, ok := input.Document.Frontmatter["sandbox"].(map[string]any); ok { | ||
| if agent, ok := sandbox["agent"].(map[string]any); ok { | ||
| failures = append(failures, walkExpressionScalars(ctx, agent["env"], "sandbox.agent.env", visit)) | ||
| } | ||
| } | ||
| } | ||
| failures := []error{err, walkOverprovisionedSecretsEnv(ctx, input.Document, visit)} | ||
| for _, block := range blocks { | ||
| if secrets, ok := block.Fields["secrets"].(string); ok && strings.EqualFold(strings.TrimSpace(secrets), "inherit") { | ||
| if uses, ok := block.Fields["uses"].(string); ok { | ||
|
|
@@ -124,6 +112,23 @@ func overprovisionedSecretsDiagnostics(ctx context.Context, input Input) ([]Diag | |
| return diagnostics, errors.Join(failures...) | ||
| } | ||
|
|
||
| func walkOverprovisionedSecretsEnv(ctx context.Context, document *aw.Document, visit func(string, string) error) error { | ||
| // engine.env and sandbox.agent.env are copied into the agent execution step | ||
| // environment but are not execution blocks. | ||
| var failures []error | ||
| if document != nil { | ||
| if engine, ok := document.Frontmatter["engine"].(map[string]any); ok { | ||
| failures = append(failures, walkExpressionScalars(ctx, engine["env"], "engine.env", visit)) | ||
| } | ||
| if sandbox, ok := document.Frontmatter["sandbox"].(map[string]any); ok { | ||
| if agent, ok := sandbox["agent"].(map[string]any); ok { | ||
| failures = append(failures, walkExpressionScalars(ctx, agent["env"], "sandbox.agent.env", visit)) | ||
| } | ||
| } | ||
| } | ||
| return errors.Join(failures...) | ||
| } | ||
|
|
||
| // isCompilerCallWorkflowFallback reports whether job has the exact shape of a | ||
| // compiler-generated call-workflow fan-out job: call-<worker> needing | ||
| // safe_outputs, gated on call_workflow_name, and calling the local worker file. | ||
|
|
@@ -174,7 +179,8 @@ func inheritDiagnostic(path, uses string) Diagnostic { | |
| } | ||
|
|
||
| // materializesSecretsContext reports whether node evaluates the secrets | ||
| // context as a whole: any reference other than secrets.NAME or secrets['NAME']. | ||
| // context as a whole rather than selecting one property. Dynamic indexes select | ||
| // one secret too, but their expressions are still visited for whole-context use. | ||
| func materializesSecretsContext(node actionlint.ExprNode) bool { | ||
| found := false | ||
| actionlint.VisitExprNode(node, func(child, parent actionlint.ExprNode, entering bool) { | ||
|
|
@@ -191,7 +197,7 @@ func materializesSecretsContext(node actionlint.ExprNode) bool { | |
| return | ||
| } | ||
| case *actionlint.IndexAccessNode: | ||
| if _, literal := parent.Index.(*actionlint.StringNode); literal && parent.Operand == child { | ||
| if parent.Operand == child { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Computed secret references are now included in step secret classification, so env bindings are rejected when the step writes to |
||
| return | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -48,9 +48,9 @@ func TestOverprovisionedSecretsActions(t *testing.T) { | |
| want: map[string]Severity{"jobs.call.secrets.all": Error}, | ||
| }, | ||
| { | ||
| name: "dynamic index and whole context in run", | ||
| name: "dynamic index selects one secret while whole context in run is blocked", | ||
| source: contractJob + " steps:\n - with: {value: '${{ " + "secrets" + "[inputs.name] }}'}\n uses: actions/github-script@v8\n - run: |\n echo start\n echo '${{ toJson(" + "secrets" + ") }}' | jq length\n", | ||
| want: map[string]Severity{"jobs.check.steps[0].with.value": Error, "jobs.check.steps[1].run": Error}, | ||
| want: map[string]Severity{"jobs.check.steps[1].run": Error}, | ||
| }, | ||
| { | ||
| name: "literal index and dereference are safe", | ||
|
|
@@ -145,14 +145,75 @@ func TestOverprovisionedSecretsAuthored(t *testing.T) { | |
| got[diagnostic.Path] = diagnostic.Severity | ||
| } | ||
| assert.Equal(t, map[string]Severity{ | ||
| "jobs.remote.secrets": Error, | ||
| "jobs.local.secrets": Warning, | ||
| "steps[0].env.ALL": Error, | ||
| "engine.env.ALL": Error, | ||
| "sandbox.agent.env.PICK": Error, | ||
| "jobs.remote.secrets": Error, | ||
| "jobs.local.secrets": Warning, | ||
| "steps[0].env.ALL": Error, | ||
| "engine.env.ALL": Error, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [/tdd] This removes the only assertion covering whole-context exposure under 💡 Suggested testAdd a whole-context entry such as @copilot please address this.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added the sandbox whole-context regression assertion in |
||
| }, got) | ||
| } | ||
|
|
||
| func TestOverprovisionedSecretsSelectors(t *testing.T) { | ||
| t.Parallel() | ||
| tests := []struct { | ||
| name string | ||
| expression string | ||
| blocked bool | ||
| }{ | ||
| { | ||
| name: "repository mapping selects one secret", | ||
| expression: "vars.GH_AW_GITHUB_AUTH_MODE == 'pat' && secrets[fromJSON(vars.GH_AW_GITHUB_READ_PAT_REPOSITORIES || '{}')[github.repository]] || ''", | ||
| }, | ||
| { | ||
| name: "serialization of a selected secret", | ||
| expression: "toJSON(secrets[inputs.name])", | ||
| }, | ||
| { | ||
| name: "literal wildcard name selects one secret", | ||
| expression: "secrets['*']", | ||
| }, | ||
| { | ||
| name: "whole context inside selector", | ||
| expression: "secrets[fromJSON(toJSON(secrets))[github.repository]]", | ||
| blocked: true, | ||
| }, | ||
| { | ||
| name: "whole context used as selector", | ||
| expression: "secrets[secrets]", | ||
| blocked: true, | ||
| }, | ||
| { | ||
| name: "wildcard projection", | ||
| expression: "toJSON(secrets.*)", | ||
| blocked: true, | ||
| }, | ||
| { | ||
| name: "whole context alongside selection", | ||
| expression: "format('{0}{1}', secrets[inputs.name], toJSON(secrets))", | ||
| blocked: true, | ||
| }, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| t.Parallel() | ||
| field := " env:\n TOKEN: ${{ " + tt.expression + " }}\n" | ||
| workflow, err := actions.Parse([]byte(contractJob + field + contractSafeSteps)) | ||
| require.NoError(t, err) | ||
| document, err := aw.Parse("---\non: issues\njobs:\n check:\n runs-on: ubuntu-latest\n" + field + contractSafeSteps + "---\nPrompt.\n") | ||
| require.NoError(t, err) | ||
| for _, input := range []Input{{Workflow: workflow, Document: document}, {Document: document}} { | ||
| diagnostics := runOverprovisionedSecrets(t, input) | ||
| if tt.blocked { | ||
| require.Len(t, diagnostics, 1) | ||
| assert.Equal(t, Error, diagnostics[0].Severity) | ||
| assert.Equal(t, "jobs.check.env.TOKEN", diagnostics[0].Path) | ||
| } else { | ||
| assert.Empty(t, diagnostics) | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestOverprovisionedSecretsCancellation(t *testing.T) { | ||
| t.Parallel() | ||
| workflow, err := actions.Parse([]byte(contractJob + contractSafeSteps)) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Computed selectors are now fingerprinted for safe-update checks, redacted, and bounded by explicit
workflow_callsecret names. Added generated-workflow regression coverage indb5c641e.