diff --git a/changelog.d/run-ids-not-card-numbers.md b/changelog.d/run-ids-not-card-numbers.md new file mode 100644 index 000000000..22d831e1f --- /dev/null +++ b/changelog.d/run-ids-not-card-numbers.md @@ -0,0 +1,4 @@ +--- +important: blocker +--- +**Posting a PR body that lists earlier /ship runs no longer stops for a card number that isn't there.** About 1 in 10 /ship run ids (`--<8 hex>`) also passes the card-number checksum, so `gstack-post` still asked for a confirmation on those bodies after the phone-number fix. The run id of a well-formed line in the earlier-runs block, and of the ship receipt itself, is now skipped for the card-number check too, only when the whole card-shaped number lies inside that run id; a card number in any other field, or written as a run id of any other shape, is still caught. Reported by @dgrant (#4725). diff --git a/docs/troubleshooting.md b/docs/troubleshooting.md index 45af2ad93..c886c1334 100644 --- a/docs/troubleshooting.md +++ b/docs/troubleshooting.md @@ -3351,8 +3351,11 @@ exact tokens it finds in these positions: session id (#4725). A phone number in another field of the same line, a `run` of any other shape, a session id under another key, a spaced or reordered line, and a block inside another fence or left open still - report. Only `pii.phone.e164` is skipped there: a session id that also - reads as a card number still asks; + report. About 1 in 10 session ids also passes the card-number checksum, so + this `run` value and the receipt's session-id `run` above also skip + `pii.cc`, but only when the whole card-shaped digit run lies inside the + value; a card number in another field, or as a `run` of any other shape, + still asks; - on a line outside any fenced code block that opens with `SHIP_RECEIPT:`, `CI_CARRY:`, `PREGATE:` or `READY_RECEIPT:` (bare, after `- `, `* ` or `> `, or after a backtick), the diff --git a/lib/gstack-post.ts b/lib/gstack-post.ts index 990060d8b..af6513154 100644 --- a/lib/gstack-post.ts +++ b/lib/gstack-post.ts @@ -569,7 +569,7 @@ const RECEIPT_LINE_IDS = [ /(?<=(? RECEIPT_LINE_IDS.flatMap(re => [...text.matchAll(re)].flatMap(m => m.indices!.slice(1).flatMap(ix => ix ? [{ line, from: ix[0] + 1, to: ix[1] }] : []))); /** @@ -582,6 +582,8 @@ const lineIdSpans = (text: string, line: number): Span[] => RECEIPT_LINE_IDS.fla * outside a fenced code block that opens with a receipt-line prefix (RECEIPT_LINE), its RECEIPT_LINE_IDS values. Only * those writers' own output qualifies, so a hand-written, schema-invalid, malformed or unclosed block exempts nothing, and the same number in * prose, in any other code block (a receipt-prefixed line inside one included), on a line without a receipt prefix or under any other key still reports. + * The two session-id spans (the receipt's run and each runs line's run) are marked card: a pii.cc finding is skipped only when its whole digit + * run lies inside one of them (gstack#4725); every other span clears pii.phone.e164 only. */ function receiptIdSpans(text: string): Span[] { const lines = text.split("\n"); @@ -612,16 +614,16 @@ function receiptIdSpans(text: string): Span[] { } return { line, v }; }; - const span = (path: Array, shape = /^\d{11,20}(?:\/\d{1,4})?$/) => { + const span = (path: Array, shape = /^\d{11,20}(?:\/\d{1,4})?$/, card?: true) => { const { line, v } = at(path); if (!shape.test(typeof v === "number" ? String(v) : typeof v === "string" ? v : "")) return; const t = lines[line]!; const key = path.at(-1); const from = t.length - t.trimStart().length + (typeof key === "string" ? JSON.stringify(key).length + 2 : 0) + 1; - spans.push({ line: line + 1, from, to: from + JSON.stringify(v).length - 1 }); + spans.push({ line: line + 1, from, to: from + JSON.stringify(v).length - 1, ...(card ? { card } : {}) }); }; const ids = (path: Array) => { const { line } = at(path); spans.push(...lineIdSpans(lines[line]!, line + 1)); }; - span(["run"], SESSION_ID); + span(["run"], SESSION_ID, true); if (typeof receipt.pregate === "string") ids(["pregate"]); receipt.requires_remote?.forEach((_, i) => ids(["requires_remote", i])); receipt.ci_carried?.forEach((_, i) => span(["ci_carried", i, "run"])); @@ -637,7 +639,7 @@ function receiptIdSpans(text: string): Span[] { if (typeof entry.head !== "string" || !/^[0-9a-f]{40}$/.test(entry.head) || typeof entry.run !== "string" || !SESSION_ID.test(entry.run)) continue; const before = Object.keys(entry).slice(0, Object.keys(entry).indexOf("run")); const from = JSON.stringify(Object.fromEntries(before.map(k => [k, entry[k]]))).length + (before.length ? 1 : 0) + '"run":'.length; - spans.push({ line: i + 1, from, to: from + JSON.stringify(entry.run).length - 1 }); + spans.push({ line: i + 1, from, to: from + JSON.stringify(entry.run).length - 1, card: true }); } } return spans; @@ -650,9 +652,12 @@ function scanParts(parts: Array<[Part, string]>, vis: RepoVisibility, selfEmail: for (const [part, text] of parts) { const result = scan(text, { repoVisibility: vis, allowlist, ...(selfEmail ? { selfEmail } : {}) }); oversize ||= result.oversize; - const runIds = result.findings.some(f => f.id === "pii.phone.e164") ? [...receiptIdSpans(text), ...approvalsGrantStopSpans(text)] : []; + const runIds: Span[] = result.findings.some(f => f.id === "pii.phone.e164" || f.id === "pii.cc") ? [...receiptIdSpans(text), ...approvalsGrantStopSpans(text)] : []; + const lines = text.split("\n"); + const cardEnd = (f: Finding) => { const re = /\b(?:\d[ -]?){13,19}\b/y; re.lastIndex = f.col - 1; return re.exec(lines[f.line - 1] ?? "") ? re.lastIndex : Infinity; }; for (const finding of result.findings) { if (finding.id === "pii.phone.e164" && runIds.some(s => s.line === finding.line && s.from <= finding.col && finding.col <= s.to)) continue; + if (finding.id === "pii.cc" && runIds.some(s => s.card && s.line === finding.line && s.from <= finding.col && cardEnd(finding) <= s.to)) continue; found.push({ part, finding }); } } diff --git a/test/gstack-post.test.ts b/test/gstack-post.test.ts index aee0d360d..b35a3f7cf 100644 --- a/test/gstack-post.test.ts +++ b/test/gstack-post.test.ts @@ -460,6 +460,42 @@ describe("gstack-post owns the redaction scan", () => { } }); + // Value: protects=a /ship session id that also reads as a Luhn-valid card number (about 1 in 10) posts without a pii.cc confirmation when it is + // the run of a runs-block line or of the ship receipt (gstack#4725, gstack#4480), while a card number in another field of the same line, a card + // number as the run, and the same id in prose or in a json block still need one; fails_when=about one /ship body in ten still gets a false + // MEDIUM hold, or the card exemption reaches past the exact run value; why_new=the runs-block and #4480 tests cover pii.phone.e164 only; + // seam=fake gh; no_claim=real gh + test("a session id that reads as a card number is skipped only as the run of a runs line or the receipt", () => { + const HEAD_A = "abc123abc123abc123abc123abc123abc123abcd"; + const CARD_RUN = "4242405-1795338725-c32255b8"; + const DIGIT_RUN = "99999-1795338725-12345678"; + const post = (args: string[], text: string) => { + const f = fakeEnv({ files: { "/b": text }, visibility: "public" }); + return { code: run([...args, "--body-file", "/b"], f), cards: () => f.stdout().split("\n").filter(l => l.startsWith("RULE: pii.cc ")), f }; + }; + const runsBlock = (...lines: string[]) => `Body.\n\n\`\`\`gstack-receipt-runs\n${lines.join("\n")}\n\`\`\`\n`; + const receiptBody = (runId: string) => placeReceipt("## What changed\n\nOne fix.\n", orderReceipt({ schema_version: 1, pr: 4725, head: HEAD_A, base: "main", version: "1.0.0.0", run: runId })); + + for (const text of [runsBlock(JSON.stringify({ head: HEAD_A, run: CARD_RUN }), JSON.stringify({ head: HEAD_A, run: DIGIT_RUN, mark: "stop" })), receiptBody(CARD_RUN), receiptBody(DIGIT_RUN)]) { + const p = post(["pr-body", "4725", "--replace"], text); + expect([p.cards(), p.code, p.f.posts().length]).toEqual([[], EXIT.posted, 1]); + } + + const holds: Array<[string, string, string]> = [ + ["a card number in reason on a line with a valid run", runsBlock(JSON.stringify({ head: HEAD_A, run: CARD_RUN, mark: "abandoned", by: "ops", reason: "card 4111111111111111" })), "card 4111111111111111"], + ["a card number as a runs line's run", runsBlock(JSON.stringify({ head: HEAD_A, run: "4111111111111111" })), '"4111111111111111"'], + ["a spaced card number as a runs line's run", runsBlock(JSON.stringify({ head: HEAD_A, run: "4111 1111 1111 1111" })), '"4111 1111'], + ["a card number as the receipt's run", receiptBody("4111111111111111"), '"run": "4111111111111111"'], + ["the session id in prose after the block", runsBlock(JSON.stringify({ head: HEAD_A, run: DIGIT_RUN })) + `\nRun ${CARD_RUN} failed.\n`, `Run ${CARD_RUN}`], + ["the session id in a json block", `Body.\n\n\`\`\`json\n${JSON.stringify({ head: HEAD_A, run: CARD_RUN })}\n\`\`\`\n`, CARD_RUN], + ]; + for (const [name, text, needle] of holds) { + const p = post(["pr-comment", "1"], text); + const line = text.split("\n").findIndex(l => l.includes(needle)) + 1; + expect([name, [...new Set(p.cards())], p.code, p.f.posts()]).toEqual([name, [`RULE: pii.cc LINE: ${line} PART: body`], EXIT.confirm, []]); + } + }); + // Value: protects=a private PR's Approvals table (renderRecord, full mode) posts through pr-body without a pii.phone.e164 confirmation when a // fix-check grant row's Covers cell names a run id like 507-7192673 (GST-3246, trail-read row 8), while the same digits in that row's // Asked or Exact words cell, in prose, or in a table no Source footer closes still need one; fails_when=every private /ship body with such a