Hub 2.2.0: a public key on the v2 heartbeat, Clear public key
Installs that registered with protocol v1 move to v2 by sending their public key on a signed v2 heartbeat: saved / matches / mismatch, never a failed heartbeat. Clear public key (2FA again) for a rebuilt server. Contracts 1.4.0 vendored; hub-protocol.md §6 updated. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -23,6 +23,7 @@ const { noticesForInstall } = require('../utils/notices');
|
||||
const ai = require('../utils/ai');
|
||||
const { PRODUCT_NAMES } = require('../utils/products');
|
||||
const { hub } = require('@crosscode/contracts');
|
||||
const { ed25519PublicFromPem } = require('@crosscode/contracts/signing');
|
||||
const { PROTOCOL_VERSIONS, isV2, parseV2, linkedProductsFor } = require('../utils/protocol');
|
||||
|
||||
const router = express.Router();
|
||||
@@ -175,6 +176,37 @@ router.post('/installs/register', registerLimiter, async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
/** The same key, ignoring line endings and surrounding whitespace. */
|
||||
function samePem(a, b) {
|
||||
const norm = s => String(s || '').replace(/\r\n/g, '\n').trim();
|
||||
return norm(a) === norm(b);
|
||||
}
|
||||
|
||||
/**
|
||||
* A public key sent on a v2 heartbeat (Hub 2.2.0): `saved` when the install had none, `matches`,
|
||||
* or `mismatch` (the saved key stays; an alert is opened once). Lets an install that registered
|
||||
* with protocol v1 move to v2 without its licence key. Never fails the heartbeat: the licence
|
||||
* must keep refreshing whatever happens to the key.
|
||||
*/
|
||||
async function acceptPublicKey(install, pem, now) {
|
||||
try {
|
||||
ed25519PublicFromPem(pem);
|
||||
} catch {
|
||||
return 'mismatch';
|
||||
}
|
||||
if (!install.publicKey) {
|
||||
await prisma.install.update({ where: { id: install.id }, data: { publicKey: `${pem.replace(/\r\n/g, '\n').trim()}\n` } });
|
||||
await prisma.installEvent.create({ data: { installId: install.id, type: 'public_key', level: 'info', message: 'Public key saved: the church\'s other products can now link to this install.', resolvedAt: now } });
|
||||
return 'saved';
|
||||
}
|
||||
if (samePem(install.publicKey, pem)) return 'matches';
|
||||
const open = await prisma.installEvent.findFirst({ where: { installId: install.id, type: 'public_key_mismatch', resolvedAt: null } });
|
||||
if (!open) {
|
||||
await prisma.installEvent.create({ data: { installId: install.id, type: 'public_key_mismatch', level: 'warning', message: 'The install sent a different public key from the one saved. Linked products keep using the saved key. If the server was restored or rebuilt, clear the saved key on this install\'s page.' } });
|
||||
}
|
||||
return 'mismatch';
|
||||
}
|
||||
|
||||
router.post('/installs/heartbeat', heartbeatLimiter, async (req, res) => {
|
||||
try {
|
||||
const install = await signedInstall(req, res);
|
||||
@@ -200,12 +232,15 @@ router.post('/installs/heartbeat', heartbeatLimiter, async (req, res) => {
|
||||
}
|
||||
}
|
||||
|
||||
const publicKeyStatus = v2 && body.publicKey ? await acceptPublicKey(install, body.publicKey, now) : null;
|
||||
|
||||
res.json({
|
||||
licence: await tokenFor(install),
|
||||
...(await releaseInfo(install, version || install.version, { v2 })),
|
||||
notices: await noticesForInstall(install.id, now),
|
||||
...(v2 ? { linkedProducts: await linkedProductsFor(install) } : {}),
|
||||
serverTime: now.toISOString(),
|
||||
...(publicKeyStatus ? { publicKeyStatus } : {}),
|
||||
});
|
||||
} catch (e) {
|
||||
console.error('[heartbeat]', e);
|
||||
|
||||
Reference in New Issue
Block a user