Read existing Client catalogs through verified workspace context

This commit is contained in:
khondokartowsif171
2026-10-19 01:34:52 +06:00
parent 6db99f96df
commit 165320cc9f
10 changed files with 189 additions and 17 deletions
+15 -3
View File
@@ -8,13 +8,13 @@ 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) {
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 });
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 });
@@ -25,7 +25,7 @@ async function fixture(t, fetchPublic) {
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 { reads, get: (suffix = '', cookie = 'a') => fetch(base + '/api/catalog-image?storeId=shop&productId=product' + suffix, { headers: cookie ? { cookie } : {} }) };
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 => {
@@ -78,3 +78,15 @@ test('concurrent image requests are bounded and capacity returns after completio
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);
});