/*
* List the allow-list and deny-list entries init-buddy can propose, filtered to this machine and repo.
*
* node scripts/permission-candidates.mjs [--project
] [--host ]... [--home ]
*
* --host takes github|gitlab|azure|gitea|forgejo and may repeat. Without it, hosts come from the
* project's git remotes (`git remote -v`): github.com, gitlab.com, dev.azure.com and visualstudio.com,
* and codeberg.org. Other hosts are ignored; pass --host for a self-hosted instance.
* --project defaults to the current directory, --home to the user's home directory.
*
* Entries use Claude Code syntax; translate them for other harnesses. The tables below mirror
* skills/init-buddy/SKILL.md ("Propose an allow list") and references/deny-list.md.
*
* Reads only the `permissions` key of ~/.claude/settings.json, .claude/settings.json, and
* .claude/settings.local.json. Other keys can hold tokens and are never read into the output.
*
* stdout: JSON { hosts, packageManager, allow: { safe, goodToHave }, deny, alreadyPresent }.
* Exit 0 when it ran, 2 on bad usage.
*/
import { execFileSync } from 'node:child_process'
import { accessSync, constants, readFileSync, statSync } from 'node:fs'
import { homedir } from 'node:os'
import { delimiter, join, resolve } from 'node:path'
import { FORCE_PUSH_DENY, GITHUB_ADMIN_MERGE_DENY } from '../permissions/deny-entries.js'
export type Host = 'github' | 'gitlab' | 'azure' | 'gitea' | 'forgejo'
export type Manager = 'pnpm' | 'npm' | 'yarn' | 'bun'
export type Effect = 'allow' | 'deny' | 'ask'
export interface Candidate {
entry: string
reason: string
}
export interface Result {
hosts: Host[]
packageManager: Manager | null
allow: { safe: Candidate[]; goodToHave: Candidate[] }
deny: { group: string; entries: Candidate[] }[]
alreadyPresent: { entry: string; effect: Effect; file: string }[]
}
const HOSTS: Host[] = ['github', 'gitlab', 'gitea', 'forgejo', 'azure']
// SKILL.md, host read-only table.
const READ_ONLY: Record = {
github: [
'gh pr view',
'gh pr list',
'gh pr diff',
'gh pr checks',
'gh issue view',
'gh issue list',
'gh run view',
'gh run list',
'gh repo view',
'gh auth status',
],
gitlab: [
'glab mr view',
'glab mr list',
'glab mr diff',
'glab issue view',
'glab issue list',
'glab ci list',
'glab ci status',
'glab repo view',
'glab auth status',
],
gitea: ['tea pulls list', 'tea issues list', 'tea repos list', 'tea login list'],
forgejo: ['fj pr view', 'fj issue view', 'fj repo view', 'fj whoami'],
azure: [
'az repos pr list',
'az repos pr show',
'az pipelines runs list',
'az pipelines runs show',
'az account show',
],
}
// SKILL.md, safe tier: read-only git (git status/diff/log/show/branch, git stash list/show) is in
// Claude Code's built-in read-only set, so none is emitted.
const SAFE_SCRIPTS = ['test', 'lint', 'check', 'typecheck']
// SKILL.md, good-to-have tier.
const GOOD_TO_HAVE_GIT: Candidate[] = [
{ entry: 'Bash(git add *)', reason: 'The change stays in the working tree or index' },
{ entry: 'Bash(git mv *)', reason: 'The change stays in the working tree or index' },
{
entry: 'Bash(git commit *)',
reason:
'Recoverable until pushed; also covers --no-verify and --amend, so only where commit hooks are not the last check',
},
{ entry: 'Bash(git stash push *)', reason: 'The stash keeps the change' },
{ entry: 'Bash(git stash apply *)', reason: 'The stash keeps the change; apply leaves the entry in place' },
]
const FIX_SCRIPTS = ['format', 'check:fix', 'lint:fix']
// deny-list.md, Entries (Claude Code).
const FORCE_PUSH_NOTE =
'Blocks a plain --force or -f before, between, or after the arguments, and a +refspec; --force-with-lease stays allowed'
const BYPASS_MERGE: { host: Host; group: string; entries: string[]; note: string }[] = [
{
host: 'github',
group: 'Bypass merge: GitHub',
entries: GITHUB_ADMIN_MERGE_DENY,
note: 'Blocks merging past the required checks with --admin',
},
{
host: 'azure',
group: 'Bypass merge: Azure DevOps',
entries: ['Bash(az repos pr update *--bypass-policy*)', 'Bash(az repos pr create *--bypass-policy*)'],
note: 'Blocks bypassing branch policy; also blocks --bypass-policy false, which does nothing anyway',
},
{
host: 'gitea',
group: 'Bypass merge: Gitea',
entries: ['Bash(tea api *force_merge*)'],
note: "Blocks the API's force_merge, the only bypass; tea pulls merge has no force flag",
},
]
const IMMEDIATE_MERGE_GITLAB = ['Bash(glab mr merge *--auto-merge=false*)', 'Bash(glab mr merge *--auto-merge false*)']
const REPO_DELETE: Record = {
github: ['Bash(gh repo delete*)'],
gitlab: ['Bash(glab repo delete*)'],
azure: ['Bash(az repos delete*)'],
gitea: ['Bash(tea repos delete*)', 'Bash(tea repos rm*)'],
forgejo: ['Bash(fj repo delete*)'],
}
const apiDelete = (cli: string) => [
`Bash(${cli} api *-X DELETE*)`,
`Bash(${cli} api *-XDELETE*)`,
`Bash(${cli} api *--method DELETE*)`,
`Bash(${cli} api *--method=DELETE*)`,
]
const API_DELETE: Partial> = {
github: apiDelete('gh'),
gitlab: apiDelete('glab'),
gitea: apiDelete('tea'),
}
const RECURSIVE_DELETE = [
'Bash(rm -rf*)',
'Bash(rm -fr*)',
'Bash(rm -Rf*)',
'Bash(rm -fR*)',
'Bash(rm -r -f*)',
'Bash(rm -f -r*)',
'Bash(rm --recursive --force*)',
'Bash(rm --force --recursive*)',
]
// Publish entries by the package manager that must be on PATH. npm is assumed present (it ships
// with Node), so its entries are not gated.
const PUBLISH: Record = {
npm: ['Bash(npm publish*)'],
pnpm: ['Bash(pnpm publish*)', 'Bash(pnpm -r publish*)'],
yarn: ['Bash(yarn npm publish*)'],
bun: ['Bash(bun publish*)'],
}
const PUBLISH_NOTE = 'Blocks publishing a package to the registry; reordered flags and publishing scripts get past'
const CHANGESETS_BARE = 'Bash(changeset publish*)'
const CHANGESETS_BY_MANAGER: Record = {
pnpm: ['Bash(pnpm changeset publish*)', 'Bash(pnpm exec changeset publish*)'],
npm: ['Bash(npx changeset publish*)'],
yarn: ['Bash(yarn changeset publish*)'],
bun: ['Bash(bunx changeset publish*)'],
}
const CHANGESETS_NOTE =
'Blocks publishing the pending changesets; a rule matches the first word, so each manager form needs its own'
const SECRETS_GENERIC = ['Read(**/.env*)', 'Read(~/.ssh/**)', 'Read(~/.aws/**)', 'Read(~/.npmrc)']
const SECRETS_NOTES: Record = {
'Read(**/.env*)': 'Blocks reading env files; also hides .env.example',
'Read(~/.ssh/**)': 'Blocks reading SSH keys',
'Read(~/.aws/**)': 'Blocks reading AWS credentials',
'Read(~/.npmrc)': 'Blocks reading the npm auth token',
}
const SECRETS_HOST: Record = {
github: 'Read(~/.config/gh/hosts.yml)',
gitlab: 'Read(~/.config/glab-cli/**)',
azure: 'Read(~/.azure/**)',
gitea: 'Read(~/.config/tea/**)',
forgejo: 'Read(~/.local/share/forgejo-cli/**)',
}
const REMOTE_HOSTS: [string, Host][] = [
['github.com', 'github'],
['gitlab.com', 'gitlab'],
['dev.azure.com', 'azure'],
['visualstudio.com', 'azure'],
['codeberg.org', 'forgejo'],
]
function usage(message: string): never {
process.stderr.write(`${message}\n`)
process.stderr.write('usage: permission-candidates.mjs [--project ] [--host ]... [--home ]\n')
process.exit(2)
}
interface Opts {
project?: string
home?: string
hosts: Host[]
}
function parseArgs(argv: string[]): Opts {
const opts: Opts = { hosts: [] }
for (let i = 0; i < argv.length; i++) {
const a = argv[i] as string
if (a !== '--project' && a !== '--host' && a !== '--home') usage(`unknown argument "${a}"`)
const value = argv[i + 1]
if (value === undefined) usage(`${a} needs a value`)
if (a === '--host') {
if (!HOSTS.includes(value as Host)) usage(`unknown host "${value}"`)
opts.hosts.push(value as Host)
} else if (a === '--project') opts.project = value
else opts.home = value
i++
}
return opts
}
function readJson(file: string): Record | undefined {
try {
const parsed: unknown = JSON.parse(readFileSync(file, 'utf8'))
return parsed && typeof parsed === 'object' && !Array.isArray(parsed)
? (parsed as Record)
: undefined
} catch {
return undefined
}
}
function onPath(name: string): boolean {
const exts = process.platform === 'win32' ? (process.env['PATHEXT'] ?? '.EXE;.CMD').split(';') : ['']
for (const dir of (process.env['PATH'] ?? '').split(delimiter)) {
if (!dir) continue
for (const ext of exts) {
const file = join(dir, name + ext)
try {
if (!statSync(file).isFile()) continue
accessSync(file, constants.X_OK)
return true
} catch {}
}
}
return false
}
function hostOfUrl(url: string): string | undefined {
return /^[a-z+]+:\/\/(?:[^@/]*@)?([^/:]+)/i.exec(url)?.[1] ?? /^(?:[^@/\s]+@)?([^:/\s]+):/.exec(url)?.[1]
}
function hostsFromRemotes(dir: string): Host[] {
let out: string
try {
out = execFileSync('git', ['-C', dir, 'remote', '-v'], { encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] })
} catch {
return []
}
const found = new Set()
for (const line of out.split('\n')) {
const url = line.split(/\s+/)[1]
const hostname = url && hostOfUrl(url)?.toLowerCase()
if (!hostname) continue
for (const [suffix, kind] of REMOTE_HOSTS) {
if (hostname === suffix || hostname.endsWith(`.${suffix}`)) found.add(kind)
}
}
return [...found]
}
function detectManager(pkg: Record | undefined, dir: string): Manager | null {
const field = typeof pkg?.['packageManager'] === 'string' ? pkg['packageManager'].split('@')[0] : undefined
if (field === 'pnpm' || field === 'npm' || field === 'yarn' || field === 'bun') return field
const locks: [string, Manager][] = [
['pnpm-lock.yaml', 'pnpm'],
['package-lock.json', 'npm'],
['yarn.lock', 'yarn'],
['bun.lock', 'bun'],
['bun.lockb', 'bun'],
]
for (const [file, manager] of locks) {
try {
if (statSync(join(dir, file)).isFile()) return manager
} catch {}
}
return null
}
function scriptEntry(manager: Manager, script: string): string {
if (manager === 'npm') return script === 'test' ? 'Bash(npm test *)' : `Bash(npm run ${script} *)`
if (manager === 'bun') return `Bash(bun run ${script} *)`
return `Bash(${manager} ${script} *)`
}
// `Bash(x:*)` and `Bash(x *)` are the same rule.
const normalize = (entry: string) => entry.replace(/:\*\)$/, ' *)')
function loadPresent(files: string[]): { entry: string; effect: Effect; file: string }[] {
const present: { entry: string; effect: Effect; file: string }[] = []
for (const file of files) {
const permissions = readJson(file)?.['permissions']
if (!permissions || typeof permissions !== 'object') continue
for (const effect of ['allow', 'deny', 'ask'] as const) {
const list = (permissions as Record)[effect]
if (!Array.isArray(list)) continue
for (const entry of list) if (typeof entry === 'string') present.push({ entry: normalize(entry), effect, file })
}
}
return present
}
export function candidates(opts: { project: string; home: string; hosts: Host[] }): Result {
const pkg = readJson(join(opts.project, 'package.json'))
const scripts = (pkg?.['scripts'] && typeof pkg['scripts'] === 'object' ? pkg['scripts'] : {}) as Record<
string,
unknown
>
const manager = detectManager(pkg, opts.project)
const hosts = HOSTS.filter((h) => opts.hosts.includes(h))
const safe: Candidate[] = []
for (const h of hosts) {
for (const cmd of READ_ONLY[h]) safe.push({ entry: `Bash(${cmd} *)`, reason: `Read-only ${h} CLI command` })
}
const goodToHave = [...GOOD_TO_HAVE_GIT]
if (manager) {
for (const s of SAFE_SCRIPTS) {
if (s in scripts)
safe.push({
entry: scriptEntry(manager, s),
reason: `Runs the repo's \`${s}\` script, which only reads; read it first`,
})
}
for (const s of FIX_SCRIPTS) {
if (s in scripts)
goodToHave.push({
entry: scriptEntry(manager, s),
reason: `Runs the repo's \`${s}\` script, which rewrites files that git can restore; read it first`,
})
}
}
const has = (m: Manager) => m === 'npm' || onPath(m)
const managers = (['pnpm', 'npm', 'yarn', 'bun'] as const).filter(has)
const mk = (entries: string[], reason: string): Candidate[] => entries.map((entry) => ({ entry, reason }))
const deny: { group: string; entries: Candidate[] }[] = [
{ group: 'Force push', entries: mk(FORCE_PUSH_DENY, FORCE_PUSH_NOTE) },
]
for (const b of BYPASS_MERGE)
if (hosts.includes(b.host)) deny.push({ group: b.group, entries: mk(b.entries, b.note) })
if (hosts.includes('gitlab'))
deny.push({
group: 'Immediate merge: GitLab',
entries: mk(
IMMEDIATE_MERGE_GITLAB,
'Blocks merging at once past a required pipeline; glab has no other bypass flag',
),
})
deny.push({
group: 'Repo delete',
entries: hosts.flatMap((h) =>
mk(REPO_DELETE[h], 'Blocks deleting the repository; other aliases the CLI accepts get past'),
),
})
deny.push({
group: 'API delete',
entries: hosts.flatMap((h) => mk(API_DELETE[h] ?? [], 'Blocks an API DELETE call; a lowercase delete gets past')),
})
deny.push({
group: 'Recursive delete',
entries: mk(RECURSIVE_DELETE, 'Blocks recursive forced deletes, including cleaning node_modules'),
})
deny.push({ group: 'Publish', entries: managers.flatMap((m) => mk(PUBLISH[m], PUBLISH_NOTE)) })
const deps = { ...(pkg?.['dependencies'] as object), ...(pkg?.['devDependencies'] as object) } as Record<
string,
unknown
>
if ('@changesets/cli' in deps)
deny.push({
group: 'Publish: Changesets',
entries: [
...mk([CHANGESETS_BARE], CHANGESETS_NOTE),
...managers.flatMap((m) => mk(CHANGESETS_BY_MANAGER[m], CHANGESETS_NOTE)),
],
})
deny.push({
group: 'Secrets',
entries: [
...SECRETS_GENERIC.map((entry) => ({ entry, reason: SECRETS_NOTES[entry] as string })),
...hosts.map((h) => ({ entry: SECRETS_HOST[h], reason: `Blocks reading the ${h} CLI login file` })),
],
})
const present = loadPresent([
join(opts.home, '.claude', 'settings.json'),
join(opts.project, '.claude', 'settings.json'),
join(opts.project, '.claude', 'settings.local.json'),
])
const alreadyPresent: Result['alreadyPresent'] = []
const keep = (list: Candidate[]) =>
list.filter((c) => {
const hit = present.find((p) => p.entry === normalize(c.entry))
if (hit) alreadyPresent.push({ entry: c.entry, effect: hit.effect, file: hit.file })
return !hit
})
const result: Result = {
hosts,
packageManager: manager,
allow: { safe: keep(safe), goodToHave: keep(goodToHave) },
deny: deny.map((g) => ({ group: g.group, entries: keep(g.entries) })).filter((g) => g.entries.length),
alreadyPresent,
}
return result
}
export async function main(argv: string[] = process.argv.slice(2)): Promise {
const opts = parseArgs(argv)
const project = resolve(opts.project ?? process.cwd())
const hosts = opts.hosts.length ? opts.hosts : hostsFromRemotes(project)
const result = candidates({ project, home: resolve(opts.home ?? homedir()), hosts })
process.stdout.write(`${JSON.stringify(result, null, 2)}\n`)
}
// Process-boundary guard: only fires when the bundle runs directly as a script.
/* istanbul ignore next -- process.argv entrypoint guard, covered by bundles.spec.ts (child process) */
if (process.argv[1]?.endsWith('permission-candidates.mjs')) await main()