Implement protected Studio catalog API and bounded public source reader

This commit is contained in:
khondokartowsif171
2026-10-18 21:10:07 +06:00
parent 3847a5e6a1
commit cbe6e0ae94
18 changed files with 1391 additions and 18 deletions
+54
View File
@@ -0,0 +1,54 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs/promises');
const path = require('node:path');
const os = require('node:os');
const { createStudioServer } = require('../server/main.cjs');
async function app(t) {
const root = await fs.mkdtemp(path.join(os.tmpdir(), 'studio-main-'));
const publicDirectory = path.join(root, 'public');
await fs.mkdir(publicDirectory);
await fs.writeFile(path.join(publicDirectory, 'index.html'), '<!doctype html><h1>Fixture Studio</h1>');
await fs.writeFile(path.join(publicDirectory, 'app.js'), '/* fixture */\n'.repeat(200));
await fs.writeFile(path.join(root, 'private.txt'), 'PRIVATE-FIXTURE-NOT-A-WEB-ASSET');
const server = createStudioServer({ publicDirectory, dataDirectory: path.join(root, 'state'), publicOrigin: 'https://studio.fixture', authMeUrl: 'http://127.0.0.1:1/auth', clientMeUrl: 'http://127.0.0.1:1/client' });
await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
t.after(async () => { server.closeAllConnections(); await new Promise(resolve => server.close(resolve)); await fs.rm(root, { recursive: true, force: true }); });
return 'http://127.0.0.1:' + server.address().port;
}
test('server serves existing static assets, compression and revalidation without starting a provider', async t => {
const base = await app(t);
const root = await fetch(base);
assert.equal(root.status, 200);
assert.match(await root.text(), /Fixture Studio/);
assert.match(root.headers.get('cache-control'), /no-store/);
const js = await fetch(base + '/app.js');
assert.equal(js.headers.get('content-encoding'), 'gzip');
assert.equal((await js.text()).length, '/* fixture */\n'.repeat(200).length);
assert.equal((await fetch(base + '/app.js', { headers: { 'If-None-Match': js.headers.get('etag') } })).status, 304);
const head = await fetch(base + '/app.js', { method: 'HEAD' });
assert.equal(head.status, 200); assert.equal(await head.text(), '');
});
test('backend source, private files and traversal paths are not static downloads', async t => {
const base = await app(t);
for (const route of ['/server/main.cjs', '/.env', '/docs', '/node_modules/x', '/%2e%2e%2fprivate.txt', '/assets/..%5cprivate.txt']) {
const result = await fetch(base + route, { headers: { Accept: 'text/html' } });
assert.equal(result.status, 404, route);
assert.doesNotMatch(await result.text(), /PRIVATE-FIXTURE/);
}
});
test('API failures stay JSON and service health is distinct from authenticated workspace availability', async t => {
const base = await app(t);
assert.equal((await fetch(base + '/health')).status, 200);
const denied = await fetch(base + '/api/get-synced-stores');
assert.equal(denied.status, 401);
assert.match(denied.headers.get('content-type'), /application\/json/);
assert.equal((await denied.json()).code, 'LOGIN_REQUIRED');
const unknown = await fetch(base + '/api/missing');
assert.equal(unknown.status, 404);
assert.match(unknown.headers.get('content-type'), /application\/json/);
});