mirror of
https://github.com/garrytan/gstack.git
synced 2026-05-16 09:12:13 +08:00
* fix: remove auth token from /health, secure extension bootstrap (CRITICAL-02 + HIGH-03) - Remove token from /health response (was leaked to any localhost process) - Write .auth.json to extension dir for Manifest V3 bootstrap - sidebar-agent reads token from state file via BROWSE_STATE_FILE env var - Remove getToken handler from extension (token via health broadcast) - Extension loads token before first health poll to prevent race condition * fix: require auth on cookie-picker data routes (CRITICAL-01) - Add Bearer token auth gate on all /cookie-picker/* data/action routes - GET /cookie-picker HTML page stays unauthenticated (UI shell) - Token embedded in served HTML for picker's fetch calls - CORS preflight now allows Authorization header * fix: add state file TTL and plaintext cookie warning (HIGH-02) - Add savedAt timestamp to state save output - Warn on load if state file older than 7 days - Auto-delete stale state files (>7 days) on server startup - Warning about plaintext cookie storage in save message * fix: innerHTML XSS in extension content script and sidepanel (MEDIUM-01) - content.js: replace innerHTML with createElement/textContent for ref panel - sidepanel.js: escape entry.command with escapeHtml() in activity feed - Both found by security audit + Codex adversarial red team * fix: symlink bypass in validateReadPath (MEDIUM-02) - Always resolve to absolute path first (fixes relative path bypass) - Use realpathSync to follow symlinks before boundary check - Throw on non-ENOENT realpathSync failures (explicit over silent) - Resolve SAFE_DIRECTORIES through realpathSync (macOS /tmp → /private/tmp) - Resolve directory part for non-existent files (ENOENT with symlinked parent) * fix: freeze hook symlink bypass and prefix collision (MEDIUM-03) - Add POSIX-portable path resolution (cd + pwd -P, works on macOS) - Fix prefix collision: /project-evil no longer matches /project freeze dir - Use trailing slash in boundary check to require directory boundary * fix: shell script injection in gstack-config and telemetry (MEDIUM-04) - gstack-config: validate keys (alphanumeric+underscore only) - gstack-config: use grep -F (fixed string) instead of -E (regex) - gstack-config: escape sed special chars in values, drop newlines - gstack-telemetry-log: sanitize REPO_SLUG and BRANCH via json_safe() * test: 20 security tests for audit remediation - server-auth: verify token removed from /health, auth on /refs, /activity/* - cookie-picker: auth required on data routes, HTML page unauthenticated - path-validation: symlink bypass blocked, realpathSync failure throws - gstack-config: regex key rejected, sed special chars preserved - state-ttl: savedAt timestamp, 7-day TTL warning - telemetry: branch/repo with quotes don't corrupt JSON - adversarial: sidepanel escapes entry.command, freeze prefix collision * chore: bump version and changelog (v0.13.1.0) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: tone down changelog — defense in depth, not catastrophic bugs Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
231 lines
8.5 KiB
TypeScript
231 lines
8.5 KiB
TypeScript
/**
|
|
* Cookie picker route handler — HTTP + Playwright glue
|
|
*
|
|
* Handles all /cookie-picker/* routes. Imports from cookie-import-browser.ts
|
|
* (decryption) and cookie-picker-ui.ts (HTML generation).
|
|
*
|
|
* Routes (no auth — localhost-only, accepted risk):
|
|
* GET /cookie-picker → serves the picker HTML page
|
|
* GET /cookie-picker/browsers → list installed browsers
|
|
* GET /cookie-picker/domains → list domains + counts for a browser
|
|
* POST /cookie-picker/import → decrypt + import cookies to Playwright
|
|
* POST /cookie-picker/remove → clear cookies for domains
|
|
* GET /cookie-picker/imported → currently imported domains + counts
|
|
*/
|
|
|
|
import type { BrowserManager } from './browser-manager';
|
|
import { findInstalledBrowsers, listProfiles, listDomains, importCookies, CookieImportError, type PlaywrightCookie } from './cookie-import-browser';
|
|
import { getCookiePickerHTML } from './cookie-picker-ui';
|
|
|
|
// ─── State ──────────────────────────────────────────────────────
|
|
// Tracks which domains were imported via the picker.
|
|
// /imported only returns cookies for domains in this Set.
|
|
// /remove clears from this Set.
|
|
const importedDomains = new Set<string>();
|
|
const importedCounts = new Map<string, number>();
|
|
|
|
// ─── JSON Helpers ───────────────────────────────────────────────
|
|
|
|
function corsOrigin(port: number): string {
|
|
return `http://127.0.0.1:${port}`;
|
|
}
|
|
|
|
function jsonResponse(data: any, opts: { port: number; status?: number }): Response {
|
|
return new Response(JSON.stringify(data), {
|
|
status: opts.status ?? 200,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Access-Control-Allow-Origin': corsOrigin(opts.port),
|
|
},
|
|
});
|
|
}
|
|
|
|
function errorResponse(message: string, code: string, opts: { port: number; status?: number; action?: string }): Response {
|
|
return jsonResponse(
|
|
{ error: message, code, ...(opts.action ? { action: opts.action } : {}) },
|
|
{ port: opts.port, status: opts.status ?? 400 },
|
|
);
|
|
}
|
|
|
|
// ─── Route Handler ──────────────────────────────────────────────
|
|
|
|
export async function handleCookiePickerRoute(
|
|
url: URL,
|
|
req: Request,
|
|
bm: BrowserManager,
|
|
authToken?: string,
|
|
): Promise<Response> {
|
|
const pathname = url.pathname;
|
|
const port = parseInt(url.port, 10) || 9400;
|
|
|
|
// CORS preflight
|
|
if (req.method === 'OPTIONS') {
|
|
return new Response(null, {
|
|
status: 204,
|
|
headers: {
|
|
'Access-Control-Allow-Origin': corsOrigin(port),
|
|
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
|
|
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
|
|
},
|
|
});
|
|
}
|
|
|
|
try {
|
|
// GET /cookie-picker — serve the picker UI
|
|
if (pathname === '/cookie-picker' && req.method === 'GET') {
|
|
const html = getCookiePickerHTML(port, authToken);
|
|
return new Response(html, {
|
|
status: 200,
|
|
headers: { 'Content-Type': 'text/html; charset=utf-8' },
|
|
});
|
|
}
|
|
|
|
// ─── Auth gate: all data/action routes below require Bearer token ───
|
|
if (authToken) {
|
|
const authHeader = req.headers.get('authorization');
|
|
if (!authHeader || authHeader !== `Bearer ${authToken}`) {
|
|
return new Response(JSON.stringify({ error: 'Unauthorized' }), {
|
|
status: 401,
|
|
headers: { 'Content-Type': 'application/json' },
|
|
});
|
|
}
|
|
}
|
|
|
|
// GET /cookie-picker/browsers — list installed browsers
|
|
if (pathname === '/cookie-picker/browsers' && req.method === 'GET') {
|
|
const browsers = findInstalledBrowsers();
|
|
return jsonResponse({
|
|
browsers: browsers.map(b => ({
|
|
name: b.name,
|
|
aliases: b.aliases,
|
|
})),
|
|
}, { port });
|
|
}
|
|
|
|
// GET /cookie-picker/profiles?browser=<name> — list profiles for a browser
|
|
if (pathname === '/cookie-picker/profiles' && req.method === 'GET') {
|
|
const browserName = url.searchParams.get('browser');
|
|
if (!browserName) {
|
|
return errorResponse("Missing 'browser' parameter", 'missing_param', { port });
|
|
}
|
|
const profiles = listProfiles(browserName);
|
|
return jsonResponse({ profiles }, { port });
|
|
}
|
|
|
|
// GET /cookie-picker/domains?browser=<name>&profile=<profile> — list domains + counts
|
|
if (pathname === '/cookie-picker/domains' && req.method === 'GET') {
|
|
const browserName = url.searchParams.get('browser');
|
|
if (!browserName) {
|
|
return errorResponse("Missing 'browser' parameter", 'missing_param', { port });
|
|
}
|
|
const profile = url.searchParams.get('profile') || 'Default';
|
|
const result = listDomains(browserName, profile);
|
|
return jsonResponse({
|
|
browser: result.browser,
|
|
domains: result.domains,
|
|
}, { port });
|
|
}
|
|
|
|
// POST /cookie-picker/import — decrypt + import to Playwright session
|
|
if (pathname === '/cookie-picker/import' && req.method === 'POST') {
|
|
let body: any;
|
|
try {
|
|
body = await req.json();
|
|
} catch {
|
|
return errorResponse('Invalid JSON body', 'bad_request', { port });
|
|
}
|
|
|
|
const { browser, domains, profile } = body;
|
|
if (!browser) return errorResponse("Missing 'browser' field", 'missing_param', { port });
|
|
if (!domains || !Array.isArray(domains) || domains.length === 0) {
|
|
return errorResponse("Missing or empty 'domains' array", 'missing_param', { port });
|
|
}
|
|
|
|
// Decrypt cookies from the browser DB
|
|
const result = await importCookies(browser, domains, profile || 'Default');
|
|
|
|
if (result.cookies.length === 0) {
|
|
return jsonResponse({
|
|
imported: 0,
|
|
failed: result.failed,
|
|
domainCounts: {},
|
|
message: result.failed > 0
|
|
? `All ${result.failed} cookies failed to decrypt`
|
|
: 'No cookies found for the specified domains',
|
|
}, { port });
|
|
}
|
|
|
|
// Add to Playwright context
|
|
const page = bm.getPage();
|
|
await page.context().addCookies(result.cookies);
|
|
|
|
// Track what was imported
|
|
for (const domain of Object.keys(result.domainCounts)) {
|
|
importedDomains.add(domain);
|
|
importedCounts.set(domain, (importedCounts.get(domain) || 0) + result.domainCounts[domain]);
|
|
}
|
|
|
|
console.log(`[cookie-picker] Imported ${result.count} cookies for ${Object.keys(result.domainCounts).length} domains`);
|
|
|
|
return jsonResponse({
|
|
imported: result.count,
|
|
failed: result.failed,
|
|
domainCounts: result.domainCounts,
|
|
}, { port });
|
|
}
|
|
|
|
// POST /cookie-picker/remove — clear cookies for domains
|
|
if (pathname === '/cookie-picker/remove' && req.method === 'POST') {
|
|
let body: any;
|
|
try {
|
|
body = await req.json();
|
|
} catch {
|
|
return errorResponse('Invalid JSON body', 'bad_request', { port });
|
|
}
|
|
|
|
const { domains } = body;
|
|
if (!domains || !Array.isArray(domains) || domains.length === 0) {
|
|
return errorResponse("Missing or empty 'domains' array", 'missing_param', { port });
|
|
}
|
|
|
|
const page = bm.getPage();
|
|
const context = page.context();
|
|
for (const domain of domains) {
|
|
await context.clearCookies({ domain });
|
|
importedDomains.delete(domain);
|
|
importedCounts.delete(domain);
|
|
}
|
|
|
|
console.log(`[cookie-picker] Removed cookies for ${domains.length} domains`);
|
|
|
|
return jsonResponse({
|
|
removed: domains.length,
|
|
domains,
|
|
}, { port });
|
|
}
|
|
|
|
// GET /cookie-picker/imported — currently imported domains + counts
|
|
if (pathname === '/cookie-picker/imported' && req.method === 'GET') {
|
|
const entries: Array<{ domain: string; count: number }> = [];
|
|
for (const domain of importedDomains) {
|
|
entries.push({ domain, count: importedCounts.get(domain) || 0 });
|
|
}
|
|
entries.sort((a, b) => b.count - a.count);
|
|
|
|
return jsonResponse({
|
|
domains: entries,
|
|
totalDomains: entries.length,
|
|
totalCookies: entries.reduce((sum, e) => sum + e.count, 0),
|
|
}, { port });
|
|
}
|
|
|
|
return new Response('Not found', { status: 404 });
|
|
} catch (err: any) {
|
|
if (err instanceof CookieImportError) {
|
|
return errorResponse(err.message, err.code, { port, status: 400, action: err.action });
|
|
}
|
|
console.error(`[cookie-picker] Error: ${err.message}`);
|
|
return errorResponse(err.message || 'Internal error', 'internal_error', { port, status: 500 });
|
|
}
|
|
}
|