waybetter-for-x/scripts/privacy-scan.mjs
2026-09-30 23:01:13 +00:00

75 lines
4.3 KiB
JavaScript

#!/usr/bin/env node
// Checks that nothing this repository publishes names a person: `waybetter privacy-scan` (the
// framework's command, waybetter/packages/waybetter/cli/privacy.mjs) over
//
// 1. every file git tracks here and in the framework submodule (and their names),
// 2. the built site (apps/web/dist, when it exists),
// 3. the npm tarballs of `waybetter` and `create-waybetter`, packed into a temporary folder.
//
// The forbidden terms are a file outside every repository ("forbiddenTerms" in
// waybetter.release.json, or WAYBETTER_FORBIDDEN_TERMS); this machine's account name is always
// added. A term is never printed: hits show the file, the line or offset and `x***`. Reviewed
// false positives: privacy-allowlist.txt.
//
// node scripts/privacy-scan.mjs everything above (bun run privacy:scan)
// node scripts/privacy-scan.mjs --fast tracked files only, and nothing at all (quietly)
// when the terms file does not exist (bun run test)
// node scripts/privacy-scan.mjs --history every blob, path, commit message and identity of
// both repositories (before a history is published)
// --all list every hit, not the first five of each file
//
// Native artifacts are checked by the release itself (`waybetter release` refuses an installer, a
// binary, a dmg or an updater bundle with a hit); to check one by hand:
// node waybetter/packages/waybetter/bin/waybetter.mjs privacy-scan <file or folder>
import { spawnSync } from 'node:child_process';
import { existsSync, mkdtempSync, readdirSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { dirname, join, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { TERMS_ENV, privacyCommand, privacyConfig } from '../waybetter/packages/waybetter/cli/privacy.mjs';
const root = resolve(dirname(fileURLToPath(import.meta.url)), '..');
const args = process.argv.slice(2);
const fast = args.includes('--fast');
const extra = args.includes('--all') ? ['--all'] : [];
const allow = ['--allow', join(root, 'privacy-allowlist.txt')];
// The same terms for every step, also the ones that run in a temporary folder.
const configured = process.env[TERMS_ENV] || privacyConfig(root).forbiddenTerms;
const terms = configured && existsSync(configured) ? ['--terms', configured] : [];
let worst = 0;
const run = (label, scanArgs, cwd = root) => {
if (!fast) console.log(`\n${label}`);
worst = Math.max(worst, privacyCommand([...scanArgs, ...terms, ...allow, ...extra, ...(fast ? ['--if-terms'] : [])], { cwd }));
};
try {
if (args.includes('--history')) {
run('history of this repository and of the framework', ['--history', '.', 'waybetter']);
} else {
run('tracked files (this repository, the framework)', ['--tracked', '.', 'waybetter']);
if (!fast) {
const site = join(root, 'apps/web/dist');
if (existsSync(site)) run('the built site (apps/web/dist)', ['apps/web/dist']);
else console.log('\nthe built site: apps/web/dist does not exist (bun run build:server), skipped');
const packed = mkdtempSync(join(tmpdir(), 'wb-pack-'));
try {
for (const name of ['waybetter', 'create-waybetter']) {
const cwd = join(root, 'waybetter/packages', name);
if (name === 'waybetter' && !existsSync(join(cwd, 'dist'))) console.log('\nnote: waybetter/packages/waybetter/dist is not built; the tarball is packed without it');
// A fixed command (no user input); the shell resolves npm's .cmd shim on Windows.
const pack = spawnSync(`npm pack --silent --pack-destination "${packed}"`, { cwd, shell: true, stdio: ['ignore', 'ignore', 'inherit'] });
if (pack.status !== 0) throw new Error(`npm pack failed in waybetter/packages/${name}`);
}
run(`npm tarballs (${readdirSync(packed).join(', ')})`, ['.'], packed);
} finally {
rmSync(packed, { recursive: true, force: true });
}
}
}
} catch (error) {
console.error(`privacy-scan: ${error instanceof Error ? error.message : error}`);
process.exit(1);
}
if (worst) console.error('\nprivacy-scan: FAILED. Fix the files above (or, for a reviewed false positive, add a line to privacy-allowlist.txt).');
process.exit(worst);