Skip to content

Use the base branch's version of files that the workflow's inputs name - #2099

Merged
mhegazy merged 1 commit into
mainfrom
claude/project-thread-a0vw5a-config
Nov 20, 2026
Merged

mhegazy merged 1 commit into
mainfrom
claude/project-thread-a0vw5a-config

Conversation

@mhegazy

@mhegazy mhegazy commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Human: @mhegazy

Requested by Mohamed · project thread

On pull requests from authors without write access, the action already takes .claude/, .mcp.json and similar files from the base branch. This does the same for files and directories in the checkout that the workflow's inputs name: settings, plugin_marketplaces, and the claude_args arguments that load config from a path, such as --settings, --mcp-config and --plugin-dir.

  • Only files that the pull request changes are replaced, or those whose .gitattributes it changes. Files from earlier steps stay.
  • The pull request's versions are kept under .claude-pr/ for reading. Claude can't edit what the inputs name.
  • Nothing changes for authors with write access, with restrict_untrusted_prs: false, or for workflows whose inputs name no files in the checkout.
  • docs/security.md lists what is covered and what isn't. Symbolic links are restored but not followed.

Restored paths are now passed to git as literal pathspecs, because these names can contain glob characters.

Upgrading: on a pull request from an author without write access, a workflow that names files in the checkout gets the base branch's version of them. Pull requests that change such a file no longer try out the change. A file that exists only in the pull request is removed, so an input that names it can fail. The run stops if an input names the whole repository as a plugin or marketplace, a path with .. after another directory, or files in a submodule that the pull request has at another commit. Reading them from outside the checkout, or writing inline JSON, works as before.

Testing: the full suite passes (1,456 tests). The change was run end to end on Linux with a mock GitHub and a scripted model for settings, --settings, --mcp-config, --plugin-dir and --plugin-dir-no-mcp. Not run on a GitHub-hosted runner, macOS or Windows.


Generated by Claude Code

On pull requests from authors without write access, the action already
takes .claude, .mcp.json and similar files from the base branch. Do the
same for files and directories that the settings, plugin_marketplaces and
claude_args inputs name, where the pull request changes them, and keep
the session from editing them.

Co-authored-by: mhegazy <mhegazy@anthropic.com>
@mhegazy mhegazy self-assigned this Nov 18, 2026

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.

