Hub client: accept the public key pasted on one line; clearer error for a bad key

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012mt6TCv85T9VKRq3cV7rXq
This commit is contained in:
2026-10-26 12:58:44 +02:00
co-authored by Claude Opus 5.5
parent b67f40c2df
commit ff98a6fdfd
3 changed files with 45 additions and 5 deletions
+5 -1
View File
@@ -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;
+19 -4
View File
@@ -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 };
+21
View File
@@ -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);