Files
opal-ios-studio/test/images.test.cjs

93 lines
4.7 KiB
JavaScript

const test = require('node:test');
const assert = require('node:assert/strict');
const http = require('node:http');
const { createImageHandler, rasterType } = require('../server/images.cjs');
const { createPublicFetcher } = require('../server/public-fetch.cjs');
const { createApiHandler } = require('../server/api.cjs');
// Public-domain 1x1 transparent PNG, used only as a binary response fixture.
const png = Buffer.from('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+a9p8AAAAASUVORK5CYII=', 'base64');
async function fixture(t, fetchPublic, options = {}) {
const reads = [];
const store = { read: async workspace => {
reads.push(workspace.tenant);
return workspace.tenant === 'a' ? [{ id: 'shop', products: [{ id: 'product', image: 'https://images.fixture/p.png' }] }] : [];
} };
const imageHandler = createImageHandler({ store, fetchPublic, maxConcurrent: 1, ...options });
const api = createApiHandler({ store, imageHandler, publicOrigin: 'https://studio.fixture', scrape: async () => {},
resolveWorkspace: async req => {
if (!req.headers.cookie) throw Object.assign(new Error('Login required'), { status: 401 });
return { tenant: req.headers.cookie === 'a' ? 'a' : 'b' };
}
});
const server = http.createServer(api);
await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
t.after(() => { server.closeAllConnections(); return new Promise(resolve => server.close(resolve)); });
const base = 'http://127.0.0.1:' + server.address().port;
return { base, reads, get: (suffix = '', cookie = 'a') => fetch(base + '/api/catalog-image?storeId=shop&productId=product' + suffix, { headers: cookie ? { cookie } : {} }) };
}
test('catalog image is authenticated and cannot be read from another workspace or arbitrary URL', async t => {
let calls = 0;
const f = await fixture(t, async () => { calls++; return { status: 200, body: png }; });
assert.equal((await f.get('', null)).status, 401);
assert.equal((await f.get('', 'b')).status, 404);
assert.equal((await f.get('&url=http://127.0.0.1')).status, 400);
assert.equal(calls, 0);
const result = await f.get();
assert.equal(result.status, 200);
assert.equal(result.headers.get('content-type'), 'image/png');
assert.match(result.headers.get('cache-control'), /no-store/);
assert.equal(result.headers.get('access-control-allow-origin'), null);
assert.equal(result.headers.get('cross-origin-resource-policy'), 'same-origin');
assert.deepEqual(Buffer.from(await result.arrayBuffer()), png);
assert.equal(calls, 1);
});
test('HTML, SVG, fake MIME and source failures never become browser image responses', async t => {
for (const body of [Buffer.from('<svg onload="alert(1)"></svg>'), Buffer.from('<html>login</html>')]) {
const f = await fixture(t, async () => ({ status: 200, headers: { 'content-type': 'image/png' }, body }));
const result = await f.get();
assert.equal(result.status, 415);
assert.match(result.headers.get('content-type'), /application\/json/);
}
const f = await fixture(t, async () => ({ status: 302, body: png }));
assert.equal((await f.get()).status, 502);
assert.equal(rasterType(Buffer.from('GIF89a')), null);
});
test('saved private image addresses are rejected before any socket is opened', async t => {
const publicFetcher = createPublicFetcher({ lookup: async () => [{ address: '127.0.0.1', family: 4 }] });
const f = await fixture(t, publicFetcher);
const result = await f.get();
assert.equal(result.status, 400);
assert.match((await result.json()).code, /PRIVATE|PUBLIC|SOURCE/);
});
test('concurrent image requests are bounded and capacity returns after completion', async t => {
let release;
const gate = new Promise(resolve => { release = resolve; });
let entered;
const started = new Promise(resolve => { entered = resolve; });
const f = await fixture(t, async () => { entered(); await gate; return { status: 200, body: png }; });
const first = f.get();
await started;
assert.equal((await f.get()).status, 503);
release();
assert.equal((await first).status, 200);
assert.equal((await f.get()).status, 200);
});
test('connected image lookup cannot use another client catalog identifier', async t => {
let sourceCalls = 0;
const f = await fixture(t, async () => { sourceCalls++; return { status: 200, body: png }; }, {
getConnected: async () => ({ id: 'connected:own', products: [{ id: 'p', image: 'https://images.fixture/p.png' }] })
});
const call = storeId => fetch(f.base + '/api/catalog-image?' + new URLSearchParams({ storeId, productId: 'p' }), { headers: { cookie: 'a' } });
assert.equal((await call('connected:other-client')).status, 404);
assert.equal(sourceCalls, 0);
assert.equal((await call('connected:own')).status, 200);
assert.equal(sourceCalls, 1);
});