Merge remote-tracking branch 'origin/pr-3894' into stewardship/20261116

This commit is contained in:
haelyra committed 2026-11-16 13:20:47 -05:00
commit 88e8a4db07
2 files changed
+82 -35

No files matched your search

+51 -35
View File
@@ -42,8 +42,9 @@ function isHookRedirectConfigKey(key) {
|| /^includeif\..*\.path$/.test(normalized);
}
function isHookRedirectSetting(setting) {
return isHookRedirectConfigKey(setting.split('=', 1)[0]);
/** Return the key from a Git configuration operand, ignoring its value. */
function configKey(setting) {
return setting.split('=', 1)[0];
}
const COMMIT_OPTIONS_WITH_VALUE = new Set([
@@ -177,6 +178,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']);
@@ -192,25 +195,27 @@ 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.
function gitEnvironmentOverride(environment, budget) {
/**
* 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);
// 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) {
@@ -220,41 +225,34 @@ 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) {
/** Return the subcommand index and literal config keys from Git global options. */
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.
@@ -274,12 +272,30 @@ function findGitBypassFlag(words, budget, start, command) {
return false;
}
function checkGitWords(words, budget, start = 0, environmentOverride = false) {
const { index, override } = scanGitGlobalOptions(words, budget, start, environmentOverride);
/** 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 (!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)) {
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()}`;
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.`;
}
@@ -1205,7 +1221,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)] });
@@ -1266,7 +1282,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;
+31
View File
@@ -67,6 +67,37 @@ 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"],
['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, 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, diagnostic);
})) 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', () => {