Repository navigation
fix(docs): refuse artifact paths that GitHub could read as workflow commands #234
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
nickvigilante
merged 4 commits into
main
from
vigilante/docs-1348-stop-the-publish-job-printing-artifact-derived-text-that
Oct 21, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
0dfe811
fix(docs): refuse artifact paths that GitHub could read as workflow c…
nickvigilante de1b7d3
fix(docs): print refused artifact paths with only allowlisted characters
nickvigilante 891bf9e
Merge remote-tracking branch 'origin/main' into vigilante/docs-1348-s…
nickvigilante c86e24e
fix(docs): report an unparsable recorded _objects.json without quotin…
nickvigilante File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| // Keeps artifact text from forming a `::cmd::` or `##[cmd]` workflow command in the publish job's output. | ||
| import { readFile } from "node:fs/promises"; | ||
| import { hasUnsafePathSegment } from "@coder/docs-manifest"; | ||
|
|
||
| // Next names dynamic-route payloads `__next.$oc$slug.txt`. | ||
| const SAFE_PATH_CHARS = "A-Za-z0-9._/@$-"; | ||
| const SAFE_ARTIFACT_PATH = new RegExp(`^[${SAFE_PATH_CHARS}]+$`); | ||
| const UNSAFE_PATH_CHAR = new RegExp(`[^${SAFE_PATH_CHARS}]`, "g"); | ||
| const MAX_ARTIFACT_PATH_LENGTH = 1024; | ||
| const SHA256 = /^[0-9a-f]{64}$/; | ||
|
|
||
| /** | ||
| * Quote artifact-derived text for a log line, truncated and with every | ||
| * character outside the path allowlist replaced by `?`. | ||
| * | ||
| * @param {unknown} value | ||
| * @param {number} [max] | ||
| */ | ||
| export function describeForLog(value, max = 80) { | ||
| const text = String(value).replace(UNSAFE_PATH_CHAR, "?"); | ||
| return JSON.stringify(text.length > max ? `${text.slice(0, max)}...` : text); | ||
| } | ||
|
|
||
| /** | ||
| * Whether an archive key from the artifact may be uploaded and printed: the | ||
| * router's traversal rule plus an allowlist with no control characters. | ||
| * | ||
| * @param {unknown} key | ||
| */ | ||
| export function isSafeArtifactPath(key) { | ||
| return ( | ||
| typeof key === "string" && | ||
| key.length > 0 && | ||
| key.length <= MAX_ARTIFACT_PATH_LENGTH && | ||
| SAFE_ARTIFACT_PATH.test(key) && | ||
| !key.startsWith("/") && | ||
| !key.endsWith("/") && | ||
| !key.includes("//") && | ||
| !hasUnsafePathSegment(key) | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Parse `_publish.json` and refuse it unless every entry is safe. Errors name | ||
| * the entry by position and never quote the artifact's text. | ||
| * | ||
| * @param {string} text | ||
| * @returns {{ version: 1; files: { path: string; sha256: string }[] }} | ||
| */ | ||
| export function parsePublishIndex(text) { | ||
| let index; | ||
| try { | ||
| index = JSON.parse(text); | ||
| } catch { | ||
| throw new Error("_publish.json is not valid JSON"); | ||
| } | ||
| if (index?.version !== 1 || !Array.isArray(index.files)) { | ||
| throw new Error("_publish.json has an unrecognised shape"); | ||
| } | ||
| index.files.forEach((file, i) => { | ||
| if (typeof file !== "object" || file === null) { | ||
| throw new Error(`_publish.json entry ${i} is not an object`); | ||
| } | ||
| if (!isSafeArtifactPath(file.path)) { | ||
| throw new Error(`_publish.json entry ${i} has an unsafe path: ${describeForLog(file.path)}`); | ||
| } | ||
| if (typeof file.sha256 !== "string" || !SHA256.test(file.sha256)) { | ||
| throw new Error(`_publish.json entry ${i} has no valid sha256`); | ||
| } | ||
| }); | ||
| return index; | ||
| } | ||
|
|
||
| /** @param {string} file */ | ||
| export async function readPublishIndex(file) { | ||
| return parsePublishIndex(await readFile(file, "utf8")); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| import assert from "node:assert/strict"; | ||
| import { mkdtemp, rm, writeFile } from "node:fs/promises"; | ||
| import { tmpdir } from "node:os"; | ||
| import { join } from "node:path"; | ||
| import { test } from "node:test"; | ||
| import { | ||
| describeForLog, | ||
| isSafeArtifactPath, | ||
| parsePublishIndex, | ||
| readPublishIndex, | ||
| } from "./publish-index.mjs"; | ||
|
|
||
| const sha256 = "a".repeat(64); | ||
| const indexOf = (...paths) => | ||
| JSON.stringify({ version: 1, files: paths.map((path) => ({ path, sha256, bytes: 1 })) }); | ||
|
|
||
| test("isSafeArtifactPath accepts the key shapes a real export produces", () => { | ||
| for (const key of [ | ||
| "beta-docs/@main/index.html", | ||
| "beta-docs/@2.36/_next/static/chunks/0cz1d0mv5g_q7.js", | ||
| "beta-docs/@main/admin/__next.$oc$slug.__PAGE__.txt", | ||
| "beta-docs/@main/images/Screenshot.PNG", | ||
| "_redirects.json", | ||
| ]) { | ||
| assert.equal(isSafeArtifactPath(key), true, key); | ||
| } | ||
| }); | ||
|
|
||
| test("isSafeArtifactPath refuses control characters, separators tricks, and traversal", () => { | ||
| for (const key of [ | ||
| "beta-docs/\n::error title=x::y\n::stop-commands::z/index.html", | ||
| "beta-docs/a\rb", | ||
| "beta-docs/a\u0000b", | ||
| "beta-docs/a b/index.html", | ||
| "beta-docs/a%2f..%2fb", | ||
| "beta-docs/../x", | ||
| "beta-docs/./x", | ||
| "beta-docs\\x", | ||
| "/beta-docs/x", | ||
| "beta-docs//x", | ||
| "beta-docs/x/", | ||
| "beta-docs/é.html", | ||
| "::error::x", | ||
| "beta-docs/##[error]x", | ||
| "beta-docs/a%0Ab", | ||
| "beta-docs/a\u2028b", | ||
| "beta-docs/a\u0005b", | ||
| "", | ||
| `beta-docs/${"a".repeat(1100)}`, | ||
| undefined, | ||
| 42, | ||
| ]) { | ||
| assert.equal(isSafeArtifactPath(key), false, JSON.stringify(key)); | ||
| } | ||
| }); | ||
|
|
||
| const WORKFLOW_COMMAND_OR_LINE_BREAK = /##\[|::|%|[\r\n\u0085\u2028\u2029]/; | ||
| const hostileTexts = [ | ||
| "##[add-mask]x", | ||
| "##[error]x", | ||
| "beta-docs/##[error title=Publish compromised]rotate the token", | ||
| "::error::x", | ||
| "a\n::error::x", | ||
| "a\r\n##[stop-commands]t", | ||
| "\u2028::x", | ||
| "\u2029##[warning]x", | ||
| "\u0085::x", | ||
| "a%0A::error::x", | ||
| "x\u001b[31m", | ||
| ]; | ||
|
|
||
| test("describeForLog never prints text the runner could read as a workflow command", () => { | ||
| for (const hostile of hostileTexts) { | ||
| assert.doesNotMatch( | ||
| describeForLog(hostile), | ||
| WORKFLOW_COMMAND_OR_LINE_BREAK, | ||
| JSON.stringify(hostile), | ||
| ); | ||
| } | ||
| assert.equal(describeForLog("beta-docs/@main/a b.html"), '"beta-docs/@main/a?b.html"'); | ||
| assert.ok(describeForLog("a".repeat(500)).length < 120); | ||
| }); | ||
|
|
||
| test("parsePublishIndex returns an index whose every path is safe", () => { | ||
| const index = parsePublishIndex(indexOf("beta-docs/index.html", "beta-docs/a/index.html")); | ||
| assert.deepEqual( | ||
| index.files.map((f) => f.path), | ||
| ["beta-docs/index.html", "beta-docs/a/index.html"], | ||
| ); | ||
| }); | ||
|
|
||
| test("parsePublishIndex refuses an unsafe path without echoing it raw", () => { | ||
| const hostile = "beta-docs/v2.38/\n::error title=pwned::injected\n::stop-commands::x/index.html"; | ||
| assert.throws( | ||
| () => parsePublishIndex(indexOf("beta-docs/index.html", hostile)), | ||
| (error) => { | ||
| assert.match(error.message, /entry 1 has an unsafe path/); | ||
| assert.doesNotMatch(error.message, WORKFLOW_COMMAND_OR_LINE_BREAK); | ||
| return true; | ||
| }, | ||
| ); | ||
| for (const text of hostileTexts) { | ||
| assert.throws( | ||
| () => parsePublishIndex(indexOf(text)), | ||
| (error) => !WORKFLOW_COMMAND_OR_LINE_BREAK.test(error.message), | ||
| JSON.stringify(text), | ||
| ); | ||
| } | ||
| }); | ||
|
|
||
| test("parsePublishIndex refuses a bad hash, a wrong shape, and invalid JSON without quoting the text", () => { | ||
| assert.throws( | ||
| () => | ||
| parsePublishIndex( | ||
| JSON.stringify({ version: 1, files: [{ path: "a.html", sha256: "z\n::error::x" }] }), | ||
| ), | ||
| (error) => /entry 0 has no valid sha256/.test(error.message) && !/::error/.test(error.message), | ||
| ); | ||
| assert.throws(() => parsePublishIndex(JSON.stringify({ version: 2, files: [] })), /unrecognised/); | ||
| assert.throws(() => parsePublishIndex(JSON.stringify({ version: 1, files: {} })), /unrecognised/); | ||
| assert.throws(() => parsePublishIndex(JSON.stringify({ version: 1, files: [null] })), /entry 0/); | ||
| assert.throws( | ||
| () => parsePublishIndex('{"files": "\n::error::injected'), | ||
| (error) => /not valid JSON/.test(error.message) && !/::error/.test(error.message), | ||
| ); | ||
| }); | ||
|
|
||
| test("readPublishIndex reads and validates a file", async () => { | ||
| const dir = await mkdtemp(join(tmpdir(), "docs-index-")); | ||
| try { | ||
| await writeFile(join(dir, "_publish.json"), indexOf("beta-docs/index.html")); | ||
| assert.equal((await readPublishIndex(join(dir, "_publish.json"))).files.length, 1); | ||
| await writeFile(join(dir, "bad.json"), indexOf("a\nb")); | ||
| await assert.rejects(() => readPublishIndex(join(dir, "bad.json")), /unsafe path/); | ||
| } finally { | ||
| await rm(dir, { recursive: true, force: true }); | ||
| } | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: the allowlist is right, but this list only covers commands that follow a newline, plus
é. Adding"::error::x"with no newline,"beta-docs/##[error]x","beta-docs/a%0Ab","beta-docs/a\u2028b"and"beta-docs/a\u0085b"would pin the guarantee the module documents.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added in de1b7d3:
::error::x,beta-docs/##[error]x,beta-docs/a%0Ab,beta-docs/a bandbeta-docs/a\u0005b.