|
| 1 | +/** |
| 2 | + * Safe findings. |
| 3 | + * |
| 4 | + * A Secretlint result carries `sourceContent` — the complete text that was |
| 5 | + * scanned. Nothing in this module ever stores, returns, throws, or logs one. |
| 6 | + * A raw result is normalized to a {@link SecretFinding} at the moment it is |
| 7 | + * produced and then dropped, so no later code has the chance to leak it. |
| 8 | + */ |
| 9 | + |
| 10 | +import { createHmac } from "node:crypto"; |
| 11 | + |
| 12 | +/** |
| 13 | + * One detection, carrying nothing that could reconstruct the secret. |
| 14 | + * |
| 15 | + * The matched value is deliberately absent, and so is Secretlint's message — |
| 16 | + * `maskSecrets: true` masks it today, but a field that is never populated |
| 17 | + * cannot be un-masked by a future change to how masking works. |
| 18 | + */ |
| 19 | +export interface SecretFinding { |
| 20 | + /** The rule that matched, e.g. `@secretlint/secretlint-rule-github`. */ |
| 21 | + ruleId: string; |
| 22 | + /** The rule's message identifier, e.g. `GITHUB_TOKEN`. */ |
| 23 | + messageId: string; |
| 24 | + /** Where in the scanned text the match began. */ |
| 25 | + location: { |
| 26 | + line: number; |
| 27 | + column: number; |
| 28 | + }; |
| 29 | + /** |
| 30 | + * A keyed digest of the matched value, comparable only within the execution |
| 31 | + * that produced it. See {@link createFingerprinter}. |
| 32 | + */ |
| 33 | + fingerprint: string; |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * Derives fingerprints for one execution. |
| 38 | + * |
| 39 | + * The digest is an HMAC under a key generated in memory for this execution, |
| 40 | + * not a plain hash. A plain hash would be an oracle: anyone holding a |
| 41 | + * candidate secret could confirm it by recomputing the digest and comparing. |
| 42 | + * Keying it removes that, and costs only cross-execution comparability, which |
| 43 | + * nothing needs — a fingerprint exists to tell "this same value appeared |
| 44 | + * twice in this run" from "these are two different values". |
| 45 | + * |
| 46 | + * The key never leaves this closure: it is not persisted, logged, exposed on |
| 47 | + * a finding, or reachable from one. |
| 48 | + */ |
| 49 | +export function createFingerprinter(): (matched: string) => string { |
| 50 | + const key = crypto.getRandomValues(new Uint8Array(32)); |
| 51 | + |
| 52 | + return function fingerprint(matched: string): string { |
| 53 | + return createHmac("sha256", key).update(matched).digest("hex").slice(0, 32); |
| 54 | + }; |
| 55 | +} |
| 56 | + |
| 57 | +/** |
| 58 | + * Normalize one Secretlint message into a safe finding. |
| 59 | + * |
| 60 | + * `source` is the text that was scanned. The matched slice is read from it, |
| 61 | + * handed straight to the fingerprinter, and goes out of scope when this |
| 62 | + * function returns — it is never assigned to the finding. |
| 63 | + */ |
| 64 | +export function toSecretFinding( |
| 65 | + message: { |
| 66 | + ruleId: string; |
| 67 | + messageId: string; |
| 68 | + range: readonly [number, number]; |
| 69 | + loc?: { start: { line: number; column: number } }; |
| 70 | + }, |
| 71 | + source: string, |
| 72 | + fingerprint: (matched: string) => string, |
| 73 | +): SecretFinding { |
| 74 | + return { |
| 75 | + ruleId: message.ruleId, |
| 76 | + messageId: message.messageId, |
| 77 | + location: { |
| 78 | + line: message.loc?.start.line ?? 1, |
| 79 | + column: message.loc?.start.column ?? 0, |
| 80 | + }, |
| 81 | + fingerprint: fingerprint(source.slice(message.range[0], message.range[1])), |
| 82 | + }; |
| 83 | +} |
| 84 | + |
| 85 | +function describe(findings: readonly SecretFinding[]): string { |
| 86 | + return findings |
| 87 | + .map((finding) => ` ${finding.ruleId} at ${finding.location.line}:${finding.location.column}`) |
| 88 | + .join("\n"); |
| 89 | +} |
| 90 | + |
| 91 | +/** |
| 92 | + * A credential was found in content that was about to be persisted. |
| 93 | + * |
| 94 | + * There is no allowlist, approval, or repair: a finding means a code or |
| 95 | + * data-flow defect that has to be fixed. The message names the rules and |
| 96 | + * positions and nothing else, so this error stays safe when it is serialized |
| 97 | + * into a close event or rendered as a diagnostic. |
| 98 | + */ |
| 99 | +export class SecretDetectedError extends Error { |
| 100 | + readonly findings: readonly SecretFinding[]; |
| 101 | + |
| 102 | + constructor(findings: readonly SecretFinding[]) { |
| 103 | + super( |
| 104 | + `secret detection rejected content before it was persisted:\n${describe(findings)}\n` + |
| 105 | + `Fix the code or data flow that produced it.`, |
| 106 | + ); |
| 107 | + this.name = "SecretDetectedError"; |
| 108 | + this.findings = snapshot(findings); |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +/** |
| 113 | + * An immutable copy of what was found. |
| 114 | + * |
| 115 | + * Freezing only the array would leave every finding — and every nested |
| 116 | + * `location` — writable through it, so a caller could edit the record of a |
| 117 | + * rejection after the fact. This error is the account of why content was |
| 118 | + * refused; it should not be editable by whoever receives it. |
| 119 | + */ |
| 120 | +function snapshot(findings: readonly SecretFinding[]): readonly SecretFinding[] { |
| 121 | + return Object.freeze( |
| 122 | + findings.map((finding) => |
| 123 | + Object.freeze({ |
| 124 | + ruleId: finding.ruleId, |
| 125 | + messageId: finding.messageId, |
| 126 | + location: Object.freeze({ |
| 127 | + line: finding.location.line, |
| 128 | + column: finding.location.column, |
| 129 | + }), |
| 130 | + fingerprint: finding.fingerprint, |
| 131 | + }), |
| 132 | + ), |
| 133 | + ); |
| 134 | +} |
| 135 | + |
| 136 | +/** |
| 137 | + * The detector itself failed, so the content could not be cleared. |
| 138 | + * |
| 139 | + * Failing closed is the point: an unusable detector must not become an open |
| 140 | + * gate. The originating error is deliberately dropped rather than attached as |
| 141 | + * `cause`, because a detector stack trace can quote the content it choked on. |
| 142 | + */ |
| 143 | +export class SecretScannerError extends Error { |
| 144 | + constructor(stage: string) { |
| 145 | + super( |
| 146 | + `secret detection failed during ${stage}, so the content was not persisted. ` + |
| 147 | + `The underlying error is withheld because it can contain scanned content.`, |
| 148 | + ); |
| 149 | + this.name = "SecretScannerError"; |
| 150 | + } |
| 151 | +} |
0 commit comments