-
Notifications
You must be signed in to change notification settings - Fork 0
feat: implement issue #875 — [#850] templates drop the S7635 NOSONAR marker on secrets: inherit (dev-lead/add-to-project/auto-rebase/dependabot-automerge) — restore + guard #876
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
Merged
+100
−9
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
test/scripts/standards-templates/s7635-secrets-inherit.bats
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| #!/usr/bin/env bats | ||
| # Template-guard for the S7635 NOSONAR marker on caller-stub templates under | ||
| # standards/workflows/ (#875, mirrors the #860 v-form/S7637 pin guard). | ||
| # | ||
| # A caller stub that hands the reusable every org secret via `secrets: inherit` | ||
| # trips SonarCloud rule `githubactions:S7635` ("only pass required secrets"). | ||
| # The reusable is first-party and fully trusted, so the stub carries an inline | ||
| # secrets: inherit # NOSONAR(githubactions:S7635) <prose> | ||
| # marker that suppresses S7635 on exactly that line and travels with the stub to | ||
| # every consumer repo. Dropping the marker regresses any consumer that copied | ||
| # the stub (re-triggers S7635 in their SonarCloud run). This guard fails in CI | ||
| # the moment a template with `secrets: inherit` loses the marker. | ||
| # | ||
| # Stubs that pass secrets EXPLICITLY (least privilege, no `secrets: inherit` | ||
| # line — e.g. add-to-project.yml, pr-review-mention.yml) do not trip S7635 and | ||
| # are correctly ignored: the guard only inspects real `secrets: inherit` lines. | ||
|
|
||
| REPO_ROOT="$(cd -- "${BATS_TEST_DIRNAME}/../../.." && pwd)" | ||
| WF_DIR="${REPO_ROOT}/standards/workflows" | ||
|
|
||
| # The exact NOSONAR token every marked line must carry (grep -F, literal). | ||
| MARKER='NOSONAR(githubactions:S7635)' | ||
|
|
||
| # Emit `secrets: inherit` lines that are REAL YAML (indented key), never a `#` | ||
| # comment mention. Anchors `secrets:` to the start-of-line-after-indent so a | ||
| # prose line like `# ... secrets: inherit ...` is not matched. | ||
| secrets_inherit_lines() { | ||
| grep -nE '^[[:space:]]*secrets:[[:space:]]+inherit([[:space:]]|$)' "$1" || true | ||
| } | ||
|
|
||
| @test "every template with 'secrets: inherit' carries the S7635 marker" { | ||
| local violations=() | ||
| local f name lines line lineno content | ||
| for f in "${WF_DIR}"/*.yml; do | ||
| name="$(basename "$f")" | ||
| lines="$(secrets_inherit_lines "$f")" | ||
| [ -n "$lines" ] || continue | ||
| while IFS= read -r line; do | ||
| lineno="${line%%:*}" | ||
| content="${line#*:}" | ||
| if ! grep -qF "$MARKER" <<<"$content"; then | ||
| violations+=("${name}:${lineno}: 'secrets: inherit' line lacks ${MARKER}") | ||
| fi | ||
| done <<<"$lines" | ||
| done | ||
| if [ "${#violations[@]}" -ne 0 ]; then | ||
| printf 'missing S7635 marker -> %s\n' "${violations[@]}" | ||
| return 1 | ||
| fi | ||
| } | ||
|
|
||
| @test "every S7635 marker uses the canonical inline shape" { | ||
| # Two spaces before the '#', then the exact token. Trailing prose is free-form | ||
| # (e.g. 'first-party trusted reusable' vs 'org secrets are scoped ...'), but | ||
| # the marker itself must be byte-consistent so SonarCloud honours it. | ||
| local violations=() | ||
| local f name lines line lineno content | ||
| for f in "${WF_DIR}"/*.yml; do | ||
| name="$(basename "$f")" | ||
| lines="$(secrets_inherit_lines "$f")" | ||
| [ -n "$lines" ] || continue | ||
| while IFS= read -r line; do | ||
| lineno="${line%%:*}" | ||
| content="${line#*:}" | ||
| grep -qF "$MARKER" <<<"$content" || continue | ||
| if ! grep -qE 'secrets: inherit # NOSONAR\(githubactions:S7635\) ' <<<"$content"; then | ||
| violations+=("${name}:${lineno}: marker is not the canonical 'secrets: inherit # NOSONAR(githubactions:S7635) <prose>' shape") | ||
| fi | ||
| done <<<"$lines" | ||
| done | ||
| if [ "${#violations[@]}" -ne 0 ]; then | ||
| printf 'bad marker shape -> %s\n' "${violations[@]}" | ||
| return 1 | ||
| fi | ||
| } | ||
|
don-petry marked this conversation as resolved.
|
||
|
|
||
| @test "the guard actually inspects at least one 'secrets: inherit' stub" { | ||
| # Positive control: without this, a broken matcher that finds nothing would let | ||
| # the first test pass vacuously (its `[ -n "$lines" ] || continue` skips every | ||
| # file). At least one template uses `secrets: inherit` by design. | ||
| local f found=0 | ||
| for f in "${WF_DIR}"/*.yml; do | ||
| [ -n "$(secrets_inherit_lines "$f")" ] && found=1 && break | ||
| done | ||
| [ "$found" -eq 1 ] || { echo "no template with a real 'secrets: inherit' line found"; return 1; } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.