Phase 1 scoped only projects/assets/bins and left recorders, sequences,
imports, comments carrying TODO(authz) markers. A scoped editor/viewer could
still read and mutate those across every project. This closes the gap using
the existing authz.js helpers — no open TODO(authz) markers remain.
- recorders: param('id') resolves project + view baseline, requireRecorderEdit
on PATCH/DELETE/start/stop, GET / filtered by accessibleProjectIds, POST /
asserts edit on the target project (null project = admin-only)
- sequences: same param pattern + requireSequenceEdit on PUT/:id,/clips,conform
and DELETE; GET//POST/ assert on the query/body project
- imports: POST /youtube asserts edit on the body projectId
- comments: router.use guard resolves project via the asset (view to read, edit
to write); also fixes the author bug (req.session.userId -> req.user.id, which
was always NULL so comments had no recorded author)
- capture: intentionally any-logged-in (shared hardware, asset scoped on
registration) — TODO replaced with a rationale note
Security fixes from review of this change:
- recorders POST /:id/start: a per-take projectId override could route a live
asset into a project the caller lacks edit on — now asserts edit on the
override target
- sequences PUT /:id/clips: spliced asset_ids weren't checked, so an editor
could pull in (and via GET /:id leak signed proxy URLs for) assets from a
project they can't access — now every clip asset must belong to the
sequence's project; pre-transaction queries moved inside try/catch so a DB
error returns 500 instead of hanging the request
- tests: recorders-access, sequences-access (incl. cross-project clip guard),
comments-access (incl. author-id regression)
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
107 lines
5.4 KiB
JavaScript
107 lines
5.4 KiB
JavaScript
// RBAC coverage for recorders: list is scoped to accessible projects, /:id
|
|
// asserts view, mutators assert edit, null-project recorders are admin-only.
|
|
// Same harness as assets-access.test.js — singleton pool on TEST_DATABASE_URL,
|
|
// req.user injected, router dynamic-imported. Skips without TEST_DATABASE_URL.
|
|
import { test } from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import express from 'express';
|
|
import { isTestDbConfigured, setupTestDb } from '../helpers/setup-db.js';
|
|
|
|
const SKIP = !isTestDbConfigured() && 'TEST_DATABASE_URL not set';
|
|
|
|
async function appWithRecorders(user) {
|
|
process.env.DATABASE_URL = process.env.TEST_DATABASE_URL;
|
|
process.env.AUTH_ENABLED = 'true';
|
|
const { default: recordersRouter } = await import('../../src/routes/recorders.js?v=' + Date.now());
|
|
const app = express();
|
|
app.use(express.json());
|
|
app.use((req, _res, next) => { req.user = user; next(); });
|
|
app.use('/api/v1/recorders', recordersRouter);
|
|
return new Promise(r => {
|
|
const srv = app.listen(0, '127.0.0.1', () =>
|
|
r({ baseUrl: 'http://127.0.0.1:' + srv.address().port, close: () => new Promise(rs => srv.close(rs)) }));
|
|
});
|
|
}
|
|
|
|
async function seed(pool) {
|
|
const proj = async (n) => (await pool.query(`INSERT INTO projects (name, s3_prefix) VALUES ($1,$1) RETURNING id`, [n])).rows[0].id;
|
|
const alpha = await proj('Alpha');
|
|
const beta = await proj('Beta');
|
|
const rec = async (pid, name) => (await pool.query(
|
|
`INSERT INTO recorders (name, source_type, project_id) VALUES ($1, 'srt', $2) RETURNING id`, [name, pid])).rows[0].id;
|
|
const recA = await rec(alpha, 'Cam A');
|
|
const recB = await rec(beta, 'Cam B');
|
|
const recNull = (await pool.query(
|
|
`INSERT INTO recorders (name, source_type, project_id) VALUES ('Unassigned','srt',NULL) RETURNING id`)).rows[0].id;
|
|
const admin = { id: (await pool.query(`INSERT INTO users (username, password_hash, role) VALUES ('adm','x','admin') RETURNING id`)).rows[0].id, role: 'admin' };
|
|
const scoped = { id: (await pool.query(`INSERT INTO users (username, password_hash, role) VALUES ('ed','x','editor') RETURNING id`)).rows[0].id, role: 'editor' };
|
|
await pool.query(`INSERT INTO project_access (project_id, subject_type, subject_id, level) VALUES ($1,'user',$2,'view')`, [alpha, scoped.id]);
|
|
return { alpha, beta, recA, recB, recNull, admin, scoped };
|
|
}
|
|
|
|
test('recorders list is scoped; admin sees all incl. null-project, scoped sees only granted', { skip: SKIP }, async () => {
|
|
const pool = await setupTestDb();
|
|
process.env.DATABASE_URL = process.env.TEST_DATABASE_URL;
|
|
try {
|
|
const { recA, admin, scoped } = await seed(pool);
|
|
|
|
let a = await appWithRecorders(admin);
|
|
let list = await (await fetch(a.baseUrl + '/api/v1/recorders')).json();
|
|
assert.equal(list.length, 3, 'admin sees all three recorders');
|
|
await a.close();
|
|
|
|
let s = await appWithRecorders(scoped);
|
|
list = await (await fetch(s.baseUrl + '/api/v1/recorders')).json();
|
|
assert.deepEqual(list.map(r => r.id), [recA], 'scoped editor sees only the granted-project recorder');
|
|
await s.close();
|
|
} finally { await pool.end(); }
|
|
});
|
|
|
|
test('recorder /:id enforces view; mutators enforce edit', { skip: SKIP }, async () => {
|
|
const pool = await setupTestDb();
|
|
process.env.DATABASE_URL = process.env.TEST_DATABASE_URL;
|
|
try {
|
|
const { recA, recB, recNull, scoped } = await seed(pool);
|
|
const s = await appWithRecorders(scoped);
|
|
|
|
// view-granted on Alpha: can GET recA, cannot GET recB (other project) or recNull (admin-only).
|
|
assert.equal((await fetch(s.baseUrl + '/api/v1/recorders/' + recA)).status, 200);
|
|
assert.equal((await fetch(s.baseUrl + '/api/v1/recorders/' + recB)).status, 403);
|
|
assert.equal((await fetch(s.baseUrl + '/api/v1/recorders/' + recNull)).status, 403);
|
|
|
|
// view-only grant cannot PATCH (edit) or start.
|
|
const patch = await fetch(s.baseUrl + '/api/v1/recorders/' + recA, {
|
|
method: 'PATCH', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name: 'x' }) });
|
|
assert.equal(patch.status, 403, 'view-level grant must not allow edit');
|
|
await s.close();
|
|
} finally { await pool.end(); }
|
|
});
|
|
|
|
test('creating a recorder requires edit; null-project create is admin-only', { skip: SKIP }, async () => {
|
|
const pool = await setupTestDb();
|
|
process.env.DATABASE_URL = process.env.TEST_DATABASE_URL;
|
|
try {
|
|
const { alpha, admin, scoped } = await seed(pool);
|
|
|
|
// scoped editor has only 'view' on Alpha → create denied.
|
|
let s = await appWithRecorders(scoped);
|
|
let r = await fetch(s.baseUrl + '/api/v1/recorders', {
|
|
method: 'POST', headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ name: 'New', source_type: 'srt', project_id: alpha }) });
|
|
assert.equal(r.status, 403);
|
|
// null project → admin-only, still denied for the editor.
|
|
r = await fetch(s.baseUrl + '/api/v1/recorders', {
|
|
method: 'POST', headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ name: 'New2', source_type: 'srt' }) });
|
|
assert.equal(r.status, 403);
|
|
await s.close();
|
|
|
|
// admin can create in any project and with no project.
|
|
let a = await appWithRecorders(admin);
|
|
r = await fetch(a.baseUrl + '/api/v1/recorders', {
|
|
method: 'POST', headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ name: 'AdminRec', source_type: 'srt' }) });
|
|
assert.equal(r.status, 201);
|
|
await a.close();
|
|
} finally { await pool.end(); }
|
|
});
|