From 950f8ce0f32aa3cf88aced8aa71c56ea54d12bf1 Mon Sep 17 00:00:00 2001 From: haelyra <49814733+haelyra@users.noreply.github.com> Date: Thu, 5 Nov 2026 10:43:53 +0100 Subject: [PATCH] fix(hooks): validate Bash commit here-document substitutions The commit-quality half of 7632669d from #3735: a single-quoted -m message keeps its backslashes, and an unquoted here-document whose body Bash would expand yields an advisory that the subject cannot be validated from the literal command. (split from commit 7632669d287118f412298d0c2b600723d114e084) --- scripts/hooks/pre-bash-commit-quality.js | 35 +++++++++++++++++---- tests/hooks/pre-bash-commit-quality.test.js | 22 +++++++++++++ 2 files changed, 51 insertions(+), 6 deletions(-) diff --git a/scripts/hooks/pre-bash-commit-quality.js b/scripts/hooks/pre-bash-commit-quality.js index 7869a6993..bfd705f42 100644 --- a/scripts/hooks/pre-bash-commit-quality.js +++ b/scripts/hooks/pre-bash-commit-quality.js @@ -167,22 +167,35 @@ function findFileIssues(filePath) { } /** - * The body of the here-document that `value` starts with, as in `$(cat <<'EOF'`, or null. + * The body and expansion mode of the here-document that `value` starts with, + * as in `$(cat <<'EOF'`, or null. * It ends at the line that holds only the delimiter, after leading tabs for <<-, which also * strips them from the body. * @param {string} value - * @returns {string|null} + * @returns {{body: string, expands: boolean}|null} */ function heredocBody(value) { const opener = /^\$\(\s*cat\s+<<(-?)\s*(['"]?)([A-Za-z_][\w.-]*)\2[ \t]*\r?\n/.exec(value); if (!opener) return null; - const [, stripTabs, , delimiter] = opener; + const [, stripTabs, quote, delimiter] = opener; const lines = value.slice(opener[0].length).split('\n').map(line => { const text = line.replace(/\r$/, ''); return stripTabs ? text.replace(/^\t+/, '') : text; }); const end = lines.indexOf(delimiter); - return end === -1 ? null : lines.slice(0, end).join('\n'); + return end === -1 ? null : { body: lines.slice(0, end).join('\n'), expands: quote === '' }; +} + +// Bash expands parameters, commands and arithmetic in an unquoted here-document. +// Only backslashes before $, `, \\ and newline suppress the following character. +function hasHeredocExpansion(body) { + for (let i = 0; i < body.length; i++) { + const ch = body[i]; + const next = body[i + 1] || ''; + if (ch === '\\' && next && '$`\\\n'.includes(next)) { i++; continue; } + if (ch === '`' || (ch === '$' && next && (/[A-Za-z0-9_]/.test(next) || '$@*#?!-({['.includes(next)))) return true; + } + return false; } /** @@ -196,7 +209,7 @@ function validateCommitMessage(command) { // newline) so an embedded escaped quote does not truncate the subject, and allowing // the OTHER quote char inside the body; when unquoted, capture the full remaining // tail, not just the first token) - const messageMatch = command.match(/(?:-m|--message)[=\s]+(?:"((?:\\[\s\S]|[^"\\])*)"|'((?:\\.|[^'\\])*)'|([^"']+?)\s*$)/); + const messageMatch = command.match(/(?:-m|--message)[=\s]+(?:"((?:\\[\s\S]|[^"\\])*)"|'([^']*)'|([^"']+?)\s*$)/); if (!messageMatch) return null; // A message written as -m "$(cat <<'EOF' ... EOF)", as Claude Code writes them, is the @@ -204,10 +217,20 @@ function validateCommitMessage(command) { const heredoc = messageMatch[1]?.startsWith('$(') ? heredocBody(command.slice(messageMatch.index + messageMatch[0].indexOf('"') + 1)) : null; + if (heredoc?.expands && hasHeredocExpansion(heredoc.body)) { + return { + message: null, + issues: [{ + type: 'unverifiable', + message: 'Cannot validate a commit message with unquoted here-document expansions', + suggestion: 'Quote the here-document delimiter to keep the message literal' + }] + }; + } // In double quotes, Bash drops the backslash before $ ` " \ and drops a backslash-newline, // a line continuation, altogether. Before any other character the backslash stays. const doubleQuoted = messageMatch[1]?.replace(/\\([\n$`"\\])/g, (escape, next) => (next === '\n' ? '' : next)); - const message = heredoc ?? doubleQuoted ?? messageMatch[2] ?? messageMatch[3]; + const message = heredoc?.body ?? doubleQuoted ?? messageMatch[2] ?? messageMatch[3]; // The format rules apply to the subject: git drops leading blank lines and trailing // whitespace, and the first line left is the subject. const subject = (message.split('\n').find(line => line.trim()) || '').trimEnd(); diff --git a/tests/hooks/pre-bash-commit-quality.test.js b/tests/hooks/pre-bash-commit-quality.test.js index faa14c932..68f5bfced 100644 --- a/tests/hooks/pre-bash-commit-quality.test.js +++ b/tests/hooks/pre-bash-commit-quality.test.js @@ -525,6 +525,28 @@ if (test('still reports a bad subject inside a here-document', () => { assert.deepStrictEqual(res.issues.map(i => i.type), ['format', 'punctuation']); })) passed++; else failed++; +if (test('warns when an unquoted here-document can expand the commit message', () => { + const command = 'git commit -m "$(cat < i.type), ['unverifiable']); + assert.deepStrictEqual( + hook.validateCommitMessage('git commit -m "$(cat <<\'EOF\'\nfix: ${EMPTY}\nEOF\n)"').issues, + [] + ); + assert.deepStrictEqual( + hook.validateCommitMessage('git commit -m "$(cat < { + const res = hook.validateCommitMessage(String.raw`git commit -m 'Update path\'`); + assert.ok(res, 'expected a validation result for valid Bash quoting'); + assert.strictEqual(res.message, 'Update path' + String.fromCharCode(92)); + assert.ok(res.issues.some(issue => issue.type === 'format')); +})) passed++; else failed++; + if (test('drops a line continuation from a double-quoted message, as Bash does', () => { const res = hook.validateCommitMessage('git commit -m "\\\nfix: handle empty input"'); assert.strictEqual(res.message, 'fix: handle empty input');