diff --git a/.gitea/workflows/review.yml b/.gitea/workflows/review.yml index 1d93a2d..2e63f31 100644 --- a/.gitea/workflows/review.yml +++ b/.gitea/workflows/review.yml @@ -128,8 +128,8 @@ jobs: echo "drafter=$drafter" >> "$GITHUB_OUTPUT" # Full history, so merge bases and the docs branch's ancestry resolve. The token - # travels as an http.extraHeader in this command's environment, so it is never - # written to .git/config or a remote URL. + # travels as an http./.extraHeader in this command's environment, so it is + # sent only to the Gitea server and never written to .git/config or a remote URL. - name: Clone the calling repository if: github.event_name != 'pull_request' || github.event.action != 'closed' env: @@ -140,7 +140,7 @@ jobs: echo "::error::the DOCS_REVIEW_BOT_TOKEN secret is not set" exit 1 fi - export GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=http.extraHeader \ + export GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0="http.${GITHUB_SERVER_URL}/.extraHeader" \ GIT_CONFIG_VALUE_0="Authorization: token $DOCS_REVIEW_BOT_TOKEN" src="$(mktemp -d)" if ! git clone -q -- "$GITHUB_SERVER_URL/$GITHUB_REPOSITORY.git" "$src"; then @@ -177,7 +177,7 @@ jobs: DOCS_REVIEW_STAGE: "1" run: | set -eu - export GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=http.extraHeader \ + export GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0="http.${GITHUB_SERVER_URL}/.extraHeader" \ GIT_CONFIG_VALUE_0="Authorization: token $DOCS_REVIEW_BOT_TOKEN" if [ "$EVENT" = "pull_request" ]; then python3 -P -m docs_review pr --number "$PR" --base "$BASE" \ @@ -220,7 +220,7 @@ jobs: npm install -g --no-fund --no-audit "@anthropic-ai/claude-code@$CLAUDE_CODE_VERSION" claude --version fi - export GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=http.extraHeader \ + export GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0="http.${GITHUB_SERVER_URL}/.extraHeader" \ GIT_CONFIG_VALUE_0="Authorization: token $DOCS_REVIEW_BOT_TOKEN" if [ "$EVENT" = "pull_request" ]; then python3 -P -m docs_review pr --number "$PR" --base "$BASE" \ diff --git a/README.md b/README.md index 85ac7bf..5cb2fe2 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,11 @@ uncovered and unguided flags for the whole repository in one issue. With - It never merges anything. - It posts nothing when nothing is flagged. +For a pull request from a branch of the same repository, Gitea runs the caller workflow +from the pull request's head, so anyone who can push a branch can change the caller's +inputs. The safeguards in this README bound what the pull request's content can do, not +what an edited caller workflow file can do. + ## What is sent to Anthropic Nothing is sent unless the repository sets `drafter: claude`. With it, the drafter @@ -99,7 +104,7 @@ The package can also be run by hand: `python3 -m docs_review pr|closed|monthly - | Input | Default | Meaning | |---|---|---| -| `drafter` | `none` | `none` flags only. `claude` also drafts edits. `local` is accepted but not yet built. Any other value, including a misspelling, behaves as `none`. | +| `drafter` | `none` | `none` flags only. `claude` also drafts edits. `local` is accepted but not yet built: it fails the job with "local drafter not built yet". Any other value, including a misspelling, behaves as `none`. | | `docs` | required | Newline-separated patterns for the living documents. | | `generated` | none | Patterns for generated files. | | `top_level` | `README.md` | The subset of `docs` the monthly pass re-reads in full. | diff --git a/docs_review/git.py b/docs_review/git.py index 0daefd6..765e14b 100644 --- a/docs_review/git.py +++ b/docs_review/git.py @@ -126,17 +126,26 @@ def commit(paths: list[str], message: str, author: tuple[str, str], cwd: Path) - ) -def push(branch: str, cwd: Path, token: str | None) -> None: +def header_key(server: str | None) -> str: + """The config key for the auth header: scoped to the Gitea server when it is known, + so the header is never sent to any other host.""" + if server: + return f"http.{server.rstrip('/')}/.extraHeader" + return "http.extraHeader" + + +def push(branch: str, cwd: Path, token: str | None, server: str | None = None) -> None: """Push branch to origin under an explicit refspec, never forced. - The token travels as an http.extraHeader set through GIT_CONFIG_* variables on this - call only, so it never reaches argv or .git/config. A branch starting with + or - - could force the push or read as an option, so it is refused.""" + The token travels as an extraHeader set through GIT_CONFIG_* variables on this call + only, so it never reaches argv or .git/config; with server (GITHUB_SERVER_URL) the + header is scoped to that URL. A branch starting with + or - could force the push or + read as an option, so it is refused.""" if not branch or branch.startswith(("+", "-")): raise GitError(f"refusing to push branch {branch!r}") extra = None if token: - extra = {"GIT_CONFIG_COUNT": "1", "GIT_CONFIG_KEY_0": "http.extraHeader", + extra = {"GIT_CONFIG_COUNT": "1", "GIT_CONFIG_KEY_0": header_key(server), "GIT_CONFIG_VALUE_0": f"Authorization: token {token}"} ref = f"refs/heads/{branch}" run(["push", "origin", f"{ref}:{ref}"], cwd, extra_env=extra) diff --git a/docs_review/run_monthly.py b/docs_review/run_monthly.py index 7d31c31..05e52bb 100644 --- a/docs_review/run_monthly.py +++ b/docs_review/run_monthly.py @@ -92,7 +92,8 @@ def _commit_message(inputs: Inputs, month: str) -> str: def _deliver(forge, inputs, base, month, branch, reuse, ident, flags, result, changed, cwd, env): git.commit(changed, _commit_message(inputs, month), ident, cwd) - git.push(branch, cwd, env.get("DOCS_REVIEW_BOT_TOKEN")) + git.push(branch, cwd, env.get("DOCS_REVIEW_BOT_TOKEN"), + env.get("GITHUB_SERVER_URL")) head = git.run(["rev-parse", "HEAD"], cwd).strip() body = render_pr_body(parse_summary(result.text), result, flags, head, changed) title = f"docs: monthly review {month}" diff --git a/docs_review/run_pr.py b/docs_review/run_pr.py index a00c499..5a3ce96 100644 --- a/docs_review/run_pr.py +++ b/docs_review/run_pr.py @@ -135,7 +135,8 @@ def _deliver(forge, inputs, args, docs_pr, ident, flags, result, changed, cwd, e branch = docs_branch(args.number) git.commit(changed, f"docs({inputs.workstream}): proposed edits (#{args.number})", ident, cwd) - git.push(branch, cwd, env.get("DOCS_REVIEW_BOT_TOKEN")) + git.push(branch, cwd, env.get("DOCS_REVIEW_BOT_TOKEN"), + env.get("GITHUB_SERVER_URL")) body = render_pr_body(parse_summary(result.text), result, flags, args.head_sha, changed) if docs_pr is not None: forge.edit_pr(docs_pr["number"], body=body) diff --git a/tests/test_cli.py b/tests/test_cli.py index 3540f7b..a1bff1a 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -4,6 +4,7 @@ import shutil import subprocess import tempfile import unittest +from unittest import mock from pathlib import Path from docs_review.cli import main @@ -169,6 +170,28 @@ class ClaudeDrafterTests(CliCase): self.fake.email) self.assertEqual(self.comment_bodies(), []) + def test_resume_fetches_with_end_of_options_and_scoped_push(self): + self.assertEqual(self.run_pr(self.feature_commit()), 0) + calls = [] + real = subprocess.run + + def record(argv, **kw): + calls.append((list(argv), kw.get("env") or {})) + return real(argv, **kw) + + server = "https://gitea.example.org" + sha = self.feature_commit() + with mock.patch("docs_review.git.subprocess.run", side_effect=record): + self.assertEqual(self.run_pr(sha, GITHUB_SERVER_URL=server), 0) + # The harness's own `git fetch -q origin` (run_pr's setup) names no branch. + fetches = [a for a, _ in calls if a[:2] == ["git", "fetch"] and a[-1] != "origin"] + self.assertTrue(fetches) + for argv in fetches: + self.assertEqual(argv[-2:], ["--end-of-options", BRANCH]) + pushes = [e for a, e in calls if a[:2] == ["git", "push"]] + self.assertEqual(len(pushes), 1) + self.assertEqual(pushes[0]["GIT_CONFIG_KEY_0"], f"http.{server}/.extraHeader") + def test_rerun_appends_and_keeps_hand_edit(self): self.assertEqual(self.run_pr(self.feature_commit()), 0) git(self.seed, "fetch", "-q", "origin", BRANCH) @@ -256,12 +279,6 @@ class CredentialAndSummaryTests(CliCase): self.assertIn("total_cost_usd: 0.12", text) self.assertIn("num_turns: 3", text) - def test_no_summary_file_without_the_variable(self): - sha = self.feature_commit() - self.env.pop("GITHUB_STEP_SUMMARY", None) - self.assertEqual(self.run_pr(sha), 0) - self.assertFalse((Path(self.env["RUNNER_TEMP"]) / "step-summary.md").exists()) - class DocsPrLifecycleTests(CliCase): def setUp(self): diff --git a/tests/test_git.py b/tests/test_git.py index cef3300..38208a8 100644 --- a/tests/test_git.py +++ b/tests/test_git.py @@ -163,10 +163,18 @@ class TestGitHardening(unittest.TestCase): git.fetch(f"--upload-pack=touch {out}", self.repo) self.assertFalse(out.exists()) - def test_flows_fetch_only_through_the_helper(self): - from docs_review import run_monthly, run_pr - for module in (run_pr, run_monthly): - self.assertNotIn('["fetch"', inspect.getsource(module)) + def test_push_scopes_header_to_the_server(self): + calls = self._record() + git.run(["checkout", "-q", "-b", "docs-review/pr-4"], self.repo) + git.push("docs-review/pr-4", self.repo, self.TOKEN, "https://gitea.example.org/") + env = calls[-1][1] + self.assertEqual(env["GIT_CONFIG_KEY_0"], "http.https://gitea.example.org/.extraHeader") + self.assertEqual(env["GIT_CONFIG_VALUE_0"], f"Authorization: token {self.TOKEN}") + + def test_header_key_falls_back_to_unscoped(self): + self.assertEqual(git.header_key(None), "http.extraHeader") + self.assertEqual(git.header_key(""), "http.extraHeader") + self.assertEqual(git.header_key("https://h.example"), "http.https://h.example/.extraHeader") def test_push_without_token_adds_no_header(self): calls = self._record()