From 69e9facd5af12b5c66c93ca258530c9a460a46fd Mon Sep 17 00:00:00 2001 From: Angga Julyananda Pradana Date: Sun, 8 Nov 2026 11:45:35 +0700 Subject: [PATCH 1/2] fix: block command-supplied Git alias invocations --- scripts/hooks/block-no-verify.js | 76 ++++++++++++++++------------- tests/hooks/block-no-verify.test.js | 26 ++++++++++ 2 files changed, 69 insertions(+), 33 deletions(-) diff --git a/scripts/hooks/block-no-verify.js b/scripts/hooks/block-no-verify.js index 5b1cfcee2..f6b4b7423 100644 --- a/scripts/hooks/block-no-verify.js +++ b/scripts/hooks/block-no-verify.js @@ -31,8 +31,8 @@ function isHookRedirectConfigKey(key) { || /^includeif\..*\.path$/.test(normalized); } -function isHookRedirectSetting(setting) { - return isHookRedirectConfigKey(setting.split('=', 1)[0]); +function configKey(setting) { + return setting.split('=', 1)[0]; } const COMMIT_OPTIONS_WITH_VALUE = new Set([ @@ -183,23 +183,23 @@ function isGitExecutable(value) { // Only literal values from this supplied shell task are tracked. No host // environment, arbitrary expansion or external configuration is read. -function gitEnvironmentOverride(environment, budget) { +function gitEnvironmentMatchesConfig(environment, budget, matches) { const count = environment.get('GIT_CONFIG_COUNT') || ''; budget.spend(count.length + environment.size + 1); // Git uses strtoul: leading ASCII whitespace/+ are accepted, trailing bytes // and counts above INT_MAX are rejected. Bound work by assignments we own. const configured = /^[ \t\r\n\v\f]*\+?[0-9]+(?![\s\S])/.test(count) ? Number(count) : 0; if (configured > 0 && configured <= 0x7fffffff && configured <= environment.size / 2) { - let override = false; + let matched = false; let complete = true; for (let i = 0; i < configured; i++) { budget.spend(); const key = environment.get(`GIT_CONFIG_KEY_${i}`); if (key === undefined || !environment.has(`GIT_CONFIG_VALUE_${i}`)) { complete = false; break; } budget.spend(key.length + 1); - override ||= isHookRedirectConfigKey(key); + matched ||= matches(key); } - if (complete && override) return true; + if (complete && matched) return true; } const parameters = environment.get('GIT_CONFIG_PARAMETERS'); if (parameters) { @@ -209,41 +209,35 @@ function gitEnvironmentOverride(environment, budget) { for (const command of scanShell(parameters, budget).commands) { for (const word of command.words) { budget.spend(word.value.length + 1); - if (isHookRedirectSetting(word.value)) return true; + if (matches(configKey(word.value))) return true; } } } return false; } -// True when a git global option at words[index] redirects hooks via config. -// The separate-operand forms always charge the operand so budget accounting -// does not depend on whether an earlier option already matched. -function gitGlobalOptionRedirectsHooks(words, index, value, budget) { - if (value === '-c' || value === '--config-env') { - const setting = words[index + 1]?.value || ''; - budget.spend(setting.length + 1); - return isHookRedirectSetting(setting); - } - if (value.startsWith('-c')) return isHookRedirectSetting(value.slice(2)); - if (value.startsWith('--config-env=')) return isHookRedirectSetting(value.slice('--config-env='.length)); - return false; -} - -// Walk git's global options after the executable. Returns the index of the -// subcommand word and whether any option redirected the hooks path. -function scanGitGlobalOptions(words, budget, start, environmentOverride) { +// Walk the global options once so configuration can be matched against the +// command that Git will invoke. +function scanGitGlobalOptions(words, budget, start) { + const keys = []; let index = start + 1; - let override = environmentOverride; for (; index < words.length; index++) { const value = words[index].value; budget.spend(value.length + 1); if (!value.startsWith('-')) break; if (value === '--') { index++; break; } - if (gitGlobalOptionRedirectsHooks(words, index, value, budget)) override = true; + if (value === '-c' || value === '--config-env') { + const setting = words[index + 1]?.value || ''; + budget.spend(setting.length + 1); + keys.push(configKey(setting)); + } else if (value.startsWith('-c')) { + keys.push(configKey(value.slice(2))); + } else if (value.startsWith('--config-env=')) { + keys.push(configKey(value.slice('--config-env='.length))); + } if (GIT_GLOBAL_VALUES.has(value)) index++; } - return { index, override }; + return { index, keys }; } // Scan a protected subcommand's own arguments for a hook-bypass flag. @@ -263,12 +257,28 @@ function findGitBypassFlag(words, budget, start, command) { return false; } -function checkGitWords(words, budget, start = 0, environmentOverride = false) { - const { index, override } = scanGitGlobalOptions(words, budget, start, environmentOverride); +function checkGitWords(words, budget, start, environment) { + const { index, keys } = scanGitGlobalOptions(words, budget, start); const command = words[index]?.value; budget.spend((command?.length || 0) + 1); - if (!PROTECTED_GIT_COMMANDS.has(command)) return null; - if (override) return `BLOCKED: Overriding core.hooksPath is not allowed with git ${command}. Git hooks must not be bypassed.`; + if (!command) return null; + if (!PROTECTED_GIT_COMMANDS.has(command)) { + // Alias bodies can chain to other aliases or execute shell commands. + // Reject the invocation without evaluating the supplied definition. + const aliasKey = `alias.${command.toLowerCase()}`; + const aliasCommandKey = `${aliasKey}.command`; + const matches = key => { + const normalized = key.toLowerCase(); + return normalized === aliasKey || normalized === aliasCommandKey; + }; + if (keys.some(matches) || gitEnvironmentMatchesConfig(environment, budget, matches)) { + return 'BLOCKED: Invoking a command-supplied Git alias is not allowed. Run the underlying Git command directly so its flags can be checked.'; + } + return null; + } + if (keys.some(isHookRedirectConfigKey) || gitEnvironmentMatchesConfig(environment, budget, isHookRedirectConfigKey)) { + return `BLOCKED: Overriding core.hooksPath is not allowed with git ${command}. Git hooks must not be bypassed.`; + } if (findGitBypassFlag(words, budget, index + 1, command)) { return `BLOCKED: --no-verify flag is not allowed with git ${command}. Git hooks must not be bypassed.`; } @@ -997,7 +1007,7 @@ function checkCommand(input) { const word = words[index]; budget.spend(word.value.length + 1); if (isGitExecutable(word.value)) { - const reason = checkGitWords(words, budget, index, gitEnvironmentOverride(environment, budget)); + const reason = checkGitWords(words, budget, index, environment); if (reason) return reason; } if (word.value !== text && /git/i.test(word.value) && /[\s'"()]/.test(word.value)) enqueue(word.value, true, { states: [shellState(environment, budget)] }); @@ -1058,7 +1068,7 @@ function checkCommand(input) { const { words } = normalized; const reason = task.opaque || role.kind === 'opaque' ? inspectOpaque(command.words, task.text, environment) - : role.kind === 'git' ? checkGitWords(words, budget, 0, gitEnvironmentOverride(environment, budget)) : null; + : role.kind === 'git' ? checkGitWords(words, budget, 0, environment) : null; if (reason) return reason; if (role.code) { if (normalized.local && words[0]?.value === 'eval') frame.sameShellCode = role.code; diff --git a/tests/hooks/block-no-verify.test.js b/tests/hooks/block-no-verify.test.js index 2cf25a1e1..65bfcfb36 100644 --- a/tests/hooks/block-no-verify.test.js +++ b/tests/hooks/block-no-verify.test.js @@ -67,6 +67,32 @@ if (test('blocks quoted core.hooksPath override argument', () => { assert.ok(r.stderr.includes('core.hooksPath'), `stderr should mention core.hooksPath: ${r.stderr}`); })) passed++; else failed++; +// --- Command-supplied Git aliases --- + +const gitAliasCases = [ + ['inline alias with a bypass flag', 2, "git -c 'alias.ci=commit --no-verify' ci -m demo"], + ['mixed-case alias with a caller-supplied flag', 2, "git -c 'Alias.Ci=commit' ci -an -m demo"], + ['shell alias', 2, "git -c 'alias.ci=!git commit --no-verify' ci -m demo"], + ['chained alias', 2, "git -c 'alias.ci=co --no-verify' -c 'alias.co=commit' ci -m demo"], + ['alias defined with a command subsection', 2, "git -c 'alias.ci.command=commit --no-verify' ci -m demo"], + ['invoked alias with a benign definition', 2, "git -c alias.st=status st"], + ['config-env alias', 2, "git --config-env alias.ci=COMMIT_ALIAS ci -m demo"], + ['attached config-env alias', 2, "git --config-env=alias.ci=COMMIT_ALIAS ci -m demo"], + ['config count alias', 2, "GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=alias.ci GIT_CONFIG_VALUE_0='commit --no-verify' git ci -m demo"], + ['config parameters alias', 2, "GIT_CONFIG_PARAMETERS=\"'alias.ci'='commit --no-verify'\" git ci -m demo"], + ['unused alias definition', 0, "git -c 'alias.ci=commit --no-verify' status"], + ['protected builtin ignores a same-name alias', 0, "git -c 'alias.commit=commit --no-verify' commit -m demo"], + ['alias definition in message text', 0, "git commit -m 'alias.ci=commit --no-verify'"], +]; + +for (const [name, expected, command] of gitAliasCases) { + if (test(name, () => { + const result = runHook({ tool_input: { command } }); + assert.strictEqual(result.code, expected, result.stderr); + if (expected === 2) assert.match(result.stderr, /Git alias/); + })) passed++; else failed++; +} + // --- Chained command false positive prevention (Comment 2) --- if (test('does not false-positive on -n belonging to git log in a chain', () => { From b37b73248b75725c58fe14d4f5700b64e8ebc191 Mon Sep 17 00:00:00 2001 From: Angga Julyananda Pradana Date: Sun, 8 Nov 2026 12:20:15 +0700 Subject: [PATCH 2/2] fix(hooks): preserve Git built-ins with same-name aliases --- scripts/hooks/block-no-verify.js | 14 ++++++++++---- tests/hooks/block-no-verify.test.js | 9 +++++++-- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/scripts/hooks/block-no-verify.js b/scripts/hooks/block-no-verify.js index f6b4b7423..0c61a8a13 100644 --- a/scripts/hooks/block-no-verify.js +++ b/scripts/hooks/block-no-verify.js @@ -31,6 +31,7 @@ function isHookRedirectConfigKey(key) { || /^includeif\..*\.path$/.test(normalized); } +/** Return the key from a Git configuration operand, ignoring its value. */ function configKey(setting) { return setting.split('=', 1)[0]; } @@ -166,6 +167,8 @@ function isNoVerifyLongFlag(value) { return value.length >= '--no-v'.length && '--no-verify'.startsWith(value); } +// These longstanding, non-deprecated built-ins take precedence over aliases. +const GIT_BUILTINS_THAT_IGNORE_ALIASES = new Set(['diff', 'log', 'status']); const PROTECTED_GIT_COMMANDS = new Set(['commit', 'push', 'merge', 'cherry-pick', 'rebase', 'am']); const GIT_GLOBAL_VALUES = new Set(['-c', '-C', '--config-env', '--work-tree', '--git-dir', '--namespace', '--super-prefix']); const SHELLS = new Set(['sh', 'bash', 'dash', 'zsh', 'ksh']); @@ -181,8 +184,10 @@ function isGitExecutable(value) { return name === 'git' || name === 'git.exe'; } -// Only literal values from this supplied shell task are tracked. No host -// environment, arbitrary expansion or external configuration is read. +/** + * Test Git configuration keys visible in literal shell environment assignments. + * Values from the host environment and Git configuration files are opaque. + */ function gitEnvironmentMatchesConfig(environment, budget, matches) { const count = environment.get('GIT_CONFIG_COUNT') || ''; budget.spend(count.length + environment.size + 1); @@ -216,8 +221,7 @@ function gitEnvironmentMatchesConfig(environment, budget, matches) { return false; } -// Walk the global options once so configuration can be matched against the -// command that Git will invoke. +/** Return the subcommand index and literal config keys from Git global options. */ function scanGitGlobalOptions(words, budget, start) { const keys = []; let index = start + 1; @@ -257,12 +261,14 @@ function findGitBypassFlag(words, budget, start, command) { return false; } +/** Check a Git invocation for hook-bypass options or a command-supplied alias. */ function checkGitWords(words, budget, start, environment) { const { index, keys } = scanGitGlobalOptions(words, budget, start); const command = words[index]?.value; budget.spend((command?.length || 0) + 1); if (!command) return null; if (!PROTECTED_GIT_COMMANDS.has(command)) { + if (GIT_BUILTINS_THAT_IGNORE_ALIASES.has(command)) return null; // Alias bodies can chain to other aliases or execute shell commands. // Reject the invocation without evaluating the supplied definition. const aliasKey = `alias.${command.toLowerCase()}`; diff --git a/tests/hooks/block-no-verify.test.js b/tests/hooks/block-no-verify.test.js index 65bfcfb36..2b96b7967 100644 --- a/tests/hooks/block-no-verify.test.js +++ b/tests/hooks/block-no-verify.test.js @@ -80,16 +80,21 @@ const gitAliasCases = [ ['attached config-env alias', 2, "git --config-env=alias.ci=COMMIT_ALIAS ci -m demo"], ['config count alias', 2, "GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=alias.ci GIT_CONFIG_VALUE_0='commit --no-verify' git ci -m demo"], ['config parameters alias', 2, "GIT_CONFIG_PARAMETERS=\"'alias.ci'='commit --no-verify'\" git ci -m demo"], + ['status ignores a same-name inline alias', 0, 'git -c alias.status=log status --porcelain'], + ['log ignores a same-name config-env alias', 0, "GIT_LOG_ALIAS='commit --no-verify' git --config-env=alias.log=GIT_LOG_ALIAS log -1"], + ['diff ignores a same-name environment alias', 0, "GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=alias.diff GIT_CONFIG_VALUE_0='commit --no-verify' git diff"], + ['uppercase status can resolve to an alias', 2, "git -c 'alias.status=commit --no-verify' STATUS"], + ['direct commit still checks flags with a same-name alias', 2, 'git -c alias.commit=status commit --no-verify', /--no-verify flag/], ['unused alias definition', 0, "git -c 'alias.ci=commit --no-verify' status"], ['protected builtin ignores a same-name alias', 0, "git -c 'alias.commit=commit --no-verify' commit -m demo"], ['alias definition in message text', 0, "git commit -m 'alias.ci=commit --no-verify'"], ]; -for (const [name, expected, command] of gitAliasCases) { +for (const [name, expected, command, diagnostic = /Git alias/] of gitAliasCases) { if (test(name, () => { const result = runHook({ tool_input: { command } }); assert.strictEqual(result.code, expected, result.stderr); - if (expected === 2) assert.match(result.stderr, /Git alias/); + if (expected === 2) assert.match(result.stderr, diagnostic); })) passed++; else failed++; }