Skip to content

feat: support opening local files - #10

Merged
sleepinginsummer merged 3 commits into
sleepinginsummer:mainfrom
Xichun123:feat/open-local-files
Jul 16, 2026
Merged

feat: support opening local files#10
sleepinginsummer merged 3 commits into
sleepinginsummer:mainfrom
Xichun123:feat/open-local-files

Conversation

@Xichun123

@Xichun123 Xichun123 commented Jul 12, 2026

Copy link
Copy Markdown
Contributor

Summary

  • add first-class open support for local filesystem paths and absolute file:// URLs
  • make file tabs discoverable and controllable through the Chrome extension
  • add structured errors, extension permission/version checks, and file-tab readiness waiting
  • protect the localhost API with a per-daemon token and Origin validation, and restrict WebSocket handshakes to Chrome extension origins
  • report per-profile file-scheme capability in status/doctor
  • document local-file usage in the bundled skill and add cross-platform CI

Behavior

open now accepts:

  • explicit http:// and https:// URLs
  • absolute file:// URLs
  • absolute local paths
  • ./, ../, and ~/ paths
  • existing bare paths, which take precedence over implicit web navigation

Local paths are resolved from the invoking CLI's working directory, canonicalized through symlinks, checked to be regular files, and converted to correctly encoded file URLs. Directories, missing files, foreign-platform paths, unsupported schemes, stale extensions, and disabled file-URL access return stable error_code values.

The extension is bumped to 2.1. File tabs are included only when Chrome's “Allow access to file URLs” permission is enabled. Active opens become the daemon's default session; background opens do not.

Compatibility

  • the daemon accepts both target and the legacy url field
  • the new CLI temporarily sends both fields with the same value
  • new CLI → old daemon remains supported
  • old CLI → new daemon is not supported because the new daemon requires x-agent-browser-token authentication
  • direct HTTP API clients must read the token from ~/.agent-browser-cli/api-token and send it in the x-agent-browser-token header
  • explicit HTTP(S) URL strings are validated but otherwise preserved

Verification

  • cargo fmt --all -- --check
  • cargo clippy --all-targets --all-features -- -D warnings
  • cargo test --all-targets --all-features — 25 tests passed
  • cargo build --release
  • syntax checks for extension JavaScript files
  • real Chrome E2E with a Unicode/space-containing local HTML path:
    • open
    • scan
    • snapshot
    • click
    • exec
  • real Chrome open of a local PDF

@sleepinginsummer sleepinginsummer left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

建议合并前处理以下两个问题:

  1. [P1] 本地文件能力缺少 API 访问保护

src/server.rs 当前使用 CorsLayer::permissive(),而 /open 接受调用方提供的绝对路径和 cwd。当扩展开启 file URL 权限后,任意网页都可以请求 localhost API,探测本地文件是否存在、打开文件,并结合现有 /exec 接口读取页面内容。仅绑定 127.0.0.1 不能阻止浏览器跨域请求。建议在本 PR 中为 API 增加随机 token,并校验 Origin,再开放本地文件能力。

  1. [P1] 多浏览器场景可能关联到错误标签页

normalize_open_result 通过 find_session_key_by_tab_id 在全部 session 中仅按 tab_id 查找。Chrome 的 tab_id 只在单个浏览器实例内唯一;多个浏览器出现相同 ID 时可能命中错误 session,造成 readiness 误判和默认 session 切换错误。建议优先从 executor_session_key 提取 browser_id/profile_id,用完整 session key 定位,并补充两个浏览器具有相同 tab_id 的测试。

本地验证:cargo test --all-targets --all-features 的 17 个测试均通过。

@Xichun123

Copy link
Copy Markdown
Contributor Author

已按建议完成两项修改:

  • HTTP API 增加随机 token 鉴权和 Origin 校验,并移除 permissive CORS。
  • 打开标签页改用 executor 的完整 session key 定位,并补充跨浏览器重复 tab_id 测试。

cargo test --all-targets --all-features 已通过(23 tests),Clippy 和 release build 也通过。请再帮忙 review,谢谢。

@sleepinginsummer sleepinginsummer left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

复查最新提交 a10f798 后,之前的 API 鉴权和多浏览器 tab_id 问题已经修复,测试也已补充。还有两个问题建议合并前处理:

  1. [P1] WebSocket 入口仍可被网页伪装成扩展

HTTP API 已增加 token 和 Origin 校验,但扩展使用的 WebSocket 服务仍未校验 Origin 或身份。普通网页可以直接连接 localhost WebSocket,发送 ext_ready 注册伪造 session,并接收后续 CLI 指令;本 PR 打开本地文件时还会把规范化后的本地路径发送到该连接。建议 WebSocket 握手同样校验扩展来源,或使用独立认证 token,并增加拒绝非扩展 Origin/无 token 连接的测试。

  1. [P2] old CLI -> new daemon 兼容性被破坏

新中间件要求所有 API 请求携带 x-agent-browser-token,旧版 CLI 不会发送该请求头,因此调用新版 daemon 会统一返回 401。这与 PR 描述中的 old CLI -> new daemon 兼容性不一致。安全修复应保留,但需要明确取消该兼容承诺并更新文档/版本策略,或者提供受控的升级兼容方案。

本地验证:cargo test --all-targets --all-features 23 个测试通过,cargo clippy --all-targets --all-features 通过。

@Xichun123

Copy link
Copy Markdown
Contributor Author

已继续处理复查意见:

  • WebSocket 握手现在仅接受合法的 chrome-extension://<extension-id> Origin;缺失或非扩展 Origin 会在 upgrade 前返回 403,并补充了握手级测试。
  • 已用 Chrome 2.1 扩展实际重连验证,连接和标签页读取正常。
  • 关于兼容性,PR 描述已明确更新为:new CLI → old daemon 支持;old CLI → new daemon 不支持,因为新版 daemon 强制要求 token。

cargo test --all-targets --all-features 已通过(25 tests),Clippy 和 release build 也通过。请再帮忙 review,谢谢。

@sleepinginsummer sleepinginsummer left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

最新提交已解决 API 鉴权、WebSocket Origin 校验和多浏览器 session 关联问题;跨平台 CI 全部通过。

@sleepinginsummer
sleepinginsummer merged commit 89b32cd into sleepinginsummer:main Jul 16, 2026
4 checks passed
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