diff --git a/apps/events/backend/src/hub/licence.js b/apps/events/backend/src/hub/licence.js index efb23d1..770687e 100644 --- a/apps/events/backend/src/hub/licence.js +++ b/apps/events/backend/src/hub/licence.js @@ -24,7 +24,11 @@ function publicKey() { const pem = hubPublicKey(); if (!pem) return null; if (pem !== publicKeyPem) { - publicKeyObject = crypto.createPublicKey(pem); + try { + publicKeyObject = crypto.createPublicKey(pem); + } catch { + throw new Error("The Hub public key can't be read — check HUB_PUBLIC_KEY in backend/.env (copy it from the Hub's Settings page)."); + } publicKeyPem = pem; } return publicKeyObject; diff --git a/apps/events/backend/src/hub/publicKey.js b/apps/events/backend/src/hub/publicKey.js index e0d672d..6bcf320 100644 --- a/apps/events/backend/src/hub/publicKey.js +++ b/apps/events/backend/src/hub/publicKey.js @@ -9,9 +9,24 @@ const PRODUCTION_PUBLIC_KEY = null; -function hubPublicKey() { - const fromEnv = (process.env.HUB_PUBLIC_KEY || '').replace(/\\n/g, '\n').trim(); - return fromEnv || PRODUCTION_PUBLIC_KEY; +/** + * A public key as proper PEM, however it was pasted: with real line breaks, with "\n", all on + * one line ("-----BEGIN PUBLIC KEY-----MCow…-----END PUBLIC KEY-----"), or just the base64. + */ +function normalisePem(input) { + const text = String(input || '').replace(/\\n/g, '\n').trim(); + if (!text) return null; + const body = text + .replace(/-----BEGIN PUBLIC KEY-----/, '') + .replace(/-----END PUBLIC KEY-----/, '') + .replace(/\s+/g, ''); + if (!/^[A-Za-z0-9+/]+={0,2}$/.test(body)) return text; // not base64 — let the caller's parse fail clearly + const lines = body.match(/.{1,64}/g).join('\n'); + return `-----BEGIN PUBLIC KEY-----\n${lines}\n-----END PUBLIC KEY-----\n`; } -module.exports = { hubPublicKey, PRODUCTION_PUBLIC_KEY }; +function hubPublicKey() { + return normalisePem(process.env.HUB_PUBLIC_KEY) || PRODUCTION_PUBLIC_KEY; +} + +module.exports = { hubPublicKey, normalisePem, PRODUCTION_PUBLIC_KEY }; diff --git a/apps/events/backend/tests/hub.test.js b/apps/events/backend/tests/hub.test.js index 724345f..fdb57ee 100644 --- a/apps/events/backend/tests/hub.test.js +++ b/apps/events/backend/tests/hub.test.js @@ -101,6 +101,27 @@ describe('read-only mode', () => { ])('%s %s keeps working', (m, p) => expect(isBlocked(m, p)).toBe(false)); }); +describe('pasting the Hub public key', () => { + const { normalisePem } = require('../src/hub/publicKey'); + const pem = publicKey.export({ type: 'spki', format: 'pem' }).toString(); + const base64 = pem.replace(/-----(BEGIN|END) PUBLIC KEY-----|\s/g, ''); + + test.each([ + ['proper PEM', pem], + ['with \\n', pem.replace(/\n/g, '\\n')], + ['all on one line', `-----BEGIN PUBLIC KEY-----${base64}-----END PUBLIC KEY-----`], + ['just the base64', base64], + ])('%s works', (label, input) => { + expect(() => crypto.createPublicKey(normalisePem(input))).not.toThrow(); + expect(verifyToken(sign(base), crypto.createPublicKey(normalisePem(input)))).not.toBeNull(); + }); + + test('empty means none', () => { + expect(normalisePem('')).toBeNull(); + expect(normalisePem(undefined)).toBeNull(); + }); +}); + test('version comparison for "update available"', () => { expect(compareVersions('1.41.0', '1.41.1')).toBeLessThan(0); expect(compareVersions('1.41.0', '1.41.0')).toBe(0);