From 6d83a2eb21d49c2fc14235100c327cd19d6e46c4 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 12 Nov 2026 01:12:02 +0000 Subject: [PATCH 1/4] Initial plan From 02902f3601dd3f88ce0ead0b4e3878bedcd31e98 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 12 Nov 2026 01:21:53 +0000 Subject: [PATCH 2/4] Handle quote-adjacent HTML attributes Co-authored-by: lpcox <15877973+lpcox@users.noreply.github.com> --- .../js/sanitize_content_attributes.test.cjs | 30 +++ actions/setup/js/sanitize_content_core.cjs | 171 ++++++++++++------ 2 files changed, 150 insertions(+), 51 deletions(-) create mode 100644 actions/setup/js/sanitize_content_attributes.test.cjs diff --git a/actions/setup/js/sanitize_content_attributes.test.cjs b/actions/setup/js/sanitize_content_attributes.test.cjs new file mode 100644 index 00000000000..23f1a9a4072 --- /dev/null +++ b/actions/setup/js/sanitize_content_attributes.test.cjs @@ -0,0 +1,30 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { createRequire } from "node:module"; + +const require = createRequire(import.meta.url); +const { convertXmlTags, sanitizeContentCore, clearRedactedDomains } = require("./sanitize_content_core.cjs"); + +beforeEach(() => { + vi.stubGlobal("core", { info: vi.fn(), debug: vi.fn(), warning: vi.fn(), error: vi.fn() }); +}); + +afterEach(() => { + clearRedactedDomains(); + vi.unstubAllGlobals(); +}); + +describe("quote-aware HTML attribute sanitization", () => { + it.each([ + ['', ''], + ["", ""], + ['', ''], + ['ok', 'ok'], + ['ok', 'ok'], + [']]>', '(![CDATA[(b title="left > right")]])'], + ])("converts %j consistently", (input, expected) => { + expect(convertXmlTags(input)).toBe(expected); + expect(sanitizeContentCore(input)).toBe(expected); + }); +}); diff --git a/actions/setup/js/sanitize_content_core.cjs b/actions/setup/js/sanitize_content_core.cjs index 83489f7fefb..b14578f3867 100644 --- a/actions/setup/js/sanitize_content_core.cjs +++ b/actions/setup/js/sanitize_content_core.cjs @@ -1226,65 +1226,134 @@ function convertXmlTags(s) { return /^https:\/\/[\w.-]+(?::\d+)?(\/[^\s<>|]*)?(?:\|[^<>]*)?$/.test(tagContent); } - // First, process CDATA sections specially - convert tags inside them and the CDATA markers - s = s.replace(//g, (match, content) => { - // Convert tags inside CDATA content - const convertedContent = content.replace(/<(\/?[A-Za-z][A-Za-z0-9]*(?:[^>]*?))>/g, "($1)"); - // Return with CDATA markers also converted to parentheses - return `(![CDATA[${convertedContent}]])`; - }); - /** - * Strips dangerous HTML attributes from an allowed tag's content string. - * Removes on* event handler attributes (e.g. onclick, ontoggle), style, - * title, and data-* attributes in all quoting forms (double-quoted, - * single-quoted, unquoted, bare). - * - * title= and data-* are stripped because their values are invisible in - * GitHub's rendered markdown (title= appears only as a hover-tooltip; - * data-* attributes are stripped by GFM's sanitizer) but arrive at the - * agent verbatim in raw text, creating a steganographic injection channel - * structurally identical to the one fixed for markdown link titles. - * - * Safe attributes such as class, open, lang, id, etc. are preserved. - * - * Note: `\s+` (requiring at least one whitespace before the attribute name) is - * intentional — HTML attributes are always separated from the tag name and from - * each other by at least one whitespace character. Using `\s*` would risk false - * matches inside tag names (e.g. matching "ong" inside "strong"). - * - * @param {string} tagContent - Tag content without surrounding angle brackets - * @returns {string} Tag content with dangerous attributes removed + * Finds a tag's closing angle bracket without treating quoted values as boundaries. + * An unterminated quote falls back to its first `>` so malformed markup is neutralized. + * @param {string} source + * @param {number} start + * @returns {{end: number, malformed: boolean}} */ - function stripDangerousAttributes(tagContent) { - // Match: one-or-more whitespace-or-slash + (on* | style | title | data-*) + optional =value - // Value forms: "...", '...', or unquoted (no whitespace / > / quote chars), or bare (no =) - // The unquoted form excludes >, whitespace, and all quote characters (', ", `) so it - // cannot consume the closing > of the tag or straddle other attribute values. - // Using [\s/]+ (instead of \s+) also strips dangerous attributes that are immediately - // preceded by a "/" with no space — e.g. the malformed . - return tagContent.replace(/[\s/]+(?:on\w+|style|title|data-[\w-]+)(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^\s>"'`]*))?/gi, ""); + function findTagEnd(source, start) { + let quote = ""; + let firstQuotedGreaterThan = -1; + for (let index = start + 1; index < source.length; index++) { + const character = source[index]; + if (quote) { + if (character === quote) { + quote = ""; + } else if (character === ">" && firstQuotedGreaterThan < 0) { + firstQuotedGreaterThan = index; + } + } else if (character === '"' || character === "'") { + quote = character; + } else if (character === ">") { + return { end: index, malformed: false }; + } + } + return quote && firstQuotedGreaterThan >= 0 ? { end: firstQuotedGreaterThan, malformed: true } : { end: -1, malformed: false }; } - // Convert opening tags: or to (tag) or (tag attr="value") - // Convert closing tags: to (/tag) - // Convert self-closing tags: or to (tag/) or (tag /) - // But preserve allowed safe tags (with dangerous attributes stripped) - return s.replace(/<(\/?[A-Za-z!][^>]*?)>/g, (match, tagContent) => { - if (isHttpsAngleBracketAutolink(tagContent)) { - return match; + /** + * Applies a transformation to tag-like markup while respecting quoted values. + * @param {string} source + * @param {(tagContent: string, match: string, malformed: boolean) => string} transform + * @returns {string} + */ + function replaceTagMarkup(source, transform) { + let output = ""; + let written = 0; + for (let index = 0; index < source.length; index++) { + if (source[index] !== "<") continue; + const firstCharacter = source[index + 1] === "/" ? source[index + 2] : source[index + 1]; + if (!firstCharacter || !/[A-Za-z!]/.test(firstCharacter)) continue; + + const boundary = findTagEnd(source, index); + if (boundary.end < 0) return written ? output + source.slice(written) : source; + const end = boundary.end + 1; + const match = source.slice(index, end); + const tagContent = source.slice(index + 1, boundary.end); + output += source.slice(written, index) + transform(tagContent, match, boundary.malformed); + written = end; + index = boundary.end; } - // Extract tag name from the content (handle closing tags and attributes) + return written ? output + source.slice(written) : source; + } + + /** + * Strips on* event handlers, style, title, and data-* attributes from tag content. + * Attribute values are consumed as quoted or unquoted tokens before the next + * attribute is considered, including when a quoted value is immediately followed + * by another attribute without whitespace. + * @param {string} tagContent + * @returns {string} + */ + function stripDangerousAttributes(tagContent) { const tagNameMatch = tagContent.match(/^\/?\s*([A-Za-z][A-Za-z0-9]*)/); - if (tagNameMatch) { - const tagName = tagNameMatch[1].toLowerCase(); - if (allowedTags.includes(tagName)) { - // Strip dangerous attributes (on* event handlers, style, title, data-*) before preserving - const sanitizedContent = stripDangerousAttributes(tagContent); - return `<${sanitizedContent}>`; + if (!tagNameMatch) return tagContent; + + let position = tagNameMatch[0].length; + let written = 0; + let output = ""; + while (position < tagContent.length) { + const separatorStart = position; + while (position < tagContent.length && (/\s/.test(tagContent[position]) || tagContent[position] === "/")) { + position++; + } + if (position >= tagContent.length) break; + + const nameStart = position; + while (position < tagContent.length && !/[\s/=>"'`<]/.test(tagContent[position])) { + position++; + } + if (position === nameStart) { + position++; + continue; + } + const attributeName = tagContent.slice(nameStart, position); + let attributeEnd = position; + let valueStart = position; + while (valueStart < tagContent.length && /\s/.test(tagContent[valueStart])) valueStart++; + if (tagContent[valueStart] === "=") { + valueStart++; + while (valueStart < tagContent.length && /\s/.test(tagContent[valueStart])) valueStart++; + if (tagContent[valueStart] === '"' || tagContent[valueStart] === "'") { + const quote = tagContent[valueStart]; + const closingQuote = tagContent.indexOf(quote, valueStart + 1); + attributeEnd = closingQuote < 0 ? tagContent.length : closingQuote + 1; + } else { + attributeEnd = valueStart; + while (attributeEnd < tagContent.length && !/[\s>"'`<]/.test(tagContent[attributeEnd])) { + attributeEnd++; + } + } + } + + if (/^(?:on\w+|style|title|data-[\w-]+)$/i.test(attributeName)) { + output += tagContent.slice(written, separatorStart); + written = attributeEnd; } + position = Math.max(attributeEnd, position); + } + return written ? output + tagContent.slice(written) : tagContent; + } + + // Convert tags inside CDATA sections, then convert the CDATA markers themselves. + s = s.replace(//g, (_match, content) => { + const convertedContent = replaceTagMarkup(content, tagContent => `(${tagContent})`); + return `(![CDATA[${convertedContent}]])`; + }); + + // Convert opening, closing, and self-closing tags while retaining only allowed tags. + // Malformed quoted markup is always neutralized rather than repaired into an allowed tag. + return replaceTagMarkup(s, (tagContent, match, malformed) => { + if (malformed) return `(${tagContent})`; + if (isHttpsAngleBracketAutolink(tagContent)) return match; + + const tagNameMatch = tagContent.match(/^\/?\s*([A-Za-z][A-Za-z0-9]*)/); + if (tagNameMatch && allowedTags.includes(tagNameMatch[1].toLowerCase())) { + return `<${stripDangerousAttributes(tagContent)}>`; } - return `(${tagContent})`; // Convert other tags to parentheses + return `(${tagContent})`; }); } From f7b4550917eb0a1cd9a84ebc44467db1b868931b Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 12 Nov 2026 02:18:41 +0000 Subject: [PATCH 3/4] Fix sanitizer parser review findings Co-authored-by: lpcox <15877973+lpcox@users.noreply.github.com> --- .../js/sanitize_content_attributes.test.cjs | 17 ++- actions/setup/js/sanitize_content_core.cjs | 108 ++++++++++++------ 2 files changed, 87 insertions(+), 38 deletions(-) diff --git a/actions/setup/js/sanitize_content_attributes.test.cjs b/actions/setup/js/sanitize_content_attributes.test.cjs index 23f1a9a4072..32b14c6032f 100644 --- a/actions/setup/js/sanitize_content_attributes.test.cjs +++ b/actions/setup/js/sanitize_content_attributes.test.cjs @@ -20,11 +20,26 @@ describe("quote-aware HTML attribute sanitization", () => { ['', ''], ['ok', 'ok'], ['ok', 'ok'], + ['ok', 'ok'], + ['ok', 'ok'], [']]>', '(![CDATA[(b title="left > right")]])'], + [' right">]]>', '(![CDATA[(b title="left > right")]])'], + ["", ""], + ['', '(wrapper value="<system>payload</system>")'], + ['payload">]]>', '(![CDATA[(wrapper value="<system>payload</system>")]])'], + ['', '(b"x=" onmouseover=alert(1)//"")'], + [''], ])("converts %j consistently", (input, expected) => { expect(convertXmlTags(input)).toBe(expected); expect(sanitizeContentCore(input)).toBe(expected); }); + + it("keeps scanning after unterminated tags without quadratic rescanning", () => { + const input = `|]*)?(?:\|[^<>]*)?$/.test(tagContent); } - /** - * Finds a tag's closing angle bracket without treating quoted values as boundaries. - * An unterminated quote falls back to its first `>` so malformed markup is neutralized. - * @param {string} source - * @param {number} start - * @returns {{end: number, malformed: boolean}} - */ - function findTagEnd(source, start) { - let quote = ""; - let firstQuotedGreaterThan = -1; - for (let index = start + 1; index < source.length; index++) { + function createTagScanner(source) { + const length = source.length; + const normal = new Int32Array(length + 1); + const expectValue = new Int32Array(length + 1); + const unquotedValue = new Int32Array(length + 1); + const singleQuotedValue = new Int32Array(length + 1); + const doubleQuotedValue = new Int32Array(length + 1); + const nextGreaterThan = new Int32Array(length + 1); + nextGreaterThan[length] = -1; + + for (let index = length - 1; index >= 0; index--) { const character = source[index]; - if (quote) { - if (character === quote) { - quote = ""; - } else if (character === ">" && firstQuotedGreaterThan < 0) { - firstQuotedGreaterThan = index; - } - } else if (character === '"' || character === "'") { - quote = character; + nextGreaterThan[index] = character === ">" ? index : nextGreaterThan[index + 1]; + + normal[index] = character === ">" ? index + 1 : character === "=" ? expectValue[index + 1] : normal[index + 1]; + if (/\s/.test(character)) { + expectValue[index] = expectValue[index + 1]; + unquotedValue[index] = normal[index + 1]; } else if (character === ">") { - return { end: index, malformed: false }; + expectValue[index] = index + 1; + unquotedValue[index] = index + 1; + } else { + expectValue[index] = character === "'" ? singleQuotedValue[index + 1] : character === '"' ? doubleQuotedValue[index + 1] : unquotedValue[index + 1]; + unquotedValue[index] = unquotedValue[index + 1]; } + + singleQuotedValue[index] = character === "'" ? normal[index + 1] : singleQuotedValue[index + 1] > 0 ? singleQuotedValue[index + 1] : character === ">" ? -(index + 1) : singleQuotedValue[index + 1]; + doubleQuotedValue[index] = character === '"' ? normal[index + 1] : doubleQuotedValue[index + 1] > 0 ? doubleQuotedValue[index + 1] : character === ">" ? -(index + 1) : doubleQuotedValue[index + 1]; } - return quote && firstQuotedGreaterThan >= 0 ? { end: firstQuotedGreaterThan, malformed: true } : { end: -1, malformed: false }; + + return { + nextGreaterThan, + findTagEnd(start) { + const result = normal[start + 1]; + return result > 0 ? { end: result - 1, malformed: false } : result < 0 ? { end: -result - 1, malformed: true } : { end: -1, malformed: false }; + }, + }; } /** * Applies a transformation to tag-like markup while respecting quoted values. * @param {string} source * @param {(tagContent: string, match: string, malformed: boolean) => string} transform + * @param {boolean} [preserveAutolinks] * @returns {string} */ - function replaceTagMarkup(source, transform) { + function replaceTagMarkup(source, transform, preserveAutolinks = false) { + const scanner = createTagScanner(source); let output = ""; let written = 0; for (let index = 0; index < source.length; index++) { @@ -1267,8 +1281,20 @@ function convertXmlTags(s) { const firstCharacter = source[index + 1] === "/" ? source[index + 2] : source[index + 1]; if (!firstCharacter || !/[A-Za-z!]/.test(firstCharacter)) continue; - const boundary = findTagEnd(source, index); - if (boundary.end < 0) return written ? output + source.slice(written) : source; + const firstGreaterThan = scanner.nextGreaterThan[index + 1]; + if (preserveAutolinks && firstGreaterThan >= 0 && isHttpsAngleBracketAutolink(source.slice(index + 1, firstGreaterThan))) { + output += source.slice(written, firstGreaterThan + 1); + written = firstGreaterThan + 1; + index = firstGreaterThan; + continue; + } + + const boundary = scanner.findTagEnd(index); + if (boundary.end < 0) { + output += source.slice(written, index + 1); + written = index + 1; + continue; + } const end = boundary.end + 1; const match = source.slice(index, end); const tagContent = source.slice(index + 1, boundary.end); @@ -1279,6 +1305,10 @@ function convertXmlTags(s) { return written ? output + source.slice(written) : source; } + function escapeAngleBrackets(text) { + return text.replace(/[<>]/g, character => (character === "<" ? "<" : ">")); + } + /** * Strips on* event handlers, style, title, and data-* attributes from tag content. * Attribute values are consumed as quoted or unquoted tokens before the next @@ -1322,13 +1352,13 @@ function convertXmlTags(s) { attributeEnd = closingQuote < 0 ? tagContent.length : closingQuote + 1; } else { attributeEnd = valueStart; - while (attributeEnd < tagContent.length && !/[\s>"'`<]/.test(tagContent[attributeEnd])) { + while (attributeEnd < tagContent.length && !/\s/.test(tagContent[attributeEnd])) { attributeEnd++; } } } - if (/^(?:on\w+|style|title|data-[\w-]+)$/i.test(attributeName)) { + if (/^(?:on\w+|style$|title$|data-)/i.test(attributeName)) { output += tagContent.slice(written, separatorStart); written = attributeEnd; } @@ -1339,22 +1369,26 @@ function convertXmlTags(s) { // Convert tags inside CDATA sections, then convert the CDATA markers themselves. s = s.replace(//g, (_match, content) => { - const convertedContent = replaceTagMarkup(content, tagContent => `(${tagContent})`); + const convertedContent = replaceTagMarkup(content, tagContent => `(${escapeAngleBrackets(tagContent)})`); return `(![CDATA[${convertedContent}]])`; }); // Convert opening, closing, and self-closing tags while retaining only allowed tags. // Malformed quoted markup is always neutralized rather than repaired into an allowed tag. - return replaceTagMarkup(s, (tagContent, match, malformed) => { - if (malformed) return `(${tagContent})`; - if (isHttpsAngleBracketAutolink(tagContent)) return match; - - const tagNameMatch = tagContent.match(/^\/?\s*([A-Za-z][A-Za-z0-9]*)/); - if (tagNameMatch && allowedTags.includes(tagNameMatch[1].toLowerCase())) { - return `<${stripDangerousAttributes(tagContent)}>`; - } - return `(${tagContent})`; - }); + return replaceTagMarkup( + s, + (tagContent, match, malformed) => { + if (malformed) return `(${escapeAngleBrackets(tagContent)})`; + + const tagNameMatch = tagContent.match(/^\/?\s*([A-Za-z][A-Za-z0-9]*)/); + const tagNameDelimiter = tagNameMatch && tagContent[tagNameMatch[0].length]; + if (tagNameMatch && (tagNameDelimiter === undefined || /[\s/]/.test(tagNameDelimiter)) && allowedTags.includes(tagNameMatch[1].toLowerCase())) { + return `<${stripDangerousAttributes(tagContent)}>`; + } + return `(${escapeAngleBrackets(tagContent)})`; + }, + true + ); } /** From 2e05c4b6629be9179fdbf78da90dea8af04f0e95 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 12 Nov 2026 02:20:05 +0000 Subject: [PATCH 4/4] Fix sanitizer delimiter nullability Co-authored-by: lpcox <15877973+lpcox@users.noreply.github.com> --- actions/setup/js/sanitize_content_core.cjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/actions/setup/js/sanitize_content_core.cjs b/actions/setup/js/sanitize_content_core.cjs index bdc1c613705..5ba6bf6a504 100644 --- a/actions/setup/js/sanitize_content_core.cjs +++ b/actions/setup/js/sanitize_content_core.cjs @@ -1381,7 +1381,7 @@ function convertXmlTags(s) { if (malformed) return `(${escapeAngleBrackets(tagContent)})`; const tagNameMatch = tagContent.match(/^\/?\s*([A-Za-z][A-Za-z0-9]*)/); - const tagNameDelimiter = tagNameMatch && tagContent[tagNameMatch[0].length]; + const tagNameDelimiter = tagNameMatch ? tagContent[tagNameMatch[0].length] : undefined; if (tagNameMatch && (tagNameDelimiter === undefined || /[\s/]/.test(tagNameDelimiter)) && allowedTags.includes(tagNameMatch[1].toLowerCase())) { return `<${stripDangerousAttributes(tagContent)}>`; }