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): serialize SSH device grant authorization [P6,H5]

Corey Olsona1f39183920f
diff --git a/relay/auth.js b/relay/auth.js
index 12cdf32a..bb71d742 100644
--- a/relay/auth.js
+++ b/relay/auth.js
@@ -274,45 +274,48 @@ export function createRelayAuth({ database, github, now = Date.now, sessionTTL =
flow.next = now() + flow.interval * 1000;
flow.busy = true;
let result;
- try { result = await github.pollDevice(flow.deviceCode); }
- catch (error) {
- if (['OAUTH_DENIED', 'OAUTH_EXPIRED'].includes(error.code)) {
+ try {
+ try { result = await github.pollDevice(flow.deviceCode); }
+ catch (error) {
+ if (['OAUTH_DENIED', 'OAUTH_EXPIRED'].includes(error.code)) {
+ deviceFlows.delete(key);
+ if (flow.confirmationTicket) deviceConfirmations.delete(hash(flow.confirmationTicket));
+ }
+ throw error;
+ }
+ if (flow.expires <= now()) {
deviceFlows.delete(key);
if (flow.confirmationTicket) deviceConfirmations.delete(hash(flow.confirmationTicket));
+ if (!flow.confirmed) discardDeviceSession(flow);
+ throw new Refused('OAUTH_EXPIRED', 'CLI sign-in expired; start again');
}
- throw error;
- }
- finally { flow.busy = false; }
- if (flow.expires <= now()) {
- deviceFlows.delete(key);
- if (flow.confirmationTicket) deviceConfirmations.delete(hash(flow.confirmationTicket));
- if (!flow.confirmed) discardDeviceSession(flow);
- throw new Refused('OAUTH_EXPIRED', 'CLI sign-in expired; start again');
- }
- if (flow.declined) {
- deviceFlows.delete(key);
- if (flow.confirmationTicket) deviceConfirmations.delete(hash(flow.confirmationTicket));
- throw new Refused('OAUTH_DENIED', 'the person declined to link this board; run pullboard relay on again to start a new sign-in');
- }
- if (result.pending) {
- if (result.slow) flow.interval += 5;
- if (Number.isFinite(result.interval) && result.interval > flow.interval) flow.interval = result.interval;
- flow.next = now() + flow.interval * 1000;
- return { pending: true, retryAfter: flow.interval };
- }
- if (flow.intent) {
- flow.session = await signedIn(result);
- if (flow.declined || flow.expires <= now()) {
- discardDeviceSession(flow);
+ if (flow.declined) {
deviceFlows.delete(key);
- deviceConfirmations.delete(hash(flow.confirmationTicket));
- throw new Refused(flow.declined ? 'OAUTH_DENIED' : 'OAUTH_EXPIRED', 'the person confirmation ended before sign-in completed; run pullboard relay on again');
+ if (flow.confirmationTicket) deviceConfirmations.delete(hash(flow.confirmationTicket));
+ throw new Refused('OAUTH_DENIED', 'the person declined to link this board; run pullboard relay on again to start a new sign-in');
}
- flow.expires = now() + 10 * 60_000;
- return { pending: true, pendingConfirmation: true, confirmationExpiresIn: 600, retryAfter: flow.interval };
+ if (result.pending) {
+ if (result.slow) flow.interval += 5;
+ if (Number.isFinite(result.interval) && result.interval > flow.interval) flow.interval = result.interval;
+ flow.next = now() + flow.interval * 1000;
+ return { pending: true, retryAfter: flow.interval };
+ }
+ if (flow.intent) {
+ flow.session = await signedIn(result);
+ if (flow.declined || flow.expires <= now()) {
+ discardDeviceSession(flow);
+ deviceFlows.delete(key);
+ deviceConfirmations.delete(hash(flow.confirmationTicket));
+ throw new Refused(flow.declined ? 'OAUTH_DENIED' : 'OAUTH_EXPIRED', 'the person confirmation ended before sign-in completed; run pullboard relay on again');
+ }
+ flow.expires = now() + 10 * 60_000;
+ return { pending: true, pendingConfirmation: true, confirmationExpiresIn: 600, retryAfter: flow.interval };
+ }
+ deviceFlows.delete(key);
+ return signedIn(result);
+ } finally {
+ flow.busy = false;
}
- deviceFlows.delete(key);
- return signedIn(result);
},
/** Start a cookie-bound browser visit for the separate one-use board confirmation nonce. */
openDeviceConfirmation(ticket) {
diff --git a/test/relay.test.js b/test/relay.test.js
index 181ce15a..d39a368b 100644
--- a/test/relay.test.js
+++ b/test/relay.test.js
@@ -216,6 +216,56 @@ test('a confirmation approved before its deadline can reach the SSH CLI just aft
assert.match(delivered.token, /^ps_[A-Za-z0-9_-]{43}$/u);
});
+test('device authorization stays serialized while GitHub confirms its user [P6, H5]', async (t) => {
+ const box = await fixture(t);
+ const intent = { board: 'ssh-board', boardName: 'fixture-board', host: 'fixture-host', repository: 'fixture/repository' };
+ const flow = await box.auth.beginDevice({ intent });
+ const originalUser = box.config.github.user;
+ let releaseLookup;
+ let announceLookup;
+ const lookupStarted = new Promise((resolve) => { announceLookup = resolve; });
+ const lookupHeld = new Promise((resolve) => { releaseLookup = resolve; });
+ let lookupCount = 0;
+ box.config.github.user = async (token) => {
+ lookupCount += 1;
+ if (lookupCount === 1) {
+ announceLookup();
+ await lookupHeld;
+ }
+ return originalUser(token);
+ };
+ box.state.deviceAuthorized = true;
+ box.advance(flow.interval * 1000);
+ let firstPoll;
+ try {
+ firstPoll = box.auth.pollDevice(flow.ticket);
+ await lookupStarted;
+ const providerPolls = () => box.calls.filter((call) => call.path === '/login/oauth/access_token').length;
+ assert.equal(providerPolls(), 1, 'the first provider poll produced the grant now being resolved');
+ box.advance(flow.interval * 1000);
+ const concurrent = await box.auth.pollDevice(flow.ticket);
+ assert.equal(concurrent.pending, true, 'a second CLI request waits while identity confirmation finishes');
+ assert.equal(concurrent.pendingConfirmation, undefined, 'the held session is not exposed as ready yet');
+ assert.equal(providerPolls(), 1, 'the concurrent CLI request cannot consume the provider grant again');
+
+ releaseLookup();
+ const pending = await firstPoll;
+ assert.equal(pending.pendingConfirmation, true);
+ assert.equal('token' in pending, false, 'the held session remains unavailable before person approval');
+ const browser = box.auth.openDeviceConfirmation(flow.confirmationTicket);
+ const status = box.auth.deviceConfirmationStatus(flow.confirmationTicket, browser.binding);
+ assert.equal(status.ready, true, 'the original browser confirmation remains attached to the authorized device flow');
+ assert.equal('token' in status, false, 'browser status never returns the held session credential');
+ const waiting = await box.auth.pollDevice(flow.ticket);
+ assert.equal(waiting.pendingConfirmation, true, 'the CLI keeps waiting for the person decision');
+ assert.equal('token' in waiting, false);
+ assert.equal(lookupCount, 1, 'one successful device grant creates one server-held user session');
+ } finally {
+ releaseLookup();
+ if (firstPoll) await firstPoll.catch(() => {});
+ }
+});
+
test('unreadable links are refused; agent tokens belong to exactly one board [H8, H1]', async (t) => {
const box = await fixture(t);
const signed = await login(box);