Moved from wayBetter for X (scripts/build-store.mjs, scripts/sign-store.mjs) into the `waybetter` npm package, generic over the app: - bin/waybetter.mjs + cli/store.mjs. `build --config <store.config.mjs>`: the config names its app and returns its items (metadata, file path + bytes, optional preview); build replaces only that app's items in the shared catalog (other apps' items and files stay), removes files only its old items used, fills url/sha256/size/apps, and validates the whole catalog with the rules apps apply natively before anything is written. `sign` (and `sign --init`: create the key once, 0600, never overwritten) signs the exact catalog bytes; `verify` checks the signature with any listed public key (rotation) and the catalog rules. The private key is never printed, copied or moved; --key defaults to $WAYBETTER_STORE_KEY. pluginHeader() for configs. - store-catalog.schema.json (format 2, `apps` per item), exported with the CLI module. - Tests: multi-app builds in one folder, refusals (bad items, duplicates, paths outside the folder, hosts), schema vs CLI rules, sign/verify round trip incl. rotation and tampering, no key material printed. jsonSchema.mjs learns minItems, uniqueItems and allOf.
106 lines
7.5 KiB
JavaScript
106 lines
7.5 KiB
JavaScript
// `waybetter store build|sign|verify` and the store catalog JSON Schema.
|
|
import assert from 'node:assert/strict';
|
|
import { execFileSync } from 'node:child_process';
|
|
import { existsSync, mkdtempSync, readFileSync, writeFileSync } from 'node:fs';
|
|
import { tmpdir } from 'node:os';
|
|
import { join } from 'node:path';
|
|
import test from 'node:test';
|
|
import { fileURLToPath } from 'node:url';
|
|
import { validate } from '../../create-waybetter/tests/jsonSchema.mjs';
|
|
import { allowedUrl, buildStore, catalogProblems, pluginHeader, sha256 } from '../cli/store.mjs';
|
|
|
|
const BIN = fileURLToPath(new URL('../bin/waybetter.mjs', import.meta.url));
|
|
const SCHEMA = JSON.parse(readFileSync(new URL('../store-catalog.schema.json', import.meta.url), 'utf8'));
|
|
const run = (...args) => execFileSync(process.execPath, [BIN, ...args], { encoding: 'utf8', stdio: 'pipe' });
|
|
/** Runs the bin expecting a non-zero exit; returns what it printed on stderr. */
|
|
function fails(...args) {
|
|
try { run(...args); } catch (error) { assert.equal(error.status, 1); return String(error.stderr); }
|
|
assert.fail(`waybetter ${args.join(' ')} succeeded`);
|
|
}
|
|
const PLUGIN = '/**\n * @name Tool\n * @version 1.2.0\n * @author me\n * @permissions ui, network:none\n */\nmodule.exports = class {};\n';
|
|
|
|
function config(app, extra = []) {
|
|
return {
|
|
app,
|
|
items: async () => [
|
|
{ kind: 'plugin', ...pluginHeader(PLUGIN), id: 'tool', category: 'tools', tags: ['x'], file: `${app}-tool.plugin.js`, path: `plugins/${app}-tool.plugin.js`, bytes: PLUGIN, preview: { path: `previews/${app}-tool.svg`, bytes: '<svg xmlns="http://www.w3.org/2000/svg"/>' } },
|
|
{ kind: 'theme', id: 'nord', name: 'Nord', version: '1.0.0', category: 'dark', path: `themes/${app}-nord.json`, bytes: '{"name":"Nord"}\n', colors: { bg: '#2e3440', text: '#eceff4', accent: '#88c0d0' } },
|
|
...extra,
|
|
],
|
|
};
|
|
}
|
|
|
|
test('plugin headers: name, version, author, permissions; unknown permissions refused', () => {
|
|
assert.deepEqual(pluginHeader(PLUGIN), { name: 'Tool', description: '', version: '1.2.0', author: 'me', permissions: ['ui', 'network:none'] });
|
|
assert.equal(pluginHeader('module.exports = 1').permissions, null);
|
|
assert.throws(() => pluginHeader('/**\n * @permissions root\n */'), /unknown permission root/);
|
|
});
|
|
|
|
test('build: each app replaces only its own items and files in the shared catalog', async () => {
|
|
const out = mkdtempSync(join(tmpdir(), 'wb-store-'));
|
|
await buildStore(config('x'), { out });
|
|
await buildStore(config('kick'), { out });
|
|
let catalog = JSON.parse(readFileSync(join(out, 'catalog.json'), 'utf8'));
|
|
assert.equal(catalog.format, 2);
|
|
assert.deepEqual(catalog.items.map(item => `${item.apps}:${item.kind}:${item.id}`), ['x:plugin:tool', 'x:theme:nord', 'kick:plugin:tool', 'kick:theme:nord']);
|
|
const tool = catalog.items[0];
|
|
assert.equal(tool.url, 'https://waybetter.fyi/store/plugins/x-tool.plugin.js');
|
|
assert.equal(tool.sha256, sha256(Buffer.from(PLUGIN)));
|
|
assert.equal(tool.size, Buffer.byteLength(PLUGIN));
|
|
assert.deepEqual(tool.permissions, ['ui', 'network:none']);
|
|
assert.deepEqual(validate(SCHEMA, catalog), [], 'the schema accepts what build writes');
|
|
assert.deepEqual(catalogProblems(catalog, { app: 'x' }), []);
|
|
// x drops its theme: its file goes, kick's stays.
|
|
const smaller = { app: 'x', items: async () => (await config('x').items()).filter(item => item.kind === 'plugin') };
|
|
await buildStore(smaller, { out });
|
|
catalog = JSON.parse(readFileSync(join(out, 'catalog.json'), 'utf8'));
|
|
assert.deepEqual(catalog.items.map(item => `${item.apps}:${item.id}`), ['kick:tool', 'kick:nord', 'x:tool']);
|
|
assert.ok(!existsSync(join(out, 'themes/x-nord.json')) && existsSync(join(out, 'themes/kick-nord.json')));
|
|
});
|
|
|
|
test('build refuses items apps would refuse, and paths outside the store folder', async () => {
|
|
const out = mkdtempSync(join(tmpdir(), 'wb-store-'));
|
|
await assert.rejects(buildStore({ app: 'X!', items: async () => [] }, { out }), /app id/);
|
|
await assert.rejects(buildStore(config('x', [{ kind: 'plugin', id: 'bad', name: 'Bad', version: '1', file: '../bad.plugin.js', path: 'plugins/bad.plugin.js', bytes: PLUGIN }]), { out }), /plugin file/);
|
|
await assert.rejects(buildStore(config('x', [{ kind: 'theme', id: 'nord', name: 'Again', version: '1', path: 'themes/again.json', bytes: '{}' }]), { out }), /duplicate/);
|
|
await assert.rejects(buildStore(config('x', [{ kind: 'theme', id: 'up', name: 'Up', version: '1', path: '../up.json', bytes: '{}' }]), { out }), /inside the store folder/);
|
|
await assert.rejects(buildStore(config('x'), { out, base: 'https://evil.example/store/' }), /host not allowed/);
|
|
assert.ok(allowedUrl('https://git.crt.fyi/CRT/repo/raw/branch/main/a.json') && !allowedUrl('https://git.crt.fyi/CRT/repo/src/a.json'));
|
|
assert.ok(!allowedUrl('https://waybetter.fyi:444/a') && !allowedUrl('http://waybetter.fyi/a') && !allowedUrl('https://u:p@waybetter.fyi/a'));
|
|
});
|
|
|
|
test('the schema refuses what apps refuse', () => {
|
|
const good = { format: 2, name: 'S', generatedAt: '2026-09-29T00:00:00Z', items: [{ apps: ['x'], kind: 'theme', id: 'nord', name: 'Nord', version: '1', url: 'https://waybetter.fyi/store/themes/nord.json', sha256: 'a'.repeat(64), size: 1 }] };
|
|
assert.deepEqual(validate(SCHEMA, good), []);
|
|
for (const [what, patch] of [['format 1', { format: 1 }], ['no apps', { apps: [] }], ['app id', { apps: ['X'] }], ['repeated app', { apps: ['x', 'x'] }], ['sha256', { sha256: 'ABC' }], ['http', { url: 'http://waybetter.fyi/a' }], ['unknown field', { run: 'code' }], ['size', { size: 0 }]]) {
|
|
const doc = structuredClone(good);
|
|
if ('format' in patch) doc.format = patch.format; else Object.assign(doc.items[0], patch);
|
|
assert.notDeepEqual(validate(SCHEMA, doc), [], `accepted ${what}`);
|
|
assert.notDeepEqual(catalogProblems(doc, { app: 'x' }), [], `cli accepted ${what}`);
|
|
}
|
|
});
|
|
|
|
test('sign and verify: a fresh key signs, the signature verifies, a changed byte does not, key material is never printed', () => {
|
|
const dir = mkdtempSync(join(tmpdir(), 'wb-store-'));
|
|
const key = join(dir, 'keys', 'store.key');
|
|
const pub = join(dir, 'store.pub');
|
|
const other = join(dir, 'other.pub');
|
|
const catalog = join(dir, 'catalog.json');
|
|
writeFileSync(catalog, JSON.stringify({ format: 2, name: 'Test', generatedAt: '2026-09-29T00:00:00Z', items: [] }));
|
|
assert.match(run('store', 'sign', '--init', '--key', key, '--pub', pub), /Created the store key/);
|
|
const again = run('store', 'sign', '--init', '--key', key, '--pub', pub);
|
|
assert.match(again, /already exists/);
|
|
assert.doesNotMatch(again + readFileSync(pub, 'utf8'), /PRIVATE|BEGIN/, 'never prints key material');
|
|
assert.match(run('store', 'sign', catalog, '--key', key, '--pub', pub), /Signed and verified/);
|
|
assert.equal(Buffer.from(readFileSync(`${catalog}.sig`, 'utf8').trim(), 'base64').length, 64);
|
|
assert.equal(Buffer.from(readFileSync(pub, 'utf8').trim(), 'base64').length, 32);
|
|
assert.match(run('store', 'verify', catalog, '--pub', pub), /Verified/);
|
|
// Rotation: any listed key verifies.
|
|
run('store', 'sign', '--init', '--key', join(dir, 'keys', 'next.key'), '--pub', other);
|
|
assert.match(run('store', 'verify', catalog, '--pub', other, '--pub', pub), /Verified/);
|
|
assert.match(fails('store', 'verify', catalog, '--pub', other), /does NOT verify/);
|
|
writeFileSync(catalog, readFileSync(catalog, 'utf8').replace('Test', 'Tesu'));
|
|
assert.match(fails('store', 'verify', catalog, '--pub', pub), /does NOT verify/);
|
|
assert.match(fails('store', 'sign', catalog, '--key', join(dir, 'missing.key')), /no store key/);
|
|
assert.match(fails('store', 'frobnicate'), /Usage/);
|
|
});
|