Files
opal-ios-studio/test/public-fetch.test.cjs

99 lines
5.5 KiB
JavaScript

const test = require('node:test');
const assert = require('node:assert/strict');
const http = require('node:http');
const zlib = require('node:zlib');
const { createPublicFetcher, publicAddress, websiteUrl } = require('../server/public-fetch.cjs');
async function fixture(t, handler) {
const server = http.createServer(handler);
await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
t.after(() => { server.closeAllConnections(); return new Promise(resolve => server.close(resolve)); });
const port = server.address().port, calls = [];
const requestImpl = (url, options, callback) => {
options.lookup(url.hostname, { all: true }, (error, addresses) => {
assert.ifError(error);
assert.ok(addresses.every(item => publicAddress(item.address)));
calls.push({ pathname: url.pathname, pinned: addresses, headers: options.headers });
});
return http.get(new URL(url.pathname + url.search, 'http://127.0.0.1:' + port), { ...options, lookup: undefined }, callback);
};
return { calls, requestImpl };
}
const lookup = async () => [{ address: '1.1.1.1', family: 4 }];
test('rejects internal, mapped-private, metadata and reserved addresses', () => {
for (const address of ['127.0.0.1', '10.1.2.3', '172.16.0.1', '192.168.1.1', '169.254.169.254', '100.64.0.1', '0.0.0.0', '::1', 'fc00::1', 'fe80::1', '::ffff:127.0.0.1', '64:ff9b::7f00:1', '2001:db8::1']) {
assert.equal(publicAddress(address), false, address);
}
assert.equal(publicAddress('1.1.1.1'), true);
assert.equal(publicAddress('2606:4700:4700::1111'), true);
});
test('rejects unsupported schemes, credentials and nonstandard ports', () => {
for (const url of ['file:///etc/passwd', 'http://user:pass@example.com/', 'http://example.com:2375/', 'ftp://example.com/']) {
assert.throws(() => websiteUrl(url), { code: 'INVALID_SOURCE_URL' });
}
});
test('blocks numeric loopback aliases and mixed DNS before opening any request', async () => {
let opened = 0;
const get = createPublicFetcher({ lookup: async () => [{ address: '1.1.1.1', family: 4 }, { address: '10.0.0.1', family: 4 }], requestImpl: () => { opened++; } });
await assert.rejects(get('http://2130706433/'), { code: 'NON_PUBLIC_SOURCE' });
await assert.rejects(get('http://mixed.fixture/'), { code: 'NON_PUBLIC_SOURCE' });
assert.equal(opened, 0);
});
test('pins validated DNS and never forwards account credentials to source websites', async t => {
const f = await fixture(t, (_req, res) => { res.end('{"products":[]}'); });
const result = await createPublicFetcher({ lookup, requestImpl: f.requestImpl })('http://catalog.fixture/products.json');
assert.equal(result.status, 200);
assert.equal(result.body.toString(), '{"products":[]}');
assert.deepEqual(f.calls[0].pinned, [{ address: '1.1.1.1', family: 4 }]);
assert.equal(f.calls[0].headers.Cookie, undefined);
assert.equal(f.calls[0].headers.Authorization, undefined);
});
test('redirects are revalidated and a private redirect is never requested', async t => {
const f = await fixture(t, (_req, res) => { res.writeHead(302, { Location: 'http://169.254.169.254/latest/' }); res.end(); });
await assert.rejects(createPublicFetcher({ lookup, requestImpl: f.requestImpl })('http://catalog.fixture/redirect'), { code: 'NON_PUBLIC_SOURCE' });
assert.equal(f.calls.length, 1);
});
test('same-host redirect cannot bypass a DNS rebind to a private address', async t => {
const f = await fixture(t, (_req, res) => { res.writeHead(302, { Location: '/next' }); res.end(); });
let queries = 0;
const dns = async () => [{ address: ++queries === 1 ? '1.1.1.1' : '127.0.0.1', family: 4 }];
await assert.rejects(createPublicFetcher({ lookup: dns, requestImpl: f.requestImpl })('http://catalog.fixture/start'), { code: 'NON_PUBLIC_SOURCE' });
assert.equal(f.calls.length, 1);
});
test('redirect loops terminate within the configured request budget', async t => {
const f = await fixture(t, (_req, res) => { res.writeHead(302, { Location: '/again' }); res.end(); });
await assert.rejects(createPublicFetcher({ lookup, requestImpl: f.requestImpl, maxRedirects: 2 })('http://catalog.fixture/start'), { code: 'SOURCE_REDIRECT_LIMIT' });
assert.equal(f.calls.length, 3);
});
test('body size limits apply to decoded compressed data', async t => {
const payload = zlib.gzipSync('x'.repeat(10000));
const f = await fixture(t, (_req, res) => { res.writeHead(200, { 'Content-Encoding': 'gzip' }); res.end(payload); });
await assert.rejects(createPublicFetcher({ lookup, requestImpl: f.requestImpl, maxBytes: 200 })('http://catalog.fixture/large'), { code: 'SOURCE_TOO_LARGE' });
});
test('valid gzip body is decoded and slow sources time out', async t => {
const f = await fixture(t, (req, res) => {
if (req.url === '/slow') { res.writeHead(200); res.write('waiting'); return; }
res.writeHead(200, { 'Content-Encoding': 'gzip' }); res.end(zlib.gzipSync('real fixture data'));
});
const get = createPublicFetcher({ lookup, requestImpl: f.requestImpl, timeoutMs: 2000 });
assert.equal((await get('http://catalog.fixture/gzip')).body.toString(), 'real fixture data');
const short = createPublicFetcher({ lookup, requestImpl: f.requestImpl, timeoutMs: 100 });
await assert.rejects(short('http://catalog.fixture/slow'), { code: 'SOURCE_TIMEOUT' });
});
test('slow DNS times out without opening a connection', async () => {
let opened = 0;
const get = createPublicFetcher({ lookup: () => new Promise(() => {}), requestImpl: () => { opened++; }, timeoutMs: 30 });
await assert.rejects(get('http://catalog.fixture/'), { code: 'SOURCE_TIMEOUT' });
assert.equal(opened, 0);
});