From fee3d086669bab9e757f4e578163a09ad45e253f Mon Sep 17 00:00:00 2001 From: L4XB Date: Thu, 5 Nov 2026 14:36:26 +0100 Subject: [PATCH] fix(hooks): drop line continuations before checking text after a commit here-document In double quotes Bash drops a backslash-newline before it expands anything, so a tail of `$\HOME` after the substitution's `)` is `$HOME` and expands. The expansion check read the tail with the continuation still in it and saw an escaped newline, so the hook validated `fix: a$HOME` with no warning. Continuations are now dropped first, while other escapes stay for the check: `\$\HOME` is still the literal `$HOME`. --- scripts/hooks/pre-bash-commit-quality.js | 5 ++++- tests/hooks/pre-bash-commit-quality.test.js | 10 ++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/scripts/hooks/pre-bash-commit-quality.js b/scripts/hooks/pre-bash-commit-quality.js index 278fd20c0..bfa1dd040 100644 --- a/scripts/hooks/pre-bash-commit-quality.js +++ b/scripts/hooks/pre-bash-commit-quality.js @@ -244,7 +244,10 @@ function readHeredocMessage(value, heredoc) { } }; } const after = /^[ \t\r\n]*\)((?:\\[\s\S]|[^"\\])*)"/.exec(value.slice(heredoc.end)); - if (!after || hasHeredocExpansion(after[1])) { + // In double quotes Bash drops a backslash-newline before it expands anything, so + // `$\HOME` is `$HOME`; other escapes stay for the expansion check to skip. + const continued = after && after[1].replace(/\\([\s\S])/g, (escape, next) => (next === '\n' ? '' : escape)); + if (!after || hasHeredocExpansion(continued)) { return { issue: { type: 'unverifiable', message: 'Cannot validate a commit message that adds commands or expansions to its here-document', diff --git a/tests/hooks/pre-bash-commit-quality.test.js b/tests/hooks/pre-bash-commit-quality.test.js index 30c78d6a6..060b2ae01 100644 --- a/tests/hooks/pre-bash-commit-quality.test.js +++ b/tests/hooks/pre-bash-commit-quality.test.js @@ -586,6 +586,15 @@ if (test('reads text after the substitution as part of the message', () => { hook.validateCommitMessage('git commit -m "$(cat <<\'EOF\'\nfix: a\nEOF\n)$HOME"').issues.map(i => i.type), ['unverifiable'] ); + // A line continuation inside it is dropped first: `$\HOME` is `$HOME`, `\$\HOME` is not. + assert.deepStrictEqual( + hook.validateCommitMessage('git commit -m "$(cat <<\'EOF\'\nfix: a\nEOF\n)$\\\nHOME"').issues.map(i => i.type), + ['unverifiable'] + ); + assert.strictEqual( + hook.validateCommitMessage('git commit -m "$(cat <<\'EOF\'\nfix: a\nEOF\n)\\$\\\nHOME"').message, + 'fix: a$HOME' + ); })) passed++; else failed++; if (process.platform !== 'win32') { @@ -601,6 +610,7 @@ if (process.platform !== 'win32') { ['"EOF"', 'fix: keep \\\\ and \\$HOME'], ["'EOF'", 'fix: update input', '.'], ["'EOF'", 'fix: update input\n\nbody line', ' and more'], + ["'EOF'", 'fix: a', '\\$\\\nHOME'], ]; inTempRepo(repoDir => { for (const [delimiter, body, suffix = ''] of forms) {