mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 18:10:06 -05:00
Seven of the DnsBleed mocha tests failed with "Timeout of 2000ms exceeded" -
every one of the ALLOW cases, while every block case passed, which is what
hid it.
The stub was at fault, not fetchSafe. It faked http.IncomingMessage with a
bare EventEmitter that emitted 'data' and 'end' from a process.nextTick,
into the void if nothing was listening yet. No real response does that: an
IncomingMessage is a PAUSED Readable that buffers its body until a listener
attaches or it is resumed, so reading late cannot lose data.
That only started to matter when fetchSafe was split into resolving the
response and then reading it, which the redirect handling needs - it has to
see the status before deciding whether to read a body at all. The nextTick
queue drains BEFORE promise microtasks, so the fake had already fired 'end'
by the time the awaited continuation attached its listeners.
Verified rather than assumed, both halves:
- against a real server: a request whose 'data' listener is attached two
nextTicks, a setImmediate and 20ms after the response callback still
receives the whole body.
- against the real fetchSafe, driven by each stub in turn: the old
EventEmitter stub times out after 2s; the Readable-backed one returns
status=200, the body, the pinned IP 93.184.216.34 and Host
example.com.
The security assertions still hold through the new stub, checked the same
way: a 302 with the default maxRedirects:0 is refused, a host resolving to
127.0.0.1 is refused with ZERO requests sent, a followed redirect pins each
hop to its own resolved IP, the abandoned hop reports destroyed=true, and
Authorization and Cookie are dropped on the cross-origin hop.
destroy()/destroyed are now the stream's own rather than a hand-rolled flag,
so "fetchSafe abandoned this body" is asserted against real stream
behaviour.
Thanks to xet7 !
538 lines
23 KiB
JavaScript
538 lines
23 KiB
JavaScript
/* eslint-env mocha */
|
|
import { expect } from 'chai';
|
|
import sinon from 'sinon';
|
|
import dns from 'dns';
|
|
import http from 'http';
|
|
import https from 'https';
|
|
import { EventEmitter } from 'events';
|
|
import { Readable } from 'stream';
|
|
import {
|
|
isIpBlocked,
|
|
validateAttachmentUrl,
|
|
} from '/models/lib/attachmentUrlValidation';
|
|
import { fetchSafe, isBlockedIPv4, isBlockedIPv6 } from '/server/lib/ssrfGuard';
|
|
|
|
// Regression tests for the DnsBleed fix: SSRF filter bypass via a public
|
|
// hostname that resolves (DNS) to a blocked internal address in outgoing
|
|
// webhooks (GHSA-66m2-4wfr-c45p, CWE-918). Incomplete-fix follow-up to
|
|
// WebhookBleed (GHSA-hc3x-hq3m-663q) and IntegrationBleed / RebindBleed.
|
|
//
|
|
// The core of the fix:
|
|
// 1. The delivery guard fetchSafe() must reject a hostname whose RESOLVED IP
|
|
// is private/loopback/link-local/reserved — for BOTH address families
|
|
// (A + AAAA), not just IPv4 as the previous dns.resolve4-only code did.
|
|
// 2. The block-list is a single shared source of truth (isIpBlocked) used by
|
|
// both the input-time validator (validateAttachmentUrl) and the
|
|
// delivery-time guard (fetchSafe), so they can never drift apart.
|
|
//
|
|
// See https://wekan.fi/hall-of-fame/dnsbleed/ and the Upcoming release CHANGELOG.
|
|
|
|
// Make dns.promises.lookup deterministic. `map` is hostname -> array of
|
|
// { address, family } records (matching dns.lookup({ all: true })). A hostname
|
|
// mapped to 'FAIL' (or absent) throws, like an unresolvable name.
|
|
function stubLookup(map) {
|
|
sinon.stub(dns.promises, 'lookup').callsFake(async host => {
|
|
const value = map[host];
|
|
if (value === undefined || value === 'FAIL') {
|
|
const err = new Error(`ENOTFOUND ${host}`);
|
|
err.code = 'ENOTFOUND';
|
|
throw err;
|
|
}
|
|
return value;
|
|
});
|
|
}
|
|
|
|
// A stand-in for http.IncomingMessage. It is a real Readable, and that matters:
|
|
// an IncomingMessage is a PAUSED stream that BUFFERS its body until something
|
|
// attaches a 'data' listener or resumes it, so no data can be lost by reading
|
|
// late.
|
|
//
|
|
// These stubs used to be bare EventEmitters that emitted 'data' and 'end' from
|
|
// a process.nextTick, into the void if nobody was listening yet - which no real
|
|
// response does. That made seven tests fail the moment fetchSafe started
|
|
// resolving the response and reading the body as two steps (the redirect
|
|
// handling needs to look at the status before deciding to read at all): the
|
|
// nextTick queue drains BEFORE promise microtasks, so the fake had already
|
|
// fired 'end' by the time the awaited continuation attached its listeners, and
|
|
// the read hung until mocha's 2s timeout. The blocked-host tests kept passing,
|
|
// because they reject before any of this, which is what hid it.
|
|
//
|
|
// Verified against a real server rather than assumed: a request whose 'data'
|
|
// listener is attached two nextTicks, a setImmediate and 20ms after the
|
|
// response callback still receives the whole body.
|
|
function fakeResponse({ statusCode = 200, headers = {}, body = '' }) {
|
|
const res = new Readable({ read() {} });
|
|
res.statusCode = statusCode;
|
|
res.headers = headers;
|
|
process.nextTick(() => {
|
|
// A destroyed response pushes nothing - that is how the redirect tests
|
|
// prove fetchSafe abandoned a body instead of reading it.
|
|
if (res.destroyed) return;
|
|
if (body) res.push(Buffer.from(body));
|
|
res.push(null);
|
|
});
|
|
return res;
|
|
}
|
|
|
|
// Replace the real TCP request with an in-memory fake so "allowed" hosts never
|
|
// hit the network. Returns a `capture` object whose `.opts` holds the request
|
|
// options fetchSafe built (so tests can assert the pinned IP / Host / SNI).
|
|
function stubTransport({ statusCode = 200, headers = {}, body = '' } = {}) {
|
|
const capture = {};
|
|
const fake = (opts, cb) => {
|
|
capture.opts = opts;
|
|
const req = new EventEmitter();
|
|
req.write = () => {};
|
|
req.end = () => {};
|
|
const res = fakeResponse({ statusCode, headers, body });
|
|
process.nextTick(() => cb(res));
|
|
return req;
|
|
};
|
|
sinon.stub(http, 'request').callsFake(fake);
|
|
sinon.stub(https, 'request').callsFake(fake);
|
|
return capture;
|
|
}
|
|
|
|
// Same, but answering with a different response per request, so a REDIRECT
|
|
// CHAIN can be exercised. `capture.calls` holds the request options of every
|
|
// hop, in order — which is how a test proves a hop was never sent at all.
|
|
function stubTransportSequence(steps) {
|
|
const capture = { calls: [] };
|
|
const fake = (opts, cb) => {
|
|
const step = steps[capture.calls.length] || { statusCode: 200, headers: {}, body: '' };
|
|
capture.calls.push(opts);
|
|
capture.opts = opts;
|
|
const req = new EventEmitter();
|
|
req.write = () => {};
|
|
req.end = () => {};
|
|
// Same Readable-backed response as above: `destroyed` and `destroy()` are
|
|
// the stream's own, so "fetchSafe abandoned this hop's body" is asserted
|
|
// against real stream behaviour instead of a hand-rolled flag.
|
|
const res = fakeResponse({
|
|
statusCode: step.statusCode,
|
|
headers: step.headers || {},
|
|
body: step.body || '',
|
|
});
|
|
process.nextTick(() => cb(res));
|
|
return req;
|
|
};
|
|
sinon.stub(http, 'request').callsFake(fake);
|
|
sinon.stub(https, 'request').callsFake(fake);
|
|
return capture;
|
|
}
|
|
|
|
async function expectReject(promise, matcher) {
|
|
let error;
|
|
try {
|
|
await promise;
|
|
} catch (e) {
|
|
error = e;
|
|
}
|
|
expect(error, 'expected the promise to reject but it resolved').to.exist;
|
|
expect(error.message).to.match(matcher);
|
|
return error;
|
|
}
|
|
|
|
describe('DnsBleed SSRF guard (GHSA-66m2-4wfr-c45p)', function () {
|
|
afterEach(function () {
|
|
sinon.restore();
|
|
});
|
|
|
|
// ── Shared block-list: single source of truth ────────────────────────────
|
|
describe('isIpBlocked() — shared block-list', function () {
|
|
const blocked = [
|
|
['127.0.0.1', 'IPv4 loopback'],
|
|
['127.9.9.9', 'IPv4 loopback range'],
|
|
['10.0.0.1', 'RFC1918 10/8'],
|
|
['172.16.0.1', 'RFC1918 172.16/12 low edge'],
|
|
['172.31.255.255', 'RFC1918 172.16/12 high edge'],
|
|
['192.168.1.1', 'RFC1918 192.168/16'],
|
|
['169.254.169.254', 'link-local / cloud metadata (IMDS)'],
|
|
['0.0.0.0', 'current network / unspecified'],
|
|
['100.64.0.1', 'CGNAT shared address (RFC6598)'],
|
|
['::1', 'IPv6 loopback'],
|
|
['::', 'IPv6 unspecified'],
|
|
['fd00::1', 'IPv6 unique-local (fd)'],
|
|
['fe80::1', 'IPv6 link-local'],
|
|
['ff02::1', 'IPv6 multicast'],
|
|
['::ffff:127.0.0.1', 'IPv4-mapped loopback'],
|
|
['::ffff:169.254.169.254', 'IPv4-mapped metadata'],
|
|
// TransitBleed (GHSA-c5xr-mg26-vq5w): IPv6 transition addresses carry an
|
|
// IPv4 destination somewhere other than a literal `::ffff:` prefix, and
|
|
// on a host with a 6to4 relay or a NAT64 gateway the packet really does
|
|
// arrive there. tests/transitbleed.test.cjs covers every form.
|
|
['0:0:0:0:0:ffff:7f00:1', 'IPv4-mapped loopback, spelled out'],
|
|
['2002:a9fe:a9fe::', '6to4 wrapping the metadata address'],
|
|
['2002:7f00:0001::', '6to4 wrapping loopback'],
|
|
['64:ff9b::a9fe:a9fe', 'NAT64 wrapping the metadata address'],
|
|
['64:ff9b::c0a8:101', 'NAT64 wrapping RFC1918'],
|
|
['2001:0:5ef5:79fd:0:0:5601:5601', 'Teredo wrapping the metadata address'],
|
|
['fec0::1', 'deprecated IPv6 site-local'],
|
|
];
|
|
blocked.forEach(([ip, label]) => {
|
|
it(`blocks ${ip} (${label})`, function () {
|
|
expect(isIpBlocked(ip)).to.equal(true);
|
|
});
|
|
});
|
|
|
|
// Negative tests: public / non-reserved addresses must be allowed.
|
|
const allowed = [
|
|
['8.8.8.8', 'public IPv4'],
|
|
['1.1.1.1', 'public IPv4'],
|
|
['93.184.216.34', 'public IPv4 (example.com)'],
|
|
['172.15.0.1', 'just below RFC1918 172.16/12'],
|
|
['172.32.0.1', 'just above RFC1918 172.16/12'],
|
|
['100.63.255.255', 'just below CGNAT 100.64/10'],
|
|
['2606:4700:4700::1111', 'public IPv6 (Cloudflare)'],
|
|
['2001:4860:4860::8888', 'public IPv6 (Google)'],
|
|
['2002:5db8:d822::', '6to4 wrapping a PUBLIC IPv4 — still reachable'],
|
|
['64:ff9b::93.184.216.34', 'NAT64 wrapping a PUBLIC IPv4 — still reachable'],
|
|
];
|
|
allowed.forEach(([ip, label]) => {
|
|
it(`allows ${ip} (${label})`, function () {
|
|
expect(isIpBlocked(ip)).to.equal(false);
|
|
});
|
|
});
|
|
|
|
it('fails closed for a non-IP string', function () {
|
|
expect(isIpBlocked('not-an-ip')).to.equal(true);
|
|
expect(isIpBlocked('')).to.equal(true);
|
|
});
|
|
});
|
|
|
|
// ── Backward-compatible family wrappers over the shared list ──────────────
|
|
describe('isBlockedIPv4() / isBlockedIPv6() wrappers', function () {
|
|
it('isBlockedIPv4 matches the shared list for IPv4', function () {
|
|
expect(isBlockedIPv4('127.0.0.1')).to.equal(true);
|
|
expect(isBlockedIPv4('169.254.169.254')).to.equal(true);
|
|
expect(isBlockedIPv4('8.8.8.8')).to.equal(false);
|
|
});
|
|
it('isBlockedIPv4 fails closed for non-IPv4 input', function () {
|
|
expect(isBlockedIPv4('::1')).to.equal(true);
|
|
expect(isBlockedIPv4('garbage')).to.equal(true);
|
|
});
|
|
it('isBlockedIPv6 matches the shared list for IPv6', function () {
|
|
expect(isBlockedIPv6('::1')).to.equal(true);
|
|
expect(isBlockedIPv6('fd00::1')).to.equal(true);
|
|
expect(isBlockedIPv6('2606:4700:4700::1111')).to.equal(false);
|
|
});
|
|
it('isBlockedIPv6 fails closed for non-IPv6 input', function () {
|
|
expect(isBlockedIPv6('127.0.0.1')).to.equal(true);
|
|
expect(isBlockedIPv6('garbage')).to.equal(true);
|
|
});
|
|
});
|
|
|
|
// ── fetchSafe: must reject before any connection ─────────────────────────
|
|
describe('fetchSafe() rejects SSRF targets before connecting', function () {
|
|
it('rejects non-http(s) protocols', async function () {
|
|
await expectReject(fetchSafe('ftp://example.com/x'), /Protocol not allowed/);
|
|
await expectReject(fetchSafe('file:///etc/passwd'), /Protocol not allowed/);
|
|
});
|
|
|
|
it('rejects an unparseable URL', async function () {
|
|
await expectReject(fetchSafe('http://'), /Invalid URL|DNS resolution failed/);
|
|
});
|
|
|
|
it('rejects a literal loopback IPv4 URL (no DNS)', async function () {
|
|
const spy = sinon.stub(dns.promises, 'lookup');
|
|
await expectReject(fetchSafe('http://127.0.0.1/'), /Blocked IP in URL/);
|
|
expect(spy.called, 'DNS must not be queried for a literal IP').to.equal(false);
|
|
});
|
|
|
|
it('rejects a literal metadata IPv4 URL', async function () {
|
|
await expectReject(
|
|
fetchSafe('http://169.254.169.254/latest/meta-data/'),
|
|
/Blocked IP in URL/,
|
|
);
|
|
});
|
|
|
|
it('rejects a literal IPv6 loopback URL', async function () {
|
|
await expectReject(fetchSafe('http://[::1]/'), /Blocked IP in URL/);
|
|
});
|
|
|
|
it('rejects decimal-integer-encoded loopback (http://2130706433)', async function () {
|
|
const spy = sinon.stub(dns.promises, 'lookup');
|
|
// Either block path is correct: the WHATWG URL parser normalises the
|
|
// integer host 2130706433 to 127.0.0.1, so the dotted-quad guard blocks it
|
|
// ("Blocked IP in URL: 127.0.0.1"); if a parser leaves it integer-encoded,
|
|
// the guard's own decimal decoder blocks it ("decodes to blocked 127.0.0.1").
|
|
await expectReject(
|
|
fetchSafe('http://2130706433/'),
|
|
/(decodes to blocked|Blocked IP in URL:)\s*127\.0\.0\.1/,
|
|
);
|
|
expect(spy.called).to.equal(false);
|
|
});
|
|
|
|
// The published PoC: a public wildcard-DNS hostname that resolves to the
|
|
// cloud-metadata IP. This is the exact DnsBleed bypass.
|
|
it('rejects 169-254-169-254.nip.io -> 169.254.169.254 (the PoC)', async function () {
|
|
stubLookup({
|
|
'169-254-169-254.nip.io': [{ address: '169.254.169.254', family: 4 }],
|
|
});
|
|
await expectReject(
|
|
fetchSafe('http://169-254-169-254.nip.io/latest/meta-data/'),
|
|
/Blocked IP 169\.254\.169\.254 resolved/,
|
|
);
|
|
});
|
|
|
|
it('rejects 127-0-0-1.nip.io -> 127.0.0.1', async function () {
|
|
stubLookup({ '127-0-0-1.nip.io': [{ address: '127.0.0.1', family: 4 }] });
|
|
await expectReject(
|
|
fetchSafe('http://127-0-0-1.nip.io/'),
|
|
/Blocked IP 127\.0\.0\.1 resolved/,
|
|
);
|
|
});
|
|
|
|
it('rejects an attacker domain whose A record points at RFC1918', async function () {
|
|
stubLookup({ 'evil.example.com': [{ address: '10.0.0.5', family: 4 }] });
|
|
await expectReject(
|
|
fetchSafe('http://evil.example.com/'),
|
|
/Blocked IP 10\.0\.0\.5 resolved/,
|
|
);
|
|
});
|
|
|
|
// Regression for the newly-closed gap: the old resolve4-only guard was
|
|
// blind to AAAA. A hostname resolving to an internal IPv6 address must now
|
|
// be rejected at delivery time.
|
|
it('rejects a hostname whose AAAA record points at internal IPv6', async function () {
|
|
stubLookup({ 'evil6.example.com': [{ address: 'fd00::1', family: 6 }] });
|
|
await expectReject(
|
|
fetchSafe('http://evil6.example.com/'),
|
|
/Blocked IP fd00::1 resolved/,
|
|
);
|
|
});
|
|
|
|
it('rejects a hostname whose AAAA record is IPv6 loopback', async function () {
|
|
stubLookup({ 'lo6.example.com': [{ address: '::1', family: 6 }] });
|
|
await expectReject(fetchSafe('http://lo6.example.com/'), /Blocked IP ::1 resolved/);
|
|
});
|
|
|
|
// Dual-stack partial-bypass: a public A record must NOT excuse an internal
|
|
// AAAA record. If ANY resolved address is blocked, the whole request fails.
|
|
it('rejects dual-stack when only the AAAA record is internal', async function () {
|
|
stubLookup({
|
|
'dual.example.com': [
|
|
{ address: '93.184.216.34', family: 4 },
|
|
{ address: 'fd00::1', family: 6 },
|
|
],
|
|
});
|
|
await expectReject(
|
|
fetchSafe('http://dual.example.com/'),
|
|
/Blocked IP fd00::1 resolved/,
|
|
);
|
|
});
|
|
|
|
it('rejects when the hostname does not resolve', async function () {
|
|
stubLookup({});
|
|
await expectReject(fetchSafe('http://nope.invalid/'), /DNS resolution failed/);
|
|
});
|
|
|
|
it('rejects when resolution returns no addresses', async function () {
|
|
stubLookup({ 'empty.example.com': [] });
|
|
await expectReject(
|
|
fetchSafe('http://empty.example.com/'),
|
|
/No DNS records returned/,
|
|
);
|
|
});
|
|
});
|
|
|
|
// ── fetchSafe: negative tests — legitimate public hosts must work ────────
|
|
describe('fetchSafe() allows public hosts and pins the connection', function () {
|
|
it('allows a public host and pins the dial to the resolved IP', async function () {
|
|
stubLookup({ 'example.com': [{ address: '93.184.216.34', family: 4 }] });
|
|
const cap = stubTransport({ statusCode: 200, body: '{"ok":true}' });
|
|
|
|
const res = await fetchSafe('http://example.com/hook', {
|
|
method: 'POST',
|
|
headers: { 'X-Test': '1' },
|
|
body: '{}',
|
|
});
|
|
|
|
expect(res.status).to.equal(200);
|
|
// Connection is pinned to the validated IP, not re-resolved by hostname.
|
|
expect(cap.opts.hostname).to.equal('93.184.216.34');
|
|
// Original Host header is preserved for virtual-hosting.
|
|
expect(cap.opts.headers.Host).to.equal('example.com');
|
|
expect(cap.opts.headers['X-Test']).to.equal('1');
|
|
expect(await res.json()).to.deep.equal({ ok: true });
|
|
});
|
|
|
|
it('prefers an IPv4 address when a dual-stack host is all-public', async function () {
|
|
stubLookup({
|
|
'dualpub.example.com': [
|
|
{ address: '93.184.216.34', family: 4 },
|
|
{ address: '2606:4700:4700::1111', family: 6 },
|
|
],
|
|
});
|
|
const cap = stubTransport({ statusCode: 200 });
|
|
const res = await fetchSafe('http://dualpub.example.com/');
|
|
expect(res.status).to.equal(200);
|
|
expect(cap.opts.hostname).to.equal('93.184.216.34');
|
|
});
|
|
|
|
it('pins to a public IPv6 address for an IPv6-only host', async function () {
|
|
stubLookup({
|
|
'v6.example.com': [{ address: '2606:4700:4700::1111', family: 6 }],
|
|
});
|
|
const cap = stubTransport({ statusCode: 200 });
|
|
const res = await fetchSafe('http://v6.example.com/');
|
|
expect(res.status).to.equal(200);
|
|
expect(cap.opts.hostname).to.equal('2606:4700:4700::1111');
|
|
});
|
|
|
|
it('allows a literal public IPv4 URL without any DNS lookup', async function () {
|
|
const spy = sinon.stub(dns.promises, 'lookup');
|
|
const cap = stubTransport({ statusCode: 204 });
|
|
const res = await fetchSafe('http://93.184.216.34/');
|
|
expect(res.status).to.equal(204);
|
|
expect(cap.opts.hostname).to.equal('93.184.216.34');
|
|
expect(spy.called).to.equal(false);
|
|
});
|
|
|
|
it('sets the TLS SNI to the original hostname for https', async function () {
|
|
stubLookup({ 'secure.example.com': [{ address: '93.184.216.34', family: 4 }] });
|
|
const cap = stubTransport({ statusCode: 200 });
|
|
const res = await fetchSafe('https://secure.example.com/');
|
|
expect(res.status).to.equal(200);
|
|
expect(cap.opts.hostname).to.equal('93.184.216.34');
|
|
expect(cap.opts.servername).to.equal('secure.example.com');
|
|
});
|
|
|
|
it('blocks redirects even when the host itself is public', async function () {
|
|
stubLookup({ 'redir.example.com': [{ address: '93.184.216.34', family: 4 }] });
|
|
stubTransport({ statusCode: 302, headers: { location: 'http://127.0.0.1/' } });
|
|
await expectReject(
|
|
fetchSafe('http://redir.example.com/'),
|
|
/Redirects are not allowed/,
|
|
);
|
|
});
|
|
});
|
|
|
|
// ── Every hop, not just the first (FollowBleed, GHSA-j9p2-jm73-p549) ──────
|
|
//
|
|
// A caller that MUST follow redirects to work at all — the Trello import,
|
|
// whose attachment URLs 302 to signed S3 URLs — passes maxRedirects. That is
|
|
// an opt-in to FOLLOWING a redirect, never an opt-out of the guard: the URL a
|
|
// redirect names is validated and pinned exactly like the one the caller
|
|
// passed. tests/followbleed.test.cjs covers this in full.
|
|
describe('fetchSafe({ maxRedirects }) validates every hop', function () {
|
|
it('refuses a redirect from a public host to loopback', async function () {
|
|
stubLookup({ 'public.example.com': [{ address: '93.184.216.34', family: 4 }] });
|
|
const cap = stubTransportSequence([
|
|
{ statusCode: 302, headers: { location: 'http://127.0.0.1:18080/secret' } },
|
|
{ statusCode: 200, body: 'INTERNAL' },
|
|
]);
|
|
await expectReject(
|
|
fetchSafe('http://public.example.com/attachment.txt', { maxRedirects: 5 }),
|
|
/Blocked IP in URL: 127\.0\.0\.1/,
|
|
);
|
|
// the loopback service was never contacted
|
|
expect(cap.calls.length).to.equal(1);
|
|
});
|
|
|
|
it('refuses a redirect to a hostname that RESOLVES to a private IP', async function () {
|
|
stubLookup({
|
|
'public.example.com': [{ address: '93.184.216.34', family: 4 }],
|
|
'evil.example.com': [{ address: '10.1.2.3', family: 4 }],
|
|
});
|
|
const cap = stubTransportSequence([
|
|
{ statusCode: 302, headers: { location: 'http://evil.example.com/' } },
|
|
]);
|
|
await expectReject(
|
|
fetchSafe('http://public.example.com/', { maxRedirects: 5 }),
|
|
/Blocked IP 10\.1\.2\.3 resolved for evil\.example\.com/,
|
|
);
|
|
expect(cap.calls.length).to.equal(1);
|
|
});
|
|
|
|
it('follows a redirect between public hosts, pinning each hop', async function () {
|
|
stubLookup({
|
|
'trello.example.com': [{ address: '93.184.216.34', family: 4 }],
|
|
's3.example.com': [{ address: '198.51.99.7', family: 4 }],
|
|
});
|
|
const cap = stubTransportSequence([
|
|
{ statusCode: 302, headers: { location: 'https://s3.example.com/signed' } },
|
|
{ statusCode: 200, body: 'FILEBYTES' },
|
|
]);
|
|
const res = await fetchSafe('https://trello.example.com/download/a.png', {
|
|
maxRedirects: 5,
|
|
});
|
|
expect(res.ok).to.equal(true);
|
|
expect(await res.text()).to.equal('FILEBYTES');
|
|
expect(cap.calls[0].hostname).to.equal('93.184.216.34');
|
|
expect(cap.calls[1].hostname).to.equal('198.51.99.7');
|
|
expect(cap.calls[1].headers.Host).to.equal('s3.example.com');
|
|
});
|
|
|
|
it('does not hand credentials to a cross-origin redirect target', async function () {
|
|
stubLookup({
|
|
'trello.example.com': [{ address: '93.184.216.34', family: 4 }],
|
|
's3.example.com': [{ address: '198.51.99.7', family: 4 }],
|
|
});
|
|
const cap = stubTransportSequence([
|
|
{ statusCode: 302, headers: { location: 'https://s3.example.com/signed' } },
|
|
{ statusCode: 200, body: 'ok' },
|
|
]);
|
|
await fetchSafe('https://trello.example.com/download/a.png', {
|
|
headers: { Authorization: 'OAuth oauth_token="SECRET"' },
|
|
maxRedirects: 5,
|
|
});
|
|
expect(cap.calls[0].headers.Authorization).to.equal('OAuth oauth_token="SECRET"');
|
|
expect(cap.calls[1].headers.Authorization).to.equal(undefined);
|
|
});
|
|
|
|
it('stops a redirect chain at the caller\'s limit', async function () {
|
|
stubLookup({ 'public.example.com': [{ address: '93.184.216.34', family: 4 }] });
|
|
stubTransportSequence([
|
|
{ statusCode: 302, headers: { location: 'http://public.example.com/2' } },
|
|
{ statusCode: 302, headers: { location: 'http://public.example.com/3' } },
|
|
{ statusCode: 302, headers: { location: 'http://public.example.com/4' } },
|
|
]);
|
|
await expectReject(
|
|
fetchSafe('http://public.example.com/1', { maxRedirects: 2 }),
|
|
/Too many redirects/,
|
|
);
|
|
});
|
|
});
|
|
|
|
// ── Input-time validator parity (validateAttachmentUrl) ──────────────────
|
|
describe('validateAttachmentUrl() — input-time DNS-aware validation', function () {
|
|
it('rejects a literal metadata IP', async function () {
|
|
const result = await validateAttachmentUrl('http://169.254.169.254/');
|
|
expect(result.valid).to.equal(false);
|
|
});
|
|
|
|
it('rejects localhost', async function () {
|
|
const result = await validateAttachmentUrl('http://localhost/hook');
|
|
expect(result.valid).to.equal(false);
|
|
});
|
|
|
|
it('rejects non-http(s) protocols', async function () {
|
|
const result = await validateAttachmentUrl('ftp://example.com/x');
|
|
expect(result.valid).to.equal(false);
|
|
});
|
|
|
|
// Same DnsBleed PoC, blocked at input time (REST create/update).
|
|
it('rejects 169-254-169-254.nip.io -> 169.254.169.254', async function () {
|
|
stubLookup({
|
|
'169-254-169-254.nip.io': [{ address: '169.254.169.254', family: 4 }],
|
|
});
|
|
const result = await validateAttachmentUrl('http://169-254-169-254.nip.io/');
|
|
expect(result.valid).to.equal(false);
|
|
expect(result.reason).to.match(/not allowed/i);
|
|
});
|
|
|
|
it('rejects a hostname whose AAAA record is internal IPv6', async function () {
|
|
stubLookup({ 'evil6.example.com': [{ address: 'fd00::1', family: 6 }] });
|
|
const result = await validateAttachmentUrl('http://evil6.example.com/');
|
|
expect(result.valid).to.equal(false);
|
|
});
|
|
|
|
it('allows a public host (negative test)', async function () {
|
|
stubLookup({ 'example.com': [{ address: '93.184.216.34', family: 4 }] });
|
|
const result = await validateAttachmentUrl('http://example.com/hook');
|
|
expect(result.valid).to.equal(true);
|
|
});
|
|
});
|
|
});
|