mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-18 16:08:56 +00:00
fix(cli): no test fixture is shaped like a live HeyGen key, and CI refuses one (#7876)
* fix(cli): no test fixture is shaped like a live HeyGen key, and CI refuses one * style(cli): format the key fixture and committed-key check
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
import { readFileSync } from "node:fs";
|
||||
import { resolve } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { readTrackedPaths } from "./check-tracked-artifacts.mjs";
|
||||
|
||||
const KEY_SHAPES = [
|
||||
/sk_V2_[A-Za-z0-9]+_[A-Za-z0-9]{8,}_[A-Za-z0-9]{24,}/g,
|
||||
/\bhg_[A-Za-z0-9]{24,}/g,
|
||||
];
|
||||
const MAX_SCANNED_BYTES = 4 * 1024 * 1024;
|
||||
|
||||
function looksRandom(value) {
|
||||
const tail = value.slice(value.lastIndexOf("_") + 1);
|
||||
const classes = [/[a-z]/, /[A-Z]/, /[0-9]/].filter((re) => re.test(tail)).length;
|
||||
return classes >= 2 && new Set(tail).size >= tail.length / 3;
|
||||
}
|
||||
|
||||
export function findKeyShapedValues(text) {
|
||||
const found = [];
|
||||
for (const shape of KEY_SHAPES) {
|
||||
for (const match of text.matchAll(shape)) {
|
||||
if (looksRandom(match[0])) found.push(match.index);
|
||||
}
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
export function lineOf(text, index) {
|
||||
return text.slice(0, index).split("\n").length;
|
||||
}
|
||||
|
||||
export function checkCommittedKeys(cwd = process.cwd()) {
|
||||
const hits = [];
|
||||
for (const path of readTrackedPaths(cwd)) {
|
||||
let buffer;
|
||||
try {
|
||||
buffer = readFileSync(resolve(cwd, path));
|
||||
} catch {
|
||||
continue;
|
||||
}
|
||||
if (buffer.length > MAX_SCANNED_BYTES || buffer.includes(0)) continue;
|
||||
const text = buffer.toString("utf8");
|
||||
for (const index of findKeyShapedValues(text)) hits.push(`${path}:${lineOf(text, index)}`);
|
||||
}
|
||||
return hits.sort();
|
||||
}
|
||||
|
||||
function main() {
|
||||
const hits = checkCommittedKeys();
|
||||
if (hits.length === 0) {
|
||||
console.log("Committed key check passed.");
|
||||
return;
|
||||
}
|
||||
console.error("Values shaped like a live HeyGen API key are tracked by Git:");
|
||||
for (const hit of hits) console.error(`- ${hit}`);
|
||||
console.error('Build test keys at runtime from obvious filler, such as "x".repeat(32).');
|
||||
process.exitCode = 1;
|
||||
}
|
||||
|
||||
const entryPath = process.argv[1] ? resolve(process.argv[1]) : null;
|
||||
if (entryPath === fileURLToPath(import.meta.url)) main();
|
||||
@@ -0,0 +1,27 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { describe, it } from "node:test";
|
||||
import { findKeyShapedValues, lineOf } from "./check-committed-keys.mjs";
|
||||
|
||||
const random = (length) =>
|
||||
Array.from({ length }, (_, i) => "aZ3kQ9mP2xR7bN5vL8cT4wY6"[(i * 7 + 3) % 24]).join("");
|
||||
const liveShapedSk = ["sk", "V2", "hgu", random(11), random(32)].join("_");
|
||||
const liveShapedHg = ["hg", random(40)].join("_");
|
||||
|
||||
describe("committed key check", () => {
|
||||
it("finds values shaped like a live sk_V2_ or hg_ key", () => {
|
||||
assert.equal(findKeyShapedValues(`const k = "${liveShapedSk}";`).length, 1);
|
||||
assert.equal(findKeyShapedValues(`key=${liveShapedHg}\n`).length, 1);
|
||||
});
|
||||
|
||||
it("passes obvious fakes and short toy values", () => {
|
||||
const fakeSk = ["sk", "V2", "hgu", "0".repeat(11), "x".repeat(32)].join("_");
|
||||
for (const text of [fakeSk, "hg_test_abc", "hg_legacy_key", "sk_V2_hgu_abcd123", "hg_x"]) {
|
||||
assert.deepEqual(findKeyShapedValues(text), [], text);
|
||||
}
|
||||
});
|
||||
|
||||
it("reports the line a value is on", () => {
|
||||
const text = `a\nb\nconst k = "${liveShapedSk}";\n`;
|
||||
assert.equal(lineOf(text, findKeyShapedValues(text)[0]), 3);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user