Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

address review: validate PEM parseability in loadCert

[._.]/ Adam Eivy7629aed23d38
diff --git a/lib/tailscale-https.js b/lib/tailscale-https.js
index 845b70fbbc..4d4e317c75 100644
--- a/lib/tailscale-https.js
+++ b/lib/tailscale-https.js
@@ -26,18 +26,29 @@ import { createServer as createHttpServer } from 'http';
import { createServer as createHttpsServer } from 'https';
import { existsSync, readFileSync, watch } from 'fs';
import { join } from 'path';
+import { createSecureContext } from 'tls';
+// `cert.pem` + `key.pem` can exist but be empty / partially-written / otherwise
+// unparseable when a prior provisioner (tailscale cert, openssl, certbot) was
+// interrupted mid-write. createHttpsServer crashes on bad PEMs, taking the
+// whole server with it — so we validate parseability here and treat invalid
+// files as "no cert", letting createTailscaleServers degrade to plain HTTP
+// instead of refusing to boot. createSecureContext is what the HTTPS server
+// uses internally, so its acceptance rules are the right thing to mirror.
function loadCert(certDir) {
if (!certDir) return null;
const certPath = join(certDir, 'cert.pem');
const keyPath = join(certDir, 'key.pem');
if (!existsSync(certPath) || !existsSync(keyPath)) return null;
- return {
- cert: readFileSync(certPath),
- key: readFileSync(keyPath),
- certPath,
- keyPath
- };
+ let cert, key;
+ try {
+ cert = readFileSync(certPath);
+ key = readFileSync(keyPath);
+ createSecureContext({ cert, key });
+ } catch {
+ return null;
+ }
+ return { cert, key, certPath, keyPath };
}
/**
diff --git a/scripts/setup-cert.js b/scripts/setup-cert.js
index 452e080467..6b8aa680d3 100644
--- a/scripts/setup-cert.js
+++ b/scripts/setup-cert.js
@@ -198,6 +198,15 @@ function maybeFallbackSelfSigned() {
const meta = readMeta();
const hasCert = hasTailscaleCert(CERT_DIR);
+ // Files exist but are unparseable (interrupted provisioning). hasTailscaleCert
+ // already returned false, so the server will boot HTTP — but the user has
+ // cert files on disk wondering why HTTPS isn't active. Tell them explicitly.
+ if (!hasCert && existsSync(CERT_PATH) && existsSync(KEY_PATH)) {
+ console.log(`⚠️ data/certs/{cert,key}.pem present but unreadable — server will boot HTTP-only.`);
+ console.log(` To recover: \`npm run setup:cert -- --self-signed\` to regenerate, or delete the files to revert.`);
+ return;
+ }
+
if (hasCert && meta?.mode === 'self-signed') {
trySelfSigned();
return;