Skip to content

feat(browser): expose cookies command for HttpOnly-aware reads#1839

Open
Benjamin-eecs wants to merge 2 commits into
jackwener:mainfrom
Benjamin-eecs:fix/browser-cookies-command
Open

feat(browser): expose cookies command for HttpOnly-aware reads#1839
Benjamin-eecs wants to merge 2 commits into
jackwener:mainfrom
Benjamin-eecs:fix/browser-cookies-command

Conversation

@Benjamin-eecs
Copy link
Copy Markdown
Contributor

Description

IPage.getCookies() already exists end-to-end on main: the daemon action 'cookies' is registered in src/browser/daemon-client.ts:24, the extension's handleCookies in extension/src/background.ts:1637 calls chrome.cookies.getAll(details) and returns the cookies including their httpOnly: c.httpOnly flag, and the BrowserCookie type in src/types.ts:14 exposes httpOnly?: boolean. Internal callers (src/cli.ts:1329 for browser analyze, src/pipeline/steps/download.ts:47,62 for the yt-dlp cookie export path) already rely on this. The capability just has no user-facing CLI verb, so agents driving the browser cannot read HttpOnly cookies that document.cookie / browser eval cannot reach. chrome.cookies.getAll is the documented way to surface HttpOnly cookies from MV3 and is the same path #1472 asked for under "httpOnly cookies via Network.getAllCookies".

Add opencli browser cookies --domain <d> | --url <u> as a thin wrapper over page.getCookies({ domain, url }). The command mirrors the read-only JSON envelope shape already used by browser console (session / captured_at / count / cookies). Scope is required: if neither --domain nor --url is provided, the command emits a missing_scope error envelope and exits with USAGE_ERROR. This matches the extension's own existing guard at extension/src/background.ts:1638 ("Cookie scope required: provide domain or url to avoid dumping all cookies") and keeps the verb from being a blanket cookie dump.

Related issue: refs #1472. This covers the second of the three use cases in that issue (HttpOnly cookies via the existing extension chrome.cookies path). The first (persistent request interception) and third (top-level await in browser eval) are out of scope for this PR.

Type of Change

  • 🐛 Bug fix
  • ✨ New feature
  • 🌐 New site adapter
  • 📝 Documentation
  • ♻️ Refactor
  • 🔧 CI / build / tooling

Checklist

  • I ran the checks relevant to this PR
  • I updated tests or docs if needed
  • I included output or screenshots when useful

Screenshots / Output

opencli browser <session> cookies --domain example.com returns the existing extension envelope unchanged, with httpOnly preserved per cookie. Sample shape:

{
  "session": "test",
  "captured_at": "2026-06-03T07:06:35.000Z",
  "count": 2,
  "cookies": [
    { "name": "session", "value": "abc", "domain": "example.com", "path": "/", "secure": true, "httpOnly": true },
    { "name": "theme", "value": "dark", "domain": "example.com", "path": "/", "secure": false, "httpOnly": false }
  ]
}

opencli browser <session> cookies (no scope) emits a missing_scope error envelope and sets a non-zero exit code, matching the existing extension-side guard and preventing accidental whole-profile dumps.

Two regression tests under describe('browser cookies command', ...) in src/cli.test.ts cover both paths against a mocked IPage.getCookies. Full src/cli.test.ts continues to pass: 164/164. Full unit sweep: 1159 passed / 1 skipped. npx tsc --noEmit clean. npm run build clean. cli-manifest.json untouched.

@Benjamin-eecs Benjamin-eecs marked this pull request as ready for review June 3, 2026 07:33
Copilot AI review requested due to automatic review settings June 3, 2026 07:33
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds a new opencli browser cookies subcommand to fetch scoped cookies from the active browser session, along with Vitest coverage.

Changes:

  • Introduce browser cookies CLI command with --domain / --url scoping and structured JSON output.
  • Add unit tests validating domain scoping, url scoping, empty results, and missing-scope error behavior.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
src/cli.ts Adds the browser cookies command implementation and JSON output envelope.
src/cli.test.ts Adds tests for the new command, including scoping and error cases.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/cli.ts
Comment on lines +1285 to +1290
console.log(JSON.stringify({
session: getPageSession(page),
captured_at: new Date().toISOString(),
count: cookies.length,
cookies,
}, null, 2));
Comment thread src/cli.test.ts
Comment on lines +2197 to +2198
describe('browser cookies command', () => {
const consoleLogSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
Comment thread src/cli.test.ts
Comment on lines +2267 to +2270
expect(browserState.page!.getCookies).not.toHaveBeenCalled();
expect(process.exitCode).toBeDefined();
const out = lastJsonLog();
expect(out.error.code).toBe('missing_scope');
Wire the existing IPage.getCookies() path to a user-facing CLI verb so agents can read HttpOnly cookies that document.cookie cannot reach.

Refs jackwener#1472
Only forward the truthy scope option so getCookies receives the same shape internal callers already use.
@Benjamin-eecs Benjamin-eecs force-pushed the fix/browser-cookies-command branch from 4cf5777 to e997ecd Compare June 4, 2026 10:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants