feat(studio): drop 3D Poster Lab and ship Multi-Tenant Multi-Expert Brain OS, Live Playground & n8n Playbooks

This commit is contained in:
Amirul Islam Redwan71
2026-11-26 13:42:25 +06:00
parent 7c5bcd9ac7
commit 6a46c419f2
8 changed files with 1274 additions and 8031 deletions
+110
View File
@@ -0,0 +1,110 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const http = require('node:http');
const fs = require('node:fs/promises');
const path = require('node:path');
const os = require('node:os');
const { createHash } = require('node:crypto');
const { createWorkspaceResolver } = require('../server/workspace.cjs');
const { createStore } = require('../server/store.cjs');
const { createApiHandler } = require('../server/api.cjs');
const { createEngineProxy } = require('../server/engine-proxy.cjs');
async function serve(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)); });
return 'http://127.0.0.1:' + server.address().port;
}
test('multi-expert proxy enforces verified tenant headers, CSRF origin, and read-only write guard', async t => {
const root = await fs.mkdtemp(path.join(os.tmpdir(), 'studio-engine-proxy-'));
t.after(() => fs.rm(root, { recursive: true, force: true }));
const identities = {
a: { id: 'actor-a', role: 'client', isActive: true, tenant: 'tenant-a', products: true },
readonly: { id: 'reader-a', role: 'team_member', isActive: true, tenant: 'tenant-a', products: false }
};
const identityUrl = await serve(t, (req, res) => {
res.setHeader('Content-Type', 'application/json');
if (req.url === '/auth') {
const u = identities[req.headers.authorization?.replace('Bearer ', '')];
res.writeHead(u ? 200 : 401);
return res.end(JSON.stringify({ user: u ? { ...u, email: u.id + '@test' } : undefined }));
}
const key = /aura_access=([^;]+)/.exec(req.headers.cookie || '')?.[1];
const u = identities[key];
if (!u) { res.writeHead(401); return res.end('{}'); }
const hash = createHash('sha256').update(u.tenant).digest('hex');
const clientId = [hash.slice(0, 8), hash.slice(8, 12), hash.slice(12, 16), hash.slice(16, 20), hash.slice(20, 32)].join('-');
return res.end(JSON.stringify({ success: true, tenant: u.tenant, clientId, actor: { id: u.id, email: u.id + '@test', role: u.role }, role: u.products ? 'manager' : 'agent', clientName: u.tenant, perms: { products: u.products } }));
});
const engineCalls = [];
const engineProxy = createEngineProxy({
baseUrl: 'http://aura-engine.internal:3005',
serviceKey: 'test-service-secret',
fetchImpl: async (url, init) => {
engineCalls.push({ url, method: init.method, headers: init.headers, body: init.body ? JSON.parse(init.body) : null });
return {
ok: true,
status: 200,
text: async () => JSON.stringify({ success: true, echoedUrl: url, tenant: init.headers['x-aura-tenant'] })
};
}
});
const api = createApiHandler({
resolveWorkspace: createWorkspaceResolver({ authMeUrl: identityUrl + '/auth', clientMeUrl: identityUrl + '/client' }),
store: createStore(root),
scrape: async () => { throw new Error('unused'); },
engineProxy,
publicOrigin: 'https://studio.fixture'
});
const base = await serve(t, api);
// 1. Unauthenticated blocked before hitting engine
const unauth = await fetch(base + '/api/experts');
assert.equal(unauth.status, 401);
assert.equal(engineCalls.length, 0);
// 2. GET /api/experts forwards canonical tenant & Bearer secret
const listRes = await fetch(base + '/api/experts?tenant=attacker', { headers: { Cookie: 'aura_access=a' } });
assert.equal(listRes.status, 200);
assert.equal(engineCalls[0].headers['x-aura-tenant'], 'tenant-a');
assert.equal(engineCalls[0].headers.Authorization, 'Bearer test-service-secret');
// 3. POST /api/experts strips spoofed tenant/client_id from body
const saveRes = await fetch(base + '/api/experts', {
method: 'POST',
headers: { Cookie: 'aura_access=a', Origin: 'https://studio.fixture', 'Content-Type': 'application/json' },
body: JSON.stringify({ name: 'Custom Closer', tenant: 'victim', client_id: '00000000-0000-0000-0000-000000000000' })
});
assert.equal(saveRes.status, 200);
assert.equal(engineCalls[1].headers['x-aura-tenant'], 'tenant-a');
assert.equal(engineCalls[1].body.tenant, undefined);
assert.equal(engineCalls[1].body.client_id, undefined);
// 4. Cross-origin POST/DELETE rejected with 403
const csrfRes = await fetch(base + '/api/experts/custom-1', {
method: 'DELETE',
headers: { Cookie: 'aura_access=a', Origin: 'https://evil.example' }
});
assert.equal(csrfRes.status, 403);
// 5. Read-only member cannot mutate experts or playbooks, but CAN run safe simulate
const roSave = await fetch(base + '/api/playbooks', {
method: 'POST',
headers: { Cookie: 'aura_access=readonly', Origin: 'https://studio.fixture', 'Content-Type': 'application/json' },
body: JSON.stringify({ playbook_key: 'ndr_auto_rescue', is_enabled: true })
});
assert.equal(roSave.status, 403);
const roSim = await fetch(base + '/api/experts/simulate', {
method: 'POST',
headers: { Cookie: 'aura_access=readonly', Origin: 'https://studio.fixture', 'Content-Type': 'application/json' },
body: JSON.stringify({ message: 'দাম কত?' })
});
assert.equal(roSim.status, 200);
});