-
Notifications
You must be signed in to change notification settings - Fork 0
fix(security-scan): name Git's initial branch for every exact-head checkout #2154
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
Open
seonghobae
wants to merge
11
commits into
main
Choose a base branch
from
fix/security-scan-checkout-default-branch-2101
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+114
−0
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
fa06536
fix(security-scan): name Git's initial branch for every exact-head ch…
seonghobae df7b856
test(security-scan): expose Git config value override gap
seonghobae af7a068
fix(security-scan): reject Git config value shadowing
seonghobae 4e2be9a
test(security-scan): reject scalar env impersonation
seonghobae 184d348
ci: verify security-scan initial-branch contract
seonghobae 7d8fe3b
test(security-scan): make scalar impersonation regression effective
seonghobae 287b0c1
test(security-scan): require direct workflow env scalars
seonghobae 77a4eac
ci: retire security-scan contract verifier
seonghobae 64444d3
Merge protected main into security-scan checkout owner
seonghobae c393e3a
chore(security-scan): adopt latest protected main
seonghobae c811d54
merge(main): sync PR #2154 security-scan checkout default branch
seonghobae 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
104 changes: 104 additions & 0 deletions
104
tests/test_security_scan_checkout_default_branch_contract.py
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,104 @@ | ||
| """Contract: the central Security Scan names Git's initial branch explicitly. | ||
|
|
||
| Every `actions/checkout` step in `security-scan.yml` initialises a fresh | ||
| repository before fetching the exact SHA. Without an initial-branch setting | ||
| Git 2.28+ prints `hint: Using 'master' as the name for the initial branch` | ||
| (plus the Git 3.0 rename warning) on every hosted job -- observed live on | ||
| consumer runs (issue #2101). The repair is process-local Git configuration | ||
| through `GIT_CONFIG_COUNT` / `GIT_CONFIG_KEY_0` / `GIT_CONFIG_VALUE_0` at the | ||
| workflow level, so it reaches the action's internal `git init` without a | ||
| global gitconfig write and without hiding stderr. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from pathlib import Path | ||
| import re | ||
|
|
||
| import pytest | ||
|
|
||
|
|
||
| WORKFLOW = Path(__file__).resolve().parents[1] / ".github/workflows/security-scan.yml" | ||
|
|
||
|
|
||
| def _workflow_level_env(workflow: str) -> str: | ||
| """Return the top-level ``env:`` mapping text (between ``permissions:`` and ``jobs:``).""" | ||
| header = workflow.split("\njobs:\n", 1)[0] | ||
| match = re.search(r"(?ms)^env:\n((?: .*\n)+)", header) | ||
| assert match, "security-scan.yml has no workflow-level env: block" | ||
| return match.group(1) | ||
|
|
||
|
|
||
| def _assert_direct_env_scalar(env: str, key: str, rendered_value: str) -> None: | ||
| """Require one exact direct scalar entry in the workflow-level env mapping.""" | ||
| pattern = rf"(?m)^ {re.escape(key)}: {re.escape(rendered_value)}$" | ||
| assert len(re.findall(pattern, env)) == 1, f"missing or duplicate direct env key: {key}" | ||
|
|
||
|
|
||
| def _assert_workflow_level_git_config(workflow: str) -> None: | ||
| """Require the reviewed process-local Git initial-branch configuration.""" | ||
| env = _workflow_level_env(workflow) | ||
| _assert_direct_env_scalar(env, "GIT_CONFIG_COUNT", '"1"') | ||
| _assert_direct_env_scalar(env, "GIT_CONFIG_KEY_0", "init.defaultBranch") | ||
| _assert_direct_env_scalar(env, "GIT_CONFIG_VALUE_0", "main") | ||
|
|
||
|
|
||
| def _assert_jobs_do_not_override_initial_branch(body: str) -> None: | ||
| """Reject job or step configuration that can shadow the workflow Git key.""" | ||
| assert "GIT_CONFIG_COUNT" not in body | ||
| assert "GIT_CONFIG_KEY_0" not in body | ||
| assert "GIT_CONFIG_VALUE_0" not in body | ||
| assert "git config --global" not in body | ||
| assert "init.defaultBranch" not in body | ||
| # The setting only matters because the exact-head checkouts exist. | ||
| assert body.count("uses: actions/checkout@") >= 6 | ||
|
|
||
|
|
||
| def test_workflow_level_git_config_names_the_initial_branch() -> None: | ||
| """The three process-local Git config variables must be set exactly.""" | ||
| workflow = WORKFLOW.read_text(encoding="utf-8") | ||
| _assert_workflow_level_git_config(workflow) | ||
| assert "#2101" in workflow.split("\njobs:\n", 1)[0] | ||
|
|
||
|
|
||
| def test_no_step_overrides_or_globalises_the_initial_branch_setting() -> None: | ||
| """Jobs must neither shadow the variables nor write a global gitconfig.""" | ||
| workflow = WORKFLOW.read_text(encoding="utf-8") | ||
| body = workflow.split("\njobs:\n", 1)[1] | ||
| _assert_jobs_do_not_override_initial_branch(body) | ||
|
|
||
|
|
||
| def test_git_config_value_override_is_rejected() -> None: | ||
| """A job-level value override must not redirect checkout back to another branch name.""" | ||
| workflow = WORKFLOW.read_text(encoding="utf-8") | ||
| body = workflow.split("\njobs:\n", 1)[1] | ||
| hostile_body = ( | ||
| " hostile-checkout:\n" | ||
| " runs-on: ubuntu-24.04\n" | ||
| " env:\n" | ||
| " GIT_CONFIG_VALUE_0: master\n" | ||
| " steps: []\n" | ||
| + body | ||
| ) | ||
| with pytest.raises(AssertionError): | ||
| _assert_jobs_do_not_override_initial_branch(hostile_body) | ||
|
|
||
|
|
||
| def test_block_scalar_cannot_impersonate_workflow_git_config() -> None: | ||
| """Indented scalar text must not count as direct workflow ``env`` authority.""" | ||
| hostile_workflow = """name: hostile | ||
| permissions: | ||
| contents: read | ||
| env: | ||
| DECOY: | | ||
| GIT_CONFIG_COUNT: \"1\" | ||
| GIT_CONFIG_KEY_0: init.defaultBranch | ||
| GIT_CONFIG_VALUE_0: main | ||
| decoy-padding | ||
| jobs: | ||
| scan: | ||
| runs-on: ubuntu-24.04 | ||
| steps: [] | ||
| """ | ||
| with pytest.raises(AssertionError): | ||
| _assert_workflow_level_git_config(hostile_workflow) | ||
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.
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.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: ContextualWisdomLab/.github
Length of output: 31862
🏁 Script executed:
Repository: ContextualWisdomLab/.github
Length of output: 24239
🏁 Script executed:
Repository: ContextualWisdomLab/.github
Length of output: 50383
checkout 목록과 불변 바인딩을 정확히 검증하십시오.
body.count("uses: actions/checkout@") >= 6은 일곱 번째 checkout과actions/checkout@v4또는 변경된 SHA를 허용합니다. 기존test_security_scan_binds_every_scan_to_immutable_pr_revisions도 다섯 개 검증 단계와 세 개의repository/ref바인딩만 확인하며, 전체 checkout 수나 action SHA pin을 확인하지 않습니다.checkout 단계를 파싱하여 정확히 6개인지 확인하십시오. 각 단계의 고정 action SHA와 해당 단계에 필요한
repository및ref값을 명시적으로 검증하십시오.🤖 Prompt for AI Agents