From 8fd6416a38773d385cf1fad7c5cc71ed9c54e44b Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Thu, 26 Nov 2026 14:11:16 -0800 Subject: [PATCH] fix(ci): prevent diagnostic output from escaping code fences Choose a fence longer than any backtick run in the diagnostic so embedded fences cannot expose diagnostic content as Markdown or HTML. Add regression tests for embedded fences and ordinary output. --- lib/ci/ci_utils.js | 8 +++++++- test/unit/ci_utils.test.js | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 test/unit/ci_utils.test.js diff --git a/lib/ci/ci_utils.js b/lib/ci/ci_utils.js index 50183540..e5835ffd 100644 --- a/lib/ci/ci_utils.js +++ b/lib/ci/ci_utils.js @@ -40,7 +40,13 @@ export function getNodeName(url) { } export function fold(summary, code) { - const dataBlock = '```\n' + code + '\n```'; + // Keep diagnostic output from closing its own code block. + let fenceLength = 3; + for (const match of String(code).matchAll(/`{3,}/g)) { + fenceLength = Math.max(fenceLength, match[0].length + 1); + } + const fence = '`'.repeat(fenceLength); + const dataBlock = fence + '\n' + code + '\n' + fence; const summaryBlock = `\n${summary}\n`; return `
${summaryBlock}\n${dataBlock}\n
`; } diff --git a/test/unit/ci_utils.test.js b/test/unit/ci_utils.test.js new file mode 100644 index 00000000..9f36c5dd --- /dev/null +++ b/test/unit/ci_utils.test.js @@ -0,0 +1,33 @@ +import { describe, it } from 'node:test'; +import assert from 'node:assert/strict'; + +import { fold } from '../../lib/ci/ci_utils.js'; + +describe('fold', () => { + for (const { name, code, fence } of [ + { name: 'ordinary output', code: 'test failed', fence: '```' }, + { name: 'empty output', code: '', fence: '```' }, + { name: 'inline backticks', code: '`one` and ``two``', fence: '```' }, + { + name: 'a fence followed by HTML and Markdown', + code: 'failure\n```\n\n\n# injected heading', + fence: '````' + }, + { + name: 'multiple backtick runs including a longer fence', + code: '```\n``````\n````', + fence: '```````' + }, + { + name: 'a long backtick run within a line', + code: 'before ````` after', + fence: '``````' + } + ]) { + it(`safely fences ${name}`, () => { + assert.equal(fold('See failures', code), + '
\nSee failures\n\n' + + `${fence}\n${code}\n${fence}\n
`); + }); + } +});