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

refactor([hoist-loopback-host-helper-to-client-lib]): hoist loopback helpers to client/src/lib + broaden IPv4 to 127.0.0.0/8

[._.]/ Adam Eivy6a2f8c28ba67
diff --git a/client/src/components/NetworkExposureWidget.jsx b/client/src/components/NetworkExposureWidget.jsx
index 052d0fa8ba..6f1c97c303 100644
--- a/client/src/components/NetworkExposureWidget.jsx
+++ b/client/src/components/NetworkExposureWidget.jsx
@@ -13,34 +13,7 @@ import {
CheckCircle
} from 'lucide-react';
import * as api from '../services/api';
-
-// Browser secure-context rule mirrored client-side: getUserMedia (and other
-// powerful APIs) are gated on the page origin being HTTPS, localhost, or
-// 127.0.0.1/::1 — anything else (Tailscale IP, LAN IP, plain hostname over
-// HTTP) silently fails. Server-side we know the scheme + bind, but only the
-// browser knows which of those origins it actually loaded from.
-// Browsers report IPv6 `window.location.hostname` with brackets (`[::1]`)
-// while the bare-form `::1` shows up in env-style configs; accept both so
-// the heuristic doesn't false-negative on `http://[::1]:5555`.
-const LOOPBACK_HOSTS = new Set(['localhost', '127.0.0.1', '::1', '[::1]']);
-
-function isLoopbackOrigin() {
- if (typeof window === 'undefined' || !window.location) return false;
- return LOOPBACK_HOSTS.has(window.location.hostname);
-}
-
-function describeMicAvailability() {
- if (typeof window === 'undefined' || !window.location) {
- return { available: true, reason: 'unknown' };
- }
- if (window.location.protocol === 'https:') {
- return { available: true, reason: 'https' };
- }
- if (isLoopbackOrigin()) {
- return { available: true, reason: 'loopback' };
- }
- return { available: false, reason: 'insecure-context' };
-}
+import { isLoopbackOrigin, describeMicAvailability } from '../lib/loopbackHost.js';
function CertModeBadge({ mode, host }) {
if (mode === 'tailscale') {
diff --git a/client/src/lib/README.md b/client/src/lib/README.md
index c7f7aa943e..88e6565177 100644
--- a/client/src/lib/README.md
+++ b/client/src/lib/README.md
@@ -56,6 +56,7 @@ grep -i "what you want to do" client/src/lib/README.md
| `clipboard.js` | `copyToClipboard`, `writeClipboardSilently`, `readClipboard` — safe across insecure-origin contexts. Use these instead of `navigator.clipboard.writeText` inline. |
| `genUtils.js` | Shared bits between Image Gen and Video Gen pages. |
| `joinInfluenceList.js` | Mirror of `joinInfluenceList` in server universe builder. |
+| `loopbackHost.js` | `isLoopbackHost(host)` / `isLoopbackOrigin()` / `describeMicAvailability()` — Secure Context / loopback-origin heuristics. Use these in any new mic, clipboard, or `getUserMedia`-gated surface; matches the full `127.0.0.0/8` range, IPv6 `::1`, and the browser-bracketed `[::1]` form. |
| `mediaNavigation.js` | `getAdjacentMedia(items, item)` — prev/next computation for lightboxes. |
| `unsorted.js` | Synthetic "Unsorted" collection from media not filed in any real collection. |
| `upsertByIdPrepend.js` | Newest-first upsert into an id-keyed list. |
diff --git a/client/src/lib/index.js b/client/src/lib/index.js
index 68e3aebf33..f76a3b041e 100644
--- a/client/src/lib/index.js
+++ b/client/src/lib/index.js
@@ -27,6 +27,7 @@ export * from './graphSimulation.js';
export * from './clipboard.js';
export * from './genUtils.js';
export * from './joinInfluenceList.js';
+export * from './loopbackHost.js';
export * from './mediaNavigation.js';
export * from './unsorted.js';
export * from './upsertByIdPrepend.js';
diff --git a/client/src/lib/loopbackHost.js b/client/src/lib/loopbackHost.js
new file mode 100644
index 0000000000..5d6482a008
--- /dev/null
+++ b/client/src/lib/loopbackHost.js
@@ -0,0 +1,47 @@
+// Browser secure-context rule mirrored client-side: getUserMedia (and other
+// powerful APIs) are gated on the page origin being HTTPS, localhost, or a
+// loopback IP (127.0.0.0/8, ::1, [::1]) — anything else (Tailscale IP, LAN
+// IP, plain hostname over HTTP) silently fails. Server-side we know the
+// scheme + bind, but only the browser knows which of those origins it
+// actually loaded from.
+//
+// Server-side counterpart: server/lib/networkExposure.js exports its own
+// `isLoopbackHost` for bind-audience classification. The client copy lives
+// here so secure-context callers (mic, clipboard heuristics, future voice
+// surfaces) can share one rule instead of re-declaring inline sets.
+//
+// Browsers report IPv6 `window.location.hostname` with brackets (`[::1]`)
+// while the bare-form `::1` shows up in env-style configs; accept both so
+// the heuristic doesn't false-negative on `http://[::1]:5555`.
+//
+// IPv4: match the full 127.0.0.0/8 range, not just `127.0.0.1`. The kernel
+// routes every 127.x.x.x address to lo0 and browsers treat the whole block
+// as a Secure Context per the spec.
+
+const LOOPBACK_HOSTS = new Set(['localhost', '::1', '[::1]']);
+const IPV4_LOOPBACK = /^127\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
+
+export function isLoopbackHost(host) {
+ if (typeof host !== 'string' || !host) return false;
+ const normalized = host.toLowerCase();
+ if (LOOPBACK_HOSTS.has(normalized)) return true;
+ return IPV4_LOOPBACK.test(normalized);
+}
+
+export function isLoopbackOrigin() {
+ if (typeof window === 'undefined' || !window.location) return false;
+ return isLoopbackHost(window.location.hostname);
+}
+
+export function describeMicAvailability() {
+ if (typeof window === 'undefined' || !window.location) {
+ return { available: true, reason: 'unknown' };
+ }
+ if (window.location.protocol === 'https:') {
+ return { available: true, reason: 'https' };
+ }
+ if (isLoopbackOrigin()) {
+ return { available: true, reason: 'loopback' };
+ }
+ return { available: false, reason: 'insecure-context' };
+}
diff --git a/client/src/lib/loopbackHost.test.js b/client/src/lib/loopbackHost.test.js
new file mode 100644
index 0000000000..3fb3e009ac
--- /dev/null
+++ b/client/src/lib/loopbackHost.test.js
@@ -0,0 +1,99 @@
+import { describe, it, expect, afterEach, vi } from 'vitest';
+import {
+ isLoopbackHost,
+ isLoopbackOrigin,
+ describeMicAvailability,
+} from './loopbackHost.js';
+
+describe('isLoopbackHost', () => {
+ it.each([
+ ['localhost', true],
+ ['LOCALHOST', true],
+ ['LocalHost', true],
+ ['127.0.0.1', true],
+ ['127.0.0.5', true],
+ ['127.1.2.3', true],
+ ['127.255.255.254', true],
+ ['::1', true],
+ ['[::1]', true],
+ ['0.0.0.0', false],
+ ['10.0.0.1', false],
+ ['100.111.11.146', false],
+ ['192.168.1.1', false],
+ ['portos.tailnet.ts.net', false],
+ ['126.0.0.1', false],
+ ['128.0.0.1', false],
+ ['', false],
+ [null, false],
+ [undefined, false],
+ [42, false],
+ ])('isLoopbackHost(%p) === %p', (input, expected) => {
+ expect(isLoopbackHost(input)).toBe(expected);
+ });
+});
+
+describe('isLoopbackOrigin', () => {
+ afterEach(() => {
+ vi.unstubAllGlobals();
+ });
+
+ it('returns false when window is undefined', () => {
+ vi.stubGlobal('window', undefined);
+ expect(isLoopbackOrigin()).toBe(false);
+ });
+
+ it('returns true for localhost', () => {
+ vi.stubGlobal('window', { location: { hostname: 'localhost', protocol: 'http:' } });
+ expect(isLoopbackOrigin()).toBe(true);
+ });
+
+ it('returns true for any 127.x.x.x', () => {
+ vi.stubGlobal('window', { location: { hostname: '127.5.6.7', protocol: 'http:' } });
+ expect(isLoopbackOrigin()).toBe(true);
+ });
+
+ it('returns true for [::1] (browser bracketed form)', () => {
+ vi.stubGlobal('window', { location: { hostname: '[::1]', protocol: 'http:' } });
+ expect(isLoopbackOrigin()).toBe(true);
+ });
+
+ it('returns false for Tailscale hostname', () => {
+ vi.stubGlobal('window', { location: { hostname: 'void.taile8179.ts.net', protocol: 'http:' } });
+ expect(isLoopbackOrigin()).toBe(false);
+ });
+});
+
+describe('describeMicAvailability', () => {
+ afterEach(() => {
+ vi.unstubAllGlobals();
+ });
+
+ it('returns unknown when window is undefined', () => {
+ vi.stubGlobal('window', undefined);
+ expect(describeMicAvailability()).toEqual({ available: true, reason: 'unknown' });
+ });
+
+ it('reports https when protocol is https', () => {
+ vi.stubGlobal('window', {
+ location: { hostname: 'portos.tailnet.ts.net', protocol: 'https:' },
+ });
+ expect(describeMicAvailability()).toEqual({ available: true, reason: 'https' });
+ });
+
+ it('reports loopback for http on localhost', () => {
+ vi.stubGlobal('window', { location: { hostname: 'localhost', protocol: 'http:' } });
+ expect(describeMicAvailability()).toEqual({ available: true, reason: 'loopback' });
+ });
+
+ it('reports loopback for http on 127.x.x.x', () => {
+ vi.stubGlobal('window', { location: { hostname: '127.0.0.42', protocol: 'http:' } });
+ expect(describeMicAvailability()).toEqual({ available: true, reason: 'loopback' });
+ });
+
+ it('reports insecure-context for http on a non-loopback origin', () => {
+ vi.stubGlobal('window', {
+ location: { hostname: '100.111.11.146', protocol: 'http:' },
+ });
+ expect(describeMicAvailability()).toEqual({ available: false, reason: 'insecure-context' });
+ });
+});