Read existing Client catalogs through verified workspace context
This commit is contained in:
@@ -144,3 +144,14 @@ test('workspace bootstrap returns canonical client identity without reading save
|
||||
assert.equal(owner.status, 409);
|
||||
assert.equal((await f.call('readonly', '/api/workspace')).body.workspace.canWriteProducts, false);
|
||||
});
|
||||
|
||||
test('connected catalog remains read-only and returns same-origin image references', async t => {
|
||||
const f = await setup(t, { getConnected: async ws => ({ id: 'connected:' + ws.clientId, readOnly: true, products: [{ id: 'connected-product', title: 'Source', rawPrice: 380, image: 'https://images.fixture/a.png' }] }) });
|
||||
const result = await f.call('a', '/api/get-synced-stores');
|
||||
assert.equal(result.body.stores[0].readOnly, true);
|
||||
assert.match(result.body.stores[0].products[0].image, /^\/api\/catalog-image\?/);
|
||||
assert.doesNotMatch(result.body.stores[0].products[0].image, /https:/);
|
||||
const removed = await f.call('a', '/api/delete-synced-store', { storeId: result.body.stores[0].id });
|
||||
assert.equal(removed.status, 403);
|
||||
assert.equal(removed.body.code, 'CONNECTED_CATALOG_READ_ONLY');
|
||||
});
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
const { createWorkspaceResolver } = require('../server/workspace.cjs');
|
||||
const { createConnectedCatalog } = require('../server/connected.cjs');
|
||||
const clientId = '11111111-1111-4111-8111-111111111111';
|
||||
function workspace(name = 'a', readConnectedCatalog) {
|
||||
return { clientId: name === 'a' ? clientId : '22222222-2222-4222-8222-222222222222', tenant: name, displayName: 'Fixture ' + name, namespace: name.repeat(64), readConnectedCatalog };
|
||||
}
|
||||
function catalog(products = []) { return { source: 'supabase', fetchedAt: '2026-10-19T00:00:00Z', products }; }
|
||||
|
||||
test('connected catalog preserves zero/unknown prices, image URLs and source timestamps without persistent writes', async () => {
|
||||
const get = createConnectedCatalog({ publicClientOrigin: 'https://client.fixture' });
|
||||
const store = await get(workspace('a', async () => catalog([
|
||||
{ id: 'one', title: 'Source <SKU>', rawPrice: 0, rawRegularPrice: 270, currency: 'BDT', image: 'https://images.fixture/product.png' },
|
||||
{ id: 'two', title: 'Missing price', rawPrice: null, currency: null }
|
||||
])));
|
||||
assert.equal(store.readOnly, true); assert.equal(store.id, 'connected:' + clientId);
|
||||
assert.equal(store.products[0].rawPrice, 0); assert.equal(store.products[0].image, 'https://images.fixture/product.png');
|
||||
assert.equal(store.products[1].rawPrice, null); assert.equal(store.products[1].currency, null);
|
||||
assert.equal(store.products[1].link, ''); assert.equal(store.fetchedAt, '2026-10-19T00:00:00Z');
|
||||
});
|
||||
|
||||
test('connected cache shares one source read for the same client and separates other workspaces', async () => {
|
||||
let reads = 0;
|
||||
const get = createConnectedCatalog({ publicClientOrigin: 'https://client.fixture' });
|
||||
const read = async () => { reads++; return catalog([{ id: 'one', title: 'Product', rawPrice: 380 }]); };
|
||||
const same = workspace('a', read);
|
||||
await Promise.all([get(same), get(same), get(same)]);
|
||||
assert.equal(reads, 1);
|
||||
await get(workspace('b', read)); assert.equal(reads, 2);
|
||||
});
|
||||
|
||||
test('failed connected reads never become empty success and can be retried after recovery', async () => {
|
||||
const get = createConnectedCatalog({ publicClientOrigin: 'https://client.fixture', ttlMs: 0 });
|
||||
let broken = true;
|
||||
const same = workspace('a', async () => { if (broken) throw Error('fixture outage'); return catalog(); });
|
||||
await assert.rejects(get(same), /fixture outage/);
|
||||
broken = false;
|
||||
assert.equal((await get(same)).products.length, 0);
|
||||
});
|
||||
|
||||
test('simultaneous cold catalog reads across clients are bounded', async () => {
|
||||
const get = createConnectedCatalog({ publicClientOrigin: 'https://client.fixture', maxConcurrentReads: 1 });
|
||||
let release, entered;
|
||||
const started = new Promise(resolve => { entered = resolve; });
|
||||
const wait = new Promise(resolve => { release = resolve; });
|
||||
const first = get(workspace('a', async () => { entered(); await wait; return catalog(); }));
|
||||
await started;
|
||||
await assert.rejects(get(workspace('b', async () => catalog())), { code: 'CONNECTED_CATALOG_BUSY' });
|
||||
release(); await first;
|
||||
assert.equal((await get(workspace('b', async () => catalog()))).products.length, 0);
|
||||
});
|
||||
|
||||
test('Client catalog read forwards only server identity and rejects another canonical client response', async () => {
|
||||
const calls = [];
|
||||
let mismatch = false;
|
||||
const resolve = createWorkspaceResolver({ authMeUrl: 'https://auth.fixture/me', clientMeUrl: 'https://client.fixture/me', clientCatalogUrl: 'https://client.fixture/studio/catalog',
|
||||
fetchImpl: async (url, options) => {
|
||||
calls.push({ url, headers: options.headers });
|
||||
const actor = { id: 'actor', email: 'actor@example.test', role: 'client' };
|
||||
if (url.includes('auth.fixture')) return Response.json({ user: { ...actor, isActive: true } });
|
||||
if (url.endsWith('/studio/catalog')) return Response.json({ success: true, clientId: mismatch ? 'other-client' : clientId, tenant: 'canonical', ...catalog() });
|
||||
return Response.json({ success: true, clientId, tenant: 'canonical', actor, perms: { products: true } });
|
||||
}
|
||||
});
|
||||
const ws = await resolve({ url: '/api/get-synced-stores?tenant=victim', headers: { cookie: 'aura_access=fixture-token; unrelated=must-not-forward', 'x-client-id': 'victim' } });
|
||||
await ws.readConnectedCatalog();
|
||||
assert.equal(calls.at(-1).headers.Cookie, 'aura_access=fixture-token');
|
||||
assert.equal(calls.at(-1).headers['x-aura-tenant'], 'canonical');
|
||||
assert.equal(calls.at(-1).headers['x-client-id'], undefined);
|
||||
mismatch = true;
|
||||
await assert.rejects(ws.readConnectedCatalog(), { code: 'CATALOG_WORKSPACE_MISMATCH' });
|
||||
});
|
||||
+15
-3
@@ -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);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user