c043897b0e
Review follow-ups on #1304. SIGNING_KEY_UNREADABLE. USAGE_ENCRYPTION_KEY defaults to JWT_SECRET, so rotating JWT_SECRET — the correct response to a suspected compromise — makes the stored Ed25519 key undecryptable. That surfaced as a generic DELIVERY_FAILED which retried forever, and it silently blocks the DELETE packet too: an operator who withdraws has their local state cleared while the collector keeps its copy. decrypt() now tags its own failure and deliver() reports it under its own name, without flagging an identity conflict — an unreadable key is not evidence of a clone. The docs already warned that losing the key breaks deletion signing; they now name the trigger and the error. The collector default is no longer an inline string in the constructor. It is a declared DEFAULT_COLLECTOR_URL, since it is a deployment choice: self-hosters point USAGE_COLLECTOR_URL at their own collector and the UI already derives every link from whatever is configured. schema.cjs is deliberately untouched — it is vendored byte-identical with picpeak-usage, and its $id is a schema identity, not a delivery address. Links in the consent dialog. It named the collector inside prose but never linked it, so an operator deciding whether to opt in could not open the destination or the public schema without retyping a URL. Both are links now, built from the configured collector. UI standards. The tab hand-rolled its surfaces as `<section className="rounded-xl border border-theme …">` and imported Button from a deep path; every other settings tab uses `<Card padding="md">` from the components/common barrel. Converted, with the feedback <form> wrapped rather than replaced so its semantics survive, and headings given the same colour tokens as ImageSecurityTab. The barrel pulls ErrorBoundary -> i18n/config, so the tab's test needed the initReactI18next shim the FaceRecognitionCard test already uses. Not changed: the delete packet reusing the current sequence. The collector handles delete before any sequence check — "possession proof is sufficient for deletion, including when a restored backup has a stale sequence" (picpeak-usage server/collector.js) — so deletion is deliberately sequence-exempt and the client is correct as written. Refs #1110
95 lines
3.6 KiB
JavaScript
95 lines
3.6 KiB
JavaScript
/**
|
|
* The signing key is encrypted with USAGE_ENCRYPTION_KEY, which defaults to
|
|
* JWT_SECRET. Rotating JWT_SECRET — the correct response to a suspected
|
|
* compromise — makes that key unreadable.
|
|
*
|
|
* Before this was named, the failure surfaced as a generic DELIVERY_FAILED
|
|
* that retried forever, and it silently blocked the DELETE packet as well:
|
|
* an operator who asked to withdraw had their local state cleared while the
|
|
* collector kept its copy, with nothing in the UI explaining why.
|
|
*/
|
|
const knex = require('knex');
|
|
const { UsageService } = require('../../src/usage/UsageService');
|
|
|
|
const SECRET_A = 'a'.repeat(48);
|
|
const SECRET_B = 'b'.repeat(48);
|
|
|
|
async function bootDb() {
|
|
const db = knex({
|
|
client: 'sqlite3',
|
|
connection: { filename: ':memory:' },
|
|
useNullAsDefault: true,
|
|
});
|
|
await db.schema.createTable('product_usage_state', (t) => {
|
|
t.integer('id').primary();
|
|
t.string('status', 30).notNullable().defaultTo('disabled');
|
|
t.boolean('notice_dismissed').notNullable().defaultTo(false);
|
|
t.string('installation_id', 64);
|
|
t.string('public_key', 59);
|
|
t.text('private_key_encrypted');
|
|
t.string('instance_binding', 64);
|
|
t.bigInteger('sequence').notNullable().defaultTo(0);
|
|
t.text('pending_packet');
|
|
t.text('last_packet');
|
|
t.text('last_receipt');
|
|
t.string('last_report_date', 10);
|
|
t.string('last_error', 80);
|
|
t.text('feedback_preferences');
|
|
t.string('lease_token', 36);
|
|
t.bigInteger('lease_until').notNullable().defaultTo(0);
|
|
});
|
|
await db('product_usage_state').insert({ id: 1 });
|
|
return db;
|
|
}
|
|
|
|
describe('usage signing key becomes unreadable after secret rotation', () => {
|
|
let db;
|
|
afterEach(async () => { if (db) await db.destroy(); db = null; });
|
|
|
|
it('names the failure instead of reporting a generic decrypt error', async () => {
|
|
db = await bootDb();
|
|
const before = new UsageService(db, { secret: SECRET_A });
|
|
const sealed = before.encrypt('the-signing-key');
|
|
|
|
// Same value, different secret — exactly what rotating JWT_SECRET does.
|
|
const after = new UsageService(db, { secret: SECRET_B });
|
|
expect(() => after.decrypt(sealed)).toThrow(
|
|
expect.objectContaining({ code: 'SIGNING_KEY_UNREADABLE' })
|
|
);
|
|
});
|
|
|
|
it('still round-trips under the unrotated secret', async () => {
|
|
db = await bootDb();
|
|
const service = new UsageService(db, { secret: SECRET_A });
|
|
expect(service.decrypt(service.encrypt('the-signing-key'))).toBe('the-signing-key');
|
|
});
|
|
|
|
it('records SIGNING_KEY_UNREADABLE rather than DELIVERY_FAILED, and does not flag an identity conflict', async () => {
|
|
db = await bootDb();
|
|
const sealed = new UsageService(db, { secret: SECRET_A }).encrypt('key');
|
|
await db('product_usage_state').where({ id: 1 }).update({
|
|
status: 'active',
|
|
installation_id: 'a'.repeat(64),
|
|
public_key: 'p'.repeat(59),
|
|
private_key_encrypted: sealed,
|
|
sequence: 1,
|
|
pending_packet: JSON.stringify({
|
|
action: 'delete', packet_id: 'x', installation_id: 'a'.repeat(64), sequence: 1,
|
|
}),
|
|
});
|
|
|
|
const service = new UsageService(db, {
|
|
secret: SECRET_B,
|
|
endpoint: 'http://127.0.0.1:9/',
|
|
// A delivery must never be attempted: signing fails first.
|
|
fetch: () => { throw new Error('network must not be reached'); },
|
|
});
|
|
await service.deliver(await db('product_usage_state').where({ id: 1 }).first());
|
|
|
|
const row = await db('product_usage_state').where({ id: 1 }).first();
|
|
expect(row.last_error).toBe('SIGNING_KEY_UNREADABLE');
|
|
// A key we cannot read is not evidence of a cloned installation.
|
|
expect(row.status).toBe('active');
|
|
});
|
|
});
|