Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
fix: verify forge refresh accounts before sharing jobs
[._.]/ Adam Eivyf8d8b1a12e0d
diff --git a/server/services/forgeScopeRefresh.js b/server/services/forgeScopeRefresh.jsindex d634b8cc0c..198f7c8797 100644--- a/server/services/forgeScopeRefresh.js+++ b/server/services/forgeScopeRefresh.js@@ -19,7 +19,7 @@* one would leave the merge failing, so that mismatch refuses up front.*/-import { spawn } from 'child_process';+import { spawn } from '../lib/childProcess.js';import { randomUUID } from 'crypto';import { execGh } from './github.js';import { REFRESHABLE_FORGE_SCOPES, forgeScopeRefreshCommand, isForgePersonalTokenRefusal } from '../lib/forgeAccessErrors.js';@@ -142,30 +142,45 @@ export async function startForgeScopeRefresh({ host, scopes, expectedAccount = n}const running = jobs.get(jobIdByHost.get(host));- if (running && !running.settled) return { jobId: running.id, host, scopes: running.scopes, adopted: true };+ if (running && !running.settled) {+ if ((running.expectedAccount || '').toLowerCase() !== (expectedAccount || '').toLowerCase()+ || scopes.some((scope) => !running.scopes.includes(scope))) {+ throw new ServerError(`A different token refresh is already in progress on ${host}. Finish or cancel it before starting this one.`, {+ status: 409, code: 'FORGE_REFRESH_IN_PROGRESS',+ });+ }+ // A reservation is not a usable job until its account check has succeeded.+ await running.ready;+ return { jobId: running.id, host, scopes: running.scopes, adopted: true };+ }// Reserve the host before the first await so two requests cannot both start a flow.- const job = { id: randomUUID(), host, scopes, clients: [], lastPayload: null, settled: false, canceled: false, child: null, timer: null, deviceCode: null };+ const job = { id: randomUUID(), host, scopes, expectedAccount, clients: [], lastPayload: null, settled: false, canceled: false, child: null, timer: null, deviceCode: null };jobs.set(job.id, job);jobIdByHost.set(host, job.id);- const active = expectedAccount ? await activeLogin(host) : null;- if (expectedAccount && active && active.toLowerCase() !== expectedAccount.toLowerCase()) {- jobs.delete(job.id);- if (jobIdByHost.get(host) === job.id) jobIdByHost.delete(host);- throw new ServerError(- `PortOS merges as ${expectedAccount} on ${host}, but gh's active account there is ${active}, and gh can only refresh the active one. `- + `Run gh auth switch -h ${host} -u ${expectedAccount}, then try again.`,- { status: 409, code: 'FORGE_ACCOUNT_NOT_ACTIVE', context: { host, expectedAccount, active } },- );- }- // Cancelled while the account check was in flight: never spawn.- if (job.canceled) {- finish(job, { type: 'canceled' });- return { jobId: job.id, host, scopes, adopted: false };- }-- spawnRefresh(job);+ job.ready = (async () => {+ const active = expectedAccount ? await activeLogin(host) : null;+ if (expectedAccount && !active) {+ throw new ServerError(`Could not verify gh's active account on ${host}. Check gh auth status -h ${host}, then try again.`, {+ status: 409, code: 'FORGE_ACCOUNT_UNVERIFIED',+ });+ }+ if (expectedAccount && active.toLowerCase() !== expectedAccount.toLowerCase()) {+ throw new ServerError(+ `PortOS merges as ${expectedAccount} on ${host}, but gh's active account there is ${active}, and gh can only refresh the active one. `+ + `Run gh auth switch -h ${host} -u ${expectedAccount}, then try again.`,+ { status: 409, code: 'FORGE_ACCOUNT_NOT_ACTIVE', context: { host, expectedAccount, active } },+ );+ }+ // Cancelled while the account check was in flight: never spawn.+ if (job.canceled) return finish(job, { type: 'canceled' });+ spawnRefresh(job);+ })().catch((error) => {+ finish(job, { type: 'error', error: error.message });+ throw error;+ });+ await job.ready;return { jobId: job.id, host, scopes, adopted: false };}diff --git a/server/services/forgeScopeRefresh.test.js b/server/services/forgeScopeRefresh.test.jsindex 235fe5bcac..5d70abe7d4 100644--- a/server/services/forgeScopeRefresh.test.js+++ b/server/services/forgeScopeRefresh.test.js@@ -2,7 +2,7 @@ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';import { EventEmitter } from 'events';const spawnMock = vi.fn();-vi.mock('child_process', () => ({ spawn: (...args) => spawnMock(...args) }));+vi.mock('../lib/childProcess.js', () => ({ spawn: (...args) => spawnMock(...args) }));vi.mock('./github.js', () => ({ execGh: vi.fn() }));import { execGh } from './github.js';@@ -126,6 +126,18 @@ describe('startForgeScopeRefresh', () => {await settle(first.jobId);});+ it('refuses to adopt a refresh for a different account or insufficient scopes', async () => {+ execGh.mockResolvedValue('owner-acct');+ const first = await startForgeScopeRefresh({ host: 'github.com', scopes: ['workflow'], expectedAccount: 'owner-acct' });++ await expect(startForgeScopeRefresh({ host: 'github.com', scopes: ['workflow'], expectedAccount: 'other-acct' }))+ .rejects.toMatchObject({ code: 'FORGE_REFRESH_IN_PROGRESS' });+ await expect(startForgeScopeRefresh({ host: 'github.com', scopes: ['workflow', 'gist'], expectedAccount: 'owner-acct' }))+ .rejects.toMatchObject({ code: 'FORGE_REFRESH_IN_PROGRESS' });+ expect(spawnMock).toHaveBeenCalledTimes(1);+ await settle(first.jobId);+ });+it('refuses a scope outside the closed set before spawning anything', async () => {await expect(startForgeScopeRefresh({ host: 'ghe.example.com', scopes: ['admin:org'] })).rejects.toMatchObject({ status: 400, code: 'FORGE_SCOPE_NOT_REFRESHABLE' });@@ -145,6 +157,35 @@ describe('startForgeScopeRefresh', () => {expect(spawnMock).not.toHaveBeenCalled();});+ it('refuses when the active account cannot be verified and releases the host reservation', async () => {+ execGh.mockRejectedValue(new Error('network unavailable'));++ await expect(startForgeScopeRefresh({ host: 'github.com', scopes: ['workflow'], expectedAccount: 'owner-acct' }))+ .rejects.toMatchObject({ status: 409, code: 'FORGE_ACCOUNT_UNVERIFIED' });+ expect(spawnMock).not.toHaveBeenCalled();++ execGh.mockResolvedValue('owner-acct');+ const { jobId, adopted } = await startForgeScopeRefresh({ host: 'github.com', scopes: ['workflow'], expectedAccount: 'owner-acct' });+ expect(adopted).toBe(false);+ await settle(jobId);+ });++ it('does not hand an adopting request a job whose account check later fails', async () => {+ let resolveLogin;+ execGh.mockImplementation(() => new Promise((resolve) => { resolveLogin = resolve; }));+ const options = { host: 'github.com', scopes: ['workflow'], expectedAccount: 'owner-acct' };+ const first = startForgeScopeRefresh(options);+ const second = startForgeScopeRefresh(options);+ const outcomes = Promise.allSettled([first, second]);++ resolveLogin('someone-else');+ expect(await outcomes).toEqual([+ expect.objectContaining({ status: 'rejected', reason: expect.objectContaining({ code: 'FORGE_ACCOUNT_NOT_ACTIVE' }) }),+ expect.objectContaining({ status: 'rejected', reason: expect.objectContaining({ code: 'FORGE_ACCOUNT_NOT_ACTIVE' }) }),+ ]);+ expect(spawnMock).not.toHaveBeenCalled();+ });+// gh refreshes only the host's ACTIVE login; refreshing it would leave a merge// that runs as another account failing with the same error.it('refuses when the account merges run as is not the host\'s active gh account', async () => {