Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 55 additions & 1 deletion src/tools/privateHost.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest';
import { privateHostReason } from './privateHost.js';
import { parseHTTPURL, privateHostReason } from './privateHost.js';

describe('privateHostReason', () => {
it('flags compressed loopback and unspecified IPv6', async () => {
Expand Down Expand Up @@ -37,3 +37,57 @@ describe('privateHostReason', () => {
expect(await privateHostReason('10.0.0.1')).toMatch(/RFC1918/i);
});
});

describe('privateHostReason IPv4 boundary cases', () => {
const cases: Array<{ host: string; want: boolean; note?: string }> = [
// this-network
{ host: '0.0.0.0', want: true },

// RFC 6598 shared address space (100.64.0.0/10)
{ host: '100.64.0.1', want: true, note: 'RFC 6598 lower bound' },
{ host: '100.100.100.100', want: true, note: 'tailscale MagicDNS resolver' },
{ host: '100.127.255.255', want: true, note: 'RFC 6598 upper bound' },

// adjacent public space that must NOT be flagged
{ host: '100.63.255.255', want: false, note: 'just below 100.64/10' },
{ host: '100.128.0.1', want: false, note: 'just above 100.64/10' },
{ host: '8.8.8.8', want: false },
{ host: '172.16.0.1', want: true },
{ host: '172.32.0.1', want: false, note: 'just above RFC1918 172.16/12' },
{ host: '172.15.0.1', want: false, note: 'just below RFC1918 172.16/12' },
];

for (const { host, want, note } of cases) {
const label = note ? `${host} (${note})` : host;
it(`${want ? 'flags' : 'allows'} ${label}`, async () => {
const reason = await privateHostReason(host);
expect(Boolean(reason)).toBe(want);
});
}
});

describe('privateHostReason hostname normalization', () => {
it('normalizes a trailing FQDN dot', async () => {
expect(await privateHostReason('localhost.')).toBeTruthy();
});

it('normalizes bracketed IPv6', async () => {
expect(await privateHostReason('[::1]')).toBeTruthy();
});
});

describe('parseHTTPURL', () => {
it('accepts http and https', () => {
expect(parseHTTPURL('http://example.com/').protocol).toBe('http:');
expect(parseHTTPURL('https://example.com/').protocol).toBe('https:');
});

it('rejects non-http(s) schemes', () => {
expect(() => parseHTTPURL('file:///etc/passwd')).toThrow(/unsupported URL scheme/);
expect(() => parseHTTPURL('gopher://example.com/')).toThrow(/unsupported URL scheme/);
});

it('rejects a malformed URL', () => {
expect(() => parseHTTPURL('not a url')).toThrow(/invalid URL/);
});
});
6 changes: 6 additions & 0 deletions src/tools/privateHost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ function privateIPv4Reason(host: string): string {
if (a === 169 && b === 254) return 'link-local/metadata IPv4';
if (a === 172 && b >= 16 && b <= 31) return 'RFC1918 private IPv4';
if (a === 192 && b === 168) return 'RFC1918 private IPv4';
// RFC 6598 shared address space (100.64.0.0/10). Not publicly routable, and in
// practice this is where real internal infrastructure lives: Tailscale/WireGuard
// overlays (incl. the 100.100.100.100 MagicDNS resolver), carrier-grade NAT, and
// several cloud providers' internal service endpoints. Without this a web_fetch
// at a tailnet host is reached silently, with no gate prompt.
if (a === 100 && b !== undefined && b >= 64 && b <= 127) return 'RFC6598 shared address space IPv4';
if (a === 0) return 'this-network IPv4';
return '';
}
Expand Down