import { fc } from "@fast-check/vitest"; import { expect } from "vitest"; import { __test } from "../index.ts"; import { prop } from "./replay.ts"; const alphaNum = fc.constantFrom( ..."ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", ); const body = fc.string({ unit: alphaNum, minLength: 20, maxLength: 24 }); const base64UrlBody = fc .tuple( fc.string({ unit: alphaNum, minLength: 19, maxLength: 23 }), fc.constantFrom("-", "_"), ) .map(([prefix, suffix]) => prefix + suffix); function ageKey(value: string) { const hrp = "age-secret-key-"; const charset = "qpzry9x8gf2tvdw0s3jn54khce6mua7l"; const data = Array.from( { length: 52 }, (_, index) => value.charCodeAt(index % value.length) & 31, ); const values = [ ...[...hrp].map((char) => char.charCodeAt(0) >> 5), 0, ...[...hrp].map((char) => char.charCodeAt(0) & 31), ...data, 0, 0, 0, 0, 0, 0, ]; let checksum = 1; for (const item of values) { const top = checksum >>> 25; checksum = (((checksum & 0x1ffffff) << 5) ^ item) >>> 0; for (let bit = 0; bit < 5; bit++) if ((top >>> bit) & 1) checksum = (checksum ^ [0x3b6a57b2, 0x26508e6d, 0x1ea119fa, 0x3d4233dd, 0x2a1462b3][ bit ]) >>> 0; } checksum ^= 1; const tail = Array.from( { length: 6 }, (_, index) => (checksum >>> (5 * (5 - index))) & 31, ); return `AGE-SECRET-KEY-1${[...data, ...tail].map((item) => charset[item]).join("")}`; } const cases = [ ["OPENROUTER_TOKEN", (v: string) => ["sk-or-v1-", v.repeat(2)].join("")], ["OPENAI_TOKEN", (v: string) => ["sk-", "proj-", v.repeat(2)].join("")], [ "ANTHROPIC_TOKEN", (v: string) => ["sk-ant-", "api03-", v.repeat(2)].join(""), ], ["HUGGINGFACE_TOKEN", (v: string) => ["hf_", v.repeat(2)].join("")], ["GITHUB_TOKEN", (v: string) => ["ghp_", v.repeat(2)].join("")], ["NPM_TOKEN", (v: string) => ["npm_", v.repeat(2)].join("")], [ "AWS_ACCESS_KEY_ID", (v: string) => ["AKIA", v.toUpperCase().slice(0, 16)].join(""), ], [ "GOOGLE_API_KEY", (v: string) => ["AIza", v.repeat(2).slice(0, 34), v.at(-1)].join(""), ], [ "STRIPE_LIVE_SECRET_KEY", (v: string) => ["sk_", "live_", v.repeat(2)].join(""), ], ["SLACK_TOKEN", (v: string) => ["xoxb-", v.repeat(2)].join("")], [ "SLACK_WEBHOOK", (v: string) => ["https://hooks.slack.com/", "services/", v.repeat(2)].join(""), ], ["SENDGRID_TOKEN", (v: string) => ["SG.", v, ".", v.repeat(3)].join("")], ["OAUTH_TOKEN", (v: string) => ["ya29.", v.repeat(3)].join("")], [ "PGP_PRIVATE_KEY", (v: string) => [ "-----BEGIN PGP ", "PRIVATE KEY BLOCK-----\n", v, "\n-----END PGP ", "PRIVATE KEY BLOCK-----", ].join(""), ], [ "COSIGN_PRIVATE_KEY", (v: string) => [ "-----BEGIN ENCRYPTED COSIGN ", "PRIVATE KEY-----\n", v, "\n-----END ENCRYPTED COSIGN ", "PRIVATE KEY-----", ].join(""), ], ["AGE_SECRET_KEY", ageKey], [ "MINISIGN_SECRET_KEY", (v: string) => { const record = Buffer.alloc(158, v.charCodeAt(0)); record.write("EdScB2", 0, "ascii"); return `untrusted comment: minisign encrypted secret key\n${record.toString("base64")}`; }, ], ] as const; const base64UrlTags = new Set([ "OPENAI_TOKEN", "ANTHROPIC_TOKEN", "GOOGLE_API_KEY", "SENDGRID_TOKEN", "OAUTH_TOKEN", ]); for (const [index, [tag, build]] of cases.entries()) { const generatedBody = base64UrlTags.has(tag) ? base64UrlBody : body; prop( `builtin-${index}-positive`, { body: generatedBody, left: fc.constantFrom("", "\n"), right: fc.constantFrom("", "\n"), }, 200, )(`${tag} accepts valid values with exact spans`, ({ body, left, right }) => { const value = build(body); const text = left + value + right; const span = __test .findBuiltinSpans(text) .find((candidate) => candidate.labels.some((item) => item.tag === tag)); expect(span?.value).toBe(value); expect(text.slice(span?.start, span?.end)).toBe(value); }); prop( `builtin-${index}-negative`, { body: generatedBody }, 200, )(`${tag} rejects one-field-invalid values`, ({ body }) => { const built = build(body); let value = `!${built.slice(1)}`; if (tag === "AGE_SECRET_KEY") value = `${built.slice(0, -1)}${built.endsWith("q") ? "p" : "q"}`; if (tag === "MINISIGN_SECRET_KEY") { const [comment, encoded] = built.split("\n"); const record = Buffer.from(encoded, "base64"); record.write("XX", 2, "ascii"); value = `${comment}\n${record.toString("base64")}`; } expect( __test .findBuiltinSpans(value) .some((span) => span.labels.some((item) => item.tag === tag)), ).toBe(false); }); }