Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions browse/src/cookie-picker-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,23 @@ export function generatePickerCode(): string {
return code;
}

/** Return true while the picker still has a live code or session. */
export function hasActivePicker(): boolean {
const now = Date.now();

for (const [code, expiry] of pendingCodes) {
if (expiry > now) return true;
pendingCodes.delete(code);
}

for (const [session, expiry] of validSessions) {
if (expiry > now) return true;
validSessions.delete(session);
}

return false;
}

/** Extract session ID from the gstack_picker cookie. */
function getSessionFromCookie(req: Request): string | null {
const cookie = req.headers.get('cookie');
Expand Down
2 changes: 2 additions & 0 deletions browse/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -763,13 +763,15 @@ if (BROWSE_PARENT_PID > 0) {
try {
process.kill(BROWSE_PARENT_PID, 0); // signal 0 = existence check only, no signal sent
} catch {
if (hasActivePicker()) return;
console.log(`[browse] Parent process ${BROWSE_PARENT_PID} exited, shutting down`);
shutdown();
}
}, 15_000);
}

// ─── Command Sets (from commands.ts — single source of truth) ───
import { hasActivePicker } from './cookie-picker-routes';
import { READ_COMMANDS, WRITE_COMMANDS, META_COMMANDS } from './commands';
export { READ_COMMANDS, WRITE_COMMANDS, META_COMMANDS };

Expand Down
53 changes: 52 additions & 1 deletion browse/test/cookie-picker-routes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import { describe, test, expect } from 'bun:test';
import { handleCookiePickerRoute, generatePickerCode } from '../src/cookie-picker-routes';
import { handleCookiePickerRoute, generatePickerCode, hasActivePicker } from '../src/cookie-picker-routes';

// ─── Mock BrowserManager ──────────────────────────────────────

Expand Down Expand Up @@ -284,6 +284,57 @@ describe('cookie-picker-routes', () => {
});
});

describe('active picker tracking', () => {
test('one-time codes keep the picker active until consumed', async () => {
const realNow = Date.now;
Date.now = () => realNow() + 3_700_000;
try {
expect(hasActivePicker()).toBe(false); // clears any stale state from prior tests
} finally {
Date.now = realNow;
}

const { bm } = mockBrowserManager();
const code = generatePickerCode();
expect(hasActivePicker()).toBe(true);

const res = await handleCookiePickerRoute(
makeUrl(`/cookie-picker?code=${code}`),
new Request('http://127.0.0.1:9470', { method: 'GET' }),
bm,
'test-token',
);

expect(res.status).toBe(302);
expect(hasActivePicker()).toBe(true); // session is now active
});

test('picker becomes inactive after an invalid session probe clears expired state', async () => {
const { bm } = mockBrowserManager();
const session = await getSessionCookie(bm, 'test-token');
expect(hasActivePicker()).toBe(true);

const realNow = Date.now;
Date.now = () => realNow() + 3_700_000;
try {
const res = await handleCookiePickerRoute(
makeUrl('/cookie-picker'),
new Request('http://127.0.0.1:9470', {
method: 'GET',
headers: { 'Cookie': `gstack_picker=${session}` },
}),
bm,
'test-token',
);

expect(res.status).toBe(403);
expect(hasActivePicker()).toBe(false);
} finally {
Date.now = realNow;
}
});
});

describe('session cookie auth', () => {
test('valid session cookie grants HTML access', async () => {
const { bm } = mockBrowserManager();
Expand Down