From a4f79753244ea928d76f3db5136dec8b6356441a Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Tue, 20 Oct 2026 15:29:51 +0200 Subject: [PATCH] Removing the API key while sharing or Cloudflare runs is allowed (the key is optional); the remove dialog warns plainly --- apps/host/server/serving.mjs | 2 +- apps/host/tests/serving-key-optional.test.mjs | 23 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 apps/host/tests/serving-key-optional.test.mjs diff --git a/apps/host/server/serving.mjs b/apps/host/server/serving.mjs index c98f3eb8..ced16fac 100644 --- a/apps/host/server/serving.mjs +++ b/apps/host/server/serving.mjs @@ -70,7 +70,7 @@ export function patch(body) { const s = getStore(); const serving = { ...(s.serving ?? {}) }; if (typeof body.otherDevices === 'boolean' && body.otherDevices !== !isLocal()) throw new Error('Change the host listen address and restart to update network access.'); - if (body.apiKey === null && (!isLocal() || sharing.active || tunnelActive())) throw new Error('Stop local sharing and Cloudflare before removing the API key.'); + // The key is optional (owner decision, 0.2.2): removing it while sharing or the tunnel runs is allowed; the app warns in plain words (local-share.mjs, cloudflare.mjs). if (body.apiKey === null) serving.apiKey = null; else if (typeof body.apiKey === 'string' && body.apiKey.trim()) serving.apiKey = body.apiKey.trim(); else if (body.generateKey) serving.apiKey = `sk-strata-${randomBytes(12).toString('hex')}`; diff --git a/apps/host/tests/serving-key-optional.test.mjs b/apps/host/tests/serving-key-optional.test.mjs new file mode 100644 index 00000000..83c550a8 --- /dev/null +++ b/apps/host/tests/serving-key-optional.test.mjs @@ -0,0 +1,23 @@ +/** The API key is optional (owner decision, 0.2.2): removing it while sharing or the Cloudflare tunnel runs is allowed, and the key stays removed. */ +import test from 'node:test'; +import assert from 'node:assert/strict'; +import { mkdtempSync, rmSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; + +const home = mkdtempSync(join(tmpdir(), 'strata-serving-key-')); +process.env.STRATA_HOME = home; +const serving = await import('../server/serving.mjs'); + +test('removing the API key while local sharing and the tunnel run is allowed', () => { + serving.init({ send() {} }, { port: 8080, host: '127.0.0.1' }); + serving.patch({ generateKey: true }); + assert.equal(serving.state().access.apiKeySet, true); + serving.setSharing({ active: true, url: 'http://192.168.1.5:8792/v1' }); + serving.setTunnel({ status: 'connected', mode: 'quick', url: 'https://example.trycloudflare.com', error: null, installed: true, originUrl: null }); + assert.equal(serving.state().access.otherDevices, true); + const after = serving.patch({ apiKey: null }); + assert.equal(after.access.apiKeySet, false); + assert.equal(serving.authorized({ headers: {} }), true); +}); +test.after(() => { try { rmSync(home, { recursive: true, force: true }); } catch { /* temp folder */ } });