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 };