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: align all cert-gated paths on hasTailscaleCert

[._.]/ Adam Eivy2b15c00232ba
diff --git a/scripts/open-ui-in-browser.js b/scripts/open-ui-in-browser.js
index 1235ac0aca..37ad3eec82 100644
--- a/scripts/open-ui-in-browser.js
+++ b/scripts/open-ui-in-browser.js
@@ -5,19 +5,19 @@
//
// Fail-soft: every failure path logs and exits 0 — never break setup/update
// because Chrome happens to be unreachable.
-import { existsSync } from 'node:fs';
import { join, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
+import { hasTailscaleCert } from '../lib/tailscale-https.js';
const ROOT = dirname(dirname(fileURLToPath(import.meta.url)));
-const CERT_PATH = join(ROOT, 'data', 'certs', 'cert.pem');
-const KEY_PATH = join(ROOT, 'data', 'certs', 'key.pem');
+const CERT_DIR = join(ROOT, 'data', 'certs');
const API_PORT = Number(process.env.PORT) || 5555;
const HTTP_LOOPBACK_PORT = Number(process.env.PORTOS_HTTP_PORT) || 5553;
-// The server only enables HTTPS when BOTH cert and key exist (matches
-// lib/tailscale-https.js loadCert) — checking only cert.pem would falsely
-// route to the loopback mirror during a half-finished setup:cert.
-const HTTPS_MODE = existsSync(CERT_PATH) && existsSync(KEY_PATH);
+// Share the same cert predicate the server's HTTPS gate uses (file presence
+// AND PEM parseability). A presence-only check would route us to :5553 even
+// when corrupt PEMs forced the server back to plain HTTP-on-:5555, so the
+// poll would time out on a port the server never bound.
+const HTTPS_MODE = hasTailscaleCert(CERT_DIR);
// When HTTPS is on, :5555 speaks TLS only — plain http:// requests hit a TLS
// mismatch and time out. The loopback HTTP mirror on :5553 serves the same
diff --git a/scripts/setup-cert.js b/scripts/setup-cert.js
index 6b8aa680d3..12b2d3efac 100644
--- a/scripts/setup-cert.js
+++ b/scripts/setup-cert.js
@@ -152,7 +152,11 @@ function daysUntil(date) {
function shouldRegenTailscale(hostname) {
if (FORCE) return true;
- if (!existsSync(CERT_PATH) || !existsSync(KEY_PATH)) return true;
+ // hasTailscaleCert covers BOTH file presence AND PEM parseability — so a
+ // half-written cert from an interrupted prior run regenerates instead of
+ // being preserved (the file would be present but createSecureContext would
+ // throw at server boot).
+ if (!hasTailscaleCert(CERT_DIR)) return true;
const meta = readMeta();
if (!meta || meta.mode !== 'tailscale' || meta.hostname !== hostname) return true;
const expiry = certExpiresAt();
@@ -164,7 +168,7 @@ function shouldRegenTailscale(hostname) {
function shouldRegenSelfSigned(ips) {
if (FORCE) return true;
- if (!existsSync(CERT_PATH) || !existsSync(KEY_PATH)) return true;
+ if (!hasTailscaleCert(CERT_DIR)) return true;
const meta = readMeta();
if (!meta || meta.mode !== 'self-signed') return true;
const prev = (meta.ips || []).slice().sort().join(',');