Skip to content

Autonomous development for real products. Governed agents build from specs you approve, follow the rules you set, and nothing merges until another agent proves it. Your decisions, judgement and taste are the bar that work must clear. Declare your vision and let agents build it.

fix(relay): require roles for board access [H13,H14,H8]

Corey Olson17581152b1c7
diff --git a/relay/README.md b/relay/README.md
index 5ec16b81..d8c7905a 100644
--- a/relay/README.md
+++ b/relay/README.md
@@ -40,14 +40,18 @@ body. A credential for one board is refused on another before any board lookup.
Use `auth.boardsFor(token)` for visibility, and `auth.linkBoard(token, board,
repository)` to link only a repository the person can read. `auth.issueToken`
requires a human session and returns a random, expiring credential scoped to one
-board/agent. `auth.revoke(token, id)` revokes one owned credential.
+board/agent. `await auth.revoke(token, id)` revokes one owned credential. Session revocation
+is independent of a board; board-token revocation requires current write access.
GitHub user credentials are used only to obtain the account's immutable id and
login at sign-in, then discarded. Permission checks use an App-signed JWT to
obtain a repository-scoped installation token asking only for Metadata read.
These App tokens stay in RAM. Private repositories require an App installation
-and a permission response for the same immutable user id. Public repositories
-are readable without a private installation. Repository ids are pinned so a
+and a permission response for the same immutable user id. Public boards are hidden unless the account has triage, write, maintain, or
+admin permission. Public metadata alone grants no board access; confirming a
+role needs an App installation. Private collaborators with read permission can
+see their boards. Board actions and minting or revoking board tokens require
+write, maintain, or admin permission, checked again on every such operation. Repository ids are pinned so a
replacement repository cannot inherit a board link.
SQLite stores account/repository identities and SHA-256 hashes of relay-issued
diff --git a/relay/auth-http.js b/relay/auth-http.js
index 66b7df16..5c53fd4f 100644
--- a/relay/auth-http.js
+++ b/relay/auth-http.js
@@ -87,12 +87,12 @@ export function createAuthHandler({ auth, publicOrigin }) {
} else {
const bearer = token(req, true);
const input = await body(req);
- json(res, 200, auth.revoke(bearer, input.id));
+ json(res, 200, await auth.revoke(bearer, input.id));
}
} catch (error) {
const code = error instanceof Refused ? error.code : 'INTERNAL_ERROR';
const message = error instanceof Refused ? error.message.replace(/^\[[^\]]+\] /, '') : 'the relay could not complete this request; retry or contact its coordinator';
- const status = code === 'AUTH_REQUIRED' ? 401 : ['NO_REPO_ACCESS', 'TOKEN_BOARD', 'TOKEN_NOT_OWNED', 'HUMAN_REQUIRED', 'BAD_ORIGIN'].includes(code) ? 403 : code === 'GITHUB_UNAVAILABLE' ? 503 : code === 'INTERNAL_ERROR' ? 500 : 400;
+ const status = code === 'BOARD_NOT_LINKED' ? 404 : code === 'AUTH_REQUIRED' ? 401 : ['NO_REPO_ACCESS', 'TOKEN_BOARD', 'TOKEN_NOT_OWNED', 'HUMAN_REQUIRED', 'BAD_ORIGIN', 'WRITE_REQUIRED'].includes(code) ? 403 : code === 'GITHUB_UNAVAILABLE' ? 503 : code === 'INTERNAL_ERROR' ? 500 : 400;
json(res, status, { error: { code, message, next: message } });
}
return true;
diff --git a/relay/auth.js b/relay/auth.js
index df9a719e..b10760a7 100644
--- a/relay/auth.js
+++ b/relay/auth.js
@@ -1,4 +1,4 @@
-/** Hash-only relay sessions and board credentials, authorized by current GitHub metadata [H8, H1]. */
+/** Hash-only relay sessions and board credentials, authorized by current GitHub roles [H8, H1, H13, H14]. */
import { createHash, randomBytes, randomUUID, timingSafeEqual } from 'node:crypto';
import { chmodSync, mkdirSync } from 'node:fs';
import { dirname } from 'node:path';
@@ -85,26 +85,37 @@ export function createRelayAuth({ database, github, now = Date.now, sessionTTL =
if (webFlows.size + deviceFlows.size >= 10_000) throw new Refused('SIGN_IN_BUSY', 'too many sign-ins are pending; retry shortly');
}
- /** Require GitHub-confirmed access, with a ten-minute read cache and a fresh check for writes. */
+ /** Hide public boards below triage, and refuse actions below write without hiding readable private boards. */
+ function authorize(permission, linked, write) {
+ if (!permission.allowed) {
+ if (permission.hidden) throw new Refused('BOARD_NOT_LINKED', 'link this board to a readable GitHub repository before using it');
+ throw new Refused('NO_REPO_ACCESS', 'GitHub cannot confirm your repository access; ask for access and retry');
+ }
+ if (write && !['write', 'maintain', 'admin'].includes(permission.role)) throw new Refused('WRITE_REQUIRED', 'board actions and token management need repository write access; ask its owner for access');
+ return { ...linked, permission: permission.role, public: permission.public };
+ }
+
+ /** Require current repository roles; cache visibility for ten minutes but never authorize a write from cache. */
async function access(row, board, write) {
const linked = db.prepare('SELECT * FROM relay_boards WHERE id=?').get(board);
if (!linked) throw new Refused('BOARD_NOT_LINKED', 'link this board to a readable GitHub repository before using it');
const key = row.user_id + ':' + board;
const prior = permissions.get(key);
const started = now();
- if (!write && prior && started >= prior.checked && started - prior.checked < ACCESS_WINDOW_MS) {
- if (!prior.allowed) throw new Refused('NO_REPO_ACCESS', 'GitHub cannot confirm your repository access; ask for access and retry');
- return linked;
- }
+ if (!write && prior && started >= prior.checked && started - prior.checked < ACCESS_WINDOW_MS) return authorize(prior, linked, false);
+ let repo;
try {
- const repo = await github.access({ id: row.user_id, login: row.login }, linked.repository);
+ repo = await github.access({ id: row.user_id, login: row.login }, linked.repository);
if (String(repo.id) !== linked.repository_id) throw new Refused('NO_REPO_ACCESS', 'the linked repository identity changed; ask the coordinator to link the correct board');
- if (!permissions.has(key) || permissions.get(key).checked <= started) permissions.set(key, { checked: started, allowed: true });
- return linked;
} catch (error) {
if (error.code === 'NO_REPO_ACCESS' && (!permissions.has(key) || permissions.get(key).checked <= started)) permissions.set(key, { checked: started, allowed: false });
throw error;
}
+ const canRead = ['read', 'triage', 'write', 'maintain', 'admin'].includes(repo.permission);
+ const hidden = repo.public && !['triage', 'write', 'maintain', 'admin'].includes(repo.permission);
+ const checked = { checked: started, allowed: canRead && !hidden, hidden, role: repo.permission, public: repo.public };
+ if (!permissions.has(key) || permissions.get(key).checked <= started) permissions.set(key, checked);
+ return authorize(checked, linked, write);
}
const auth = {
@@ -164,20 +175,22 @@ export function createRelayAuth({ database, github, now = Date.now, sessionTTL =
async authenticate(token, { board, write = false } = {}) {
const row = credential(token);
if (row.kind === 'board' && board !== row.board) throw new Refused('TOKEN_BOARD', 'use this token only on its named board; obtain another token for another board');
+ let repository;
if (board !== undefined) {
identifier(board, 'BAD_BOARD');
- await access(row, board, write);
+ repository = await access(row, board, write);
credential(token); // A revocation or expiry during the awaited provider call must take effect.
}
- return { id: row.id, kind: row.kind, user: { id: row.user_id, login: row.login }, ...(row.board ? { board: row.board, agent: row.agent } : {}), expires: row.expires };
+ return { id: row.id, kind: row.kind, user: { id: row.user_id, login: row.login }, ...(row.board ? { board: row.board, agent: row.agent } : {}), ...(repository ? { permission: repository.permission, public: repository.public } : {}), expires: row.expires };
},
- /** Accept a link only from a signed-in human with current repository read access. */
+ /** Accept a link only from a signed-in human with current repository write access. */
async linkBoard(token, board, repository) {
identifier(board, 'BAD_BOARD');
repositoryName(repository);
const row = credential(token);
if (row.kind !== 'session') throw new Refused('HUMAN_REQUIRED', 'sign in as a person to link a board');
const repo = await github.access({ id: row.user_id, login: row.login }, repository);
+ authorize({ allowed: true, hidden: false, role: repo.permission, public: repo.public }, {}, true);
credential(token);
open();
const existing = db.prepare('SELECT * FROM relay_boards WHERE id=?').get(board);
@@ -193,7 +206,7 @@ export function createRelayAuth({ database, github, now = Date.now, sessionTTL =
const visible = [];
for (const board of boards) {
try { await auth.authenticate(token, { board: board.id }); visible.push({ id: board.id, repository: board.repository }); }
- catch (error) { if (error.code !== 'NO_REPO_ACCESS') throw error; }
+ catch (error) { if (!['NO_REPO_ACCESS', 'BOARD_NOT_LINKED'].includes(error.code)) throw error; }
}
return visible;
},
@@ -208,11 +221,13 @@ export function createRelayAuth({ database, github, now = Date.now, sessionTTL =
return { ...issue(principal.user, 'board', board, agent, expiresIn), board, agent };
},
/** Revoke one owned credential; this never revokes an unrelated session or board token. */
- revoke(token, id) {
+ async revoke(token, id) {
const row = credential(token);
if (typeof id !== 'string') throw new Refused('TOKEN_NOT_OWNED', 'name one of your relay credentials to revoke');
- const target = db.prepare('SELECT id,user_id FROM relay_credentials WHERE id=?').get(id);
+ const target = db.prepare('SELECT id,user_id,kind,board FROM relay_credentials WHERE id=?').get(id);
if (!target || target.user_id !== row.user_id || (row.kind !== 'session' && row.id !== id)) throw new Refused('TOKEN_NOT_OWNED', 'revoke only your own credential; sign in as its owner');
+ if (target.kind === 'board') await auth.authenticate(token, { board: target.board, write: true });
+ credential(token);
db.prepare('UPDATE relay_credentials SET revoked=1 WHERE id=?').run(id);
return { id, revoked: true };
},
diff --git a/relay/github.js b/relay/github.js
index 597d7a80..775f1a3a 100644
--- a/relay/github.js
+++ b/relay/github.js
@@ -180,7 +180,7 @@ export function createGitHubClient({ clientId, clientSecret, callbackURL, oauthB
if (publicRepo.private !== false || !Number.isSafeInteger(publicRepo.id) || publicRepo.id <= 0) {
throw new Refused('NO_REPO_ACCESS', 'install the GitHub App on this repository and ask for repository access');
}
- return { id: String(publicRepo.id), name: repositoryName(publicRepo.full_name) };
+ return { id: String(publicRepo.id), name: repositoryName(publicRepo.full_name), public: true, permission: 'none' };
}
if (!Number.isSafeInteger(installation.id) || installation.id <= 0) throw new Refused('NO_REPO_ACCESS', 'GitHub did not confirm an App installation; install the App on this repository');
const cacheKey = String(installation.id) + ':' + name.toLowerCase();
@@ -199,14 +199,16 @@ export function createGitHubClient({ clientId, clientSecret, callbackURL, oauthB
try {
const repo = await request(api, path, { token: credential.token });
if (!Number.isSafeInteger(repo.id) || repo.id <= 0 || typeof repo.private !== 'boolean') throw new Refused('NO_REPO_ACCESS', 'GitHub did not confirm repository metadata; check the App installation');
- if (repo.private) {
- if (!user || !/^[A-Za-z0-9-]+$/.test(user.login)) throw new Refused('NO_REPO_ACCESS', 'sign in again to confirm your GitHub account');
- const permission = await request(api, path + '/collaborators/' + encodeURIComponent(user.login) + '/permission', { token: credential.token });
- if (String(permission.user?.id) !== String(user.id) || !['read', 'triage', 'write', 'maintain', 'admin'].includes(permission.permission)) {
- throw new Refused('NO_REPO_ACCESS', 'your GitHub account cannot read this repository; ask for access or sign in again');
- }
+ if (!user || !/^[A-Za-z0-9-]+$/.test(user.login)) throw new Refused('NO_REPO_ACCESS', 'sign in again to confirm your GitHub account');
+ let permission = 'none';
+ try {
+ const answer = await request(api, path + '/collaborators/' + encodeURIComponent(user.login) + '/permission', { token: credential.token });
+ if (String(answer.user?.id) === String(user.id) && ['read', 'triage', 'write', 'maintain', 'admin'].includes(answer.permission)) permission = answer.permission;
+ } catch (error) {
+ if (repo.private || error.code !== 'NO_REPO_ACCESS') throw error;
}
- return { id: String(repo.id), name: repositoryName(repo.full_name) };
+ if (repo.private && permission === 'none') throw new Refused('NO_REPO_ACCESS', 'your GitHub account cannot read this repository; ask for access or sign in again');
+ return { id: String(repo.id), name: repositoryName(repo.full_name), public: !repo.private, permission };
} catch (error) {
if (error.code === 'NO_REPO_ACCESS') installationTokens.delete(cacheKey);
throw error;
diff --git a/test/relay-fixture.js b/test/relay-fixture.js
index 6cccb90c..3cc7fba6 100644
--- a/test/relay-fixture.js
+++ b/test/relay-fixture.js
@@ -7,7 +7,7 @@ import { once } from 'node:events';
/** Start a provider with real signed JWT verification and one-use browser/device grants. */
export async function githubFixture(t) {
const { privateKey, publicKey } = generateKeyPairSync('rsa', { modulusLength: 2048 });
- const state = { access: true, public: false, installed: true, repositoryID: 100, accountID: 7, permissionAccountID: 7, deviceAuthorized: false, slow: false, redirect: null };
+ const state = { access: true, permission: 'write', public: false, installed: true, repositoryID: 100, accountID: 7, permissionAccountID: 7, deviceAuthorized: false, slow: false, redirect: null };
const calls = [];
const codes = new Map();
const userTokens = new Set();
@@ -101,7 +101,7 @@ export async function githubFixture(t) {
if (url.pathname === '/repos/fixture/repository') return reply(res, isInstallation || state.public ? 200 : 404, { id: state.repositoryID, full_name: 'fixture/repository', private: !state.public });
if (url.pathname === '/repos/fixture/repository/collaborators/fixture-user/permission') {
if (state.beforePermission) await state.beforePermission();
- return reply(res, isInstallation ? 200 : 403, { permission: state.access ? 'read' : 'none', user: { id: state.permissionAccountID } });
+ return reply(res, isInstallation ? 200 : 403, { permission: state.access ? state.permission : 'none', user: { id: state.permissionAccountID } });
}
return reply(res, 404, { message: 'not available' });
}
diff --git a/test/relay-github.test.js b/test/relay-github.test.js
index e580446c..c603fcd8 100644
--- a/test/relay-github.test.js
+++ b/test/relay-github.test.js
@@ -41,7 +41,7 @@ test('App metadata checks current private access and immutable account identity
const fixture = await githubFixture(t);
const client = createGitHubClient(fixture.config);
const user = { id: '7', login: 'fixture-user' };
- assert.deepEqual(await client.access(user, 'fixture/repository'), { id: '100', name: 'fixture/repository' });
+ assert.deepEqual(await client.access(user, 'fixture/repository'), { id: '100', name: 'fixture/repository', public: false, permission: 'write' });
fixture.state.access = false;
await assert.rejects(client.access(user, 'fixture/repository'), { code: 'NO_REPO_ACCESS' });
fixture.state.access = true;
diff --git a/test/relay-http.test.js b/test/relay-http.test.js
index 010b191d..3aef177e 100644
--- a/test/relay-http.test.js
+++ b/test/relay-http.test.js
@@ -108,3 +108,20 @@ test('HTTP errors are versioned and bounded; production cookies are Secure [H8,
assert.match(captured['Set-Cookie'], /; Secure$/);
assert.throws(() => createAuthHandler({ auth: box.auth, publicOrigin: 'http://relay.example.invalid' }), { code: 'RELAY_CONFIG' });
});
+
+test('plain public readers receive the exact missing-board HTTP refusal [H13,H14,H8]', async (t) => {
+ const box = await relay(t);
+ box.state.public = true;
+ const signed = await browserLogin(box);
+ const headers = { cookie: signed.cookie, origin: box.origin, 'Content-Type': 'application/json' };
+ const linked = await fetch(box.origin + '/auth/boards/link', { method: 'POST', headers, body: JSON.stringify({ board: 'alpha', repository: 'fixture/repository' }) });
+ assert.equal(linked.status, 200);
+ box.state.permission = 'none';
+ const known = await fetch(box.origin + '/auth/tokens', { method: 'POST', headers, body: JSON.stringify({ board: 'alpha', agent: 'worker' }) });
+ const absent = await fetch(box.origin + '/auth/tokens', { method: 'POST', headers, body: JSON.stringify({ board: 'missing', agent: 'worker' }) });
+ assert.equal(known.status, 404);
+ assert.equal(absent.status, 404);
+ assert.deepEqual(await known.json(), await absent.json());
+ const visible = await fetch(box.origin + '/auth/boards', { headers: { cookie: signed.cookie } });
+ assert.deepEqual((await visible.json()).boards, []);
+});
diff --git a/test/relay.test.js b/test/relay.test.js
index a44230c0..e5bec2e8 100644
--- a/test/relay.test.js
+++ b/test/relay.test.js
@@ -98,7 +98,7 @@ test('credentials are hash-only, expire, revoke individually and survive relay r
const first = await box.auth.issueToken(signed.token, { board: 'alpha', agent: 'one', expiresIn: 10_000 });
const second = await box.auth.issueToken(signed.token, { board: 'alpha', agent: 'two', expiresIn: 20_000 });
assert.notEqual(first.token, second.token);
- box.auth.revoke(signed.token, first.id);
+ await box.auth.revoke(signed.token, first.id);
await assert.rejects(box.auth.authenticate(first.token, { board: 'alpha' }), { code: 'AUTH_REQUIRED' });
assert.equal((await box.auth.authenticate(second.token, { board: 'alpha' })).agent, 'two');
const persisted = readFileSync(box.database);
@@ -129,7 +129,7 @@ test('revoking during an awaited GitHub write check refuses the pending write [H
box.state.beforePermission = async () => { entered(); await held; };
const pending = box.auth.authenticate(signed.token, { board: 'alpha', write: true });
await started;
- box.auth.revoke(signed.token, signed.id);
+ await box.auth.revoke(signed.token, signed.id);
release();
await assert.rejects(pending, { code: 'AUTH_REQUIRED' });
});
@@ -161,3 +161,72 @@ test('expired browser/device grants cannot create sessions and concurrent polls
assert.equal(results.filter(result => result.pending).length, 1);
await assert.rejects(box.auth.pollDevice(grant.ticket), { code: 'OAUTH_EXPIRED' });
});
+
+test('a public reader sees no board and cannot act, mint or revoke; refusal matches a missing board [H13,H14,H8]', async (t) => {
+ const box = await fixture(t);
+ box.state.public = true;
+ const signed = await login(box);
+ await box.auth.linkBoard(signed.token, 'alpha', 'fixture/repository');
+ const existing = await box.auth.issueToken(signed.token, { board: 'alpha', agent: 'worker' });
+ box.state.permission = 'none';
+ let missing;
+ await assert.rejects(box.auth.authenticate(signed.token, { board: 'absent', write: true }), error => {
+ missing = { code: error.code, message: error.message }; return error.code === 'BOARD_NOT_LINKED';
+ });
+ await assert.rejects(box.auth.authenticate(signed.token, { board: 'alpha', write: true }), error => {
+ assert.deepEqual({ code: error.code, message: error.message }, missing); return true;
+ });
+ assert.deepEqual(await box.auth.boardsFor(signed.token), []);
+ await assert.rejects(box.auth.issueToken(signed.token, { board: 'alpha', agent: 'second' }), { code: 'BOARD_NOT_LINKED' });
+ await assert.rejects(box.auth.revoke(signed.token, existing.id), { code: 'BOARD_NOT_LINKED' });
+ box.state.permission = 'write';
+ assert.equal((await box.auth.authenticate(existing.token, { board: 'alpha', write: true })).agent, 'worker', 'denied revocation did not alter the token');
+});
+
+test('a public triage account sees its board but cannot act, mint or revoke its tokens [H13,H14,H8]', async (t) => {
+ const box = await fixture(t);
+ box.state.public = true;
+ const signed = await login(box);
+ await box.auth.linkBoard(signed.token, 'alpha', 'fixture/repository');
+ const existing = await box.auth.issueToken(signed.token, { board: 'alpha', agent: 'worker' });
+ box.state.permission = 'triage';
+ await assert.rejects(box.auth.authenticate(signed.token, { board: 'alpha', write: true }), { code: 'WRITE_REQUIRED' });
+ assert.deepEqual((await box.auth.boardsFor(signed.token)).map(board => board.id), ['alpha']);
+ assert.equal((await box.auth.authenticate(existing.token, { board: 'alpha' })).permission, 'triage');
+ await assert.rejects(box.auth.issueToken(signed.token, { board: 'alpha', agent: 'second' }), { code: 'WRITE_REQUIRED' });
+ await assert.rejects(box.auth.revoke(signed.token, existing.id), { code: 'WRITE_REQUIRED' });
+});
+
+test('write, maintain and admin accounts act and mint while private readers only see [H13,H14,H8]', async (t) => {
+ const box = await fixture(t);
+ const signed = await login(box);
+ await box.auth.linkBoard(signed.token, 'alpha', 'fixture/repository');
+ let existing;
+ for (const permission of ['write', 'maintain', 'admin']) {
+ box.state.permission = permission;
+ assert.equal((await box.auth.authenticate(signed.token, { board: 'alpha', write: true })).permission, permission);
+ existing = await box.auth.issueToken(signed.token, { board: 'alpha', agent: 'worker' });
+ assert.equal((await box.auth.authenticate(existing.token, { board: 'alpha', write: true })).permission, permission);
+ await box.auth.revoke(signed.token, existing.id);
+ await assert.rejects(box.auth.authenticate(existing.token, { board: 'alpha' }), { code: 'AUTH_REQUIRED' });
+ }
+ box.state.permission = 'write';
+ existing = await box.auth.issueToken(signed.token, { board: 'alpha', agent: 'reader' });
+ box.state.permission = 'read';
+ await assert.rejects(box.auth.authenticate(signed.token, { board: 'alpha', write: true }), { code: 'WRITE_REQUIRED' });
+ assert.equal((await box.auth.boardsFor(signed.token)).length, 1);
+ assert.equal((await box.auth.authenticate(existing.token, { board: 'alpha' })).permission, 'read');
+ await assert.rejects(box.auth.issueToken(signed.token, { board: 'alpha', agent: 'no' }), { code: 'WRITE_REQUIRED' });
+ await assert.rejects(box.auth.revoke(signed.token, existing.id), { code: 'WRITE_REQUIRED' });
+});
+
+test('an explicit read role still hides a public board below triage [H13,H14,H8]', async (t) => {
+ const box = await fixture(t);
+ box.state.public = true;
+ const signed = await login(box);
+ await box.auth.linkBoard(signed.token, 'alpha', 'fixture/repository');
+ box.state.permission = 'read';
+ assert.deepEqual(await box.auth.boardsFor(signed.token), []);
+ await assert.rejects(box.auth.authenticate(signed.token, { board: 'alpha', write: true }), { code: 'BOARD_NOT_LINKED' });
+ await assert.rejects(box.auth.issueToken(signed.token, { board: 'alpha', agent: 'worker' }), { code: 'BOARD_NOT_LINKED' });
+});