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
25 changes: 25 additions & 0 deletions src/lib/target-url.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,31 @@ describe('assertNotLocal — IPv6 hardening (SSRF bypass guard)', () => {
});
});

describe('assertNotLocal — trailing-dot FQDN normalization (SSRF bypass guard)', () => {
// `localhost.` is the fully-qualified form of `localhost` (RFC 6761 reserves
// both to resolve to loopback). It previously bypassed the
// `host === 'localhost'` check because the WHATWG URL parser keeps the
// trailing dot on named hosts (IP literals are dot-normalized, named hosts
// are not).
it('blocks http://localhost. (trailing-dot loopback)', () => {
expectBlocked('http://localhost.');
});

it('blocks http://localhost.:8080 (trailing-dot loopback with port)', () => {
expectBlocked('http://localhost.:8080');
});

it('blocks http://localhost%2e (percent-encoded trailing dot)', () => {
expectBlocked('http://localhost%2e');
});

// A legitimate public FQDN with a trailing dot must still be allowed
// (no false positive from the dot strip).
it('allows https://example.com. (public FQDN with trailing dot)', () => {
expectAllowed('https://example.com.');
});
});

describe('assertNotLocal — allowed public URLs', () => {
it('allows https://example.com', () => {
expectAllowed('https://example.com');
Expand Down
9 changes: 8 additions & 1 deletion src/lib/target-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,14 @@ export function assertNotLocal(rawUrl: string): void {
throw localTargetError('target-url', 'must use http or https scheme');
}

const host = parsed.hostname.toLowerCase();
// Normalize a single trailing dot in the hostname. `localhost.` is the
// fully-qualified form of `localhost` (RFC 6761 reserves both to resolve to
// loopback), so `http://localhost.` must be rejected just like
// `http://localhost`. Without this strip, the trailing-dot form (also
// reachable via `localhost%2e`) slips past the `host === 'localhost'` check.
// IP literals are already dot-normalized by the WHATWG URL parser, so this
// only affects named hosts.
const host = parsed.hostname.toLowerCase().replace(/\.$/, '');

// Loopback / unspecified.
if (host === 'localhost' || host === '0.0.0.0') {
Expand Down