Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ponytail-reviewer.lock.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .github/workflows/pr-code-quality-reviewer.lock.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .github/workflows/pr-specification-reviewer.lock.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 51 additions & 6 deletions actions/setup/js/sanitize_content_core.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,9 @@ function sanitizeUrlProtocols(s) {
// another protocol name (e.g. "ttps://" inside "https://…").
// (?!https://) — negative lookahead: explicitly excludes https://, which is
// passed through to sanitizeUrlDomains for domain filtering.
let result = normalized.replace(/(?<![a-z0-9])(?!https:\/\/)([a-z][a-z0-9+.-]*)(:\/\/)([\w.-]*)([^\s]*)/gi, (_match, scheme, _slashes, domain, _rest) => {
// URL redaction must leave HTML attribute quotes and tag boundaries intact:
// attribute stripping has already run, so deleting them could pull prose into a tag.
let result = normalized.replace(/(?<![a-z0-9])(?!https:\/\/)([a-z][a-z0-9+.-]*)(:\/\/)([\w.-]*)([^\s"'<>]*)/gi, (_match, scheme, _slashes, domain, _rest) => {
const fullMatch = _match;
if (!domain) {
// No host present (e.g. "file:///path" or bare "http://"). Use the scheme
Expand All @@ -407,7 +409,7 @@ function sanitizeUrlProtocols(s) {
// For schemes that do not use "//", a targeted blocklist is used because a
// fully general single-colon pattern produces too many false positives
// (e.g. "key:value" in YAML, "std::vector" in C++, "C:\path" on Windows).
return result.replace(/(?:data|javascript|vbscript|about|mailto|tel|magnet):[^\s]+/gi, match => {
return result.replace(/(?:data|javascript|vbscript|about|mailto|tel|magnet):(?=[^\s])[^\s"'<>]*/gi, match => {
const protocolMatch = match.match(/^([^:]+):/);
if (protocolMatch) {
const protocol = protocolMatch[1] + ":";
Expand Down Expand Up @@ -441,7 +443,7 @@ function sanitizeUrlDomains(s, allowed) {
// 3. Allow optional port (:8080)
// 4. Allow optional path and query string (but not trailing commas/periods)
// 5. Stop before another https:// URL in query params (using negative lookahead)
const httpsUrlRegex = /https:\/\/([\w.-]+(?::\d+)?)(\/(?:(?!https:\/\/)[^\s,])*)?/gi;
const httpsUrlRegex = /https:\/\/([\w.-]+(?::\d+)?)(\/(?:(?!https:\/\/)[^\s,"'<>])*)?/gi;

/**
* Shared domain-allowlist check and redaction logic.
Expand Down Expand Up @@ -542,7 +544,7 @@ function sanitizeUrlDomains(s, allowed) {
// 2: separator (// or a backslash variant)
// 3: hostname (and optional port)
// 4: optional path
const protoRelativeUrlRegex = new RegExp(`(^|${URL_START_DELIMITERS})([/\\\\]{2})([\\w.-]+(?::\\d+)?)((?:/(?:(?![/\\\\]{2})[^\\s,])*)?)`, "gi");
const protoRelativeUrlRegex = new RegExp(`(^|${URL_START_DELIMITERS})([/\\\\]{2})([\\w.-]+(?::\\d+)?)((?:/(?:(?![/\\\\]{2})[^\\s,"'<>])*)?)`, "gi");

s = s.replace(protoRelativeUrlRegex, (match, prefix, _separator, hostnameWithPort, path = "") => {
// Normalize the separator to "//" so a backslash form can never survive as
Expand Down Expand Up @@ -604,6 +606,39 @@ function sanitizeUrlDomains(s, allowed) {
const urlOrWwwRegex = /https:\/\/|(^|[^a-z0-9])(www\.)/gi;
const parts = [];
const authorityUserinfoAt = new Uint8Array(s.length + 1);
const htmlTagAt = new Uint8Array(s.length);
const htmlTagQuotedAt = new Uint8Array(s.length);
const maxHtmlTagLength = 4096;
let inHtmlTag = false;
let htmlQuote = "";
let htmlTagStart = -1;
for (let i = 0; i < s.length; i++) {
const character = s[i];
if (inHtmlTag && ((/\r|\n/.test(character) && htmlQuote) || i - htmlTagStart >= maxHtmlTagLength)) {
inHtmlTag = false;
htmlQuote = "";
htmlTagStart = -1;
}
if (!inHtmlTag && character === "<" && /[a-z]/i.test(s[i + 1] || "")) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The scanner enters inHtmlTag for any <letter... prefix and never recovers when the tag is malformed or has an unterminated quote. For example, <b don't leaves htmlQuote set through the remainder of the string, so later (www/redacted). candidates are parsed with HTML-tag termination rules and can have their URL boundary changed by unrelated prose. Since this state is attacker-controlled input, please bound/reset malformed tag state (for example at a newline or a conservative maximum) and add a regression for an unclosed tag before a later URL.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added recovery for unterminated quoted tags at line breaks and a 4096-character cap for tag state, with regressions for both recovery paths. The later URL boundary is preserved in b72ba7759f.

let tagNameEnd = i + 2;
while (/[a-z0-9:-]/i.test(s[tagNameEnd] || "")) tagNameEnd++;
if (/[\s/>]/.test(s[tagNameEnd] || "")) {
inHtmlTag = true;
htmlTagStart = i;
}
}
if (!inHtmlTag) continue;
htmlTagAt[i] = 1;
htmlTagQuotedAt[i] = htmlQuote ? 1 : 0;
if (htmlQuote) {
if (character === htmlQuote) htmlQuote = "";
} else if (character === '"' || character === "'") {
htmlQuote = character;
} else if (character === ">") {
inHtmlTag = false;
htmlTagStart = -1;
}
}
let hasAuthorityUserinfo = false;
for (let i = s.length - 1; i >= 0; i--) {
const character = s[i];
Expand Down Expand Up @@ -647,12 +682,22 @@ function sanitizeUrlDomains(s, allowed) {
const prefix = match[1];
const candidateStart = match.index + match[0].length - match[2].length;
const hasAuthorityUserinfo = authorityUserinfoAt[candidateStart] === 1;
const isHtmlTagCandidate = htmlTagAt[candidateStart] === 1;
let end = candidateStart;
let depth = 0;
let inAuthority = true;
for (; end < s.length; end++) {
const character = s[end];
if (character === "\\" && end + 1 < s.length) {
// GFM permits ">" inside a userinfo authority, but HTML tag candidates
// must stop there even when later prose contains "@".
if (
/[\s|\[\]]/.test(character) ||
(isHtmlTagCandidate && /["']/.test(character) && !hasAuthorityUserinfo) ||
(/[<>]/.test(character) && (isHtmlTagCandidate ? !htmlTagQuotedAt[end] : !inAuthority || !hasAuthorityUserinfo)) ||
(/["']/.test(character) && (!inAuthority || !hasAuthorityUserinfo))
) {
break;
} else if (character === "\\" && end + 1 < s.length && !/["'<>]/.test(s[end + 1])) {
end++;
} else if (character === "`" && (!inAuthority || !hasAuthorityUserinfo || prefix === "`")) {
break;
Expand All @@ -663,7 +708,7 @@ function sanitizeUrlDomains(s, allowed) {
depth--;
} else if (character === "/" || character === "?" || character === "#") {
inAuthority = false;
} else if (/[\s|\[\]]/.test(character) || (/[*_~]/.test(character) && s.slice(end + 1, end + 5).toLowerCase() === "www.")) {
} else if (/[*_~]/.test(character) && s.slice(end + 1, end + 5).toLowerCase() === "www.") {
break;
}
}
Expand Down
139 changes: 139 additions & 0 deletions actions/setup/js/sanitize_content_url_boundaries.test.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { createRequire } from "node:module";

const require = createRequire(import.meta.url);
const { sanitizeContentCore, sanitizeUrlProtocols, sanitizeUrlDomains, clearRedactedDomains } = require("./sanitize_content_core.cjs");
const { sanitizeContent } = require("./sanitize_content.cjs");

beforeEach(() => {
vi.stubGlobal("core", { info: vi.fn(), debug: vi.fn(), warning: vi.fn(), error: vi.fn() });
vi.stubEnv("GH_AW_ALLOWED_DOMAINS", "github.com");
vi.stubEnv("GH_AW_SAFE_OUTPUTS_URLS", "allowed-only");
vi.stubEnv("GITHUB_SERVER_URL", "");
vi.stubEnv("GITHUB_API_URL", "");
});

afterEach(() => {
clearRedactedDomains();
vi.unstubAllEnvs();
vi.unstubAllGlobals();
});

const urls = [
...["data:image/png;base64,AAAA==", "javascript:alert(1)", "vbscript:msgbox(1)", "about:blank", "mailto:test@example.invalid", "tel:123456", "magnet:?xt=fixture", "DaTa%253Aimage/png;base64,AAAA==", "file:///image.png"].map(url => [
url,
"(redacted)",
]),
...["http://", "ftp://", "https://", "//", "\\\\", "www."].map(prefix => [`${prefix}outside.invalid/image.png`, prefix === "www." ? "(www[.]outside.invalid/redacted)" : "(outside.invalid/redacted)"]),
];

const sanitizers = [
["core", text => sanitizeContentCore(text)],
["default", text => sanitizeContent(text)],
["selective mentions", text => sanitizeContent(text, { allowedAliases: ["synthetic"] })],
["deferred mentions", text => sanitizeContent(text, { deferMentions: true })],
];

describe.each(["allowed-only", "allowed-or-code-region"])("URL tag boundaries under %s", policy => {
describe.each(sanitizers)("%s sanitization", (_name, sanitize) => {
it.each(urls)("keeps %s redaction inside the original image", (url, redacted) => {
vi.stubEnv("GH_AW_SAFE_OUTPUTS_URLS", policy);
for (const quote of ['"', "'", ""]) {
for (const tail of [' text "onerror=alert(1)>', '\n\nSteps: run "and check\nonerror=alert(1)>']) {
const tag = `<img src=${quote}${redacted}${quote}>`;
const input = `<img src=${quote}${url}${quote} onload="alert(2)" style="display:none">`;
const output = sanitize(input + tail);
expect(output).toBe(tag + tail);
expect(output.match(/<img\b[^>]*>/g)).toEqual([tag]);
expect(sanitize(output)).toBe(output);
}
}
});

it("preserves allowed image URLs and layout attributes", () => {
vi.stubEnv("GH_AW_SAFE_OUTPUTS_URLS", policy);
const input = '<img src="https://github.com/image.png" width="12" height="8" alt="image">';
expect(sanitize(input)).toBe(input);
});

it("retains the configured code-region URL policy", () => {
vi.stubEnv("GH_AW_SAFE_OUTPUTS_URLS", policy);
const input = "```text\ndata:image/png;base64,AAAA==\n```";
expect(sanitize(input)).toBe(policy === "allowed-only" ? "```text\n(redacted)\n```" : input);
});
});
});

describe("URL matcher structural delimiters", () => {
it("preserves the tag boundary in the reported minimal reproduction", () => {
expect(sanitizeContentCore('<img src="data:image/png;base64,AAAA=="> text "onerror=alert(1)>')).toBe('<img src="(redacted)"> text "onerror=alert(1)>');
});

it.each(["javascript", "vbscript", "about"])("redacts %s when a quote or angle bracket immediately follows the scheme", scheme => {
for (const delimiter of ['"', "'", "<", ">"]) {
expect(sanitizeUrlProtocols(`${scheme}:${delimiter}payload${delimiter}`)).toBe(`(redacted)${delimiter}payload${delimiter}`);
}
});

it("does not redact blocked-scheme prose when a space follows the colon", () => {
expect(sanitizeUrlProtocols("about: blank")).toBe("about: blank");
});

it.each(urls)("does not consume delimiters after %s", (url, redacted) => {
const filter = /^(?:https:|[/\\]|www\.)/.test(url) ? text => sanitizeUrlDomains(text, ["github.com"]) : sanitizeUrlProtocols;
for (const delimiter of ['"', "'", ">", "<"]) {
expect(filter(`${url}${delimiter}tail`)).toBe(`${redacted}${delimiter}tail`);
}
});

it.each(urls)("does not consume an adjacent tag after %s", (url, redacted) => {
expect(sanitizeContent(`<img src=${url}><b>next</b>`)).toBe(`<img src=${redacted}><b>next</b>`);
});

it("does not treat a backslash as escaping an HTML attribute quote", () => {
expect(sanitizeUrlDomains('www.outside.invalid/path\\"tail', [])).toBe('(www[.]outside.invalid/redacted)"tail');
});

it("keeps a bare www attribute candidate inside its HTML tag", () => {
const input = '<img src="safe" width=10 www.outside.invalid>text@outside.invalid/path';
const expected = '<img src="safe" width=10 (www[.]outside.invalid/redacted)>text@outside.invalid/path';
expect(sanitizeUrlDomains(input, ["github.com"])).toBe(expected);
expect(sanitizeContent(input)).toBe(expected);
});

it.each([
['"www.github.com"@outside.invalid/path', '"(outside.invalid/redacted)'],
['command=www.github.com"@outside.invalid/path', "command=(outside.invalid/redacted)"],
])("validates the full www authority in prose: %s", (input, expected) => {
expect(sanitizeUrlDomains(input, ["github.com"])).toBe(expected);
});

it.each([
['<img alt="www.github.com"@evil.example/x">', '<img alt="(evil.example/redacted)">'],
['<b www.github.com"@evil.invalid/x', "<b (evil.invalid/redacted)"],
['<b www.github.com"@-evil.invalid/x', "<b (evil.invalid/redacted)"],
["a <b www.github.com'@_evil.invalid/x", "a <b (evil.invalid/redacted)"],
])("validates the full www authority inside tag-like input: %s", (input, expected) => {
expect(sanitizeUrlDomains(input, ["github.com"])).toBe(expected);
});

it("recovers from an unterminated HTML quote before filtering later prose", () => {
const input = "<b don't\n www.outside.invalid/path>tail";
expect(sanitizeUrlDomains(input, ["github.com"])).toBe("<b don't\n (www[.]outside.invalid/redacted)>tail");
});

it("bounds scanner state for an excessively long unterminated tag", () => {
const prefix = `<b "${"x".repeat(4092)}`;
expect(sanitizeUrlDomains(`${prefix} www.outside.invalid/path>tail`, ["github.com"])).toBe(`${prefix} (www[.]outside.invalid/redacted)>tail`);
});

it.each(["", " ", "\n"])("does not consume an image boundary when userinfo-like prose follows an attribute with %j spacing", spacing => {
for (const [url, expected] of [
["www.github.com", "www.github.com"],
["www.outside.invalid", "(www[.]outside.invalid/redacted)"],
]) {
const tail = '>text@outside.invalid/path "onerror=alert(1)>';
expect(sanitizeContent(`<img src =${spacing}${url}${tail}`)).toBe(`<img src =${spacing}${expected}${tail}`);
}
});
});
Loading