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. |
| 10 | import { readFileSync, writeFileSync } from 'node:fs'; |
| 11 | import { dirname, join } from 'node:path'; |
| 12 | import { fileURLToPath } from 'node:url'; |
| 13 | |
| 14 | const HERE = dirname(fileURLToPath(import.meta.url)); |
| 15 | const src = readFileSync(process.argv[2], 'utf8'); |
| 16 | |
| 17 | const 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 | }; |
| 22 | const strictLine = grab(/var SECRET_RE = \/.*\/i;/, 'SECRET_RE'); |
| 23 | const looseLine = grab(/var SECRET_RE_LOOSE = \/.*\/i;/, 'SECRET_RE_LOOSE'); |
| 24 | const fpFn = grab(/function fingerprint\(v\) \{[\s\S]*?\n\t\}/, 'fingerprint'); |
| 25 | const mod = new Function(strictLine + '\n' + looseLine + '\n' + fpFn + |
| 26 | '\nreturn { SECRET_RE: SECRET_RE, SECRET_RE_LOOSE: SECRET_RE_LOOSE, fingerprint: fingerprint };')(); |
| 27 | |
| 28 | const 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 | ]; |
| 35 | const prefixes = ['', 'x', 'x_', 'x.', 'x-', 'X_', '_']; |
| 36 | const suffixes = ['', '_', '.', '-', 'x', 'enc', 'ENC', 'Enc', 'enc_', 'enc.', 'encx', 'enc1', 'é', '_x']; |
| 37 | |
| 38 | const rows = []; |
| 39 | for (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 | } |
| 45 | writeFileSync(join(HERE, 'lens_secret_re.tsv'), rows.join('\n') + '\n'); |
| 46 | |
| 47 | const 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 | ]; |
| 52 | const fps = inputs.map(i => JSON.stringify(i) + '\t' + JSON.stringify(mod.fingerprint(i))); |
| 53 | fps.push(JSON.stringify(null) + '\t' + JSON.stringify(mod.fingerprint(null))); |
| 54 | fps.push(JSON.stringify(12345) + '\t' + JSON.stringify(mod.fingerprint(12345))); |
| 55 | writeFileSync(join(HERE, 'lens_fingerprint.tsv'), fps.join('\n') + '\n'); |
| 56 | console.log('wrote ' + rows.length + ' names and ' + fps.length + ' fingerprints'); |