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: refuse loopback and link-local hosts in peer announcements (#12428)

[._.]/ Adam Eivy7642462e0fed
diff --git a/server/services/instances.js b/server/services/instances.js
index 7bc8fac261..7e101fd5c9 100644
--- a/server/services/instances.js
+++ b/server/services/instances.js
@@ -9,6 +9,8 @@
*/
import net from 'net';
+import dns from 'dns/promises';
+import { isBlockedIngestHost } from '../lib/catalogValidation.js';
import crypto from 'crypto';
import { loadData, withData, UNKNOWN_INSTANCE_ID, getInstanceId, getSelf } from './instanceIdentity.js';
import { createKeyCachedQueue } from '../lib/createKeyCachedQueue.js';
@@ -245,6 +247,24 @@ function validHost(str) {
export { validHost };
+// An announce is unauthenticated, so unlike an operator-entered host it must not
+// name this machine or a link-local/metadata service (probePeer would GET it).
+function announcedHost(str) {
+ const host = validHost(str);
+ return host && !isBlockedIngestHost(host) ? host : undefined;
+}
+
+// A DNS host that answers loopback/link-local would turn the fixed-path probe
+// and relay into an SSRF read. LAN and tailnet answers stay allowed; a lookup
+// failure is left to the fetch itself to report as unreachable.
+async function assertPeerHostNotLocal(peer) {
+ if (peer.transport === 'tailcat' || !peer.host || net.isIP(peer.host)) return;
+ const answers = await dns.lookup(peer.host, { all: true }).catch(() => []);
+ if (answers.some(({ address }) => isBlockedIngestHost(address))) {
+ throw new Error(`peer host ${peer.host} resolves to a loopback or link-local address`);
+ }
+}
+
// Default sync categories. Everything that replicates the user's own CONTENT is
// off until explicitly enabled per-peer; the exception is `usage`, which is
// default-ON (see DEFAULT_ON_SYNC_CATEGORIES below).
@@ -911,6 +931,7 @@ export async function probePeer(peer) {
// fetches, AND their body reads, so a single PROBE_TIMEOUT_MS bounds the whole
// probe (the instanceId read is inside the budget, as it was before).
await withAbortTimeout(PROBE_TIMEOUT_MS, async (signal) => {
+ await assertPeerHostNotLocal(peer);
// Pass our own instanceId as `forPeer` so the peer also returns ITS cursor
// into our data (`cursorForYou`) — our push-frontier toward it. Best-effort:
// an unresolved/unknown id just omits the param and we get the inbound-only
@@ -1148,7 +1169,7 @@ export async function handleAnnounce({ address, port, instanceId, name, host })
matchedByAddress = true;
}
- const normalizedHost = validHost(host);
+ const normalizedHost = announcedHost(host);
if (existing?.syncSecret) return { created: false, peer: existing };
diff --git a/server/services/instances.test.js b/server/services/instances.test.js
index 3e44c00da2..5671cc3a73 100644
--- a/server/services/instances.test.js
+++ b/server/services/instances.test.js
@@ -27,6 +27,7 @@ vi.mock('../lib/asyncMutex.js', () => ({
createMutex: () => (fn) => fn()
}));
+vi.mock('dns/promises', () => ({ default: { lookup: vi.fn() } }));
vi.mock('./instanceEvents.js', () => ({
instanceEvents: {
emit: vi.fn()
@@ -55,6 +56,7 @@ vi.mock('../lib/tailscale.js', () => ({
// fetch is stubbed per-test in beforeEach and restored in afterEach
+import dns from 'dns/promises';
import { stopForwardForPeer } from './tailcatPeer.js';
import { readJSONFile, atomicWrite } from '../lib/fileUtils.js';
import { getTailscaleStatus } from '../lib/tailscale.js';
@@ -1193,6 +1195,22 @@ describe('instances.js', () => {
...overrides
});
+ it('refuses to probe a DNS host that resolves to loopback or link-local, but probes a tailnet answer', async () => {
+ const peer = makePeer({ host: 'rebind.example.com' });
+ readJSONFile.mockResolvedValue({ self: null, peers: [peer] });
+ for (const address of ['127.0.0.1', '169.254.169.254']) {
+ dns.lookup.mockResolvedValueOnce([{ address, family: 4 }]);
+ const result = await probePeer(peer);
+ expect(result.status).toBe('offline');
+ expect(JSON.stringify(result.lastProbe)).toContain('loopback or link-local');
+ }
+
+ dns.lookup.mockResolvedValueOnce([{ address: '100.64.0.7', family: 4 }]);
+ fetch.mockResolvedValue({ ok: true, json: () => Promise.resolve({ instanceId: 'remote-id' }) });
+ const allowed = await probePeer(peer);
+ expect(JSON.stringify(allowed.lastProbe)).not.toContain('loopback or link-local');
+ });
+
it('should mark peer online on successful probe', async () => {
const peer = makePeer();
const peers = [peer];
@@ -1646,6 +1664,16 @@ describe('instances.js', () => {
expect(atomicWrite).toHaveBeenCalledWith(expect.any(String), expect.objectContaining({ peers: [result.peer] }));
});
+ it.each(['localhost', 'api.localhost', '127.0.0.1', '169.254.169.254'])(
+ 'stores no host for a new peer announcing %s, and adopts a *.ts.net host', async (badHost) => {
+ readJSONFile.mockResolvedValue({ self: null, peers: [] });
+ const refused = await handleAnnounce({ address: '192.0.2.10', port: 5555, instanceId: 'bad', host: badHost });
+ expect(refused.peer.host).toBeNull();
+ readJSONFile.mockResolvedValue({ self: null, peers: [] });
+ const ok = await handleAnnounce({ address: '192.0.2.11', port: 5555, instanceId: 'good', host: 'box.tail1234.ts.net' });
+ expect(ok.peer.host).toBe('box.tail1234.ts.net');
+ });
+
it('should create a new peer from announcement', async () => {
readJSONFile.mockResolvedValue({ self: null, peers: [] });
// probePeer will call fetch