Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 53 additions & 1 deletion scripts/hooks/block-no-verify.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ const COMMIT_OPTIONS_WITH_VALUE = new Set([
'--fixup',
'--squash',
'--pathspec-from-file',
// `git commit --trailer "Key: value"` — the value is data, so a token that
// looks like a flag must not be classified as one.
'--trailer',
]);

const COMMIT_OPTIONS_WITH_INLINE_VALUE = [
Expand Down Expand Up @@ -247,6 +250,50 @@ function getCommitShortValueOption(value) {
return null;
}

/**
* `git push` options that consume the FOLLOWING token as their value. Without
* these, `git push --push-option --no-verify` reads the value as a flag and is
* refused even though git sends it verbatim to the server and still runs the
* hooks. Long `--flag=value` forms need no entry: they are a single token.
*/
const PUSH_OPTIONS_WITH_VALUE = new Set([
'-o',
'--push-option',
'--receive-pack',
'--exec',
'--repo',
]);
// NOT in the set: `--force-with-lease`. Its value is OPTIONAL and inline-only,
// so the bare form consumes nothing and the next token is a real flag. Listing
// it would make `git push --force-with-lease --no-verify` skip the very flag
// this hook exists to catch. Confirmed against git: every option above answers
// "requires a value" when given none, while a bare `git push
// --force-with-lease` parses and fails later on the push destination.

/**
* Git resolves any UNAMBIGUOUS prefix of a long option, so `--no-verif` and
* `--no-veri` bypass the hooks exactly as `--no-verify` does. Matching the full
* spelling alone let two characters off the end walk straight past this gate.
*
* Verified against real git with a failing pre-commit hook:
* git commit -> hook ran, commit refused
* git commit --no-verif -> committed, hook skipped
* git commit --no-veri -> committed, hook skipped
*
* Matching every prefix cannot catch `--no-verbose`, because that is not a
* prefix of `--no-verify`. The shortest forms (`--no-v`, `--no-ve`) are
* ambiguous and git rejects them itself, so treating them as a bypass attempt
* costs nothing — no working command uses them.
*
* @param {string} value
* @returns {boolean}
*/
function isNoVerifyLongFlag(value) {
// `--no-` alone is not an attempt at anything; require at least one more char.
if (value.length <= '--no-'.length) return false;
return '--no-verify'.startsWith(value);
}

function isCommitNoVerifyShortFlag(value) {
if (!value.startsWith('-') || value.startsWith('--') || value === '-') {
return false;
Expand Down Expand Up @@ -422,7 +469,12 @@ function hasNoVerifyFlag(input, command, offset) {
}
}

if (value === '--no-verify') return true;
if (command === 'push' && PUSH_OPTIONS_WITH_VALUE.has(value)) {
skipNext = true;
continue;
}
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved

if (isNoVerifyLongFlag(value)) return true;
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved

// For commit, -n is shorthand for --no-verify.
if (command === 'commit' && isCommitNoVerifyShortFlag(value)) {
Expand Down
94 changes: 94 additions & 0 deletions tests/hooks/block-no-verify.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,100 @@ 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++;

// --- Abbreviated long flags ---
// Git resolves any unambiguous prefix of a long option, so these bypass the
// hooks exactly as the full spelling does. Verified against real git with a
// failing pre-commit hook: `git commit --no-verif` and `--no-veri` both commit.

for (const flag of ['--no-verif', '--no-veri', '--no-ver', '--no-ve', '--no-v']) {
if (test(`blocks abbreviated ${flag} on git commit`, () => {
const r = runHook({ tool_input: { command: `git commit ${flag} -m "msg"` } });
assert.strictEqual(r.code, 2, `expected exit 2, got ${r.code}`);
assert.ok(r.stderr.includes('BLOCKED'), `stderr should contain BLOCKED: ${r.stderr}`);
})) passed++; else failed++;
}

if (test('blocks abbreviated --no-verif on git push', () => {
const r = runHook({ tool_input: { command: 'git push --no-verif' } });
assert.strictEqual(r.code, 2, `expected exit 2, got ${r.code}`);
assert.ok(r.stderr.includes('BLOCKED'), `stderr should contain BLOCKED: ${r.stderr}`);
})) passed++; else failed++;

// The guard must match prefixes OF --no-verify, not everything starting with
// --no-. `--no-verbose` is a real, harmless git option and is not a prefix of
// --no-verify, so it must stay allowed.
if (test('still allows --no-verbose (not a prefix of --no-verify)', () => {
const r = runHook({ tool_input: { command: 'git commit --no-verbose -m "msg"' } });
assert.strictEqual(r.code, 0, `expected exit 0, got ${r.code}: ${r.stderr}`);
})) passed++; else failed++;

if (test('still allows --no-edit', () => {
const r = runHook({ tool_input: { command: 'git commit --no-edit --amend' } });
assert.strictEqual(r.code, 0, `expected exit 0, got ${r.code}: ${r.stderr}`);
})) passed++; else failed++;

if (test('still allows the literal text in a commit message', () => {
const r = runHook({ tool_input: { command: 'git commit -m "no-verif was the bug"' } });
assert.strictEqual(r.code, 0, `expected exit 0, got ${r.code}: ${r.stderr}`);
})) passed++; else failed++;

// --- Value-taking options must not have their VALUE read as a flag ---
// `--push-option`/`-o`/`--trailer` consume the next token as data. Git sends it
// verbatim and still runs the hooks, so refusing these is a false positive.
// These were already refused for the full spelling before abbreviations were
// matched; handling the option arity fixes both.

for (const command of [
'git push --push-option --no-verify',
'git push --push-option --no-verif',
'git push -o --no-verify',
'git push --receive-pack --no-verify',
'git commit --trailer --no-verify -m "msg"',
'git commit --trailer --no-verif -m "msg"',
]) {
if (test(`does not block a value-taking option's value: ${command}`, () => {
const r = runHook({ tool_input: { command } });
assert.strictEqual(r.code, 0, `expected exit 0, got ${r.code}: ${r.stderr}`);
})) passed++; else failed++;
}

// `--force-with-lease` takes an OPTIONAL, inline-only value, so the bare form
// consumes nothing and the next token is a real flag. Confirmed against git:
// every option in PUSH_OPTIONS_WITH_VALUE answers "requires a value" when given
// none, while a bare `git push --force-with-lease` parses and fails later on the
// push destination. Treating it as value-taking would skip the very flag this
// hook exists to catch.
for (const command of [
'git push --force-with-lease --no-verify',
'git push --force-with-lease --no-verif',
'git push --force-with-lease=main:abc123 --no-verify',
]) {
if (test(`still blocks no-verify after --force-with-lease: ${command}`, () => {
const r = runHook({ tool_input: { command } });
assert.strictEqual(r.code, 2, `expected exit 2, got ${r.code}`);
assert.ok(r.stderr.includes('BLOCKED'), `stderr should contain BLOCKED: ${r.stderr}`);
})) passed++; else failed++;
}

if (test('still allows a bare --force-with-lease push', () => {
const r = runHook({ tool_input: { command: 'git push --force-with-lease' } });
assert.strictEqual(r.code, 0, `expected exit 0, got ${r.code}: ${r.stderr}`);
})) passed++; else failed++;

// The inline `=value` form consumes nothing, so a real flag after it must still
// be caught — otherwise the arity handling above becomes its own bypass.
if (test('still blocks --no-verify after an inline --push-option=value', () => {
const r = runHook({ tool_input: { command: 'git push --push-option=ci.skip --no-verify' } });
assert.strictEqual(r.code, 2, `expected exit 2, got ${r.code}`);
assert.ok(r.stderr.includes('BLOCKED'), `stderr should contain BLOCKED: ${r.stderr}`);
})) passed++; else failed++;

if (test('still blocks --no-verif after an inline --trailer=value', () => {
const r = runHook({ tool_input: { command: 'git commit --trailer=Key:val --no-verif -m "msg"' } });
assert.strictEqual(r.code, 2, `expected exit 2, got ${r.code}`);
assert.ok(r.stderr.includes('BLOCKED'), `stderr should contain BLOCKED: ${r.stderr}`);
})) 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', () => {
Expand Down