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

fix: block private/metadata hosts in layeredIntelligence fetchHttpSource (#2513)

Adam Eivy793c0d978eca
diff --git a/.changelog/NEXT.md b/.changelog/NEXT.md
index 8ea0db9e43..7bd709e113 100644
--- a/.changelog/NEXT.md
+++ b/.changelog/NEXT.md
@@ -3,3 +3,4 @@
## Fixed
- **[issue-2510] MidiPianoRoll: clear the stale hover overlay when the transcription data changes.** The piano-roll kept its hovered note/chord identity (objects from the previous parse) when the `data`/`chords` props swapped on a mounted instance, so a re-transcription or Retry while a hover was active and the pointer sat still would keep stroking the old note at coordinates mapped into the new file until the next pointer move or Escape. A `useEffect` now drops the hover identity whenever `data`/`chords` change.
+- **[issue-2513] Block loopback/metadata hosts in Layered Intelligence custom `http` sources.** `fetchHttpSource` used a bare `fetch` with only an `http(s)` scheme check, so a hand-edited config could point a custom source at `127.0.0.1`, `169.254.169.254`, or `metadata.google.internal` and exfiltrate up to 8KB into the reasoner prompt. It now routes through the shared SSRF-guarded `fetchPublicText` (default posture): loopback, link-local, and cloud-metadata hosts are blocked and redirects are revalidated, while LAN/Tailscale hosts stay intentionally allowed for this single-user tool.
diff --git a/server/services/layeredIntelligence.js b/server/services/layeredIntelligence.js
index 7ec61d752b..2014f4c363 100644
--- a/server/services/layeredIntelligence.js
+++ b/server/services/layeredIntelligence.js
@@ -25,6 +25,7 @@ import { readFile, writeFile, appendFile, realpath } from 'fs/promises';
import { existsSync } from 'fs';
import { DAY, tryReadFile, readJSONFile, safeJSONParse, PATHS } from '../lib/fileUtils.js';
import { bufferedSpawn } from '../lib/bufferedSpawn.js';
+import { fetchPublicText } from '../lib/safeUrlFetch.js';
import { createTicket, searchIssues, addLabels, escapeJql } from './jira.js';
import { computeWindowedStats } from './taskLearning/store.js';
@@ -674,14 +675,21 @@ export function customSourceKey(custom) {
* Fetch an http(s) custom source for the loop's prompt. Deterministic read, no
* LLM. Rejects any non-http(s) scheme (defense in depth over the Zod refine),
* bounds the request with a 10s timeout, and returns null on any failure so a
- * dead URL just omits the key rather than throwing. `fetchImpl` is injectable
- * for tests.
+ * dead URL just omits the key rather than throwing.
+ *
+ * SSRF-guarded via `fetchPublicText` (default posture): loopback, link-local,
+ * and the cloud-metadata endpoint (127.0.0.1, 169.254.169.254,
+ * metadata.google.internal, ::1) are blocked so a hand-edited/hostile config
+ * can't exfiltrate them into the reasoner prompt, and redirects are revalidated
+ * against the same gate. LAN/private hosts (Tailscale peers, a home wiki) stay
+ * ALLOWED intentionally — PortOS is a single-user tool where a custom source
+ * legitimately points at the home network, and the URL is operator-configured.
+ * `throwOnUnsafe: false` makes a blocked host omit the key like any other dead
+ * URL instead of bubbling a 400. `fetchText` is injectable for tests.
*/
-export async function fetchHttpSource(url, { timeoutMs = 10_000, fetchImpl = fetch } = {}) {
+export async function fetchHttpSource(url, { timeoutMs = 10_000, fetchText = fetchPublicText } = {}) {
if (typeof url !== 'string' || !/^https?:\/\//i.test(url)) return null;
- const res = await fetchImpl(url, { signal: AbortSignal.timeout(timeoutMs) }).catch(() => null);
- if (!res || !res.ok) return null;
- const text = await res.text().catch(() => null);
+ const text = await fetchText(url, { timeoutMs, throwOnUnsafe: false }).catch(() => null);
return text || null;
}
diff --git a/server/services/layeredIntelligence.test.js b/server/services/layeredIntelligence.test.js
index a944084fc8..6a8c3e1caf 100644
--- a/server/services/layeredIntelligence.test.js
+++ b/server/services/layeredIntelligence.test.js
@@ -740,21 +740,39 @@ describe('customSourceKey', () => {
describe('fetchHttpSource', () => {
it('returns body text on a 2xx http(s) response', async () => {
- const fetchImpl = vi.fn().mockResolvedValue({ ok: true, text: async () => 'remote body' });
- expect(await fetchHttpSource('https://example.com/x', { fetchImpl })).toBe('remote body');
+ const fetchText = vi.fn().mockResolvedValue('remote body');
+ expect(await fetchHttpSource('https://example.com/x', { fetchText })).toBe('remote body');
+ // Routed through the SSRF-guarded fetcher, failing soft (no thrown 400).
+ expect(fetchText).toHaveBeenCalledWith('https://example.com/x', expect.objectContaining({ throwOnUnsafe: false }));
});
- it('rejects a non-http scheme without calling fetch', async () => {
- const fetchImpl = vi.fn();
- expect(await fetchHttpSource('file:///etc/hosts', { fetchImpl })).toBeNull();
- expect(fetchImpl).not.toHaveBeenCalled();
+ it('rejects a non-http scheme without calling the fetcher', async () => {
+ const fetchText = vi.fn();
+ expect(await fetchHttpSource('file:///etc/hosts', { fetchText })).toBeNull();
+ expect(fetchText).not.toHaveBeenCalled();
});
- it('returns null on a non-ok response', async () => {
- const fetchImpl = vi.fn().mockResolvedValue({ ok: false, text: async () => 'nope' });
- expect(await fetchHttpSource('https://example.com/x', { fetchImpl })).toBeNull();
+ it('returns null when the fetcher yields null (non-ok / dead URL)', async () => {
+ const fetchText = vi.fn().mockResolvedValue(null);
+ expect(await fetchHttpSource('https://example.com/x', { fetchText })).toBeNull();
});
- it('returns null when fetch throws (dead URL / timeout)', async () => {
- const fetchImpl = vi.fn().mockRejectedValue(new Error('boom'));
- expect(await fetchHttpSource('https://example.com/x', { fetchImpl })).toBeNull();
+ it('returns null when the fetcher throws (timeout)', async () => {
+ const fetchText = vi.fn().mockRejectedValue(new Error('boom'));
+ expect(await fetchHttpSource('https://example.com/x', { fetchText })).toBeNull();
+ });
+
+ // SSRF: exercise the REAL fetchPublicText guard (default injected). IP-literal
+ // hosts are classified synchronously — no network — so these are deterministic.
+ it('blocks the cloud-metadata endpoint (169.254.169.254) — omits the key, no network', async () => {
+ expect(await fetchHttpSource('http://169.254.169.254/latest/meta-data/')).toBeNull();
+ });
+ it('blocks IPv4 loopback (127.0.0.1)', async () => {
+ expect(await fetchHttpSource('http://127.0.0.1:5555/api/settings')).toBeNull();
+ });
+ it('blocks IPv6 loopback ([::1])', async () => {
+ expect(await fetchHttpSource('http://[::1]/')).toBeNull();
+ });
+ it('allows a public URL through the guard (real fetch stubbed at fetchText seam)', async () => {
+ const fetchText = vi.fn().mockResolvedValue('public body');
+ expect(await fetchHttpSource('https://example.com/feed', { fetchText })).toBe('public body');
});
});