Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_net/tests/data/lens_oracle.mjs

3.0 KiB, 1 run

created by r1870400018:65654, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1// Regenerates the two oracle tables `tests/lens.rs` reads, from the JavaScript the Rust code mirrors.
2//
3// node lens_oracle.mjs <path to daimond/www/js/debugshare.js>
4//
5// It lifts `SECRET_RE`, `SECRET_RE_LOOSE` and `fingerprint` out of the source text, so a
6// transcription error cannot hide in the Rust, and runs them over a generated set of names and
7// strings. `lens_secret_re.tsv` holds `name <TAB> strict <TAB> loose`, one per line, and
8// `lens_fingerprint.tsv` holds `input <TAB> output`, each JSON-encoded, because a fingerprint's
9// head can hold a line break.
10import { readFileSync, writeFileSync } from 'node:fs';
11import { dirname, join } from 'node:path';
12import { fileURLToPath } from 'node:url';
13
14const HERE = dirname(fileURLToPath(import.meta.url));
15const src = readFileSync(process.argv[2], 'utf8');
16
17const grab = (re, what) => {
18 const m = re.exec(src);
19 if (!m) throw new Error('not found in the source: ' + what);
20 return m[0];
21};
22const strictLine = grab(/var SECRET_RE = \/.*\/i;/, 'SECRET_RE');
23const looseLine = grab(/var SECRET_RE_LOOSE = \/.*\/i;/, 'SECRET_RE_LOOSE');
24const fpFn = grab(/function fingerprint\(v\) \{[\s\S]*?\n\t\}/, 'fingerprint');
25const mod = new Function(strictLine + '\n' + looseLine + '\n' + fpFn +
26 '\nreturn { SECRET_RE: SECRET_RE, SECRET_RE_LOOSE: SECRET_RE_LOOSE, fingerprint: fingerprint };')();
27
28const words = [
29 'apikey', 'api_key', 'key', 'token', 'secret', 'passphrase', 'password', 'salt', 'wrapped',
30 'wrappedpriv', 'sealed', 'seal', 'privatekey', 'priv', 'mnemonic', 'seed', 'masterkey',
31 // Decoys, and the camelCase shapes the loose companion exists for.
32 'keys', 'tokens', 'tokenStats', 'pushToken', 'refreshSecret', 'monkey', 'keyboard', 'apiKey',
33 'ApiKey', 'API_KEY', 'Wrapped', 'sealedBox', 'oxenymKeys', 'durable', 'x',
34];
35const prefixes = ['', 'x', 'x_', 'x.', 'x-', 'X_', '_'];
36const suffixes = ['', '_', '.', '-', 'x', 'enc', 'ENC', 'Enc', 'enc_', 'enc.', 'encx', 'enc1', 'é', '_x'];
37
38const rows = [];
39for (const w of words) for (const p of prefixes) for (const s of suffixes) {
40 const name = p + w + s;
41 const strict = mod.SECRET_RE.test(name);
42 const loose = strict || mod.SECRET_RE_LOOSE.test(name);
43 rows.push(name + '\t' + (strict ? 1 : 0) + '\t' + (loose ? 1 : 0));
44}
45writeFileSync(join(HERE, 'lens_secret_re.tsv'), rows.join('\n') + '\n');
46
47const inputs = [
48 '', 'a', 'abc', 'abcdef', 'abcdefg', 'zz-not-a-key-0123456789', 'hello world',
49 'correct horse battery staple', 'tab\there', 'line\nbreak', 'quote"back\\slash', 'é', 'éèêëàâ',
50 '日本語のパスフレーズ', 'ab😀cd', '😀😀😀', 'x'.repeat(300), '12345', 'true',
51];
52const fps = inputs.map(i => JSON.stringify(i) + '\t' + JSON.stringify(mod.fingerprint(i)));
53fps.push(JSON.stringify(null) + '\t' + JSON.stringify(mod.fingerprint(null)));
54fps.push(JSON.stringify(12345) + '\t' + JSON.stringify(mod.fingerprint(12345)));
55writeFileSync(join(HERE, 'lens_fingerprint.tsv'), fps.join('\n') + '\n');
56console.log('wrote ' + rows.length + ' names and ' + fps.length + ' fingerprints');