Comment thread src/entrypoints/run.ts
Comment on lines +382 to 388
restoredConfigPaths.push(
...restoredToolConfig,
...configPaths.replaced,
);
}
if (isOtherBranch) {
// A branch from the base has none of the pull request's version of them.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 (optional) On the usual untrusted pull request (base is the default branch), a stage-everything commit by Claude now reverts the pull request's own changes to any named plugin, settings or MCP path. run.ts:382 adds the replaced paths to restoredConfigPaths, but run.ts:389 writes .restored-paths only when isOtherBranch. Fix: pin configPaths.replaced for the commit wrapper on every untrusted checkout, not only when the config branch differs from the base.

Why this was flagged

Trigger: a non-write author's pull request targets the default branch and changes a file under a path the workflow names (e.g. --plugin-dir ci/plugin). chooseConfigBranch returns the base itself when baseBranch === defaultBranch (config-branch.ts:43), so isOtherBranch is false at run.ts:364. restoreConfigPathsFromBase overwrites the PR's files with the base version in the working tree (config-paths.ts:337-369), and run.ts:389 skips pinRestoredPaths. scripts/git-commit.sh:74-94 only excludes paths listed in .restored-paths, so git-commit.sh -a or a prior git add -A stages the base content and the commit undoes the contributor's change. On the base branch these named files stayed at the PR head, so no commit could revert them. docs/security.md:68 says only that replaced files stay in the tree; the population is new (arbitrary directories, not the fixed .claude/ list), and Claude is also denied Edit on them so it cannot put them back.

Verification: Trigger: a non-write author's PR targets the default branch, changes a file under a path the workflow's inputs name, and Claude commits with stage-everything. The only call to pinRestoredPaths is inside if (isOtherBranch) at run.ts:387-389, so scripts/.restored-paths is never written on this path. scripts/git-commit.sh:73-94 excludes or resets restored paths only when .restored-paths exists. Not pre-existing for these paths: on the base they were never replaced.

Finding ID: f-cdf96f03

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, and not changed here. It is how the other restored paths already behave when the config branch is the base, and docs/security.md says the replaced files stay in the working tree here too. Pinning both kinds would be a change of its own.


Generated by Claude Code

Comment on lines +328 to +329
if (changed.some((name) => base.has(name) && isSubmodule(base.get(name)!))) {
throw new PathError(OTHER_SUBMODULE);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 (optional) Outside contributors whose pull request is merely behind the base branch get a failed run when a named config directory holds a submodule the base has since bumped. The check at config-paths.ts:328 compares the base branch tip with the pull request's index, not with what the pull request changed, and throws before looking at the disk. Fix: stop only when the pull request itself moved the submodule (compare against the merge base), and skip a submodule that is not checked out on disk instead of throwing.

Why this was flagged

Trigger: a workflow names a directory that contains (or lies inside) a submodule, e.g. --plugin-dir plugins, and a non-write author's pull request is checked out at its head (issue_comment or pull_request_target) while the base branch has moved the submodule since the PR branched. differs at config-paths.ts:311 sees ours?.oid !== theirs?.oid because base is refs/remotes/origin/<base> tip (config-paths.ts:171) and pr is git ls-files --stage. config-paths.ts:328-329 then throws OTHER_SUBMODULE for every such name, with no check of whether the PR touched it or whether the submodule is even initialised on disk. The PathError propagates through run.ts:377 and the action fails. On the base branch this code does not exist and the same run proceeds. The docs sentence ("a submodule that the pull request has at another commit") reads as the PR moving it; a stale PR that never touched it hits the same stop, and it keeps failing until the contributor rebases.

Verification: base is the base branch tip (config-paths.ts:171) and pr is the index of the checked-out head (config-paths.ts:192). differs is true on any oid mismatch (config-paths.ts:314), with no merge-base comparison. config-paths.ts:328-329 then throws PathError(OTHER_SUBMODULE). This is before any disk check. On the base commit this call does not exist, so the same run proceeds.

Finding ID: f-e62eab9f

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct. A shallow checkout has no merge base to compare with, so this compares the two branches, as for the other files. The error says to bring the pull request up to date.


Generated by Claude Code

Comment on lines +206 to +208
denyRules: [...new Set([...targets.map(toText), ...replaced])]
.map((path) => path.replace(/[\\*?[\]]/g, "\\$&"))
.flatMap((path) => [`Edit(./${path})`, `Edit(./${path}/**)`]),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 (optional) On pull requests from authors without write access, Claude can be refused edits to unrelated files that share a name with a root-level path the workflow names. Line 208 emits Edit(./${path}), and a pattern with no inner slash matches at any depth, as docs/security.md:66 says of Edit(./.npmrc) ("in any directory"). So --plugin-dir plugin also denies src/plugin/**. Fix: anchor every rule to the repository root so it matches only the named path, including single-segment paths, for example Edit(/${path}).

Why this was flagged

This depends on Claude Code matching Edit(./name) like a gitignore pattern, which this checkout cannot show but the repository relies on: src/github/operations/tool-config.ts:414-415 says its Edit(./.npmrc) and Edit(./node_modules/**) rules "cover subdirectories too". Trigger: a run on a pull request from an author without write access (src/entrypoints/run.ts:375) whose inputs name a path directly in the repository root, such as --plugin-dir plugin or --mcp-config mcp.json. restoreConfigPaths returns Edit(./plugin) and Edit(./plugin/**) for every target, changed or not (config-paths.ts:206-208), and run.ts:419 adds them to the session's disallowed tools. Edits to src/plugin/index.ts or packages/a/mcp.json are then denied. The base branch adds no such rule. The glob escaping at config-paths.ts:207 does not anchor the pattern, and test/config-paths.test.ts:358 only covers two-segment paths, which their inner slash anchors.

Verification: src/github/operations/config-paths.ts:206-208 builds rules for every target. For target plugin this yields Edit(./plugin), which has no inner slash. src/entrypoints/run.ts:381 and :419 pass these rules into the untrusted-PR disallowed tools. The new tests pin only multi-segment paths (test/config-paths.test.ts:340, :370-373); the single-segment case is untested.

Finding ID: f-d09e0575

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct for names in the root. The rules for .npmrc and similar files match at any depth as well, and this errs on the side of denying an edit, only on these pull requests.


Generated by Claude Code

Comment on lines +247 to +250
own,
...sources.flatMap((source) =>
// A bare name can be in the marketplace's plugin root.
[source, ...(pluginRoot ? [posix.join(pluginRoot, source)] : [])].map(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 (optional) With a root marketplace that sets metadata.pluginRoot, an unrelated top-level directory named like a plugin is swapped for the base branch's version and locked against edits on untrusted pull requests. config-paths.ts:250 emits both source and pluginRoot/source as targets for every bare plugin name. Fix: when pluginRoot is set, resolve a bare name only under it, and keep the root-level form for sources written as explicit ./ paths.

Why this was flagged

Trigger: plugin_marketplaces: ./ on a repository whose base .claude-plugin/marketplace.json has metadata.pluginRoot: "./plugins" and a plugin with source: "docs" (or any name that is also a top-level directory). marketplacePaths returns docs and plugins/docs (config-paths.ts:247-253). replaceFiles then treats every tracked file under the top-level docs/ as config: files the pull request changed are overwritten with the base blob, files only the pull request has are removed (config-paths.ts:334-347), and Edit(./docs) / Edit(./docs/**) deny rules are added (config-paths.ts:205-208). Claude reviewing or fixing a documentation pull request from a non-write author sees the base text in place and cannot edit it. On the base branch none of this happens. No guard checks whether the root-level directory is a plugin at all (e.g. has .claude-plugin/plugin.json on the base branch).

Verification: Triggers when plugin_marketplaces names the repository root, the base branch's .claude-plugin/marketplace.json sets metadata.pluginRoot, and a plugin's bare source equals the name of a tracked top-level directory. Line 250 of src/github/operations/config-paths.ts builds [source, ...(pluginRoot ? [posix.join(pluginRoot, source)] : [])], so every string source yields both the root-level form and the pluginRoot form.

Finding ID: f-8ba9f263

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, and kept for now. Covering both places is the safer side if the two ways of resolving a bare name differ.


Generated by Claude Code

Comment on lines +345 to +347
for (const name of removed) {
const entry = pr.get(name)!;
if (entry.oid !== base.get(name)?.oid) snapshotForReview(name, entry);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 (optional) Low severity: a pull request that changes many files under a named directory now makes the run start two git processes per file before the session begins. replaceFiles calls snapshotForReview once per changed file at config-paths.ts:347. That helper runs git cat-file blob and then git rev-parse on every call. Its only size guard skips the root node_modules, so a plugin's vendored node_modules is copied in full. Fix: bound the snapshot work for every named directory, for example read all blobs with one readBlobs call and call ensureClaudePrExcludedFromGit once. The cost is run time and disk, not correctness.

Why this was flagged

The workflow names a directory, for example --plugin-dir plugins/foo, and an author without write access opens a pull request that adds or changes thousands of files under it. run.ts:377 calls restoreConfigPathsFromBase, and config-paths.ts:345-348 loops over every such file and calls snapshotForReview when its oid differs from the base. tool-config.ts:288 spawns git cat-file blob per file and tool-config.ts:292 calls ensureClaudePrExcludedFromGit, which spawns git rev-parse --git-path info/exclude (restore-config.ts:211-231). At a few milliseconds per synchronous spawn, 5,000 files cost about half a minute and 100,000 files several minutes, and each file is duplicated under .claude-pr/. On the base branch this helper only ran for .npmrc, bunfig.toml and package.json files. The guard at tool-config.ts:286 does not help, because rootName (tool-config.ts:87-97) matches node_modules only at the repository root.

Verification: src/github/operations/config-paths.ts:345-348 loops over every removed name and calls snapshotForReview(name, entry). There is no cap on the count. Each changed file costs two synchronous execFileSync git processes. tool-config.ts:286 skips only rootName(path) === "node_modules". On the base, snapshotForReview was private to tool-config.ts.

Finding ID: f-f63aec8e

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct. A cost only for pull requests that change very many files in a named directory. A good follow-up.


Generated by Claude Code

Comment thread test/config-paths.test.ts
Comment on lines +320 to +322
test("a name that differs in case is another file where the file system says so", () => {
write("docs/guide.md", "base\n");
write("Docs/guide.md/readme", "base\n");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 nit (optional): On a case-insensitive file system (default macOS, Windows) this test throws instead of skipping, so bun test fails for developers there. The second write at test/config-paths.test.ts:322 calls mkdirSync("Docs/guide.md", { recursive: true }). That name is the regular file docs/guide.md written one line earlier, so the call fails with EEXIST. The isCaseSensitive early return at line 325 is never reached. Fix: detect case sensitivity before the conflicting write, for example with a separate probe file, and return early on every case-insensitive file system.

Why this was flagged

The trigger is running the suite on a case-insensitive file system; it arrives through the test "a name that differs in case is another file where the file system says so" at test/config-paths.test.ts:320. The helper write at test/config-paths.test.ts:36-40 first runs mkdirSync(dirname(path), { recursive: true }). Line 321 creates the regular file docs/guide.md, and line 322 then asks for the directory Docs/guide.md, which on such a file system is that same regular file, so a recursive mkdir fails with EEXIST. The guard const isCaseSensitive = read("docs/guide.md") === "base\n" at line 323 and if (!isCaseSensitive) return; at line 325 come after the throw, and on a case-sensitive file system the comparison is always true, so the guard never skips anything. The base branch has no such test, so the suite passes there on macOS. CI runs on Linux and is not affected; no safeguard applies because the probe is placed after the failing call.

Verification: In test/config-paths.test.ts, line 321 creates the regular file docs/guide.md. Line 322 asks for the directory Docs/guide.md; on a case-insensitive file system that name is the regular file from line 321, so the helper throws. Line 325: if (!isCaseSensitive) return; is therefore dead code. .github/workflows/ci.yml:12 and :24 run bun test on ubuntu-latest only.

Finding ID: f-a0e52632

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct: the probe comes too late. CI runs on Linux, so I'll fix it with the next push rather than rerun everything for it.


Generated by Claude Code

@mhegazy
mhegazy merged commit 3c38c04 into main Nov 20, 2026
78 checks passed
@mhegazy
mhegazy deleted the claude/project-thread-a0vw5a-config branch November 20, 2026 22:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants