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 `$\<newline>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: `\$\<newline>HOME` is
still the literal `$HOME`.
This commit is contained in:
L4XB committed 2026-11-05 14:36:26 +01:00
1 parent 2ff19fafa0
commit fee3d08666
2 files changed
+14 -1

No files matched your search

+4 -1
View File
@@ -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
// `$\<newline>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',
@@ -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: `$\<newline>HOME` is `$HOME`, `\$\<newline>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) {