fix(885): a handover written in the YAML grammar names no secret, so the guard reads the KEY too

The detector was exhaustive over the `${{ }}` expression grammar and blind to
`jobs.<id>.secrets: inherit` on a `uses:` job, which passes the caller's whole
store to the called workflow while naming nothing. `secret_refs` reads only
inside expression spans — correctly, since outside one `secrets.` is a full stop
— and `inherit` is a plain scalar, so such a job was put in the derived
population by `pull_request_jobs`, walked, and reported CLEAN. Measured against
the predecessor:

  stored_secret_faults('synthetic.yml', {True: {'pull_request': None},
    'jobs': {'reused': {'uses': './.gitea/workflows/reusable.yml',
                        'secrets': 'inherit'}}})            -> []
  ... the same job with secrets: {TOK: '${{ secrets.RENOVATE_TOKEN }}'} -> 1 fault

so the miss was specific to the VALUE SHAPE, not the key. That is the failure the
done-condition names — a new job joining the population unprotected without
reddening anything — in a guard whose stated selling point is exhaustiveness over
the grammar and no exemption list.

`opaque_secret_handovers` now faults a `secrets:` key whose value is not a mapping
of names, under the existing `secrets.*` whole-context sentinel, and both fault
sites read through one `held_secret_names` so the workflow scope and the job
subtree cannot drift on which references are forgiven. The test is on the value
shape and not on the word `inherit`, for the reason the residue counter is not a
match on `toJSON`: any non-mapping value hands over a set the guard cannot
enumerate, a spelling act_runner grows later included.

Both halves of the predecessor measurement are re-derived every run rather than
left as prose: the new test asserts `secret_names(job) - INJECTED_SECRETS` — the
collector verbatim as it read before this clause — empty on the same fixtures it
asserts the fault on, and asserts the job is in the population. Reverting
`held_secret_names` to that expression reddens that test and only that test
(measured: 1 failed, 18 passed).

The clause reads the DOCUMENT only and the text-versus-walk cross-check cannot
cover it — there is no expression for its half to match, which is a stronger
reason than the shared-blind-spot one the cross-check already discloses. Said at
the definition, in the cross-check's "STRUCTURALLY CANNOT REPORT" paragraph, and
in the record, rather than left to be discovered; it does not redden the
cross-check either, since the clause feeds the fault collector and not
`secret_name_counts`.

Whether Gitea 1.27.1 / act_runner resolves `workflow_call` + `secrets: inherit`
on this instance was NOT probed — that affects reachability today, not the
guard's silence, and the direction is the one the spelling rows already take.
No tracked workflow uses a job-level `uses:`, so nothing reddens.

