Repository navigation
ORB-170: Gate root files with a closed allowlist #680
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
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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,67 @@ | ||
| import { spawnSync } from "node:child_process" | ||
| import { cpSync, mkdirSync, writeFileSync } from "node:fs" | ||
| import { join } from "node:path" | ||
|
|
||
| import { T, check, root, toolPath } from "./_harness.mjs" | ||
|
|
||
| const stageRepository = (label, allowedFiles = []) => { | ||
| const repository = join(root, "root-allowlist", label) | ||
| const tools = join(repository, "tools") | ||
| mkdirSync(tools, { recursive: true }) | ||
| cpSync(toolPath("check-root-allowlist.mjs"), join(tools, "check-root-allowlist.mjs")) | ||
| writeFileSync(join(tools, "root-allowlist.json"), `${JSON.stringify(allowedFiles, null, 2)}\n`) | ||
| return { repository, script: join(tools, "check-root-allowlist.mjs") } | ||
| } | ||
|
|
||
| const checkUndeclaredFile = (name) => { | ||
| const fixture = stageRepository(name.replaceAll(/[^a-z]/g, "-")) | ||
| writeFileSync(join(fixture.repository, name), "throwaway\n") | ||
| check( | ||
| "check-root-allowlist.mjs", | ||
| `rejects undeclared root file ${name}`, | ||
| [], | ||
| { status: 1, stderr: new RegExp(name.replaceAll(".", "\\.")) }, | ||
| { path: fixture.script, cwd: fixture.repository }, | ||
| ) | ||
| } | ||
|
|
||
| export const cases = () => { | ||
| const clean = stageRepository("clean-linked-worktree") | ||
| writeFileSync(join(clean.repository, ".git"), "gitdir: elsewhere\n") | ||
| check( | ||
| "check-root-allowlist.mjs", | ||
| "accepts a clean linked worktree where .git is a file", | ||
| [], | ||
| { status: 0 }, | ||
| { path: clean.script, cwd: clean.repository }, | ||
| ) | ||
|
|
||
| checkUndeclaredFile(".agent-scratch-extract.mjs") | ||
| checkUndeclaredFile(".tmp-extract.mjs") | ||
| checkUndeclaredFile("extract-tmp.mjs") | ||
|
|
||
| const ignored = stageRepository("ignored-untracked", [".gitignore"]) | ||
| writeFileSync(join(ignored.repository, ".gitignore"), ".tmp-*\n") | ||
| writeFileSync(join(ignored.repository, ".tmp-extract.mjs"), "throwaway\n") | ||
| spawnSync("git", ["init", "-q"], { cwd: ignored.repository }) | ||
| const checkIgnored = spawnSync("git", ["check-ignore", ".tmp-extract.mjs"], { cwd: ignored.repository }) | ||
| const checkUntracked = spawnSync("git", ["ls-files", "--error-unmatch", ".tmp-extract.mjs"], { cwd: ignored.repository }) | ||
| T("check-root-allowlist.mjs: fixture is gitignored and untracked", checkIgnored.status === 0 && checkUntracked.status !== 0) | ||
| check( | ||
| "check-root-allowlist.mjs", | ||
| "rejects a gitignored and untracked root file", | ||
| [], | ||
| { status: 1, stderr: /\.tmp-extract\.mjs/ }, | ||
| { path: ignored.script, cwd: ignored.repository }, | ||
| ) | ||
|
|
||
| const declared = stageRepository("declared", ["deliberate-root-file.md"]) | ||
| writeFileSync(join(declared.repository, "deliberate-root-file.md"), "architecture\n") | ||
| check( | ||
| "check-root-allowlist.mjs", | ||
| "accepts a root file named in the allowlist data", | ||
| [], | ||
| { status: 0 }, | ||
| { path: declared.script, cwd: declared.repository }, | ||
| ) | ||
| } |
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,65 @@ | ||
| #!/usr/bin/env node | ||
| /** | ||
| * Gate repository-root files by location, not by guessed filename patterns. | ||
| * | ||
| * The allowlist is deliberately a closed set: every unlisted root file fails regardless of its | ||
| * name, extension, or leading dot. Adding a legitimate root file therefore requires a visible | ||
| * data change instead of another pattern that only recognizes shapes already seen. | ||
| */ | ||
|
|
||
| import { readdirSync, readFileSync } from "node:fs" | ||
| import { dirname, join, resolve } from "node:path" | ||
| import { fileURLToPath } from "node:url" | ||
|
|
||
| const USAGE = `usage: check-root-allowlist.mjs | ||
|
|
||
| Fails when the repository root contains a file not named in root-allowlist.json. | ||
| Directories are outside this gate. The .git worktree pointer is always ignored. | ||
|
|
||
| --help, -h print this usage and exit 0 | ||
|
|
||
| exit codes: 0 every root file is declared, 1 undeclared root files exist, 2 usage or configuration error` | ||
|
|
||
| if (process.argv.includes("--help") || process.argv.includes("-h")) { | ||
| console.log(USAGE) | ||
| process.exit(0) | ||
| } | ||
|
|
||
| if (process.argv.length > 2) { | ||
| console.error(`check-root-allowlist: takes no arguments, got: ${process.argv.slice(2).join(" ")}\n`) | ||
| console.error(USAGE) | ||
| process.exit(2) | ||
| } | ||
|
|
||
| const toolsDirectory = dirname(fileURLToPath(import.meta.url)) | ||
| const allowlistPath = join(toolsDirectory, "root-allowlist.json") | ||
| const repositoryRoot = resolve(toolsDirectory, "..") | ||
|
|
||
| let allowedNames | ||
| try { | ||
| allowedNames = JSON.parse(readFileSync(allowlistPath, "utf8")) | ||
| } catch (error) { | ||
| console.error(`check-root-allowlist: cannot read ${allowlistPath}: ${error.message}`) | ||
| process.exit(2) | ||
| } | ||
|
|
||
| if ( | ||
| !Array.isArray(allowedNames) || | ||
| allowedNames.some((name) => typeof name !== "string" || name.length === 0) || | ||
| new Set(allowedNames).size !== allowedNames.length | ||
| ) { | ||
| console.error("check-root-allowlist: root-allowlist.json must be an array of unique, non-empty filenames") | ||
| process.exit(2) | ||
| } | ||
|
|
||
| const allowed = new Set(allowedNames) | ||
| const undeclared = readdirSync(repositoryRoot, { withFileTypes: true }) | ||
| .filter((entry) => entry.name !== ".git" && !entry.isDirectory() && !allowed.has(entry.name)) | ||
| .map((entry) => entry.name) | ||
| .sort() | ||
|
|
||
| if (undeclared.length > 0) { | ||
| console.error("Root allowlist violation: declare or remove these repository-root files:") | ||
| for (const name of undeclared) console.error(` ${name}`) | ||
| process.exit(1) | ||
| } |
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,23 @@ | ||
| [ | ||
| ".claudeignore", | ||
| ".gitattributes", | ||
| ".gitignore", | ||
| ".npmrc", | ||
| "AGENTS.md", | ||
| "CLAUDE.md", | ||
| "DESIGN.md", | ||
| "FEATURES.md", | ||
| "README.md", | ||
| "RENDER-CORRECTNESS.md", | ||
| "TESTING.md", | ||
| "app.json", | ||
| "architecture.html", | ||
| "architecture.json", | ||
| "lefthook.yml", | ||
| "package-lock.json", | ||
| "package.json", | ||
| "sonar-project.properties", | ||
| "tsconfig.base.json", | ||
| "tsconfig.json", | ||
| "turbo.json" | ||
| ] |
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
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.
When a developer has a legitimate local root
.env/.env.localfile (patterns this repo still ignores in.gitignore), this new pre-commit command runscheck-root-allowlist, whosereaddirSyncscan includes ignored and untracked root files, so every commit is blocked even when nothing root-related is staged. Because contributors are also told not to bypass git hooks, this breaks the local secret/env workflow; either skip known local-only ignored files in the hook or run the closed allowlist against tracked/CI contents only.AGENTS.md reference: AGENTS.md:L78-L80
Useful? React with 👍 / 👎.