Refs #885
Decisions-Edit: yes

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015QqCpYFsKgnAnx6jVwrKiV
This commit is contained in:
2026-09-05 15:15:43 +02:00
co-authored by Claude Fable 5.1
parent fe05526ef4
commit c325cd6ee4
3 changed files with 170 additions and 5 deletions
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -505,6 +505,75 @@ def secret_names(node: object) -> set[str]:
return set(secret_name_counts(node))
# The second way a document hands stored secrets to a run: a `secrets:` KEY, which names them in the
# YAML grammar rather than inside an expression. `jobs.<id>.secrets:` on a `uses:` (reusable-workflow)
# job passes the caller's store to the called workflow, and its value takes one of two shapes — a
# MAPPING of name -> value, whose values are `${{ }}` expressions the collector above already reads,
# or the bare scalar `inherit`, which hands over the WHOLE store while naming nothing at all.
#
# `inherit` is the `toJSON(secrets)` shape one level up, and it defeats the collector for the same
# reason the span scoping is correct: `secret_refs` reads only inside `${{ }}`, and `inherit` is not
# an expression. Measured 2026-09-05 against the predecessor of this commit, a `pull_request` job of
# `{'uses': './.gitea/workflows/reusable.yml', 'secrets': 'inherit'}` was put in the population by
# `pull_request_jobs`, walked, and reported CLEAN (`stored_secret_faults(...) == []`), while the
# mapping spelling produced one correctly-named fault — so the miss was in the VALUE SHAPE, not the
# key. That is precisely a new job silently joining the population unprotected. Both halves of that
# measurement are RE-DERIVED every run rather than left as prose — `secret_names(job) -
# INJECTED_SECRETS` is the predecessor collector verbatim, and
# `test_a_SECRETS_HANDOVER_naming_nothing_is_reported_under_the_WHOLE_CONTEXT_sentinel` asserts it
# empty on the same fixture it asserts the fault on.
#
# The test is on the value shape and not on the word `inherit`, for the reason the residue counter is
# not a match on `toJSON`: any value that is not a mapping of names hands over something this guard
# cannot enumerate, including a spelling act_runner grows later. Whether this instance's runner
# resolves `workflow_call` + `secrets: inherit` at all was NOT probed from here; the direction makes
# that acceptable, as it does for the expression spellings — an unsupported shape costs a spurious
# demand on a job nobody has written, and the omission cost the whole store.
#
# This clause is DOCUMENT-ONLY, stated here rather than left to be discovered: `secrets:` is a plain
# YAML key, so the text-versus-walk cross-check has nothing to match on and does not cover it. It
# also does not REDDEN that cross-check, because the clause feeds the fault collector and not
# `secret_name_counts`, which both halves read through.
#
# The key is matched case-INSENSITIVELY, the same direction `_SECRET_REF` takes: the grammar spells it
# lowercase, and reading `Secrets:` as one too can only ever be too demanding.
SECRETS_KEY = "secrets"
def opaque_secret_handovers(node: object) -> int:
"""`secrets:` keys in a subtree whose value is not a mapping of names — occurrences, not sites.
Structure-blind for the reason `secret_name_counts` is: walking for the key anywhere rather than
at `jobs.<id>.secrets` avoids pinning the one location today's grammar documents
(`testing.guard-derives-population-from-source`).
"""
found = 0
stack: list[object] = [node]
while stack:
item = stack.pop()
if isinstance(item, dict):
for key, value in item.items():
if isinstance(key, str) and key.lower() == SECRETS_KEY and not isinstance(value, dict):
found += 1
stack.append(value)
elif isinstance(item, list):
stack.extend(item)
return found
def held_secret_names(node: object) -> set[str]:
"""Every STORED secret a subtree hands to the run: the names it references, plus the whole-context
sentinel when it hands over a set this guard cannot enumerate.
The one place `INJECTED_SECRETS` is subtracted, so the two fault sites below cannot drift apart on
which references are forgiven.
"""
held = secret_names(node) - INJECTED_SECRETS
if opaque_secret_handovers(node):
held.add(WHOLE_SECRETS_CONTEXT)
return held
def outside_jobs(doc: object) -> dict:
"""Everything in a workflow document EXCEPT `jobs:` — the workflow scope.
@@ -525,7 +594,7 @@ def stored_secret_faults(rel: str, doc: object) -> list[str]:
faults: list[str] = []
if not runs_on_pull_request(doc):
return faults
workflow_scope = sorted(secret_names(outside_jobs(doc)) - INJECTED_SECRETS)
workflow_scope = sorted(held_secret_names(outside_jobs(doc)))
if workflow_scope:
faults.append(
f"{rel}: the WORKFLOW SCOPE (outside `jobs:`) names stored secret(s) on the "
@@ -538,7 +607,7 @@ def stored_secret_faults(rel: str, doc: object) -> list[str]:
f"list: `ci.pr-route-carries-no-stored-credential`."
)
for job_id, job in pull_request_jobs(doc):
named = sorted(secret_names(job) - INJECTED_SECRETS)
named = sorted(held_secret_names(job))
if named:
faults.append(
f"{rel}: job `{job_id}` names stored secret(s) on the pull_request route: "
@@ -621,6 +690,12 @@ def test_the_DOCUMENT_walk_finds_every_secret_reference_the_TEXT_does() -> None:
but as `secrets.*` — the whole-context sentinel — because the index contains no literal, which is
the fail-closed direction and is asserted in
`test_the_collector_sees_every_SPELLING_of_a_secret_reference`.
It equally cannot report a handover written in the YAML grammar rather than the expression
grammar — `jobs.<id>.secrets: inherit` on a `uses:` job — and for a stronger reason than a shared
blind spot: there is no expression for the text half to match at all. That shape is judged by
`opaque_secret_handovers`, which reads the DOCUMENT only, and is asserted in
`test_a_SECRETS_HANDOVER_naming_nothing_is_reported_under_the_WHOLE_CONTEXT_sentinel`.
"""
disagreements: list[str] = []
total = Counter()
@@ -831,6 +906,82 @@ def test_the_collector_sees_every_SPELLING_of_a_secret_reference() -> None:
assert walk_versus_text_faults("synthetic.yml", as_text) == [], sentence
def test_a_SECRETS_HANDOVER_naming_nothing_is_reported_under_the_WHOLE_CONTEXT_sentinel() -> None:
"""`secrets: inherit` hands the WHOLE store to a `uses:` job while naming no secret at all.
The `toJSON(secrets)` shape one level up — in the YAML grammar rather than the expression
grammar — and so the one shape the span scoping above cannot see, since `inherit` is a plain
scalar and not an expression. Driven against the REAL predecessor rather than a hand-written
mutant: `secret_names(job) - INJECTED_SECRETS` is the collector exactly as it read before this
clause, and it is asserted empty on every row here while the job IS in the population — walked,
and reported clean. That is a job joining the population unprotected without reddening anything.
Whether act_runner on this instance resolves `workflow_call` + `secrets: inherit` was NOT probed
from here; it affects reachability today, not the guard's silence, and the direction is the same
one the spelling rows take — an unsupported shape costs a spurious demand on a job nobody has
written.
"""
for value in ("inherit", "INHERIT", None, ["REGISTRY_PASSWORD"]):
job = {"uses": "./.gitea/workflows/reusable.yml", "secrets": value}
doc = {True: {"pull_request": None}, "jobs": {"reused": job}}
assert [job_id for job_id, _ in pull_request_jobs(doc)] == ["reused"], value
assert secret_names(job) - INJECTED_SECRETS == set(), (
f"a `secrets:` value of {value!r} is supposed to name nothing the reference collector "
"can see — if it does, this row proves nothing about the handover clause."
)
faults = stored_secret_faults("synthetic.yml", doc)
assert len(faults) == 1, (value, faults)
assert WHOLE_SECRETS_CONTEXT in faults[0], (value, faults)
# The KEY is read case-insensitively, the direction `_SECRET_REF` takes — asserted rather than
# only claimed in the comment beside it.
cased = {True: {"pull_request": None}, "jobs": {"reused": {"uses": "./x.yml", "Secrets": "inherit"}}}
assert len(stored_secret_faults("synthetic.yml", cased)) == 1
# The MAPPING spelling is not what this clause reports: every value in it is an expression the
# reference collector already reads, so it faults by NAME and not under the sentinel.
named = {
True: {"pull_request": None},
"jobs": {"reused": {"uses": "./x.yml", "secrets": {"TOK": "${{ secrets.RENOVATE_TOKEN }}"}}},
}
faults = stored_secret_faults("synthetic.yml", named)
assert len(faults) == 1, faults
assert "RENOVATE_TOKEN" in faults[0] and WHOLE_SECRETS_CONTEXT not in faults[0], faults
# A `workflow_call` DECLARATION is a mapping of names, not a handover — the negative control that
# keeps the clause from faulting every reusable workflow that declares its own inputs.
declaring = {
True: {"pull_request": None, "workflow_call": {"secrets": {"TOK": {"required": True}}}},
"jobs": {"j": {"steps": [{"run": "true"}]}},
}
assert stored_secret_faults("synthetic.yml", declaring) == []
# The workflow scope is judged through the same helper, so the clause widens with it.
scoped = {True: {"pull_request": None}, "secrets": "inherit", "jobs": {}}
scope_faults = stored_secret_faults("synthetic.yml", scoped)
assert len(scope_faults) == 1 and "WORKFLOW SCOPE" in scope_faults[0], scope_faults
# And it is judged on the route only, and only for a job the trigger reaches.
off_route = {True: {"push": None}, "jobs": {"reused": {"uses": "./x.yml", "secrets": "inherit"}}}
assert stored_secret_faults("synthetic.yml", off_route) == []
gated = {
True: {"pull_request": None},
"jobs": {
"reused": {"if": "github.event_name != 'pull_request'", "uses": "./x.yml", "secrets": "inherit"},
},
}
assert stored_secret_faults("synthetic.yml", gated) == []
# The clause is DOCUMENT-ONLY: `secrets:` is a plain YAML key, so the text half of the
# cross-check has nothing to match and must stay SILENT rather than report a disagreement over a
# reference neither half can name.
as_text = "jobs:\n reused:\n uses: ./x.yml\n secrets: inherit\n"
assert secret_name_counts(yaml.safe_load(as_text)) == Counter()
assert walk_versus_text_faults("synthetic.yml", as_text) == []
def test_the_collector_reports_a_WORKFLOW_SCOPE_reference_no_job_if_can_reach() -> None:
"""The workflow scope is judged even when every job is gated OFF the route.