Compare commits
46
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5f50ca075c | ||
|
|
e7548c3eb5 | ||
|
|
b77a1e4568 | ||
|
|
7c499885e5 | ||
|
|
fc2621559b | ||
|
|
8b55c553e6 | ||
|
|
6b99d304b1 | ||
|
|
900a5b5cde | ||
|
|
ab9b8d96ca | ||
|
|
5c734c51b6 | ||
|
|
dc54855784 | ||
|
|
ed61be3359 | ||
|
|
54b61f4375 | ||
|
|
9ae82d620e | ||
|
|
8f67cff3e1 | ||
|
|
e70d841a94 | ||
|
|
6032deabd5 | ||
|
|
27dff19105 | ||
|
|
21d591ee65 | ||
|
|
a6b2f5a192 | ||
|
|
7a1ca90416 | ||
|
|
ad0fb424c3 | ||
|
|
f62e204038 | ||
|
|
6f4e63ba91 | ||
|
|
98622a7415 | ||
|
|
3d032f9e88 | ||
|
|
d027659571 | ||
|
|
44b6218ef9 | ||
|
|
e93acc68f8 | ||
|
|
d2a33cc005 | ||
|
|
c26afbaba6 | ||
|
|
ffb386e3af | ||
|
|
947d150561 | ||
|
|
06a29251a2 | ||
|
|
0eacec9b9f | ||
|
|
7159012173 | ||
|
|
1b3d41e579 | ||
|
|
dbf272ced7 | ||
|
|
64140879d5 | ||
|
|
d3bfd76c96 | ||
|
|
47dfe760be | ||
|
|
0db6604105 | ||
|
|
007fd1b27f | ||
|
|
3d1132af90 | ||
|
|
90ba44cd38 | ||
|
|
52f8ead0f2 |
@@ -15,6 +15,11 @@ jobs:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Setup Node.js
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version-file: .node-version
|
||||
|
||||
- name: Check version sync
|
||||
run: node scripts/check-version-sync.js
|
||||
|
||||
@@ -54,12 +59,10 @@ jobs:
|
||||
- name: Setup Node.js
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: 24
|
||||
node-version-file: .node-version
|
||||
|
||||
- name: Install pnpm
|
||||
uses: pnpm/action-setup@v4
|
||||
with:
|
||||
version: 10
|
||||
|
||||
- name: Install dependencies
|
||||
run: pnpm install --filter dashboard
|
||||
@@ -209,7 +212,7 @@ jobs:
|
||||
- name: Setup Node.js
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: 24
|
||||
node-version-file: .node-version
|
||||
|
||||
- name: Setup Rust toolchain
|
||||
uses: dtolnay/rust-toolchain@stable
|
||||
|
||||
@@ -0,0 +1,137 @@
|
||||
name: Release binaries
|
||||
|
||||
# Build per-platform binaries and attach them to the GitHub Release for the
|
||||
# pushed tag. No npm, no tokens — only the built-in GITHUB_TOKEN. Consumers
|
||||
# install with: curl -fsSL .../install.sh | sh
|
||||
on:
|
||||
push:
|
||||
tags:
|
||||
- 'v*'
|
||||
workflow_dispatch:
|
||||
inputs:
|
||||
tag:
|
||||
description: 'Existing tag to (re)build binaries for, e.g. v0.27.0-fork.12'
|
||||
required: true
|
||||
|
||||
permissions:
|
||||
contents: write
|
||||
|
||||
concurrency: release-binaries-${{ github.ref }}
|
||||
|
||||
jobs:
|
||||
build:
|
||||
name: Build ${{ matrix.name }}
|
||||
runs-on: ${{ matrix.os }}
|
||||
timeout-minutes: 30
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
include:
|
||||
- { name: Linux x64, os: ubuntu-latest, target: x86_64-unknown-linux-gnu, asset: agent-browser-linux-x64, use_zigbuild: true, ext: '' }
|
||||
- { name: Linux ARM64, os: ubuntu-latest, target: aarch64-unknown-linux-gnu, asset: agent-browser-linux-arm64, use_zigbuild: true, ext: '' }
|
||||
- { name: Linux musl x64, os: ubuntu-latest, target: x86_64-unknown-linux-musl, asset: agent-browser-linux-musl-x64, use_zigbuild: true, ext: '' }
|
||||
- { name: Linux musl ARM64, os: ubuntu-latest, target: aarch64-unknown-linux-musl, asset: agent-browser-linux-musl-arm64, use_zigbuild: true, ext: '' }
|
||||
- { name: Windows x64, os: ubuntu-latest, target: x86_64-pc-windows-gnu, asset: agent-browser-win32-x64, use_zigbuild: false, ext: '.exe' }
|
||||
- { name: macOS x64, os: macos-latest, target: x86_64-apple-darwin, asset: agent-browser-darwin-x64, use_zigbuild: false, ext: '' }
|
||||
- { name: macOS ARM64, os: macos-latest, target: aarch64-apple-darwin, asset: agent-browser-darwin-arm64, use_zigbuild: false, ext: '' }
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v6
|
||||
with:
|
||||
ref: ${{ github.event.inputs.tag || github.ref }}
|
||||
|
||||
- name: Setup Rust toolchain
|
||||
uses: dtolnay/rust-toolchain@stable
|
||||
with:
|
||||
targets: ${{ matrix.target }}
|
||||
|
||||
- name: Install cross-compilation tools (Linux)
|
||||
if: runner.os == 'Linux'
|
||||
run: |
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y gcc-aarch64-linux-gnu gcc-x86-64-linux-gnu mingw-w64
|
||||
|
||||
- name: Install cargo-zigbuild
|
||||
if: matrix.use_zigbuild
|
||||
run: |
|
||||
pip3 install ziglang
|
||||
cargo install cargo-zigbuild
|
||||
|
||||
- name: Configure Rust linkers
|
||||
if: runner.os == 'Linux'
|
||||
run: |
|
||||
mkdir -p ~/.cargo
|
||||
cat >> ~/.cargo/config.toml << 'EOF'
|
||||
[target.aarch64-unknown-linux-gnu]
|
||||
linker = "aarch64-linux-gnu-gcc"
|
||||
|
||||
[target.x86_64-pc-windows-gnu]
|
||||
linker = "x86_64-w64-mingw32-gcc"
|
||||
EOF
|
||||
|
||||
- name: Cache Rust build artifacts
|
||||
uses: Swatinem/rust-cache@v2
|
||||
with:
|
||||
workspaces: cli
|
||||
|
||||
- name: Build (zigbuild)
|
||||
if: matrix.use_zigbuild
|
||||
run: cargo zigbuild --release --manifest-path cli/Cargo.toml --target ${{ matrix.target }}
|
||||
|
||||
- name: Build (cargo)
|
||||
if: '!matrix.use_zigbuild'
|
||||
run: cargo build --release --manifest-path cli/Cargo.toml --target ${{ matrix.target }}
|
||||
|
||||
- name: Package (.tar.gz + .sha256)
|
||||
shell: bash
|
||||
run: |
|
||||
set -euo pipefail
|
||||
mkdir -p dist
|
||||
src="cli/target/${{ matrix.target }}/release/agent-browser${{ matrix.ext }}"
|
||||
# The binary inside every archive is named `agent-browser` (or .exe);
|
||||
# install.sh extracts that fixed name regardless of platform.
|
||||
cp "$src" "dist/agent-browser${{ matrix.ext }}"
|
||||
chmod +x "dist/agent-browser${{ matrix.ext }}" || true
|
||||
( cd dist
|
||||
tar czf "${{ matrix.asset }}.tar.gz" "agent-browser${{ matrix.ext }}"
|
||||
if command -v sha256sum >/dev/null 2>&1; then
|
||||
sha256sum "${{ matrix.asset }}.tar.gz" > "${{ matrix.asset }}.tar.gz.sha256"
|
||||
else
|
||||
shasum -a 256 "${{ matrix.asset }}.tar.gz" > "${{ matrix.asset }}.tar.gz.sha256"
|
||||
fi
|
||||
)
|
||||
|
||||
- name: Upload artifact
|
||||
uses: actions/upload-artifact@v7
|
||||
with:
|
||||
name: ${{ matrix.asset }}
|
||||
path: dist/${{ matrix.asset }}.tar.gz*
|
||||
retention-days: 3
|
||||
|
||||
release:
|
||||
name: Attach binaries to GitHub Release
|
||||
needs: build
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 10
|
||||
permissions:
|
||||
contents: write
|
||||
steps:
|
||||
- name: Download all artifacts
|
||||
uses: actions/download-artifact@v8
|
||||
with:
|
||||
path: dist
|
||||
merge-multiple: true
|
||||
|
||||
- name: List assets
|
||||
run: ls -la dist
|
||||
|
||||
- name: Attach to release
|
||||
uses: softprops/action-gh-release@v3
|
||||
with:
|
||||
tag_name: ${{ github.event.inputs.tag || github.ref_name }}
|
||||
files: |
|
||||
dist/*.tar.gz
|
||||
dist/*.tar.gz.sha256
|
||||
fail_on_unmatched_files: true
|
||||
# keep existing release notes if the release was created beforehand
|
||||
append_body: false
|
||||
@@ -1,331 +0,0 @@
|
||||
name: Release
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
workflow_dispatch:
|
||||
|
||||
concurrency: ${{ github.workflow }}-${{ github.ref }}
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
jobs:
|
||||
check-release:
|
||||
name: Check for new version
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 5
|
||||
permissions:
|
||||
contents: read
|
||||
outputs:
|
||||
should_release: ${{ steps.check.outputs.should_release }}
|
||||
needs_github_release: ${{ steps.check.outputs.needs_github_release }}
|
||||
version: ${{ steps.check.outputs.version }}
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Compare package.json version to npm and check GitHub release
|
||||
id: check
|
||||
run: |
|
||||
LOCAL_VERSION=$(node -p "require('./package.json').version")
|
||||
echo "Local version: $LOCAL_VERSION"
|
||||
|
||||
NPM_VERSION=$(npm view agent-browser version 2>/dev/null || echo "0.0.0")
|
||||
echo "npm version: $NPM_VERSION"
|
||||
|
||||
if [ "$LOCAL_VERSION" != "$NPM_VERSION" ]; then
|
||||
echo "Version changed: $NPM_VERSION -> $LOCAL_VERSION"
|
||||
echo "should_release=true" >> "$GITHUB_OUTPUT"
|
||||
echo "needs_github_release=true" >> "$GITHUB_OUTPUT"
|
||||
else
|
||||
echo "Version unchanged on npm, skipping build and publish"
|
||||
echo "should_release=false" >> "$GITHUB_OUTPUT"
|
||||
|
||||
# Check if GitHub release exists; it may be missing if a prior run
|
||||
# published to npm but failed before creating the release.
|
||||
TAG="v$LOCAL_VERSION"
|
||||
if gh release view "$TAG" &>/dev/null; then
|
||||
echo "GitHub release $TAG exists"
|
||||
echo "needs_github_release=false" >> "$GITHUB_OUTPUT"
|
||||
else
|
||||
echo "GitHub release $TAG is missing, will rebuild and create it"
|
||||
echo "needs_github_release=true" >> "$GITHUB_OUTPUT"
|
||||
fi
|
||||
fi
|
||||
echo "version=$LOCAL_VERSION" >> "$GITHUB_OUTPUT"
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
|
||||
build-binaries:
|
||||
name: Build ${{ matrix.name }}
|
||||
needs: check-release
|
||||
if: needs.check-release.outputs.should_release == 'true' || needs.check-release.outputs.needs_github_release == 'true'
|
||||
runs-on: ${{ matrix.os }}
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
include:
|
||||
- name: Linux x64
|
||||
os: ubuntu-latest
|
||||
target: x86_64-unknown-linux-gnu
|
||||
binary: agent-browser-linux-x64
|
||||
use_zigbuild: true
|
||||
- name: Linux ARM64
|
||||
os: ubuntu-latest
|
||||
target: aarch64-unknown-linux-gnu
|
||||
binary: agent-browser-linux-arm64
|
||||
use_zigbuild: true
|
||||
- name: Linux musl x64
|
||||
os: ubuntu-latest
|
||||
target: x86_64-unknown-linux-musl
|
||||
binary: agent-browser-linux-musl-x64
|
||||
use_zigbuild: true
|
||||
- name: Linux musl ARM64
|
||||
os: ubuntu-latest
|
||||
target: aarch64-unknown-linux-musl
|
||||
binary: agent-browser-linux-musl-arm64
|
||||
use_zigbuild: true
|
||||
- name: Windows x64
|
||||
os: ubuntu-latest
|
||||
target: x86_64-pc-windows-gnu
|
||||
binary: agent-browser-win32-x64.exe
|
||||
use_zigbuild: false
|
||||
- name: macOS x64
|
||||
os: macos-latest
|
||||
target: x86_64-apple-darwin
|
||||
binary: agent-browser-darwin-x64
|
||||
use_zigbuild: false
|
||||
- name: macOS ARM64
|
||||
os: macos-latest
|
||||
target: aarch64-apple-darwin
|
||||
binary: agent-browser-darwin-arm64
|
||||
use_zigbuild: false
|
||||
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Setup pnpm
|
||||
uses: pnpm/action-setup@v4
|
||||
with:
|
||||
version: 9
|
||||
|
||||
- name: Setup Node.js
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: '24'
|
||||
cache: pnpm
|
||||
|
||||
- name: Install npm dependencies
|
||||
run: pnpm install --frozen-lockfile
|
||||
|
||||
- name: Sync version
|
||||
run: pnpm run version:sync
|
||||
|
||||
- name: Build dashboard
|
||||
run: pnpm --filter dashboard build
|
||||
|
||||
- name: Setup Rust toolchain
|
||||
uses: dtolnay/rust-toolchain@stable
|
||||
with:
|
||||
targets: ${{ matrix.target }}
|
||||
|
||||
- name: Install cross-compilation tools (Linux)
|
||||
if: runner.os == 'Linux'
|
||||
run: |
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y gcc-aarch64-linux-gnu gcc-x86-64-linux-gnu mingw-w64
|
||||
|
||||
- name: Install cargo-zigbuild
|
||||
if: matrix.use_zigbuild
|
||||
run: |
|
||||
pip3 install ziglang
|
||||
cargo install cargo-zigbuild
|
||||
|
||||
- name: Configure Rust linkers
|
||||
if: runner.os == 'Linux'
|
||||
run: |
|
||||
mkdir -p ~/.cargo
|
||||
cat >> ~/.cargo/config.toml << 'EOF'
|
||||
[target.aarch64-unknown-linux-gnu]
|
||||
linker = "aarch64-linux-gnu-gcc"
|
||||
|
||||
[target.x86_64-pc-windows-gnu]
|
||||
linker = "x86_64-w64-mingw32-gcc"
|
||||
EOF
|
||||
|
||||
- name: Cache Rust build artifacts
|
||||
uses: Swatinem/rust-cache@v2
|
||||
with:
|
||||
workspaces: cli
|
||||
|
||||
- name: Build with zigbuild
|
||||
if: matrix.use_zigbuild
|
||||
run: cargo zigbuild --release --manifest-path cli/Cargo.toml --target ${{ matrix.target }}
|
||||
|
||||
- name: Build with cargo
|
||||
if: '!matrix.use_zigbuild'
|
||||
run: cargo build --release --manifest-path cli/Cargo.toml --target ${{ matrix.target }}
|
||||
|
||||
- name: Copy binary
|
||||
run: |
|
||||
mkdir -p artifacts
|
||||
if [[ "${{ matrix.target }}" == *"windows"* ]]; then
|
||||
cp cli/target/${{ matrix.target }}/release/agent-browser.exe artifacts/${{ matrix.binary }}
|
||||
else
|
||||
cp cli/target/${{ matrix.target }}/release/agent-browser artifacts/${{ matrix.binary }}
|
||||
chmod +x artifacts/${{ matrix.binary }}
|
||||
fi
|
||||
|
||||
- name: Upload artifact
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: ${{ matrix.binary }}
|
||||
path: artifacts/${{ matrix.binary }}
|
||||
retention-days: 7
|
||||
|
||||
publish:
|
||||
name: Publish to npm
|
||||
needs: [check-release, build-binaries]
|
||||
if: needs.check-release.outputs.should_release == 'true'
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 15
|
||||
environment: Release
|
||||
permissions:
|
||||
contents: read
|
||||
id-token: write
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Setup pnpm
|
||||
uses: pnpm/action-setup@v4
|
||||
with:
|
||||
version: 9
|
||||
|
||||
- name: Setup Node.js
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: '24'
|
||||
cache: pnpm
|
||||
registry-url: 'https://registry.npmjs.org'
|
||||
|
||||
- name: Install dependencies
|
||||
run: pnpm install --frozen-lockfile
|
||||
|
||||
- name: Download all binary artifacts
|
||||
uses: actions/download-artifact@v4
|
||||
with:
|
||||
path: artifacts/
|
||||
|
||||
- name: Move binaries to bin directory
|
||||
run: |
|
||||
mkdir -p bin
|
||||
find artifacts -type f -name 'agent-browser-*' -exec mv {} bin/ \;
|
||||
rm -rf artifacts
|
||||
chmod +x bin/agent-browser-* 2>/dev/null || true
|
||||
echo "Binaries in bin/:"
|
||||
ls -la bin/
|
||||
|
||||
- name: Verify all binaries exist
|
||||
run: |
|
||||
EXPECTED_BINARIES=(
|
||||
"agent-browser-linux-x64"
|
||||
"agent-browser-linux-arm64"
|
||||
"agent-browser-linux-musl-x64"
|
||||
"agent-browser-linux-musl-arm64"
|
||||
"agent-browser-win32-x64.exe"
|
||||
"agent-browser-darwin-x64"
|
||||
"agent-browser-darwin-arm64"
|
||||
)
|
||||
MIN_SIZE=100000
|
||||
ERRORS=0
|
||||
for binary in "${EXPECTED_BINARIES[@]}"; do
|
||||
if [ ! -f "bin/$binary" ]; then
|
||||
echo "ERROR: Missing bin/$binary"
|
||||
ERRORS=$((ERRORS + 1))
|
||||
else
|
||||
SIZE=$(stat -c%s "bin/$binary" 2>/dev/null || stat -f%z "bin/$binary")
|
||||
if [ "$SIZE" -lt "$MIN_SIZE" ]; then
|
||||
echo "ERROR: bin/$binary is too small ($SIZE bytes, expected >= $MIN_SIZE)"
|
||||
ERRORS=$((ERRORS + 1))
|
||||
else
|
||||
echo "OK: bin/$binary ($SIZE bytes)"
|
||||
fi
|
||||
fi
|
||||
done
|
||||
if [ "$ERRORS" -gt 0 ]; then
|
||||
echo "Error: $ERRORS binary issues found"
|
||||
exit 1
|
||||
fi
|
||||
echo "All 7 platform binaries present and valid"
|
||||
|
||||
- name: Publish to npm
|
||||
run: npm publish --provenance
|
||||
|
||||
github-release:
|
||||
name: Create GitHub Release
|
||||
needs: [check-release, build-binaries, publish]
|
||||
if: always() && needs.build-binaries.result == 'success' && needs.check-release.outputs.needs_github_release == 'true'
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 10
|
||||
permissions:
|
||||
contents: write
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Download all artifacts
|
||||
uses: actions/download-artifact@v4
|
||||
with:
|
||||
path: artifacts/
|
||||
|
||||
- name: Move binaries to bin directory
|
||||
run: |
|
||||
mkdir -p bin
|
||||
find artifacts -type f -name 'agent-browser-*' -exec mv {} bin/ \;
|
||||
rm -rf artifacts
|
||||
chmod +x bin/agent-browser-* 2>/dev/null || true
|
||||
ls -la bin/
|
||||
|
||||
- name: Verify binaries exist
|
||||
run: |
|
||||
BINARY_COUNT=$(ls bin/agent-browser-* 2>/dev/null | wc -l)
|
||||
if [ "$BINARY_COUNT" -lt 7 ]; then
|
||||
echo "Error: Expected 7 binaries, found $BINARY_COUNT"
|
||||
ls -la bin/
|
||||
exit 1
|
||||
fi
|
||||
echo "Found $BINARY_COUNT binaries"
|
||||
|
||||
- name: Extract changelog entry
|
||||
run: |
|
||||
VERSION="${{ needs.check-release.outputs.version }}"
|
||||
awk '/<!-- release:start -->/{found=1; next} /<!-- release:end -->/{found=0} found{print}' CHANGELOG.md > /tmp/release-notes.md
|
||||
|
||||
LINES=$(wc -l < /tmp/release-notes.md | tr -d ' ')
|
||||
if [ "$LINES" -lt 2 ]; then
|
||||
echo "Error: No release notes found between <!-- release:start --> and <!-- release:end --> markers in CHANGELOG.md"
|
||||
exit 1
|
||||
fi
|
||||
echo "Extracted release notes for $VERSION ($LINES lines)"
|
||||
|
||||
- name: Create GitHub Release
|
||||
run: |
|
||||
VERSION="${{ needs.check-release.outputs.version }}"
|
||||
TAG="v$VERSION"
|
||||
|
||||
if gh release view "$TAG" &>/dev/null; then
|
||||
echo "Release $TAG already exists, uploading assets..."
|
||||
gh release upload "$TAG" bin/agent-browser-* --clobber
|
||||
else
|
||||
echo "Creating release $TAG..."
|
||||
gh release create "$TAG" \
|
||||
--title "$TAG" \
|
||||
--notes-file /tmp/release-notes.md \
|
||||
bin/agent-browser-*
|
||||
fi
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
@@ -0,0 +1 @@
|
||||
24
|
||||
@@ -21,9 +21,20 @@ For basic usage, commands, and API reference, see the [upstream documentation](h
|
||||
## Install
|
||||
|
||||
```bash
|
||||
npm install -g agent-browser-stealth
|
||||
curl -fsSL https://raw.githubusercontent.com/leeguooooo/agent-browser-stealth/main/install.sh | sh
|
||||
```
|
||||
|
||||
Downloads the prebuilt binary for your platform from the latest [GitHub Release](https://github.com/leeguooooo/agent-browser-stealth/releases) and installs `agent-browser` (+ the `abs` alias). No npm, no tokens.
|
||||
|
||||
<details>
|
||||
<summary>Other ways to install</summary>
|
||||
|
||||
- **Pin a version:** `AGENT_BROWSER_VERSION=v0.27.0-fork.12 curl -fsSL https://raw.githubusercontent.com/leeguooooo/agent-browser-stealth/main/install.sh | sh`
|
||||
- **Custom location:** `AGENT_BROWSER_BIN_DIR=$HOME/bin curl -fsSL … | sh`
|
||||
- **Windows:** download `agent-browser-win32-x64.tar.gz` from the [Releases page](https://github.com/leeguooooo/agent-browser-stealth/releases) and put `agent-browser.exe` on your PATH.
|
||||
- **npm (legacy):** `npm install -g agent-browser-stealth` — still published, but GitHub Releases is the primary channel now.
|
||||
</details>
|
||||
|
||||
### Install the AI agent skills
|
||||
|
||||
The repo ships SKILL.md files for Claude Code, Cursor, etc. Pull them into the current project with [skills.sh](https://skills.sh):
|
||||
@@ -34,14 +45,39 @@ npx skills add leeguooooo/agent-browser-stealth
|
||||
|
||||
This drops `skills/agent-browser` (and the specialized `skill-data/{core,electron,slack,dogfood,agentcore,vercel-sandbox}`) into your project so your AI agent gets the right usage patterns and pre-approved bash permissions for `agent-browser`, `agent-browser-stealth`, and `abs`.
|
||||
|
||||
## Setup (one time)
|
||||
## Command names
|
||||
|
||||
Enable Chrome DevTools Protocol in your Chrome:
|
||||
`agent-browser`, `agent-browser-stealth`, and `abs` are **the same binary** —
|
||||
`abs` is just a short alias. There is no separate "stealth executable"; stealth
|
||||
is a runtime behavior (see [Anti-detection](#anti-detection) below), applied
|
||||
automatically based on whether you attach to your real Chrome or `--launch` a
|
||||
fresh one.
|
||||
|
||||
1. Open `chrome://inspect/#remote-debugging` in Chrome
|
||||
2. Toggle the switch on
|
||||
## Setup: connect to your Chrome
|
||||
|
||||
That's it. This setting persists across Chrome restarts.
|
||||
Attaching uses the Chrome DevTools Protocol, which Chrome only exposes when it is
|
||||
**launched with a remote-debugging port**. This is a startup flag, not a setting
|
||||
— the `chrome://inspect` toggle alone is **not** enough (it only enables target
|
||||
discovery, not the CDP attach).
|
||||
|
||||
**Recommended — fully quit Chrome, then relaunch with the port:**
|
||||
|
||||
```bash
|
||||
# macOS
|
||||
open -a "Google Chrome" --args --remote-debugging-port=9222
|
||||
# Linux
|
||||
google-chrome --remote-debugging-port=9222
|
||||
# Windows: add --remote-debugging-port=9222 to your Chrome shortcut's target
|
||||
```
|
||||
|
||||
Then run `agent-browser open <url>` — it auto-discovers the port and attaches.
|
||||
On first attach, **Chrome 136+ shows an "Allow remote debugging?" dialog — click
|
||||
Allow once** (it persists for that Chrome session).
|
||||
|
||||
**No setup / don't want to touch your real Chrome?** Use
|
||||
`agent-browser --launch open <url>` to spawn a fresh isolated stealth browser
|
||||
(full anti-detection patches applied; see below). This always works without any
|
||||
port setup and is what CI uses automatically.
|
||||
|
||||
## Usage
|
||||
|
||||
@@ -57,21 +93,32 @@ agent-browser screenshot ./page.png
|
||||
|
||||
The agent operates in your Chrome — you'll see tabs opening, pages loading, clicks happening in real time. You can take over at any point (e.g. solve a CAPTCHA), then let the agent continue.
|
||||
|
||||
### Standalone mode
|
||||
### Standalone mode (`--launch`)
|
||||
|
||||
If you need a separate browser (CI, testing, etc.):
|
||||
Spawn a separate browser instead of attaching to your running Chrome:
|
||||
|
||||
```bash
|
||||
# Throwaway: fresh, EMPTY profile — no cookies, no login (good for CI/testing)
|
||||
agent-browser --launch open https://example.com
|
||||
|
||||
# Keep your login: launch with your real Chrome profile (cookies/sessions intact)
|
||||
agent-browser --launch --profile auto open https://x.com/home
|
||||
# or name it explicitly: --profile Default / --profile "Profile 1"
|
||||
```
|
||||
|
||||
> ⚠️ Plain `--launch` (no `--profile`) uses a **temporary empty profile** — you will
|
||||
> NOT be logged into anything. For logged-in sites use `--profile auto` (picks the
|
||||
> Chrome profile you used most recently) or `--profile <name>`. agent-browser prints
|
||||
> a warning when you `--launch` without a profile.
|
||||
|
||||
In CI environments, standalone mode is used automatically.
|
||||
|
||||
## Anti-detection
|
||||
|
||||
When connected to your real Chrome, we inject **zero** JavaScript patches. Your browser's fingerprint is completely genuine.
|
||||
When connected to your real Chrome, we inject **zero** JavaScript patches. Your browser's fingerprint is completely genuine. The guiding rule is **native CDP/Chrome overrides over JS lies** — a re-defined getter is itself detectable; a native override isn't.
|
||||
|
||||
The only thing we do is call `Emulation.setAutomationOverride` via CDP to set `navigator.webdriver = false` at the native Chrome level — undetectable by lie-detection systems like CreepJS.
|
||||
- `navigator.webdriver = false` via `Emulation.setAutomationOverride` (native, undetectable by CreepJS-style lie tests).
|
||||
- **`Runtime.enable` is left OFF by default.** A live `Runtime` domain is a detectable CDP signal (the patchright/rebrowser "runtime leak") — even when attached to your real Chrome. We only enable it when you opt into console/error capture (see below). `click`, `fill`, `eval`, etc. work without it.
|
||||
|
||||
**Test results (connected to real Chrome):**
|
||||
|
||||
@@ -83,6 +130,17 @@ The only thing we do is call `Emulation.setAutomationOverride` via CDP to set `n
|
||||
|
||||
When using `--launch` mode (standalone browser), a full suite of 32 stealth patches is applied for headless Chrome.
|
||||
|
||||
### Tuning knobs (environment variables)
|
||||
|
||||
| Variable | Default | Effect |
|
||||
|---|---|---|
|
||||
| `AGENT_BROWSER_CAPTURE_CONSOLE` | off | Enable `Runtime` domain so `console` / `errors` capture page output. Off keeps the stealthiest profile. |
|
||||
| `AGENT_BROWSER_TIMEZONE` | unset | `--launch` only. An IANA id (e.g. `Asia/Tokyo`) sets the timezone natively (Intl + Date follow, no JS lie) to match a proxy; `auto` derives one from the locale. |
|
||||
| `AGENT_BROWSER_BLOCK_WEBRTC` | auto | `--launch` only. Auto-forces WebRTC through the proxy when one is set (no real-IP leak). `1` hides the local IP without a proxy; `0` opts out. |
|
||||
| `AGENT_BROWSER_HIDE_CANVAS` | off | `--launch` only. Adds session-stable canvas/audio fingerprint noise. Off by default (noise is itself a "lie"). |
|
||||
| `AGENT_BROWSER_ADAPTIVE_REF` | on | When a saved `@ref` moves and the role/name re-query fails, relocate it by fingerprint similarity (high score + clear margin required, else it fails loudly). `0` disables. |
|
||||
| `AGENT_BROWSER_CLICK_MODE` | _(auto)_ | Click strategy. Default scrolls the target into view, dispatches a coordinate click, and falls back to a DOM `.click()` if a floating layer occludes the point. `dom` always uses `.click()` (best for autocomplete/menu items that close on blur); `coord` is strict coordinate-only (hard-fail on occlusion). |
|
||||
|
||||
## Differences from upstream
|
||||
|
||||
Based on [agent-browser v0.27.0](https://github.com/vercel-labs/agent-browser). Changes:
|
||||
|
||||
Generated
+21
-1
@@ -45,7 +45,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "agent-browser-stealth"
|
||||
version = "0.27.0-fork.2"
|
||||
version = "0.27.0-fork.16"
|
||||
dependencies = [
|
||||
"aes-gcm",
|
||||
"async-trait",
|
||||
@@ -57,6 +57,7 @@ dependencies = [
|
||||
"hex",
|
||||
"hmac",
|
||||
"image",
|
||||
"include_dir",
|
||||
"libc",
|
||||
"regex-lite",
|
||||
"reqwest",
|
||||
@@ -1048,6 +1049,25 @@ version = "1.12.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e7c5cedc30da3a610cac6b4ba17597bdf7152cf974e8aab3afb3d54455e371c8"
|
||||
|
||||
[[package]]
|
||||
name = "include_dir"
|
||||
version = "0.7.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "923d117408f1e49d914f1a379a309cffe4f18c05cf4e3d12e613a15fc81bd0dd"
|
||||
dependencies = [
|
||||
"include_dir_macros",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "include_dir_macros"
|
||||
version = "0.7.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7cab85a7ed0bd5f0e76d93846e0147172bed2e2d3f859bcc33a8d9699cad1a75"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "indexmap"
|
||||
version = "2.13.0"
|
||||
|
||||
+2
-1
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "agent-browser-stealth"
|
||||
version = "0.27.0-fork.2"
|
||||
version = "0.27.0-fork.16"
|
||||
edition = "2021"
|
||||
description = "Fast browser automation CLI for AI agents"
|
||||
license = "Apache-2.0"
|
||||
@@ -19,6 +19,7 @@ serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
regex-lite = "0.1"
|
||||
dirs = "5.0"
|
||||
include_dir = "0.7"
|
||||
base64 = "0.22"
|
||||
getrandom = "0.2"
|
||||
tokio = { version = "1", features = ["rt-multi-thread", "macros", "net", "io-util", "time", "sync", "signal", "process"] }
|
||||
|
||||
+87
-3
@@ -614,17 +614,44 @@ fn parse_command_inner(args: &[String], flags: &Flags) -> Result<Value, ParseErr
|
||||
return Ok(cmd);
|
||||
}
|
||||
|
||||
// --gone / --hidden: wait for an element to leave the DOM or
|
||||
// become invisible. Useful after a click that's supposed to
|
||||
// close a dialog, so the next command fails fast instead of
|
||||
// racing into a half-rendered UI.
|
||||
let state_override = if rest.iter().any(|&s| s == "--gone" || s == "--detached") {
|
||||
Some("detached")
|
||||
} else if rest.iter().any(|&s| s == "--hidden") {
|
||||
Some("hidden")
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
// Default: selector or timeout
|
||||
if let Some(arg) = rest.first() {
|
||||
// First non-flag positional is selector or numeric timeout
|
||||
let positional = rest.iter().find(|&&s| !s.starts_with("--"));
|
||||
let timeout_ms = rest
|
||||
.iter()
|
||||
.position(|&s| s == "--timeout")
|
||||
.and_then(|idx| rest.get(idx + 1))
|
||||
.and_then(|s| s.parse::<u64>().ok());
|
||||
|
||||
if let Some(arg) = positional {
|
||||
if let Ok(timeout) = arg.parse::<u64>() {
|
||||
Ok(json!({ "id": id, "action": "wait", "timeout": timeout }))
|
||||
} else {
|
||||
Ok(json!({ "id": id, "action": "wait", "selector": arg }))
|
||||
let mut cmd = json!({ "id": id, "action": "wait", "selector": arg });
|
||||
if let Some(state) = state_override {
|
||||
cmd["state"] = json!(state);
|
||||
}
|
||||
if let Some(t) = timeout_ms {
|
||||
cmd["timeout"] = json!(t);
|
||||
}
|
||||
Ok(cmd)
|
||||
}
|
||||
} else {
|
||||
Err(ParseError::MissingArguments {
|
||||
context: "wait".to_string(),
|
||||
usage: "wait <selector|ms|--url|--load|--fn|--text>",
|
||||
usage: "wait <selector|ms> [--gone|--hidden] [--timeout ms]",
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1039,6 +1066,18 @@ fn parse_command_inner(args: &[String], flags: &Flags) -> Result<Value, ParseErr
|
||||
// === Get ===
|
||||
"get" => parse_get(&rest, &id),
|
||||
|
||||
// Top-level shortcuts for `get <x>` status reads — users naturally type
|
||||
// `agent-browser url` / `cdp-url` / `title` without the `get` prefix
|
||||
// (and expect `cdp-url`/`cdp_url` to work interchangeably).
|
||||
"url" | "cdp-url" | "cdp_url" | "title" | "html" | "text" | "value"
|
||||
| "count" | "box" | "styles" | "attr" => {
|
||||
let sub = if cmd == "cdp_url" { "cdp-url" } else { cmd };
|
||||
let mut get_args: Vec<&str> = Vec::with_capacity(rest.len() + 1);
|
||||
get_args.push(sub);
|
||||
get_args.extend_from_slice(&rest);
|
||||
parse_get(&get_args, &id)
|
||||
}
|
||||
|
||||
// === Is (state checks) ===
|
||||
"is" => parse_is(&rest, &id),
|
||||
|
||||
@@ -2724,6 +2763,7 @@ mod tests {
|
||||
provider: None,
|
||||
ignore_https_errors: false,
|
||||
allow_file_access: false,
|
||||
hide_scrollbars: true,
|
||||
device: None,
|
||||
auto_connect: false,
|
||||
force_launch: false,
|
||||
@@ -2739,6 +2779,7 @@ mod tests {
|
||||
cli_proxy: false,
|
||||
cli_proxy_bypass: false,
|
||||
cli_allow_file_access: false,
|
||||
cli_hide_scrollbars: false,
|
||||
cli_annotate: false,
|
||||
cli_download_path: false,
|
||||
cli_headed: false,
|
||||
@@ -5185,4 +5226,47 @@ mod tests {
|
||||
let cmd = parse_command(&args("find role button"), &default_flags()).unwrap();
|
||||
assert_eq!(cmd["subaction"], "click");
|
||||
}
|
||||
|
||||
// === wait --gone / --hidden ===
|
||||
|
||||
#[test]
|
||||
fn test_wait_selector_default_visible() {
|
||||
let cmd = parse_command(&args("wait .toast"), &default_flags()).unwrap();
|
||||
assert_eq!(cmd["action"], "wait");
|
||||
assert_eq!(cmd["selector"], ".toast");
|
||||
assert!(cmd.get("state").is_none(), "default state stays implicit");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_wait_selector_gone_sets_detached_state() {
|
||||
let cmd = parse_command(&args("wait .toast --gone"), &default_flags()).unwrap();
|
||||
assert_eq!(cmd["selector"], ".toast");
|
||||
assert_eq!(cmd["state"], "detached");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_wait_selector_hidden_sets_hidden_state() {
|
||||
let cmd = parse_command(&args("wait .toast --hidden"), &default_flags()).unwrap();
|
||||
assert_eq!(cmd["state"], "hidden");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_wait_gone_with_timeout() {
|
||||
let cmd = parse_command(
|
||||
&args("wait .modal --gone --timeout 2000"),
|
||||
&default_flags(),
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(cmd["selector"], ".modal");
|
||||
assert_eq!(cmd["state"], "detached");
|
||||
assert_eq!(cmd["timeout"], 2000);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_wait_numeric_timeout_still_works() {
|
||||
// `wait 500` keeps meaning "sleep 500ms", not "wait for selector 500"
|
||||
let cmd = parse_command(&args("wait 500"), &default_flags()).unwrap();
|
||||
assert_eq!(cmd["timeout"], 500);
|
||||
assert!(cmd.get("selector").is_none());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -412,6 +412,7 @@ pub struct DaemonOptions<'a> {
|
||||
pub proxy_password: Option<&'a str>,
|
||||
pub ignore_https_errors: bool,
|
||||
pub allow_file_access: bool,
|
||||
pub hide_scrollbars: bool,
|
||||
pub profile: Option<&'a str>,
|
||||
pub state: Option<&'a str>,
|
||||
pub provider: Option<&'a str>,
|
||||
@@ -476,6 +477,10 @@ fn apply_daemon_env(cmd: &mut Command, session: &str, opts: &DaemonOptions) {
|
||||
if opts.allow_file_access {
|
||||
cmd.env("AGENT_BROWSER_ALLOW_FILE_ACCESS", "1");
|
||||
}
|
||||
cmd.env(
|
||||
"AGENT_BROWSER_HIDE_SCROLLBARS",
|
||||
if opts.hide_scrollbars { "1" } else { "0" },
|
||||
);
|
||||
if let Some(prof) = opts.profile {
|
||||
cmd.env("AGENT_BROWSER_PROFILE", prof);
|
||||
}
|
||||
|
||||
@@ -65,6 +65,7 @@ pub(super) fn check(checks: &mut Vec<Check>) {
|
||||
proxy_password: None,
|
||||
ignore_https_errors: false,
|
||||
allow_file_access: false,
|
||||
hide_scrollbars: true,
|
||||
profile: None,
|
||||
state: None,
|
||||
provider: None,
|
||||
|
||||
+56
-1
@@ -70,6 +70,7 @@ pub struct Config {
|
||||
pub user_agent: Option<String>,
|
||||
pub provider: Option<String>,
|
||||
pub device: Option<String>,
|
||||
pub hide_scrollbars: Option<bool>,
|
||||
pub ignore_https_errors: Option<bool>,
|
||||
pub allow_file_access: Option<bool>,
|
||||
pub cdp: Option<String>,
|
||||
@@ -131,6 +132,7 @@ impl Config {
|
||||
user_agent: other.user_agent.or(self.user_agent),
|
||||
provider: other.provider.or(self.provider),
|
||||
device: other.device.or(self.device),
|
||||
hide_scrollbars: other.hide_scrollbars.or(self.hide_scrollbars),
|
||||
ignore_https_errors: other.ignore_https_errors.or(self.ignore_https_errors),
|
||||
allow_file_access: other.allow_file_access.or(self.allow_file_access),
|
||||
cdp: other.cdp.or(self.cdp),
|
||||
@@ -187,6 +189,12 @@ fn env_var_is_truthy(name: &str) -> bool {
|
||||
}
|
||||
}
|
||||
|
||||
fn env_var_bool(name: &str) -> Option<bool> {
|
||||
env::var(name)
|
||||
.ok()
|
||||
.map(|val| !matches!(val.to_lowercase().as_str(), "0" | "false" | "no" | ""))
|
||||
}
|
||||
|
||||
/// Parse an optional boolean value after a flag. Returns (value, consumed_next_arg).
|
||||
/// Recognizes "true" as true, "false" as false. Bare flag defaults to true.
|
||||
fn parse_bool_arg(args: &[String], i: usize) -> (bool, bool) {
|
||||
@@ -306,6 +314,7 @@ pub struct Flags {
|
||||
pub provider: Option<String>,
|
||||
pub ignore_https_errors: bool,
|
||||
pub allow_file_access: bool,
|
||||
pub hide_scrollbars: bool,
|
||||
pub device: Option<String>,
|
||||
pub auto_connect: bool,
|
||||
pub force_launch: bool,
|
||||
@@ -343,6 +352,7 @@ pub struct Flags {
|
||||
pub cli_proxy: bool,
|
||||
pub cli_proxy_bypass: bool,
|
||||
pub cli_allow_file_access: bool,
|
||||
pub cli_hide_scrollbars: bool,
|
||||
pub cli_annotate: bool,
|
||||
pub cli_download_path: bool,
|
||||
pub cli_headed: bool,
|
||||
@@ -443,6 +453,9 @@ pub fn parse_flags(args: &[String]) -> Flags {
|
||||
|| config.ignore_https_errors.unwrap_or(false),
|
||||
allow_file_access: env_var_is_truthy("AGENT_BROWSER_ALLOW_FILE_ACCESS")
|
||||
|| config.allow_file_access.unwrap_or(false),
|
||||
hide_scrollbars: env_var_bool("AGENT_BROWSER_HIDE_SCROLLBARS")
|
||||
.or(config.hide_scrollbars)
|
||||
.unwrap_or(true),
|
||||
device: env::var("AGENT_BROWSER_IOS_DEVICE").ok().or(config.device),
|
||||
auto_connect: !env_var_is_truthy("AGENT_BROWSER_NO_AUTO_CONNECT")
|
||||
&& (env_var_is_truthy("AGENT_BROWSER_AUTO_CONNECT")
|
||||
@@ -518,6 +531,7 @@ pub fn parse_flags(args: &[String]) -> Flags {
|
||||
cli_proxy: false,
|
||||
cli_proxy_bypass: false,
|
||||
cli_allow_file_access: false,
|
||||
cli_hide_scrollbars: false,
|
||||
cli_annotate: false,
|
||||
cli_download_path: false,
|
||||
cli_headed: false,
|
||||
@@ -677,6 +691,14 @@ pub fn parse_flags(args: &[String]) -> Flags {
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
"--hide-scrollbars" => {
|
||||
let (val, consumed) = parse_bool_arg(args, i);
|
||||
flags.hide_scrollbars = val;
|
||||
flags.cli_hide_scrollbars = true;
|
||||
if consumed {
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
"--device" => {
|
||||
if let Some(d) = args.get(i + 1) {
|
||||
flags.device = Some(d.clone());
|
||||
@@ -852,6 +874,7 @@ pub fn clean_args(args: &[String]) -> Vec<String> {
|
||||
"--debug",
|
||||
"--ignore-https-errors",
|
||||
"--allow-file-access",
|
||||
"--hide-scrollbars",
|
||||
"--auto-connect",
|
||||
"--launch",
|
||||
"--new",
|
||||
@@ -933,6 +956,7 @@ pub fn clean_args(args: &[String]) -> Vec<String> {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::test_utils::EnvGuard;
|
||||
|
||||
fn args(s: &str) -> Vec<String> {
|
||||
s.split_whitespace().map(String::from).collect()
|
||||
@@ -1176,6 +1200,7 @@ mod tests {
|
||||
"userAgent": "test-agent",
|
||||
"provider": "ios",
|
||||
"device": "iPhone 15",
|
||||
"hideScrollbars": false,
|
||||
"ignoreHttpsErrors": true,
|
||||
"allowFileAccess": true,
|
||||
"cdp": "9222",
|
||||
@@ -1201,6 +1226,7 @@ mod tests {
|
||||
assert_eq!(config.user_agent.as_deref(), Some("test-agent"));
|
||||
assert_eq!(config.provider.as_deref(), Some("ios"));
|
||||
assert_eq!(config.device.as_deref(), Some("iPhone 15"));
|
||||
assert_eq!(config.hide_scrollbars, Some(false));
|
||||
assert_eq!(config.ignore_https_errors, Some(true));
|
||||
assert_eq!(config.allow_file_access, Some(true));
|
||||
assert_eq!(config.cdp.as_deref(), Some("9222"));
|
||||
@@ -1454,6 +1480,33 @@ mod tests {
|
||||
assert!(flags.cli_allow_file_access);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_hide_scrollbars_default_true() {
|
||||
let guard = EnvGuard::new(&["AGENT_BROWSER_HIDE_SCROLLBARS"]);
|
||||
guard.remove("AGENT_BROWSER_HIDE_SCROLLBARS");
|
||||
let flags = parse_flags(&args("open example.com"));
|
||||
assert!(flags.hide_scrollbars);
|
||||
assert!(!flags.cli_hide_scrollbars);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_hide_scrollbars_false() {
|
||||
let guard = EnvGuard::new(&["AGENT_BROWSER_HIDE_SCROLLBARS"]);
|
||||
guard.remove("AGENT_BROWSER_HIDE_SCROLLBARS");
|
||||
let flags = parse_flags(&args("--hide-scrollbars false open"));
|
||||
assert!(!flags.hide_scrollbars);
|
||||
assert!(flags.cli_hide_scrollbars);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_hide_scrollbars_bare_defaults_true() {
|
||||
let guard = EnvGuard::new(&["AGENT_BROWSER_HIDE_SCROLLBARS"]);
|
||||
guard.remove("AGENT_BROWSER_HIDE_SCROLLBARS");
|
||||
let flags = parse_flags(&args("--hide-scrollbars open"));
|
||||
assert!(flags.hide_scrollbars);
|
||||
assert!(flags.cli_hide_scrollbars);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_auto_connect_false() {
|
||||
let flags = parse_flags(&args("--auto-connect false open"));
|
||||
@@ -1462,7 +1515,9 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_clean_args_removes_bool_flag_with_value() {
|
||||
let cleaned = clean_args(&args("--headed false --debug true open example.com"));
|
||||
let cleaned = clean_args(&args(
|
||||
"--headed false --debug true --hide-scrollbars false open example.com",
|
||||
));
|
||||
assert_eq!(cleaned, vec!["open", "example.com"]);
|
||||
}
|
||||
|
||||
|
||||
+78
-5
@@ -60,6 +60,23 @@ fn print_json_error_with_type(message: impl AsRef<str>, error_type: &str) {
|
||||
}));
|
||||
}
|
||||
|
||||
fn should_send_hide_scrollbars_launch_option(
|
||||
cli_hide_scrollbars: bool,
|
||||
hide_scrollbars: bool,
|
||||
) -> bool {
|
||||
cli_hide_scrollbars || !hide_scrollbars
|
||||
}
|
||||
|
||||
fn apply_hide_scrollbars_launch_option(
|
||||
launch_cmd: &mut serde_json::Value,
|
||||
cli_hide_scrollbars: bool,
|
||||
hide_scrollbars: bool,
|
||||
) {
|
||||
if should_send_hide_scrollbars_launch_option(cli_hide_scrollbars, hide_scrollbars) {
|
||||
launch_cmd["hideScrollbars"] = json!(hide_scrollbars);
|
||||
}
|
||||
}
|
||||
|
||||
struct ParsedProxy {
|
||||
server: String,
|
||||
username: Option<String>,
|
||||
@@ -514,6 +531,18 @@ fn main() {
|
||||
let mut flags = parse_flags(&args);
|
||||
let clean = clean_args(&args);
|
||||
|
||||
// Loudly warn when launching a fresh browser with no profile: it gets a
|
||||
// temporary EMPTY profile (no cookies / no login). For logged-in sites the
|
||||
// user almost always wants --profile auto (their real Chrome profile).
|
||||
// Skipped under CI (force_launch is implicit there and login isn't expected).
|
||||
if flags.force_launch && flags.profile.is_none() && env::var("CI").is_err() {
|
||||
eprintln!(
|
||||
"⚠ --launch uses a temporary EMPTY browser profile (no cookies, no login). \
|
||||
For logged-in sites, add `--profile auto` (or `--profile Default`) to reuse \
|
||||
your real Chrome session."
|
||||
);
|
||||
}
|
||||
|
||||
let has_help = args.iter().any(|a| a == "--help" || a == "-h");
|
||||
let has_version = args.iter().any(|a| a == "--version" || a == "-V");
|
||||
|
||||
@@ -741,6 +770,7 @@ fn main() {
|
||||
proxy_password: proxy_password.as_deref(),
|
||||
ignore_https_errors: flags.ignore_https_errors,
|
||||
allow_file_access: flags.allow_file_access,
|
||||
hide_scrollbars: flags.hide_scrollbars,
|
||||
profile: flags.profile.as_deref(),
|
||||
state: flags.state.as_deref(),
|
||||
provider: flags.provider.as_deref(),
|
||||
@@ -814,6 +844,7 @@ fn main() {
|
||||
},
|
||||
flags.ignore_https_errors.then_some("--ignore-https-errors"),
|
||||
flags.cli_allow_file_access.then_some("--allow-file-access"),
|
||||
flags.cli_hide_scrollbars.then_some("--hide-scrollbars"),
|
||||
flags.cli_download_path.then_some("--download-path"),
|
||||
flags.cli_headed.then_some("--headed"),
|
||||
]
|
||||
@@ -822,11 +853,24 @@ fn main() {
|
||||
.collect();
|
||||
|
||||
if !ignored_flags.is_empty() && !flags.json {
|
||||
eprintln!(
|
||||
"{} {} ignored: daemon already running. Use 'agent-browser close' first to restart with new options.",
|
||||
color::warning_indicator(),
|
||||
ignored_flags.join(", ")
|
||||
);
|
||||
// Special case: --headed is irrelevant in CDP-attach mode
|
||||
// (your existing Chrome is always already visible). The
|
||||
// "agent-browser close + reopen" advice doesn't help because
|
||||
// the new daemon will attach right back to the same Chrome.
|
||||
// Don't suggest a useless workaround.
|
||||
if ignored_flags == ["--headed"] {
|
||||
eprintln!(
|
||||
"{} --headed has no effect when attached to your running Chrome (it's already visible). \
|
||||
Pass --launch to spawn a separate browser if you need to control headedness.",
|
||||
color::warning_indicator(),
|
||||
);
|
||||
} else {
|
||||
eprintln!(
|
||||
"{} {} ignored: daemon already running. Use 'agent-browser close' first to restart with new options.",
|
||||
color::warning_indicator(),
|
||||
ignored_flags.join(", ")
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1049,6 +1093,10 @@ fn main() {
|
||||
|| flags.args.is_some()
|
||||
|| flags.user_agent.is_some()
|
||||
|| flags.allow_file_access
|
||||
|| should_send_hide_scrollbars_launch_option(
|
||||
flags.cli_hide_scrollbars,
|
||||
flags.hide_scrollbars,
|
||||
)
|
||||
|| flags.color_scheme.is_some()
|
||||
|| flags.download_path.is_some()
|
||||
|| flags.engine.is_some()
|
||||
@@ -1123,6 +1171,12 @@ fn main() {
|
||||
launch_cmd["allowFileAccess"] = json!(true);
|
||||
}
|
||||
|
||||
apply_hide_scrollbars_launch_option(
|
||||
&mut launch_cmd,
|
||||
flags.cli_hide_scrollbars,
|
||||
flags.hide_scrollbars,
|
||||
);
|
||||
|
||||
if let Some(ref cs) = flags.color_scheme {
|
||||
launch_cmd["colorScheme"] = json!(cs);
|
||||
}
|
||||
@@ -1475,4 +1529,23 @@ mod tests {
|
||||
"Daemon process exited during startup:\nline \"quoted\"\u{001b}[2mansi\u{001b}[22m"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_hide_scrollbars_launch_option_serialization() {
|
||||
assert!(!should_send_hide_scrollbars_launch_option(false, true));
|
||||
assert!(should_send_hide_scrollbars_launch_option(false, false));
|
||||
assert!(should_send_hide_scrollbars_launch_option(true, true));
|
||||
|
||||
let mut default_cmd = json!({ "action": "launch" });
|
||||
apply_hide_scrollbars_launch_option(&mut default_cmd, false, true);
|
||||
assert!(default_cmd.get("hideScrollbars").is_none());
|
||||
|
||||
let mut config_false_cmd = json!({ "action": "launch" });
|
||||
apply_hide_scrollbars_launch_option(&mut config_false_cmd, false, false);
|
||||
assert_eq!(config_false_cmd["hideScrollbars"], false);
|
||||
|
||||
let mut cli_true_cmd = json!({ "action": "launch" });
|
||||
apply_hide_scrollbars_launch_option(&mut cli_true_cmd, true, true);
|
||||
assert_eq!(cli_true_cmd["hideScrollbars"], true);
|
||||
}
|
||||
}
|
||||
|
||||
+239
-41
@@ -197,6 +197,7 @@ fn launch_hash(opts: &LaunchOptions) -> u64 {
|
||||
opts.proxy_password.hash(&mut h);
|
||||
opts.user_agent.hash(&mut h);
|
||||
opts.allow_file_access.hash(&mut h);
|
||||
opts.hide_scrollbars.hash(&mut h);
|
||||
h.finish()
|
||||
}
|
||||
|
||||
@@ -632,6 +633,8 @@ impl DaemonState {
|
||||
.send_command_no_params("Network.enable", Some(iframe_sid.as_str()))
|
||||
.await;
|
||||
}
|
||||
// Hide automation markers in this cross-origin iframe session too.
|
||||
apply_stealth_via_mgr(mgr, iframe_sid.as_str()).await;
|
||||
}
|
||||
}
|
||||
for sid in &drained.detached_iframe_sessions {
|
||||
@@ -1510,6 +1513,28 @@ async fn connect_auto_with_fresh_tab() -> Result<BrowserManager, String> {
|
||||
.client
|
||||
.send_command("Page.bringToFront", None, Some(&session_id))
|
||||
.await;
|
||||
|
||||
// Liveness probe: confirm the CDP session can actually round-trip
|
||||
// before returning success. Without this, a zombie CDP socket (process
|
||||
// alive, websocket dead) would let `connect_auto` and `tab_new` succeed,
|
||||
// we'd return Ok, the next user command would silently no-op, and
|
||||
// `agent-browser open URL` would exit 0 with the browser still on
|
||||
// about:blank. Failing here lets the caller surface the real error.
|
||||
if let Err(e) = mgr
|
||||
.client
|
||||
.send_command("Runtime.evaluate", Some(serde_json::json!({
|
||||
"expression": "1",
|
||||
"returnByValue": true,
|
||||
})), Some(&session_id))
|
||||
.await
|
||||
{
|
||||
return Err(format!(
|
||||
"CDP session is unresponsive after attaching ({}). \
|
||||
The browser may have lost its DevTools connection. \
|
||||
Try: agent-browser close, then re-run.",
|
||||
e
|
||||
));
|
||||
}
|
||||
Ok(mgr)
|
||||
}
|
||||
|
||||
@@ -1582,11 +1607,18 @@ async fn auto_launch(state: &mut DaemonState) -> Result<(), String> {
|
||||
// Return a helpful error guiding the user to enable it.
|
||||
return Err(format!(
|
||||
"Could not connect to your Chrome browser.\n\n\
|
||||
To let agent-browser work with your existing Chrome (recommended):\n\
|
||||
If Chrome showed an \"Allow remote debugging?\" dialog, click \
|
||||
Allow and re-run — that consent is what lets agent-browser attach.\n\n\
|
||||
Otherwise, to let agent-browser reuse your logged-in Chrome (recommended):\n\
|
||||
{}\n\n\
|
||||
Or start a standalone browser with: agent-browser --launch open <url>\n\n\
|
||||
Tip: On Chrome 144+, you can enable CDP without restarting:\n\
|
||||
Open chrome://inspect/#remote-debugging and toggle it on.",
|
||||
Or launch a separate browser that KEEPS your login state:\n \
|
||||
agent-browser --launch --profile auto open <url>\n\
|
||||
(plain `--launch` alone uses a temporary EMPTY profile — no cookies, \
|
||||
no logged-in sessions.)\n\n\
|
||||
Note: remote debugging is a startup flag, not a Chrome setting — \
|
||||
chrome://inspect/#remote-debugging only enables target discovery and \
|
||||
does NOT expose the CDP HTTP API on /json/version. \
|
||||
A full restart with --remote-debugging-port=<port> is required.",
|
||||
chrome_relaunch_hint(),
|
||||
));
|
||||
}
|
||||
@@ -1734,45 +1766,62 @@ fn chrome_relaunch_hint() -> &'static str {
|
||||
/// Called after every successful launch / CDP connect / auto-connect.
|
||||
/// Uses `CdpAttach` mode for external connections (minimal patches) and
|
||||
/// `FullLaunch` mode for newly launched Chrome (all patches).
|
||||
async fn apply_stealth_to_browser(state: &DaemonState) {
|
||||
if env::var("AGENT_BROWSER_STEALTH").map(|v| v == "0").unwrap_or(false) {
|
||||
return; // Explicitly disabled
|
||||
}
|
||||
let Some(ref mgr) = state.browser else {
|
||||
return;
|
||||
};
|
||||
let Ok(session_id) = mgr.active_session_id() else {
|
||||
return;
|
||||
};
|
||||
/// Whether stealth is enabled (default on; `AGENT_BROWSER_STEALTH=0` disables).
|
||||
fn stealth_enabled() -> bool {
|
||||
!env::var("AGENT_BROWSER_STEALTH")
|
||||
.map(|v| v == "0")
|
||||
.unwrap_or(false)
|
||||
}
|
||||
|
||||
// Determine mode: if we attached to an external browser, use minimal patches.
|
||||
// The user's real Chrome already has a genuine fingerprint — heavy patches
|
||||
// would create detectable "lies" (e.g. creepjs hasIframeProxy).
|
||||
/// Apply stealth patches to ONE CDP session of the given browser.
|
||||
///
|
||||
/// Stealth scripts are registered per-session via
|
||||
/// `Page.addScriptToEvaluateOnNewDocument`, so they do NOT carry over to new
|
||||
/// tabs or cross-origin iframe sessions created after the initial page. We must
|
||||
/// re-apply to every session the user can touch, otherwise automation markers
|
||||
/// (and, in FullLaunch mode, the HeadlessChrome UA) leak on those surfaces.
|
||||
async fn apply_stealth_via_mgr(mgr: &BrowserManager, session_id: &str) {
|
||||
if !stealth_enabled() {
|
||||
return;
|
||||
}
|
||||
// Determine mode: an external attach uses minimal patches (the user's real
|
||||
// Chrome already has a genuine fingerprint — heavy patches create detectable
|
||||
// "lies" like creepjs hasIframeProxy); a fresh launch uses the full set.
|
||||
let mode = if mgr.is_cdp_connection() {
|
||||
stealth::StealthMode::CdpAttach
|
||||
} else {
|
||||
stealth::StealthMode::FullLaunch
|
||||
};
|
||||
|
||||
let locale = env::var("AGENT_BROWSER_LOCALE").ok();
|
||||
if let Err(e) = stealth::apply_stealth(
|
||||
&mgr.client,
|
||||
session_id,
|
||||
mode,
|
||||
locale.as_deref(),
|
||||
)
|
||||
.await
|
||||
{
|
||||
eprintln!("[stealth] Failed to apply stealth patches: {}", e);
|
||||
if let Err(e) = stealth::apply_stealth(&mgr.client, session_id, mode, locale.as_deref()).await {
|
||||
eprintln!("[stealth] failed to apply patches to session {session_id}: {e}");
|
||||
}
|
||||
// Also inject into the current page (already loaded before our init script)
|
||||
// Also inject into the current page (already loaded before our init script).
|
||||
if let Err(e) =
|
||||
stealth::apply_stealth_to_current_page(&mgr.client, session_id, mode, locale.as_deref()).await
|
||||
stealth::apply_stealth_to_current_page(&mgr.client, session_id, mode, locale.as_deref())
|
||||
.await
|
||||
{
|
||||
eprintln!("[stealth] Failed to patch current page: {}", e);
|
||||
eprintln!("[stealth] failed to patch current page for session {session_id}: {e}");
|
||||
}
|
||||
}
|
||||
|
||||
/// Apply stealth to a specific session of the active browser (no-op if no
|
||||
/// browser or stealth disabled).
|
||||
async fn apply_stealth_to_session(state: &DaemonState, session_id: &str) {
|
||||
if let Some(ref mgr) = state.browser {
|
||||
apply_stealth_via_mgr(mgr, session_id).await;
|
||||
}
|
||||
}
|
||||
|
||||
/// Apply stealth to the active page session (initial connect/launch).
|
||||
async fn apply_stealth_to_browser(state: &DaemonState) {
|
||||
let session_id = match state.browser.as_ref().and_then(|m| m.active_session_id().ok()) {
|
||||
Some(sid) => sid.to_string(),
|
||||
None => return,
|
||||
};
|
||||
apply_stealth_to_session(state, &session_id).await;
|
||||
}
|
||||
|
||||
/// If the previous daemon left a `.restore-url` sidecar (because it was killed
|
||||
/// by a version-mismatch restart), navigate the freshly-connected browser to
|
||||
/// that URL so `agent-browser get url` after `npm i -g` upgrade still reports
|
||||
@@ -1853,11 +1902,24 @@ fn launch_options_from_env() -> LaunchOptions {
|
||||
.unwrap_or(false),
|
||||
color_scheme: env::var("AGENT_BROWSER_COLOR_SCHEME").ok(),
|
||||
download_path: env::var("AGENT_BROWSER_DOWNLOAD_PATH").ok(),
|
||||
hide_scrollbars: hide_scrollbars_from_env(),
|
||||
viewport_size: None,
|
||||
use_real_keychain: false,
|
||||
}
|
||||
}
|
||||
|
||||
fn hide_scrollbars_from_env() -> bool {
|
||||
env::var("AGENT_BROWSER_HIDE_SCROLLBARS")
|
||||
.map(|v| !matches!(v.to_ascii_lowercase().as_str(), "0" | "false" | "no" | ""))
|
||||
.unwrap_or(true)
|
||||
}
|
||||
|
||||
fn hide_scrollbars_from_launch_cmd(cmd: &Value) -> bool {
|
||||
cmd.get("hideScrollbars")
|
||||
.and_then(|v| v.as_bool())
|
||||
.unwrap_or_else(hide_scrollbars_from_env)
|
||||
}
|
||||
|
||||
async fn try_auto_restore_state(state: &mut DaemonState) {
|
||||
let session_name = match state.session_name.as_deref() {
|
||||
Some(n) if !n.is_empty() => n.to_string(),
|
||||
@@ -2021,6 +2083,7 @@ async fn handle_launch(cmd: &Value, state: &mut DaemonState) -> Result<Value, St
|
||||
.get("downloadPath")
|
||||
.and_then(|v| v.as_str())
|
||||
.map(String::from),
|
||||
hide_scrollbars: hide_scrollbars_from_launch_cmd(cmd),
|
||||
viewport_size: None,
|
||||
use_real_keychain: false,
|
||||
};
|
||||
@@ -2110,11 +2173,18 @@ async fn handle_launch(cmd: &Value, state: &mut DaemonState) -> Result<Value, St
|
||||
Err(_e) => {
|
||||
return Err(format!(
|
||||
"Could not connect to your Chrome browser.\n\n\
|
||||
To let agent-browser work with your existing Chrome (recommended):\n\
|
||||
If Chrome showed an \"Allow remote debugging?\" dialog, click \
|
||||
Allow and re-run — that consent is what lets agent-browser attach.\n\n\
|
||||
Otherwise, to let agent-browser reuse your logged-in Chrome (recommended):\n\
|
||||
{}\n\n\
|
||||
Or start a standalone browser with: agent-browser --launch open <url>\n\n\
|
||||
Tip: On Chrome 144+, you can enable CDP without restarting:\n\
|
||||
Open chrome://inspect/#remote-debugging and toggle it on.",
|
||||
Or launch a separate browser that KEEPS your login state:\n \
|
||||
agent-browser --launch --profile auto open <url>\n\
|
||||
(plain `--launch` alone uses a temporary EMPTY profile — no cookies, \
|
||||
no logged-in sessions.)\n\n\
|
||||
Note: remote debugging is a startup flag, not a Chrome setting — \
|
||||
chrome://inspect/#remote-debugging only enables target discovery and \
|
||||
does NOT expose the CDP HTTP API on /json/version. \
|
||||
A full restart with --remote-debugging-port=<port> is required.",
|
||||
chrome_relaunch_hint(),
|
||||
));
|
||||
}
|
||||
@@ -2254,6 +2324,11 @@ async fn handle_launch(cmd: &Value, state: &mut DaemonState) -> Result<Value, St
|
||||
load_storage_state_or_rollback(state, &storage_state_owned).await?;
|
||||
|
||||
apply_launch_init_scripts(state).await;
|
||||
// Apply stealth patches (the 32 JS patches + HeadlessChrome UA strip in
|
||||
// FullLaunch mode). The fresh-launch path was missing this — only the launch
|
||||
// FLAGS (e.g. --disable-blink-features) were applied, so the JS patches never
|
||||
// ran and navigator.userAgent kept the HeadlessChrome marker.
|
||||
apply_stealth_to_browser(state).await;
|
||||
|
||||
Ok(json!({ "launched": true }))
|
||||
}
|
||||
@@ -3105,6 +3180,15 @@ async fn handle_wait(cmd: &Value, state: &mut DaemonState) -> Result<Value, Stri
|
||||
.get("state")
|
||||
.and_then(|v| v.as_str())
|
||||
.unwrap_or("visible");
|
||||
// @-ref support: if the selector is `@e12` style, poll the ref map +
|
||||
// accessibility tree instead of `document.querySelector`. This makes
|
||||
// `wait @e8 --gone` a usable "assert modal still mounted" primitive
|
||||
// for SPA flows where the only stable identity is the AX role+name
|
||||
// captured at snapshot time.
|
||||
if selector.starts_with('@') {
|
||||
wait_for_ref(state, selector, state_str, timeout_ms).await?;
|
||||
return Ok(json!({ "waited": "ref", "ref": selector, "state": state_str }));
|
||||
}
|
||||
wait_for_selector(&mgr.client, &session_id, selector, state_str, timeout_ms).await?;
|
||||
return Ok(json!({ "waited": "selector", "selector": selector }));
|
||||
}
|
||||
@@ -3316,6 +3400,49 @@ async fn handle_reload(state: &mut DaemonState) -> Result<Value, String> {
|
||||
// Wait helpers
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// Poll-based wait for a ref-identified element. Resolves the @-ref by
|
||||
/// re-running the ref-identity verification each iteration. The supported
|
||||
/// states mirror selector-based waits:
|
||||
///
|
||||
/// - "visible" / "attached" — succeed when the ref resolves to a node
|
||||
/// whose AX role + name still match the snapshot entry
|
||||
/// - "detached" / "hidden" — succeed when the ref no longer matches
|
||||
/// (node removed OR re-textified to something else)
|
||||
///
|
||||
/// Times out with a "ref X did not become {state}" error.
|
||||
async fn wait_for_ref(
|
||||
state: &mut DaemonState,
|
||||
ref_selector: &str,
|
||||
desired_state: &str,
|
||||
timeout_ms: u64,
|
||||
) -> Result<(), String> {
|
||||
let want_present = !matches!(desired_state, "detached" | "hidden");
|
||||
let deadline = std::time::Instant::now() + std::time::Duration::from_millis(timeout_ms);
|
||||
loop {
|
||||
let mgr = state.browser.as_ref().ok_or("Browser not launched")?;
|
||||
let session_id = mgr.active_session_id()?.to_string();
|
||||
let resolved = super::element::resolve_element_object_id(
|
||||
&mgr.client,
|
||||
&session_id,
|
||||
&state.ref_map,
|
||||
ref_selector,
|
||||
&state.iframe_sessions,
|
||||
)
|
||||
.await;
|
||||
let present = resolved.is_ok();
|
||||
if present == want_present {
|
||||
return Ok(());
|
||||
}
|
||||
if std::time::Instant::now() >= deadline {
|
||||
return Err(format!(
|
||||
"Timeout: ref {} did not become {} within {}ms",
|
||||
ref_selector, desired_state, timeout_ms
|
||||
));
|
||||
}
|
||||
tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
|
||||
}
|
||||
}
|
||||
|
||||
async fn wait_for_selector(
|
||||
client: &super::cdp::client::CdpClient,
|
||||
session_id: &str,
|
||||
@@ -3563,13 +3690,38 @@ async fn handle_console(cmd: &Value, state: &mut DaemonState) -> Result<Value, S
|
||||
state.event_tracker.clear_console();
|
||||
Ok(json!({ "cleared": true }))
|
||||
} else {
|
||||
let result = state.event_tracker.get_console_json();
|
||||
let mut result = state.event_tracker.get_console_json();
|
||||
if !console_capture_active(state) {
|
||||
if let Some(obj) = result.as_object_mut() {
|
||||
obj.insert("hint".to_string(), json!(CONSOLE_DISABLED_HINT));
|
||||
}
|
||||
}
|
||||
Ok(result)
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether the active browser session has the CDP `Runtime` domain enabled
|
||||
/// (required to receive console/error events). OFF by default for stealth.
|
||||
fn console_capture_active(state: &DaemonState) -> bool {
|
||||
state
|
||||
.browser
|
||||
.as_ref()
|
||||
.map(|b| b.capture_console)
|
||||
.unwrap_or(false)
|
||||
}
|
||||
|
||||
const CONSOLE_DISABLED_HINT: &str =
|
||||
"console/error capture is disabled for stealth (Runtime.enable is a detectable CDP \
|
||||
signal). Restart the session with AGENT_BROWSER_CAPTURE_CONSOLE=1 to capture page output.";
|
||||
|
||||
async fn handle_errors(state: &DaemonState) -> Result<Value, String> {
|
||||
Ok(state.event_tracker.get_errors_json())
|
||||
let mut result = state.event_tracker.get_errors_json();
|
||||
if !console_capture_active(state) {
|
||||
if let Some(obj) = result.as_object_mut() {
|
||||
obj.insert("hint".to_string(), json!(CONSOLE_DISABLED_HINT));
|
||||
}
|
||||
}
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
async fn handle_state_save(cmd: &Value, state: &DaemonState) -> Result<Value, String> {
|
||||
@@ -3857,13 +4009,26 @@ async fn handle_tab_list(state: &DaemonState) -> Result<Value, String> {
|
||||
}
|
||||
|
||||
async fn handle_tab_new(cmd: &Value, state: &mut DaemonState) -> Result<Value, String> {
|
||||
let mgr = state.browser.as_mut().ok_or("Browser not launched")?;
|
||||
let url = cmd.get("url").and_then(|v| v.as_str());
|
||||
let label = cmd.get("label").and_then(|v| v.as_str());
|
||||
state.ref_map.clear();
|
||||
state.iframe_sessions.clear();
|
||||
state.active_frame_id = None;
|
||||
mgr.tab_new(url, label).await
|
||||
let result = {
|
||||
let mgr = state.browser.as_mut().ok_or("Browser not launched")?;
|
||||
mgr.tab_new(url, label).await?
|
||||
};
|
||||
// A new tab is a new CDP session; stealth scripts registered on the prior
|
||||
// session don't carry over, so patch the new tab too.
|
||||
if let Some(sid) = state
|
||||
.browser
|
||||
.as_ref()
|
||||
.and_then(|m| m.active_session_id().ok())
|
||||
.map(|s| s.to_string())
|
||||
{
|
||||
apply_stealth_to_session(state, &sid).await;
|
||||
}
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
async fn handle_tab_switch(cmd: &Value, state: &mut DaemonState) -> Result<Value, String> {
|
||||
@@ -8570,17 +8735,21 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_launch_options_from_env_defaults() {
|
||||
let _guard = EnvGuard::new(&["AGENT_BROWSER_HEADED"]);
|
||||
let guard = EnvGuard::new(&["AGENT_BROWSER_HEADED", "AGENT_BROWSER_HIDE_SCROLLBARS"]);
|
||||
guard.remove("AGENT_BROWSER_HEADED");
|
||||
guard.remove("AGENT_BROWSER_HIDE_SCROLLBARS");
|
||||
let opts = launch_options_from_env();
|
||||
assert!(opts.headless);
|
||||
assert!(opts.args.is_empty());
|
||||
assert!(!opts.allow_file_access);
|
||||
assert!(opts.hide_scrollbars);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_launch_options_from_env_headed_flag() {
|
||||
let _guard = EnvGuard::new(&["AGENT_BROWSER_HEADED"]);
|
||||
_guard.set("AGENT_BROWSER_HEADED", "1");
|
||||
let guard = EnvGuard::new(&["AGENT_BROWSER_HEADED", "AGENT_BROWSER_HIDE_SCROLLBARS"]);
|
||||
guard.set("AGENT_BROWSER_HEADED", "1");
|
||||
guard.remove("AGENT_BROWSER_HIDE_SCROLLBARS");
|
||||
let opts = launch_options_from_env();
|
||||
assert!(
|
||||
!opts.headless,
|
||||
@@ -8588,6 +8757,35 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_launch_options_from_env_hide_scrollbars_false() {
|
||||
let guard = EnvGuard::new(&["AGENT_BROWSER_HIDE_SCROLLBARS"]);
|
||||
guard.set("AGENT_BROWSER_HIDE_SCROLLBARS", "false");
|
||||
let opts = launch_options_from_env();
|
||||
assert!(!opts.hide_scrollbars);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_launch_cmd_hide_scrollbars_missing_uses_env_default() {
|
||||
let guard = EnvGuard::new(&["AGENT_BROWSER_HIDE_SCROLLBARS"]);
|
||||
guard.set("AGENT_BROWSER_HIDE_SCROLLBARS", "false");
|
||||
|
||||
assert!(!hide_scrollbars_from_launch_cmd(&json!({
|
||||
"action": "launch"
|
||||
})));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_launch_cmd_hide_scrollbars_explicit_overrides_env_default() {
|
||||
let guard = EnvGuard::new(&["AGENT_BROWSER_HIDE_SCROLLBARS"]);
|
||||
guard.set("AGENT_BROWSER_HIDE_SCROLLBARS", "false");
|
||||
|
||||
assert!(hide_scrollbars_from_launch_cmd(&json!({
|
||||
"action": "launch",
|
||||
"hideScrollbars": true
|
||||
})));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_har_entry_to_json_enriches_request_and_response() {
|
||||
// wall_time: 2026-03-15T12:00:00Z = 1_773_576_000
|
||||
|
||||
@@ -0,0 +1,364 @@
|
||||
//! Adaptive @ref relocation.
|
||||
//!
|
||||
//! When a saved `@ref`'s DOM node is gone (stale `backendNodeId`) and the
|
||||
//! role/name/nth re-query also fails, we score the current page's candidate
|
||||
//! elements against the ref's stored [`ElementFingerprint`] and relocate to the
|
||||
//! best match — but ONLY when confident: the best candidate must clear a high
|
||||
//! absolute threshold AND beat the runner-up by a clear margin. This matches the
|
||||
//! project's "fail loudly rather than mis-click" posture (see the identity and
|
||||
//! occlusion guards in `element.rs`).
|
||||
//!
|
||||
//! Everything in this module is pure and browser-free so the scoring can be
|
||||
//! unit-tested directly.
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
/// Minimum absolute similarity (0..1) for a relocation candidate to be accepted.
|
||||
pub const ADAPTIVE_THRESHOLD: f64 = 0.70;
|
||||
/// Minimum gap between the best and second-best candidate to avoid ambiguity.
|
||||
pub const ADAPTIVE_MARGIN: f64 = 0.15;
|
||||
|
||||
/// A structural/semantic fingerprint of an element, captured at snapshot time so
|
||||
/// a moved element can be re-identified after the page mutates.
|
||||
///
|
||||
/// Populated purely from the accessibility tree we already walk (`TreeNode`), so
|
||||
/// capturing it costs no extra CDP round-trips — `TreeNode` has no DOM tag or
|
||||
/// attributes (those would need an N×`DOM.describeNode` storm per snapshot), so
|
||||
/// `tag` holds the AX **role** and `attrs` holds discriminating AX properties
|
||||
/// (value/url/level/checked), not DOM `id`/`class`.
|
||||
#[derive(Debug, Clone, Default, PartialEq)]
|
||||
pub struct ElementFingerprint {
|
||||
/// AX role, e.g. "button" (used where a DOM tag would otherwise go).
|
||||
pub tag: String,
|
||||
/// Accessible name / visible text — the dominant identity signal.
|
||||
pub text: String,
|
||||
/// Discriminating AX properties: value, url, level, checked. Keyed by name.
|
||||
pub attrs: BTreeMap<String, String>,
|
||||
/// Ancestor role signatures from nearest to farthest, e.g. "form" / "list".
|
||||
pub ancestors: Vec<String>,
|
||||
/// Parent role.
|
||||
pub parent_tag: String,
|
||||
/// Parent accessible name / text.
|
||||
pub parent_text: String,
|
||||
/// Index among same-role siblings.
|
||||
pub sibling_index: u32,
|
||||
/// Count of same-role siblings.
|
||||
pub sibling_count: u32,
|
||||
}
|
||||
|
||||
/// Component weights. They sum to 1.0 so the total score lands in 0..1.
|
||||
/// Tuned for AX-derived fingerprints: the accessible name dominates, with role
|
||||
/// and tree structure carrying disambiguation when the name has changed (which
|
||||
/// is exactly when the exact role+name+nth fallback failed and we got here).
|
||||
const W_TAG: f64 = 0.20;
|
||||
const W_TEXT: f64 = 0.40;
|
||||
const W_ATTRS: f64 = 0.10;
|
||||
const W_ANCESTORS: f64 = 0.20;
|
||||
const W_PARENT_SIBLING: f64 = 0.10;
|
||||
|
||||
/// Per-attribute importance for the attribute-overlap score. Strong identity
|
||||
/// signals (a link's url) outweigh weak ones (heading level).
|
||||
fn attr_weight(name: &str) -> f64 {
|
||||
match name {
|
||||
"url" | "value" => 3.0,
|
||||
"checked" => 2.0,
|
||||
_ => 1.0,
|
||||
}
|
||||
}
|
||||
|
||||
/// Levenshtein-based string similarity in 0..1 (1.0 = identical). Two empty
|
||||
/// strings are treated as a perfect match (consistent absence of text).
|
||||
pub fn string_similarity(a: &str, b: &str) -> f64 {
|
||||
if a == b {
|
||||
return 1.0;
|
||||
}
|
||||
let a: Vec<char> = a.chars().collect();
|
||||
let b: Vec<char> = b.chars().collect();
|
||||
let max_len = a.len().max(b.len());
|
||||
if max_len == 0 {
|
||||
return 1.0;
|
||||
}
|
||||
let dist = levenshtein(&a, &b);
|
||||
1.0 - (dist as f64 / max_len as f64)
|
||||
}
|
||||
|
||||
fn levenshtein(a: &[char], b: &[char]) -> usize {
|
||||
if a.is_empty() {
|
||||
return b.len();
|
||||
}
|
||||
if b.is_empty() {
|
||||
return a.len();
|
||||
}
|
||||
let mut prev: Vec<usize> = (0..=b.len()).collect();
|
||||
let mut cur = vec![0usize; b.len() + 1];
|
||||
for (i, &ca) in a.iter().enumerate() {
|
||||
cur[0] = i + 1;
|
||||
for (j, &cb) in b.iter().enumerate() {
|
||||
let cost = if ca == cb { 0 } else { 1 };
|
||||
cur[j + 1] = (prev[j + 1] + 1).min(cur[j] + 1).min(prev[j] + cost);
|
||||
}
|
||||
std::mem::swap(&mut prev, &mut cur);
|
||||
}
|
||||
prev[b.len()]
|
||||
}
|
||||
|
||||
/// Jaccard similarity over whitespace-separated tokens (used for `class`).
|
||||
fn token_jaccard(a: &str, b: &str) -> f64 {
|
||||
let sa: std::collections::BTreeSet<&str> = a.split_whitespace().collect();
|
||||
let sb: std::collections::BTreeSet<&str> = b.split_whitespace().collect();
|
||||
if sa.is_empty() && sb.is_empty() {
|
||||
return 1.0;
|
||||
}
|
||||
let inter = sa.intersection(&sb).count() as f64;
|
||||
let union = sa.union(&sb).count() as f64;
|
||||
if union == 0.0 {
|
||||
1.0
|
||||
} else {
|
||||
inter / union
|
||||
}
|
||||
}
|
||||
|
||||
/// Length-ratio of the longest common subsequence over two ancestor sequences.
|
||||
fn lcs_ratio(a: &[String], b: &[String]) -> f64 {
|
||||
if a.is_empty() && b.is_empty() {
|
||||
return 1.0;
|
||||
}
|
||||
if a.is_empty() || b.is_empty() {
|
||||
return 0.0;
|
||||
}
|
||||
let mut dp = vec![vec![0usize; b.len() + 1]; a.len() + 1];
|
||||
for i in 0..a.len() {
|
||||
for j in 0..b.len() {
|
||||
dp[i + 1][j + 1] = if a[i] == b[j] {
|
||||
dp[i][j] + 1
|
||||
} else {
|
||||
dp[i][j + 1].max(dp[i + 1][j])
|
||||
};
|
||||
}
|
||||
}
|
||||
let lcs = dp[a.len()][b.len()] as f64;
|
||||
(2.0 * lcs) / (a.len() + b.len()) as f64
|
||||
}
|
||||
|
||||
fn attr_score(base: &BTreeMap<String, String>, cand: &BTreeMap<String, String>) -> f64 {
|
||||
let mut names: std::collections::BTreeSet<&str> = std::collections::BTreeSet::new();
|
||||
names.extend(base.keys().map(|s| s.as_str()));
|
||||
names.extend(cand.keys().map(|s| s.as_str()));
|
||||
if names.is_empty() {
|
||||
return 1.0; // no attributes on either side — neutral
|
||||
}
|
||||
let mut total = 0.0;
|
||||
let mut got = 0.0;
|
||||
for name in names {
|
||||
let w = attr_weight(name);
|
||||
total += w;
|
||||
// present on only one side → no credit
|
||||
if let (Some(a), Some(b)) = (base.get(name), cand.get(name)) {
|
||||
if name == "class" {
|
||||
got += w * token_jaccard(a, b);
|
||||
} else if a == b {
|
||||
got += w;
|
||||
}
|
||||
}
|
||||
}
|
||||
if total == 0.0 {
|
||||
1.0
|
||||
} else {
|
||||
got / total
|
||||
}
|
||||
}
|
||||
|
||||
fn parent_sibling_score(base: &ElementFingerprint, cand: &ElementFingerprint) -> f64 {
|
||||
// Split the 0.10 budget: parent tag 0.4, parent text 0.3, sibling pos 0.3.
|
||||
let parent_tag = if base.parent_tag == cand.parent_tag {
|
||||
1.0
|
||||
} else {
|
||||
0.0
|
||||
};
|
||||
let parent_text = string_similarity(&base.parent_text, &cand.parent_text);
|
||||
let span = base.sibling_count.max(1) as f64;
|
||||
let delta = (base.sibling_index as i64 - cand.sibling_index as i64).unsigned_abs() as f64;
|
||||
let sibling = 1.0 - (delta / span).min(1.0);
|
||||
0.4 * parent_tag + 0.3 * parent_text + 0.3 * sibling
|
||||
}
|
||||
|
||||
/// Similarity score in 0..1 between a stored baseline and a candidate element.
|
||||
pub fn score(base: &ElementFingerprint, cand: &ElementFingerprint) -> f64 {
|
||||
let tag = if base.tag == cand.tag { 1.0 } else { 0.0 };
|
||||
let text = string_similarity(&base.text, &cand.text);
|
||||
let attrs = attr_score(&base.attrs, &cand.attrs);
|
||||
let ancestors = lcs_ratio(&base.ancestors, &cand.ancestors);
|
||||
let parent_sibling = parent_sibling_score(base, cand);
|
||||
|
||||
W_TAG * tag
|
||||
+ W_TEXT * text
|
||||
+ W_ATTRS * attrs
|
||||
+ W_ANCESTORS * ancestors
|
||||
+ W_PARENT_SIBLING * parent_sibling
|
||||
}
|
||||
|
||||
/// Why a relocation was rejected.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum RejectReason {
|
||||
/// No candidates to score.
|
||||
NoCandidates,
|
||||
/// Best score below [`ADAPTIVE_THRESHOLD`].
|
||||
LowScore { best: f64 },
|
||||
/// Best score too close to the runner-up (below [`ADAPTIVE_MARGIN`]).
|
||||
Ambiguous { best: f64, second: f64 },
|
||||
}
|
||||
|
||||
/// A successful relocation decision.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct Relocation {
|
||||
/// Chosen candidate's backend node id.
|
||||
pub backend_node_id: i64,
|
||||
/// Winning score.
|
||||
pub score: f64,
|
||||
/// Runner-up score (0.0 when there was only one candidate).
|
||||
pub second_score: f64,
|
||||
}
|
||||
|
||||
/// Pick the best candidate, accepting only when confident. `candidates` is a
|
||||
/// list of `(backend_node_id, fingerprint)` for the current page.
|
||||
pub fn pick_best(
|
||||
base: &ElementFingerprint,
|
||||
candidates: &[(i64, ElementFingerprint)],
|
||||
threshold: f64,
|
||||
margin: f64,
|
||||
) -> Result<Relocation, RejectReason> {
|
||||
if candidates.is_empty() {
|
||||
return Err(RejectReason::NoCandidates);
|
||||
}
|
||||
let mut scored: Vec<(i64, f64)> = candidates
|
||||
.iter()
|
||||
.map(|(id, fp)| (*id, score(base, fp)))
|
||||
.collect();
|
||||
// Highest score first; stable enough for deterministic ties.
|
||||
scored.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
|
||||
|
||||
let (best_id, best) = scored[0];
|
||||
let second = scored.get(1).map(|(_, s)| *s).unwrap_or(0.0);
|
||||
|
||||
if best < threshold {
|
||||
return Err(RejectReason::LowScore { best });
|
||||
}
|
||||
if best - second < margin {
|
||||
return Err(RejectReason::Ambiguous { best, second });
|
||||
}
|
||||
Ok(Relocation {
|
||||
backend_node_id: best_id,
|
||||
score: best,
|
||||
second_score: second,
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn fp(tag: &str, text: &str, attrs: &[(&str, &str)]) -> ElementFingerprint {
|
||||
ElementFingerprint {
|
||||
tag: tag.to_string(),
|
||||
text: text.to_string(),
|
||||
attrs: attrs
|
||||
.iter()
|
||||
.map(|(k, v)| (k.to_string(), v.to_string()))
|
||||
.collect(),
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn identical_fingerprints_score_one() {
|
||||
let a = fp("button", "Submit", &[("id", "go"), ("class", "btn primary")]);
|
||||
assert!((score(&a, &a) - 1.0).abs() < 1e-9);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn different_tag_caps_score_below_threshold() {
|
||||
let a = fp("button", "Submit", &[("id", "go")]);
|
||||
let b = fp("a", "Submit", &[("id", "go")]);
|
||||
// Same text + same attrs but different role: must lose the role weight
|
||||
// (W_TAG = 0.20), landing around 0.80 and below a perfect match.
|
||||
let s = score(&a, &b);
|
||||
assert!(s < 0.85 && s > 0.75, "got {s}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn string_similarity_basics() {
|
||||
assert_eq!(string_similarity("abc", "abc"), 1.0);
|
||||
assert_eq!(string_similarity("", ""), 1.0);
|
||||
assert!(string_similarity("Submit", "Submit now") > 0.5);
|
||||
assert!(string_similarity("Add post", "Post all") < 0.6);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn class_uses_token_overlap() {
|
||||
let a = fp("div", "", &[("class", "card primary big")]);
|
||||
let b = fp("div", "", &[("class", "card primary")]);
|
||||
// partial class overlap should still score high (tag+text match, attrs partial)
|
||||
let s = score(&a, &b);
|
||||
assert!(s > 0.85, "got {s}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ancestors_lcs() {
|
||||
let mut a = fp("button", "OK", &[]);
|
||||
let mut b = fp("button", "OK", &[]);
|
||||
a.ancestors = vec!["form#f".into(), "div.col".into(), "body".into()];
|
||||
// b wrapped in an extra div — DOM path changed but mostly preserved
|
||||
b.ancestors = vec!["form#f".into(), "div.wrap".into(), "div.col".into(), "body".into()];
|
||||
let s = score(&a, &b);
|
||||
assert!(s > 0.85, "got {s}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pick_best_accepts_clear_winner() {
|
||||
let base = fp("button", "Submit", &[("id", "go")]);
|
||||
let winner = fp("button", "Submit", &[("id", "go")]);
|
||||
let other = fp("a", "Home", &[("href", "/")]);
|
||||
let out = pick_best(
|
||||
&base,
|
||||
&[(10, other), (20, winner)],
|
||||
ADAPTIVE_THRESHOLD,
|
||||
ADAPTIVE_MARGIN,
|
||||
)
|
||||
.expect("should accept");
|
||||
assert_eq!(out.backend_node_id, 20);
|
||||
assert!(out.score > out.second_score);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pick_best_rejects_ambiguous_twins() {
|
||||
let base = fp("button", "Delete", &[("class", "btn danger")]);
|
||||
// Two near-identical delete buttons — must refuse to guess.
|
||||
let twin_a = fp("button", "Delete", &[("class", "btn danger")]);
|
||||
let twin_b = fp("button", "Delete", &[("class", "btn danger")]);
|
||||
let err = pick_best(
|
||||
&base,
|
||||
&[(1, twin_a), (2, twin_b)],
|
||||
ADAPTIVE_THRESHOLD,
|
||||
ADAPTIVE_MARGIN,
|
||||
)
|
||||
.unwrap_err();
|
||||
assert!(matches!(err, RejectReason::Ambiguous { .. }), "got {err:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pick_best_rejects_low_score() {
|
||||
let base = fp("button", "Submit order", &[("id", "checkout")]);
|
||||
let junk = fp("span", "unrelated footer text", &[("class", "muted")]);
|
||||
let err = pick_best(&base, &[(1, junk)], ADAPTIVE_THRESHOLD, ADAPTIVE_MARGIN).unwrap_err();
|
||||
assert!(matches!(err, RejectReason::LowScore { .. }), "got {err:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pick_best_no_candidates() {
|
||||
let base = fp("button", "x", &[]);
|
||||
assert_eq!(
|
||||
pick_best(&base, &[], ADAPTIVE_THRESHOLD, ADAPTIVE_MARGIN).unwrap_err(),
|
||||
RejectReason::NoCandidates
|
||||
);
|
||||
}
|
||||
}
|
||||
+113
-15
@@ -305,12 +305,64 @@ pub struct BrowserManager {
|
||||
/// Origins visited during this session, used by save_state to collect cross-origin localStorage.
|
||||
visited_origins: HashSet<String>,
|
||||
next_tab_id: u32,
|
||||
/// Whether to enable the CDP `Runtime` domain (console / error / exception capture).
|
||||
/// OFF by default for stealth: a live `Runtime.enable` is a detectable CDP signal
|
||||
/// (the patchright / rebrowser "runtime leak") — even when attached to the user's
|
||||
/// real Chrome. Opt in via `AGENT_BROWSER_CAPTURE_CONSOLE=1` when you need the
|
||||
/// `console` / `errors` commands to return page output.
|
||||
pub capture_console: bool,
|
||||
}
|
||||
|
||||
/// Whether console/error capture (and thus `Runtime.enable`) is opted into for this
|
||||
/// daemon. Defaults to `false` so the common automation path leaves no Runtime-domain
|
||||
/// fingerprint. Set `AGENT_BROWSER_CAPTURE_CONSOLE=1` (or `true`) to turn it on.
|
||||
pub fn console_capture_enabled() -> bool {
|
||||
std::env::var("AGENT_BROWSER_CAPTURE_CONSOLE")
|
||||
.ok()
|
||||
.map(|v| v == "1" || v.eq_ignore_ascii_case("true"))
|
||||
.unwrap_or(false)
|
||||
}
|
||||
|
||||
const LIGHTPANDA_CDP_CONNECT_TIMEOUT: Duration = Duration::from_secs(5);
|
||||
const LIGHTPANDA_CDP_CONNECT_POLL_INTERVAL: Duration = Duration::from_millis(100);
|
||||
const LIGHTPANDA_TARGET_INIT_TIMEOUT: Duration = Duration::from_secs(10);
|
||||
|
||||
/// Outcome of a single `Browser.getVersion` liveness probe.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
enum LivenessProbe {
|
||||
/// Chrome answered — the connection is definitely alive.
|
||||
Responded,
|
||||
/// The CDP transport errored (WebSocket closed/reset) — the socket is gone.
|
||||
TransportError,
|
||||
/// The probe timed out with no response.
|
||||
TimedOut,
|
||||
}
|
||||
|
||||
/// Decide whether a CDP connection should be considered alive from one probe.
|
||||
///
|
||||
/// The subtle case is [`LivenessProbe::TimedOut`]. For a browser we launched
|
||||
/// ourselves (`is_external_attach == false`) a hung CDP socket is a real
|
||||
/// problem and the daemon should reconnect. But for an *externally attached*
|
||||
/// browser — the stealth fork's default, where we attach to the user's real
|
||||
/// Chrome — a slow/no response is almost always Chrome being briefly busy or,
|
||||
/// critically, showing the Chrome 136+ "Allow remote debugging?" consent modal,
|
||||
/// which blocks CDP responses until the user clicks Allow.
|
||||
///
|
||||
/// Treating that timeout as "dead" tears down the already-consented connection
|
||||
/// and forces a reconnect, which re-pops the consent prompt; repeated on every
|
||||
/// command it produces an endless prompt loop and a connection storm that can
|
||||
/// freeze Chrome. So for external attaches we keep the connection alive on
|
||||
/// timeout. A genuinely dead external socket instead surfaces as
|
||||
/// [`LivenessProbe::TransportError`] (and Chrome being closed by the user is a
|
||||
/// transport error, not a timeout), so zombie-socket detection is preserved.
|
||||
fn connection_alive_from_probe(probe: LivenessProbe, is_external_attach: bool) -> bool {
|
||||
match probe {
|
||||
LivenessProbe::Responded => true,
|
||||
LivenessProbe::TransportError => false,
|
||||
LivenessProbe::TimedOut => is_external_attach,
|
||||
}
|
||||
}
|
||||
|
||||
impl BrowserManager {
|
||||
pub async fn launch(options: LaunchOptions, engine: Option<&str>) -> Result<Self, String> {
|
||||
let engine = engine.unwrap_or("chrome");
|
||||
@@ -377,6 +429,7 @@ impl BrowserManager {
|
||||
ignore_https_errors,
|
||||
visited_origins: HashSet::new(),
|
||||
next_tab_id: 1,
|
||||
capture_console: console_capture_enabled(),
|
||||
};
|
||||
manager.discover_and_attach_targets().await?;
|
||||
manager
|
||||
@@ -466,6 +519,7 @@ impl BrowserManager {
|
||||
ignore_https_errors: false,
|
||||
visited_origins: HashSet::new(),
|
||||
next_tab_id: 1,
|
||||
capture_console: console_capture_enabled(),
|
||||
};
|
||||
|
||||
if direct_page {
|
||||
@@ -593,9 +647,14 @@ impl BrowserManager {
|
||||
self.client
|
||||
.send_command_no_params("Page.enable", Some(session_id))
|
||||
.await?;
|
||||
self.client
|
||||
.send_command_no_params("Runtime.enable", Some(session_id))
|
||||
.await?;
|
||||
// `Runtime.enable` leaves a detectable CDP signal (the patchright/rebrowser
|
||||
// "runtime leak"), so only enable it when console/error capture is opted in.
|
||||
// `Runtime.evaluate` / `Runtime.callFunctionOn` work fine without it.
|
||||
if self.capture_console {
|
||||
self.client
|
||||
.send_command_no_params("Runtime.enable", Some(session_id))
|
||||
.await?;
|
||||
}
|
||||
// Resume the target if it is paused waiting for the debugger.
|
||||
// This is needed for real browser sessions (Chrome 144+) where targets
|
||||
// are paused after attach until explicitly resumed. No-op otherwise.
|
||||
@@ -629,9 +688,12 @@ impl BrowserManager {
|
||||
self.client
|
||||
.send_command_no_params("Page.enable", None)
|
||||
.await?;
|
||||
self.client
|
||||
.send_command_no_params("Runtime.enable", None)
|
||||
.await?;
|
||||
// See `enable_domains`: `Runtime.enable` is a CDP fingerprint, gated on opt-in.
|
||||
if self.capture_console {
|
||||
self.client
|
||||
.send_command_no_params("Runtime.enable", None)
|
||||
.await?;
|
||||
}
|
||||
let _ = self
|
||||
.client
|
||||
.send_command_no_params("Runtime.runIfWaitingForDebugger", None)
|
||||
@@ -829,21 +891,27 @@ impl BrowserManager {
|
||||
self.default_timeout_ms
|
||||
}
|
||||
|
||||
/// Checks if the CDP connection is alive by sending a simple command.
|
||||
/// Returns false if the command times out or fails.
|
||||
/// Checks if the CDP connection is alive by sending a `Browser.getVersion`
|
||||
/// probe. See [`connection_alive_from_probe`] for how the outcome maps to a
|
||||
/// liveness verdict — in particular why a timeout does NOT tear down an
|
||||
/// externally-attached browser.
|
||||
pub async fn is_connection_alive(&self) -> bool {
|
||||
let timeout = tokio::time::Duration::from_secs(3);
|
||||
let result = tokio::time::timeout(
|
||||
let probe = match tokio::time::timeout(
|
||||
timeout,
|
||||
self.client
|
||||
.send_command_no_params("Browser.getVersion", None),
|
||||
)
|
||||
.await;
|
||||
|
||||
match result {
|
||||
Ok(Ok(_)) => true,
|
||||
Ok(Err(_)) | Err(_) => false,
|
||||
}
|
||||
.await
|
||||
{
|
||||
Ok(Ok(_)) => LivenessProbe::Responded,
|
||||
Ok(Err(_)) => LivenessProbe::TransportError,
|
||||
Err(_) => LivenessProbe::TimedOut,
|
||||
};
|
||||
// No child process => we attached to an external browser (the user's
|
||||
// real Chrome — the stealth fork's default).
|
||||
let is_external_attach = self.browser_process.is_none();
|
||||
connection_alive_from_probe(probe, is_external_attach)
|
||||
}
|
||||
|
||||
/// Non-blocking check whether the locally-launched browser process has exited
|
||||
@@ -1617,6 +1685,7 @@ async fn initialize_lightpanda_manager(
|
||||
ignore_https_errors: false,
|
||||
visited_origins: HashSet::new(),
|
||||
next_tab_id: 1,
|
||||
capture_console: console_capture_enabled(),
|
||||
};
|
||||
|
||||
match discover_and_attach_lightpanda_targets(&mut manager, deadline).await {
|
||||
@@ -1728,6 +1797,35 @@ mod tests {
|
||||
assert_eq!(format_tab_id(42), "t42");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn liveness_responded_is_alive_for_both_kinds() {
|
||||
assert!(connection_alive_from_probe(LivenessProbe::Responded, true));
|
||||
assert!(connection_alive_from_probe(LivenessProbe::Responded, false));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn liveness_transport_error_is_dead_for_both_kinds() {
|
||||
// A closed/reset WebSocket is a genuine death — reconnect in both cases.
|
||||
assert!(!connection_alive_from_probe(LivenessProbe::TransportError, true));
|
||||
assert!(!connection_alive_from_probe(LivenessProbe::TransportError, false));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn liveness_timeout_keeps_external_attach_alive() {
|
||||
// Regression guard for the remote-debugging consent storm: a timed-out
|
||||
// probe must NOT tear down an externally-attached browser, otherwise the
|
||||
// daemon reconnects and re-pops Chrome's "Allow remote debugging?" modal
|
||||
// on every command (endless prompts + browser freeze).
|
||||
assert!(connection_alive_from_probe(LivenessProbe::TimedOut, true));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn liveness_timeout_marks_launched_browser_dead() {
|
||||
// A browser we launched that stops responding is a real problem worth a
|
||||
// reconnect (and has no consent modal to worry about).
|
||||
assert!(!connection_alive_from_probe(LivenessProbe::TimedOut, false));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_tab_ref_id() {
|
||||
assert_eq!(TabRef::parse("t1"), Ok(TabRef::Id(1)));
|
||||
|
||||
+302
-97
@@ -103,6 +103,9 @@ pub struct LaunchOptions {
|
||||
pub ignore_https_errors: bool,
|
||||
pub color_scheme: Option<String>,
|
||||
pub download_path: Option<String>,
|
||||
/// Hide native scrollbars in headless Chromium screenshots by launching
|
||||
/// Chrome with `--hide-scrollbars`.
|
||||
pub hide_scrollbars: bool,
|
||||
/// Initial viewport dimensions used for `--window-size` so the content
|
||||
/// area matches the desired viewport from the start.
|
||||
pub viewport_size: Option<(u32, u32)>,
|
||||
@@ -130,6 +133,7 @@ impl Default for LaunchOptions {
|
||||
ignore_https_errors: false,
|
||||
color_scheme: None,
|
||||
download_path: None,
|
||||
hide_scrollbars: true,
|
||||
viewport_size: None,
|
||||
use_real_keychain: false,
|
||||
}
|
||||
@@ -142,6 +146,30 @@ struct ChromeArgs {
|
||||
temp_user_data_dir: Option<PathBuf>,
|
||||
}
|
||||
|
||||
/// Decide the `--force-webrtc-ip-handling-policy` value, if any, for a launched
|
||||
/// Chrome. Returns `None` to leave WebRTC at Chrome's default behavior.
|
||||
fn webrtc_ip_handling_policy(has_proxy: bool) -> Option<&'static str> {
|
||||
let opt_in = std::env::var("AGENT_BROWSER_BLOCK_WEBRTC").ok();
|
||||
let explicitly_off = opt_in
|
||||
.as_deref()
|
||||
.is_some_and(|v| v == "0" || v.eq_ignore_ascii_case("false"));
|
||||
if explicitly_off {
|
||||
return None;
|
||||
}
|
||||
let explicitly_on = opt_in
|
||||
.as_deref()
|
||||
.is_some_and(|v| v == "1" || v.eq_ignore_ascii_case("true"));
|
||||
if has_proxy {
|
||||
// Force all WebRTC UDP through the proxy so the real IP can't leak.
|
||||
Some("disable_non_proxied_udp")
|
||||
} else if explicitly_on {
|
||||
// No proxy, but the user asked to hide the local network IP.
|
||||
Some("default_public_interface_only")
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn build_chrome_args(options: &LaunchOptions) -> Result<ChromeArgs, String> {
|
||||
let mut args = vec![
|
||||
"--remote-debugging-port=0".to_string(),
|
||||
@@ -178,6 +206,13 @@ fn build_chrome_args(options: &LaunchOptions) -> Result<ChromeArgs, String> {
|
||||
// injected in headless mode). Skip --headless when extensions are loaded.
|
||||
if options.headless && !has_extensions {
|
||||
args.push("--headless=new".to_string());
|
||||
// Linux paints native scrollbars into viewport screenshots unless
|
||||
// Chrome is launched with this flag. `--hide-scrollbars` is
|
||||
// presence-based, so agent-browser exposes --hide-scrollbars false
|
||||
// as the public opt-out instead of forwarding a fake inverse switch.
|
||||
if options.hide_scrollbars {
|
||||
args.push("--hide-scrollbars".to_string());
|
||||
}
|
||||
// Enable SwiftShader software rendering in headless mode. This
|
||||
// prevents silent crashes in environments where GPU drivers are
|
||||
// missing or restricted (VMs, containers, some cloud machines)
|
||||
@@ -193,6 +228,20 @@ fn build_chrome_args(options: &LaunchOptions) -> Result<ChromeArgs, String> {
|
||||
args.push(format!("--proxy-bypass-list={}", bypass));
|
||||
}
|
||||
|
||||
// WebRTC IP-leak handling. WebRTC enumerates ICE candidates that can expose
|
||||
// the machine's real local/public IP even when HTTP traffic goes through a
|
||||
// proxy — defeating the proxy. `--force-webrtc-ip-handling-policy` is a real
|
||||
// Chrome privacy switch (no detectable JS lie), applied here for launched
|
||||
// Chrome only (an attached real Chrome keeps the user's own flags).
|
||||
// - proxy set -> `disable_non_proxied_udp`: force WebRTC through
|
||||
// the proxy so the real IP can't leak.
|
||||
// - AGENT_BROWSER_BLOCK_WEBRTC=1 (no proxy) -> `default_public_interface_only`:
|
||||
// hide the local network IP (Brave/uBlock default).
|
||||
// Opt out entirely with AGENT_BROWSER_BLOCK_WEBRTC=0.
|
||||
if let Some(policy) = webrtc_ip_handling_policy(options.proxy.is_some()) {
|
||||
args.push(format!("--force-webrtc-ip-handling-policy={}", policy));
|
||||
}
|
||||
|
||||
let (user_data_dir, temp_user_data_dir) = if let Some(ref profile) = options.profile {
|
||||
let expanded = expand_tilde(profile);
|
||||
let dir = PathBuf::from(&expanded);
|
||||
@@ -653,6 +702,62 @@ pub fn read_devtools_active_port(user_data_dir: &Path) -> Option<(u16, String)>
|
||||
Some((port, ws_path))
|
||||
}
|
||||
|
||||
/// Remove leftover Chrome temp profile directories from daemons that were
|
||||
/// hard-killed. `ChromeProcess::drop` cleans these up on a normal exit, but a
|
||||
/// `kill -9` (version-mismatch restart, OOM, crash) skips Drop and leaks ~50MB
|
||||
/// per session under the system temp dir. On daemon startup we sweep them — but
|
||||
/// ONLY dirs that no running process still references as `--user-data-dir`, so
|
||||
/// a profile in active use is never deleted.
|
||||
pub fn cleanup_orphaned_chrome_profiles() {
|
||||
let tmp = std::env::temp_dir();
|
||||
let Ok(entries) = std::fs::read_dir(&tmp) else {
|
||||
return;
|
||||
};
|
||||
// Snapshot live process command lines once. If we can't determine them,
|
||||
// skip cleanup entirely rather than risk deleting an in-use profile.
|
||||
let Some(live_cmdlines) = running_process_cmdlines() else {
|
||||
return;
|
||||
};
|
||||
for entry in entries.flatten() {
|
||||
let name = entry.file_name();
|
||||
if !name.to_string_lossy().starts_with("agent-browser-chrome-") {
|
||||
continue;
|
||||
}
|
||||
let path = entry.path();
|
||||
let path_str = path.to_string_lossy();
|
||||
let in_use = live_cmdlines
|
||||
.iter()
|
||||
.any(|cmd| cmd.contains(path_str.as_ref()));
|
||||
if !in_use {
|
||||
let _ = std::fs::remove_dir_all(&path);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
fn running_process_cmdlines() -> Option<Vec<String>> {
|
||||
let output = std::process::Command::new("ps")
|
||||
.args(["-axww", "-o", "command="])
|
||||
.output()
|
||||
.ok()?;
|
||||
if !output.status.success() {
|
||||
return None;
|
||||
}
|
||||
Some(
|
||||
String::from_utf8_lossy(&output.stdout)
|
||||
.lines()
|
||||
.map(|l| l.to_string())
|
||||
.collect(),
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(not(unix))]
|
||||
fn running_process_cmdlines() -> Option<Vec<String>> {
|
||||
// Best-effort: skip cleanup where we can't cheaply enumerate full process
|
||||
// command lines, to avoid deleting a profile that is still in use.
|
||||
None
|
||||
}
|
||||
|
||||
pub async fn auto_connect_cdp() -> Result<String, String> {
|
||||
let user_data_dirs = get_chrome_user_data_dirs();
|
||||
|
||||
@@ -674,57 +779,61 @@ pub async fn auto_connect_cdp() -> Result<String, String> {
|
||||
}
|
||||
}
|
||||
|
||||
Err("No running Chrome instance found. Launch Chrome with --remote-debugging-port or use --cdp.".to_string())
|
||||
Err("No running Chrome with remote debugging found. Remote debugging is a \
|
||||
startup flag, not a setting: fully quit Chrome and relaunch it with \
|
||||
--remote-debugging-port=9222 (then agent-browser auto-connects), or pass \
|
||||
--cdp <port>/--launch."
|
||||
.to_string())
|
||||
}
|
||||
|
||||
/// Resolve a CDP WebSocket URL from a DevToolsActivePort entry.
|
||||
///
|
||||
/// Tries the exact WebSocket path from DevToolsActivePort first (single
|
||||
/// prompt on M144+), then falls back to legacy HTTP discovery for older
|
||||
/// Chrome versions. This order avoids triggering duplicate remote-debugging
|
||||
/// permission prompts (#1210, #1206).
|
||||
/// Returns the exact browser WebSocket URL from DevToolsActivePort, gated only
|
||||
/// by a consent-free TCP liveness check. Falls back to HTTP discovery on the
|
||||
/// same port for older Chrome layouts.
|
||||
///
|
||||
/// Crucially, this does NOT open a throwaway verification WebSocket. On
|
||||
/// Chrome 136+ the "Allow remote debugging?" consent is granted *per
|
||||
/// connection*: a probe WebSocket we then close would consume the user's one
|
||||
/// Allow click, leaving the real connection (opened afterwards) unconsented —
|
||||
/// which manifests as an endless prompt loop or a hung command. By skipping the
|
||||
/// probe, the real connection is the single WebSocket the user consents to.
|
||||
/// (Background: #1210, #1206 duplicate-prompt reports.)
|
||||
async fn resolve_cdp_from_active_port(port: u16, ws_path: &str) -> Result<String, String> {
|
||||
let ws_url = format!("ws://127.0.0.1:{}{}", port, ws_path);
|
||||
if verify_ws_endpoint(&ws_url).await {
|
||||
return Ok(ws_url);
|
||||
// Consent-free liveness: a bare TCP connect does not trigger the
|
||||
// remote-debugging consent flow (that fires on the CDP/WebSocket upgrade),
|
||||
// so we can tell "Chrome is listening" from "stale DevToolsActivePort"
|
||||
// without burning a prompt.
|
||||
if tcp_port_alive(port).await {
|
||||
return Ok(format!("ws://127.0.0.1:{}{}", port, ws_path));
|
||||
}
|
||||
|
||||
// Pre-M144 fallback: HTTP endpoints (/json/version, /json/list, etc.)
|
||||
// Port isn't accepting connections (stale file / different layout). Fall
|
||||
// back to HTTP discovery for older Chrome before giving up.
|
||||
if let Ok(ws_url) = discover_cdp_url("127.0.0.1", port, None).await {
|
||||
return Ok(ws_url);
|
||||
}
|
||||
|
||||
Err(format!(
|
||||
"Cannot connect to Chrome on port {}: both direct WebSocket and HTTP discovery failed",
|
||||
"Cannot connect to Chrome on port {}: port not reachable and HTTP discovery failed",
|
||||
port
|
||||
))
|
||||
}
|
||||
|
||||
/// Verify that a WebSocket endpoint is a live CDP server by sending
|
||||
/// `Browser.getVersion` and checking for a valid response.
|
||||
async fn verify_ws_endpoint(ws_url: &str) -> bool {
|
||||
use futures_util::{SinkExt, StreamExt};
|
||||
use tokio_tungstenite::tungstenite::Message;
|
||||
|
||||
let timeout = Duration::from_secs(2);
|
||||
let result = tokio::time::timeout(timeout, async {
|
||||
let (mut ws, _) = tokio_tungstenite::connect_async(ws_url).await.ok()?;
|
||||
let cmd = r#"{"id":1,"method":"Browser.getVersion"}"#;
|
||||
ws.send(Message::Text(cmd.into())).await.ok()?;
|
||||
while let Some(Ok(msg)) = ws.next().await {
|
||||
if let Message::Text(text) = msg {
|
||||
if let Ok(v) = serde_json::from_str::<serde_json::Value>(&text) {
|
||||
if v.get("id").and_then(|id| id.as_u64()) == Some(1) {
|
||||
let _ = ws.close(None).await;
|
||||
return Some(());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
})
|
||||
.await;
|
||||
matches!(result, Ok(Some(())))
|
||||
/// Consent-free check that something is accepting TCP connections on
|
||||
/// `127.0.0.1:port`. Unlike a CDP/WebSocket probe, a bare TCP connect does not
|
||||
/// trigger Chrome's "Allow remote debugging?" consent prompt, so it is safe to
|
||||
/// use for liveness before handing the URL to the single real connection.
|
||||
async fn tcp_port_alive(port: u16) -> bool {
|
||||
let timeout = Duration::from_secs(1);
|
||||
matches!(
|
||||
tokio::time::timeout(
|
||||
timeout,
|
||||
tokio::net::TcpStream::connect(("127.0.0.1", port)),
|
||||
)
|
||||
.await,
|
||||
Ok(Ok(_))
|
||||
)
|
||||
}
|
||||
|
||||
/// Returns the default Chrome user-data directory paths for the current platform.
|
||||
@@ -846,6 +955,18 @@ pub fn list_chrome_profiles(user_data_dir: &Path) -> Vec<ChromeProfile> {
|
||||
/// 3. Case-insensitive directory name match
|
||||
///
|
||||
/// Returns the resolved directory name, or an error with available profiles.
|
||||
/// Read `profile.last_used` (the directory name of the profile Chrome opened
|
||||
/// most recently) from a user-data dir's `Local State`. Used to resolve
|
||||
/// `--profile auto`.
|
||||
fn read_last_used_profile(user_data_dir: &Path) -> Option<String> {
|
||||
let content = std::fs::read_to_string(user_data_dir.join("Local State")).ok()?;
|
||||
let json: serde_json::Value = serde_json::from_str(&content).ok()?;
|
||||
json.get("profile")?
|
||||
.get("last_used")?
|
||||
.as_str()
|
||||
.map(String::from)
|
||||
}
|
||||
|
||||
pub fn resolve_chrome_profile(user_data_dir: &Path, input: &str) -> Result<String, String> {
|
||||
let profiles = list_chrome_profiles(user_data_dir);
|
||||
|
||||
@@ -857,6 +978,21 @@ pub fn resolve_chrome_profile(user_data_dir: &Path, input: &str) -> Result<Strin
|
||||
));
|
||||
}
|
||||
|
||||
// "auto": pick the profile Chrome last used (else "Default", else the first
|
||||
// one), so `--profile auto` reuses the real logged-in profile without the
|
||||
// user having to name it explicitly.
|
||||
if input.eq_ignore_ascii_case("auto") {
|
||||
if let Some(lu) = read_last_used_profile(user_data_dir) {
|
||||
if let Some(p) = profiles.iter().find(|p| p.directory == lu) {
|
||||
return Ok(p.directory.clone());
|
||||
}
|
||||
}
|
||||
if let Some(p) = profiles.iter().find(|p| p.directory == "Default") {
|
||||
return Ok(p.directory.clone());
|
||||
}
|
||||
return Ok(profiles[0].directory.clone());
|
||||
}
|
||||
|
||||
// Tier 1: exact directory name match
|
||||
if let Some(p) = profiles.iter().find(|p| p.directory == input) {
|
||||
return Ok(p.directory.clone());
|
||||
@@ -1245,6 +1381,37 @@ mod tests {
|
||||
use super::*;
|
||||
use crate::test_utils::EnvGuard;
|
||||
|
||||
#[test]
|
||||
fn webrtc_policy_forces_proxy_when_proxy_set() {
|
||||
let g = EnvGuard::new(&["AGENT_BROWSER_BLOCK_WEBRTC"]);
|
||||
g.remove("AGENT_BROWSER_BLOCK_WEBRTC");
|
||||
// Proxy set, no env: always force WebRTC through the proxy.
|
||||
assert_eq!(
|
||||
webrtc_ip_handling_policy(true),
|
||||
Some("disable_non_proxied_udp")
|
||||
);
|
||||
// No proxy, no env: leave WebRTC at Chrome's default.
|
||||
assert_eq!(webrtc_ip_handling_policy(false), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn webrtc_policy_opt_in_and_opt_out() {
|
||||
let g = EnvGuard::new(&["AGENT_BROWSER_BLOCK_WEBRTC"]);
|
||||
|
||||
g.set("AGENT_BROWSER_BLOCK_WEBRTC", "1");
|
||||
assert_eq!(
|
||||
webrtc_ip_handling_policy(false),
|
||||
Some("default_public_interface_only")
|
||||
);
|
||||
|
||||
// Explicit opt-out wins even when a proxy is set.
|
||||
g.set("AGENT_BROWSER_BLOCK_WEBRTC", "0");
|
||||
assert_eq!(webrtc_ip_handling_policy(true), None);
|
||||
assert_eq!(webrtc_ip_handling_policy(false), None);
|
||||
|
||||
g.remove("AGENT_BROWSER_BLOCK_WEBRTC");
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
fn spawn_noop_child() -> Child {
|
||||
Command::new("/bin/sh")
|
||||
@@ -1360,6 +1527,7 @@ mod tests {
|
||||
};
|
||||
let result = build_chrome_args(&opts).unwrap();
|
||||
assert!(result.args.iter().any(|a| a == "--headless=new"));
|
||||
assert!(result.args.iter().any(|a| a == "--hide-scrollbars"));
|
||||
assert!(result
|
||||
.args
|
||||
.iter()
|
||||
@@ -1380,6 +1548,7 @@ mod tests {
|
||||
};
|
||||
let result = build_chrome_args(&opts).unwrap();
|
||||
assert!(!result.args.iter().any(|a| a.contains("--headless")));
|
||||
assert!(!result.args.iter().any(|a| a == "--hide-scrollbars"));
|
||||
assert!(!result
|
||||
.args
|
||||
.iter()
|
||||
@@ -1434,6 +1603,23 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_build_args_hide_scrollbars_false_suppresses_default_hide_scrollbars() {
|
||||
let opts = LaunchOptions {
|
||||
headless: true,
|
||||
hide_scrollbars: false,
|
||||
..Default::default()
|
||||
};
|
||||
let result = build_chrome_args(&opts).unwrap();
|
||||
assert!(
|
||||
!result.args.iter().any(|a| a == "--hide-scrollbars"),
|
||||
"--hide-scrollbars false should suppress agent-browser's default hide switch"
|
||||
);
|
||||
if let Some(ref dir) = result.temp_user_data_dir {
|
||||
let _ = std::fs::remove_dir_all(dir);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_build_args_start_maximized_suppresses_default_window_size() {
|
||||
let opts = LaunchOptions {
|
||||
@@ -1474,6 +1660,10 @@ mod tests {
|
||||
!result.args.iter().any(|a| a.contains("--headless")),
|
||||
"headless flag should be omitted when extensions are present"
|
||||
);
|
||||
assert!(
|
||||
!result.args.iter().any(|a| a == "--hide-scrollbars"),
|
||||
"scrollbars should remain visible when extensions force headed mode"
|
||||
);
|
||||
assert!(
|
||||
!result.args.iter().any(|a| a.contains("--window-size")),
|
||||
"window-size should be omitted when extensions force headed mode"
|
||||
@@ -1579,6 +1769,44 @@ mod tests {
|
||||
assert!(!is_chrome_profile_name("relative/path"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_resolve_chrome_profile_auto_prefers_last_used() {
|
||||
let tmp = std::env::temp_dir().join("ab-auto-lastused-test");
|
||||
let _ = std::fs::remove_dir_all(&tmp);
|
||||
std::fs::create_dir_all(&tmp).unwrap();
|
||||
let local_state = serde_json::json!({
|
||||
"profile": {
|
||||
"last_used": "Profile 2",
|
||||
"info_cache": { "Default": {"name": "Person 1"}, "Profile 2": {"name": "Work"} }
|
||||
}
|
||||
});
|
||||
std::fs::write(
|
||||
tmp.join("Local State"),
|
||||
serde_json::to_string(&local_state).unwrap(),
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(resolve_chrome_profile(&tmp, "auto").unwrap(), "Profile 2");
|
||||
assert_eq!(resolve_chrome_profile(&tmp, "AUTO").unwrap(), "Profile 2");
|
||||
let _ = std::fs::remove_dir_all(&tmp);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_resolve_chrome_profile_auto_falls_back_to_default() {
|
||||
let tmp = std::env::temp_dir().join("ab-auto-default-test");
|
||||
let _ = std::fs::remove_dir_all(&tmp);
|
||||
std::fs::create_dir_all(&tmp).unwrap();
|
||||
let local_state = serde_json::json!({
|
||||
"profile": { "info_cache": { "Default": {"name": "Person 1"}, "Profile 2": {"name": "Work"} } }
|
||||
});
|
||||
std::fs::write(
|
||||
tmp.join("Local State"),
|
||||
serde_json::to_string(&local_state).unwrap(),
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(resolve_chrome_profile(&tmp, "auto").unwrap(), "Default");
|
||||
let _ = std::fs::remove_dir_all(&tmp);
|
||||
}
|
||||
|
||||
/// Helper to create a fake Chrome user-data dir with a `Local State` file.
|
||||
fn create_fake_local_state(base: &Path, profiles: &[(&str, &str)]) {
|
||||
let mut info_cache = serde_json::Map::new();
|
||||
@@ -1870,83 +2098,60 @@ mod tests {
|
||||
// auto_connect_cdp discovery-order tests (#1210, #1206)
|
||||
// -------------------------------------------------------------------
|
||||
|
||||
/// When DevToolsActivePort provides a ws_path and the port is reachable,
|
||||
/// `resolve_cdp_from_active_port` should return the exact ws_path URL
|
||||
/// WITHOUT calling HTTP discovery first.
|
||||
/// When the port is live, `resolve_cdp_from_active_port` returns the exact
|
||||
/// DevToolsActivePort ws_path URL via a consent-free TCP check — it does NOT
|
||||
/// probe with a verification WebSocket (which would burn Chrome 136+'s
|
||||
/// per-connection remote-debugging consent on a throwaway socket).
|
||||
#[tokio::test]
|
||||
async fn test_resolve_cdp_from_active_port_prefers_ws_path() {
|
||||
use futures_util::{SinkExt, StreamExt};
|
||||
use tokio_tungstenite::tungstenite::Message as WsMsg;
|
||||
|
||||
async fn test_resolve_cdp_from_active_port_returns_ws_path_without_probe() {
|
||||
// A bound listener makes the port TCP-reachable. We do NOT accept/serve
|
||||
// any WebSocket — resolve must succeed from the bare TCP check alone.
|
||||
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let port = listener.local_addr().unwrap().port();
|
||||
let ws_path = "/devtools/browser/test-uuid-1234".to_string();
|
||||
let ws_path = "/devtools/browser/test-uuid-1234";
|
||||
|
||||
let server = tokio::spawn(async move {
|
||||
// accept: verify_ws_endpoint() WebSocket handshake
|
||||
let (stream, _) = listener.accept().await.unwrap();
|
||||
let mut ws = tokio_tungstenite::accept_async(stream).await.unwrap();
|
||||
if let Some(Ok(WsMsg::Text(text))) = ws.next().await {
|
||||
let req: serde_json::Value = serde_json::from_str(&text).unwrap();
|
||||
let id = req.get("id").unwrap();
|
||||
let reply = format!(
|
||||
r#"{{"id":{},"result":{{"protocolVersion":"1.3","product":"Chrome/147"}}}}"#,
|
||||
id
|
||||
);
|
||||
ws.send(WsMsg::Text(reply)).await.unwrap();
|
||||
}
|
||||
let _ = ws.close(None).await;
|
||||
});
|
||||
|
||||
let result = resolve_cdp_from_active_port(port, &ws_path).await;
|
||||
assert!(result.is_ok(), "should succeed: {:?}", result);
|
||||
let url = result.unwrap();
|
||||
assert!(
|
||||
url.contains("test-uuid-1234"),
|
||||
"should use exact ws_path from DevToolsActivePort, got: {}",
|
||||
url
|
||||
let result = resolve_cdp_from_active_port(port, ws_path).await;
|
||||
assert!(result.is_ok(), "should succeed when port is live: {:?}", result);
|
||||
assert_eq!(
|
||||
result.unwrap(),
|
||||
format!("ws://127.0.0.1:{}{}", port, ws_path),
|
||||
"should return the exact DevToolsActivePort URL untouched"
|
||||
);
|
||||
assert_eq!(url, format!("ws://127.0.0.1:{}{}", port, ws_path));
|
||||
server.await.unwrap();
|
||||
drop(listener);
|
||||
}
|
||||
|
||||
/// When the exact ws_path connection fails, `resolve_cdp_from_active_port`
|
||||
/// should fall back to HTTP discovery.
|
||||
/// Regression guard for the consent storm: resolving the URL must only do a
|
||||
/// bare TCP connect, never a WebSocket/CDP handshake. On Chrome 136+ a
|
||||
/// handshake on a throwaway socket consumes the user's one "Allow remote
|
||||
/// debugging?" click, leaving the real connection unconsented (endless
|
||||
/// prompts / hang).
|
||||
#[tokio::test]
|
||||
async fn test_resolve_cdp_from_active_port_falls_back_to_http_discovery() {
|
||||
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
||||
async fn test_resolve_cdp_from_active_port_does_not_open_websocket() {
|
||||
use tokio::io::AsyncReadExt;
|
||||
|
||||
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let port = listener.local_addr().unwrap().port();
|
||||
|
||||
let server = tokio::spawn(async move {
|
||||
// 1st accept: verify_ws_endpoint() ws_path probe — reject (just close)
|
||||
let (s1, _) = listener.accept().await.unwrap();
|
||||
drop(s1);
|
||||
|
||||
// 2nd accept: HTTP /json/version from discover_cdp_url()
|
||||
let (mut s2, _) = listener.accept().await.unwrap();
|
||||
let mut buf = [0u8; 2048];
|
||||
let _ = s2.read(&mut buf).await;
|
||||
let body = format!(
|
||||
r#"{{"webSocketDebuggerUrl":"ws://127.0.0.1:{}/devtools/browser/fallback-uuid"}}"#,
|
||||
port
|
||||
);
|
||||
let resp = format!(
|
||||
"HTTP/1.1 200 OK\r\nContent-Length: {}\r\nContent-Type: application/json\r\n\r\n{}",
|
||||
body.len(),
|
||||
body
|
||||
);
|
||||
s2.write_all(resp.as_bytes()).await.unwrap();
|
||||
let (mut stream, _) = listener.accept().await.unwrap();
|
||||
// The liveness check connects then drops without writing anything.
|
||||
// Assert we receive no WebSocket upgrade bytes (EOF / no data).
|
||||
let mut buf = [0u8; 128];
|
||||
let read = tokio::time::timeout(
|
||||
Duration::from_millis(500),
|
||||
stream.read(&mut buf),
|
||||
)
|
||||
.await;
|
||||
match read {
|
||||
Ok(Ok(n)) => assert_eq!(n, 0, "resolve must not send a WS/CDP handshake"),
|
||||
Ok(Err(_)) | Err(_) => {} // closed or nothing sent — both fine
|
||||
}
|
||||
});
|
||||
|
||||
let result = resolve_cdp_from_active_port(port, "/devtools/browser/nonexistent-uuid").await;
|
||||
assert!(result.is_ok(), "should fall back to HTTP: {:?}", result);
|
||||
let url = result.unwrap();
|
||||
assert!(
|
||||
url.contains("fallback-uuid"),
|
||||
"should use HTTP discovery fallback, got: {}",
|
||||
url
|
||||
let result = resolve_cdp_from_active_port(port, "/devtools/browser/abc").await;
|
||||
assert_eq!(
|
||||
result.unwrap(),
|
||||
format!("ws://127.0.0.1:{}/devtools/browser/abc", port)
|
||||
);
|
||||
server.await.unwrap();
|
||||
}
|
||||
|
||||
@@ -58,8 +58,12 @@ pub async fn discover_cdp_url_with_timeout(
|
||||
match discover_cdp_ws(host, port, timeout).await {
|
||||
Ok(ws_url) => Ok(append_query(&ws_url, query)),
|
||||
Err(ws_err) => Err(format!(
|
||||
"All CDP discovery methods failed for {}:{}: /json/version: {}; /json/list: {}; WebSocket: {}",
|
||||
host, port, version_err, list_err, ws_err
|
||||
"All CDP discovery methods failed for {host}:{port}. \
|
||||
Note: Chrome 136+ no longer serves the HTTP discovery endpoints \
|
||||
(/json/version, /json/list), so `--cdp <port>` cannot find the target — \
|
||||
use the default auto-connect (just `agent-browser open <url>`), which reads \
|
||||
DevToolsActivePort and attaches over WebSocket. \
|
||||
(details: /json/version: {version_err}; /json/list: {list_err}; WebSocket: {ws_err})"
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,6 +59,10 @@ pub async fn run_daemon(session: &str) {
|
||||
}
|
||||
}
|
||||
|
||||
// Sweep temp Chrome profiles leaked by hard-killed daemons (Drop doesn't
|
||||
// run on kill -9). Only removes dirs no live process references.
|
||||
super::cdp::chrome::cleanup_orphaned_chrome_profiles();
|
||||
|
||||
let pid_path = socket_dir.join(format!("{}.pid", session));
|
||||
let _ = fs::write(&pid_path, process::id().to_string());
|
||||
|
||||
|
||||
@@ -94,6 +94,61 @@ async fn create_storage_state_with_cookie(path: &str, cookie_name: &str, cookie_
|
||||
assert_success(&resp);
|
||||
}
|
||||
|
||||
async fn send_raw_http_request(port: u64, request: &str) -> String {
|
||||
let mut stream = tokio::net::TcpStream::connect(format!("127.0.0.1:{port}"))
|
||||
.await
|
||||
.expect("HTTP client should connect to stream server");
|
||||
stream
|
||||
.write_all(request.as_bytes())
|
||||
.await
|
||||
.expect("HTTP request should be written");
|
||||
stream
|
||||
.shutdown()
|
||||
.await
|
||||
.expect("HTTP client write side should shut down");
|
||||
|
||||
let mut response = Vec::new();
|
||||
stream
|
||||
.read_to_end(&mut response)
|
||||
.await
|
||||
.expect("HTTP response should be read");
|
||||
String::from_utf8(response).expect("HTTP response should be utf-8")
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
async fn spawn_fake_daemon_socket(
|
||||
socket_dir: &std::path::Path,
|
||||
session_name: &str,
|
||||
) -> tokio::sync::oneshot::Receiver<String> {
|
||||
use tokio::io::AsyncBufReadExt;
|
||||
|
||||
let socket_path = socket_dir.join(format!("{session_name}.sock"));
|
||||
let _ = std::fs::remove_file(&socket_path);
|
||||
let listener =
|
||||
tokio::net::UnixListener::bind(&socket_path).expect("fake daemon socket should bind");
|
||||
let (tx, rx) = tokio::sync::oneshot::channel();
|
||||
|
||||
tokio::spawn(async move {
|
||||
let Ok((stream, _)) = listener.accept().await else {
|
||||
return;
|
||||
};
|
||||
let mut reader = tokio::io::BufReader::new(stream);
|
||||
let mut command = String::new();
|
||||
if reader.read_line(&mut command).await.is_err() {
|
||||
return;
|
||||
}
|
||||
|
||||
let mut stream = reader.into_inner();
|
||||
let _ = stream
|
||||
.write_all(br#"{"success":true,"data":{"ok":true}}"#)
|
||||
.await;
|
||||
let _ = stream.write_all(b"\n").await;
|
||||
let _ = tx.send(command);
|
||||
});
|
||||
|
||||
rx
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Core: launch, navigate, evaluate, url, title, close
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -363,6 +418,98 @@ async fn e2e_runtime_stream_enable_before_launch_attaches_and_disables() {
|
||||
let _ = std::fs::remove_dir_all(&socket_dir);
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[tokio::test]
|
||||
#[ignore]
|
||||
async fn e2e_stream_command_requires_same_origin_before_daemon_relay() {
|
||||
let guard = EnvGuard::new(&["AGENT_BROWSER_SOCKET_DIR", "AGENT_BROWSER_SESSION"]);
|
||||
let temp_parent = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
|
||||
.join("target")
|
||||
.join("t");
|
||||
std::fs::create_dir_all(&temp_parent).expect("socket temp parent should be created");
|
||||
let socket_dir = tempfile::Builder::new()
|
||||
.prefix("ab-e2e-")
|
||||
.tempdir_in(temp_parent)
|
||||
.expect("socket dir should be created");
|
||||
guard.set(
|
||||
"AGENT_BROWSER_SOCKET_DIR",
|
||||
socket_dir
|
||||
.path()
|
||||
.to_str()
|
||||
.expect("socket dir should be utf-8"),
|
||||
);
|
||||
guard.set("AGENT_BROWSER_SESSION", "x");
|
||||
|
||||
let mut state = DaemonState::new();
|
||||
let resp = execute_command(
|
||||
&json!({ "id": "1", "action": "stream_enable", "port": 0 }),
|
||||
&mut state,
|
||||
)
|
||||
.await;
|
||||
assert_success(&resp);
|
||||
let port = get_data(&resp)["port"]
|
||||
.as_u64()
|
||||
.expect("stream enable should report the bound port");
|
||||
|
||||
let mut daemon_command = spawn_fake_daemon_socket(socket_dir.path(), "x").await;
|
||||
let body = r#"{"action":"tabs"}"#;
|
||||
let cross_origin_request = format!(
|
||||
"POST /api/command HTTP/1.1\r\nHost: localhost:{port}\r\nOrigin: https://evil.example\r\nContent-Type: application/json\r\nContent-Length: {}\r\n\r\n{}",
|
||||
body.len(),
|
||||
body
|
||||
);
|
||||
|
||||
let response = send_raw_http_request(port, &cross_origin_request).await;
|
||||
assert!(
|
||||
response.starts_with("HTTP/1.1 403 Forbidden"),
|
||||
"unexpected cross-origin response: {response}"
|
||||
);
|
||||
assert!(
|
||||
!response.contains("Access-Control-Allow-Origin: *"),
|
||||
"forbidden command response exposed wildcard CORS: {response}"
|
||||
);
|
||||
assert!(
|
||||
tokio::time::timeout(std::time::Duration::from_millis(100), &mut daemon_command)
|
||||
.await
|
||||
.is_err(),
|
||||
"cross-origin command request reached daemon relay"
|
||||
);
|
||||
|
||||
let same_origin_request = format!(
|
||||
"POST /api/command HTTP/1.1\r\nHost: localhost:{port}\r\nOrigin: http://localhost:{port}\r\nContent-Type: application/json\r\nContent-Length: {}\r\n\r\n{}",
|
||||
body.len(),
|
||||
body
|
||||
);
|
||||
let response = send_raw_http_request(port, &same_origin_request).await;
|
||||
assert!(
|
||||
response.starts_with("HTTP/1.1 200 OK"),
|
||||
"unexpected same-origin response: {response}"
|
||||
);
|
||||
assert!(
|
||||
response.contains(&format!(
|
||||
"Access-Control-Allow-Origin: http://localhost:{port}"
|
||||
)),
|
||||
"same-origin command response did not reflect origin: {response}"
|
||||
);
|
||||
assert!(
|
||||
!response.contains("Access-Control-Allow-Origin: *"),
|
||||
"same-origin command response exposed wildcard CORS: {response}"
|
||||
);
|
||||
|
||||
let relayed = tokio::time::timeout(std::time::Duration::from_secs(1), daemon_command)
|
||||
.await
|
||||
.expect("same-origin request should reach fake daemon")
|
||||
.expect("fake daemon should return relayed command");
|
||||
assert!(relayed.contains(r#""action":"tabs""#), "{relayed}");
|
||||
|
||||
let resp = execute_command(
|
||||
&json!({ "id": "2", "action": "stream_disable" }),
|
||||
&mut state,
|
||||
)
|
||||
.await;
|
||||
assert_success(&resp);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Snapshot with refs and ref-based click
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
+386
-8
@@ -2,6 +2,7 @@ use std::collections::HashMap;
|
||||
|
||||
use serde_json::Value;
|
||||
|
||||
use super::adaptive::{self, ElementFingerprint};
|
||||
use super::cdp::client::CdpClient;
|
||||
use super::cdp::types::*;
|
||||
|
||||
@@ -13,6 +14,9 @@ pub struct RefEntry {
|
||||
pub nth: Option<usize>,
|
||||
pub selector: Option<String>,
|
||||
pub frame_id: Option<String>,
|
||||
/// AX fingerprint captured at snapshot time, used by adaptive relocation when
|
||||
/// the node is gone and the role/name/nth re-query also fails.
|
||||
pub fingerprint: Option<ElementFingerprint>,
|
||||
}
|
||||
|
||||
pub struct RefMap {
|
||||
@@ -57,10 +61,19 @@ impl RefMap {
|
||||
nth,
|
||||
selector: None,
|
||||
frame_id: frame_id.map(|s| s.to_string()),
|
||||
fingerprint: None,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
/// Attach an AX fingerprint to an existing ref (set during snapshot, used by
|
||||
/// adaptive relocation). No-op if the ref is unknown.
|
||||
pub fn set_fingerprint(&mut self, ref_id: &str, fingerprint: ElementFingerprint) {
|
||||
if let Some(entry) = self.map.get_mut(ref_id) {
|
||||
entry.fingerprint = Some(fingerprint);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_selector(
|
||||
&mut self,
|
||||
ref_id: String,
|
||||
@@ -78,6 +91,7 @@ impl RefMap {
|
||||
nth,
|
||||
selector: Some(selector),
|
||||
frame_id: None,
|
||||
fingerprint: None,
|
||||
},
|
||||
);
|
||||
}
|
||||
@@ -146,6 +160,46 @@ pub fn parse_ref(input: &str) -> Option<String> {
|
||||
None
|
||||
}
|
||||
|
||||
/// When a saved `@ref`'s node is gone and the role/name/nth re-query also failed,
|
||||
/// try to relocate the element by AX fingerprint similarity. Returns the chosen
|
||||
/// backend node id only when confident (high score + clear margin over the
|
||||
/// runner-up). Opt out with `AGENT_BROWSER_ADAPTIVE_REF=0`.
|
||||
async fn relocate_stale_ref(
|
||||
client: &CdpClient,
|
||||
ref_id: &str,
|
||||
entry: &RefEntry,
|
||||
session_id: &str,
|
||||
iframe_sessions: &HashMap<String, String>,
|
||||
) -> Option<i64> {
|
||||
if std::env::var("AGENT_BROWSER_ADAPTIVE_REF").as_deref() == Ok("0") {
|
||||
return None;
|
||||
}
|
||||
let baseline = entry.fingerprint.as_ref()?;
|
||||
let candidates = super::snapshot::collect_current_fingerprints(
|
||||
client,
|
||||
session_id,
|
||||
entry.frame_id.as_deref(),
|
||||
iframe_sessions,
|
||||
)
|
||||
.await
|
||||
.ok()?;
|
||||
match adaptive::pick_best(
|
||||
baseline,
|
||||
&candidates,
|
||||
adaptive::ADAPTIVE_THRESHOLD,
|
||||
adaptive::ADAPTIVE_MARGIN,
|
||||
) {
|
||||
Ok(reloc) => {
|
||||
eprintln!(
|
||||
"[adaptive] relocated {ref_id} ({} \"{}\") score={:.2} second={:.2} -> backendNodeId {}",
|
||||
entry.role, entry.name, reloc.score, reloc.second_score, reloc.backend_node_id
|
||||
);
|
||||
Some(reloc.backend_node_id)
|
||||
}
|
||||
Err(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn resolve_element_center(
|
||||
client: &CdpClient,
|
||||
session_id: &str,
|
||||
@@ -163,11 +217,44 @@ pub async fn resolve_element_center(
|
||||
|
||||
// Try cached backend_node_id first (fast path)
|
||||
if let Some(backend_node_id) = entry.backend_node_id {
|
||||
let mut active_id = backend_node_id;
|
||||
// Identity check: React often re-uses the same DOM node when
|
||||
// re-rendering — backendNodeId stays the same but accessibleName
|
||||
// / role changes. Without this verification, `click @e20` (saved
|
||||
// when the button said "Add post") happily clicks the *same*
|
||||
// node that now says "Post all", silently submitting the thread.
|
||||
//
|
||||
// On mismatch, try adaptive fingerprint relocation before failing:
|
||||
// a confident high-score/high-margin match is a stronger identity
|
||||
// signal than role+name, and lets a moved+renamed element still
|
||||
// resolve. If relocation isn't confident, surface the original
|
||||
// identity error. Set AGENT_BROWSER_VERIFY_REF=0 to skip the check
|
||||
// (and thus relocation) entirely.
|
||||
if std::env::var("AGENT_BROWSER_VERIFY_REF").as_deref() != Ok("0") {
|
||||
if let Err(e) = verify_ref_identity(
|
||||
client,
|
||||
effective_session_id,
|
||||
backend_node_id,
|
||||
&ref_id,
|
||||
&entry.role,
|
||||
&entry.name,
|
||||
)
|
||||
.await
|
||||
{
|
||||
match relocate_stale_ref(client, &ref_id, entry, session_id, iframe_sessions)
|
||||
.await
|
||||
{
|
||||
Some(id) => active_id = id,
|
||||
None => return Err(e),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let result: Result<DomGetBoxModelResult, String> = client
|
||||
.send_command_typed(
|
||||
"DOM.getBoxModel",
|
||||
&DomGetBoxModelParams {
|
||||
backend_node_id: Some(backend_node_id),
|
||||
backend_node_id: Some(active_id),
|
||||
node_id: None,
|
||||
object_id: None,
|
||||
},
|
||||
@@ -177,13 +264,33 @@ pub async fn resolve_element_center(
|
||||
|
||||
if let Ok(r) = result {
|
||||
let (x, y) = box_model_center(&r.model);
|
||||
// Occlusion check: a transient overlay (X.com's "click
|
||||
// outside to close" mask, modal backdrop, sticky banner,
|
||||
// etc.) can land on top of our target between snapshot
|
||||
// and click. Coordinates are correct, but
|
||||
// `document.elementFromPoint(x, y)` returns the overlay
|
||||
// — and the click goes to the overlay's handler, not
|
||||
// ours. Catch it here so the user gets "occluded by
|
||||
// DIV[testid=mask]" instead of "modal silently closed +
|
||||
// thread submitted by accident".
|
||||
//
|
||||
// Set AGENT_BROWSER_VERIFY_CLICK_TARGET=0 to skip.
|
||||
if std::env::var("AGENT_BROWSER_VERIFY_CLICK_TARGET").as_deref() != Ok("0") {
|
||||
if let Err(e) =
|
||||
verify_click_target(client, effective_session_id, active_id, &ref_id, x, y)
|
||||
.await
|
||||
{
|
||||
return Err(e);
|
||||
}
|
||||
}
|
||||
return Ok((x, y, effective_session_id.to_string()));
|
||||
}
|
||||
// backend_node_id is stale; re-query the accessibility tree below
|
||||
}
|
||||
|
||||
// Fallback: re-query the accessibility tree to find a fresh node by role/name
|
||||
let fresh_id = find_node_id_by_role_name(
|
||||
// Fallback: re-query the accessibility tree to find a fresh node by role/name.
|
||||
// If that fails, try adaptive fingerprint relocation before giving up.
|
||||
let fresh_id = match find_node_id_by_role_name(
|
||||
client,
|
||||
session_id,
|
||||
&entry.role,
|
||||
@@ -192,7 +299,16 @@ pub async fn resolve_element_center(
|
||||
entry.frame_id.as_deref(),
|
||||
iframe_sessions,
|
||||
)
|
||||
.await?;
|
||||
.await
|
||||
{
|
||||
Ok(id) => id,
|
||||
Err(e) => match relocate_stale_ref(client, &ref_id, entry, session_id, iframe_sessions)
|
||||
.await
|
||||
{
|
||||
Some(id) => id,
|
||||
None => return Err(e),
|
||||
},
|
||||
};
|
||||
let result: DomGetBoxModelResult = client
|
||||
.send_command_typed(
|
||||
"DOM.getBoxModel",
|
||||
@@ -230,11 +346,36 @@ pub async fn resolve_element_object_id(
|
||||
|
||||
// Try cached backend_node_id first (fast path)
|
||||
if let Some(backend_node_id) = entry.backend_node_id {
|
||||
let mut active_id = backend_node_id;
|
||||
// Same identity guard as resolve_element_center — see that
|
||||
// function for why React DOM-node-reuse breaks ref-based
|
||||
// interactions if we skip this, and why a confident adaptive
|
||||
// relocation is allowed to override an identity mismatch.
|
||||
if std::env::var("AGENT_BROWSER_VERIFY_REF").as_deref() != Ok("0") {
|
||||
if let Err(e) = verify_ref_identity(
|
||||
client,
|
||||
effective_session_id,
|
||||
backend_node_id,
|
||||
&ref_id,
|
||||
&entry.role,
|
||||
&entry.name,
|
||||
)
|
||||
.await
|
||||
{
|
||||
match relocate_stale_ref(client, &ref_id, entry, session_id, iframe_sessions)
|
||||
.await
|
||||
{
|
||||
Some(id) => active_id = id,
|
||||
None => return Err(e),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let result: Result<DomResolveNodeResult, String> = client
|
||||
.send_command_typed(
|
||||
"DOM.resolveNode",
|
||||
&DomResolveNodeParams {
|
||||
backend_node_id: Some(backend_node_id),
|
||||
backend_node_id: Some(active_id),
|
||||
node_id: None,
|
||||
object_group: Some("agent-browser".to_string()),
|
||||
},
|
||||
@@ -250,8 +391,9 @@ pub async fn resolve_element_object_id(
|
||||
// backend_node_id is stale; re-query the accessibility tree below
|
||||
}
|
||||
|
||||
// Fallback: re-query the accessibility tree to find a fresh node by role/name
|
||||
let fresh_id = find_node_id_by_role_name(
|
||||
// Fallback: re-query the accessibility tree to find a fresh node by role/name.
|
||||
// If that fails, try adaptive fingerprint relocation before giving up.
|
||||
let fresh_id = match find_node_id_by_role_name(
|
||||
client,
|
||||
session_id,
|
||||
&entry.role,
|
||||
@@ -260,7 +402,16 @@ pub async fn resolve_element_object_id(
|
||||
entry.frame_id.as_deref(),
|
||||
iframe_sessions,
|
||||
)
|
||||
.await?;
|
||||
.await
|
||||
{
|
||||
Ok(id) => id,
|
||||
Err(e) => match relocate_stale_ref(client, &ref_id, entry, session_id, iframe_sessions)
|
||||
.await
|
||||
{
|
||||
Some(id) => id,
|
||||
None => return Err(e),
|
||||
},
|
||||
};
|
||||
let result: DomResolveNodeResult = client
|
||||
.send_command_typed(
|
||||
"DOM.resolveNode",
|
||||
@@ -333,6 +484,233 @@ fn resolve_frame_session<'a>(
|
||||
.unwrap_or(session_id)
|
||||
}
|
||||
|
||||
/// Verify that the cached backendNodeId still has the same accessible role
|
||||
/// and name it had when the snapshot ran. Catches the case where React (or
|
||||
/// any reconciler) reused the DOM node for a different component instance
|
||||
/// — same physical node, different semantics.
|
||||
///
|
||||
/// On mismatch, returns an actionable error naming both the snapshot label
|
||||
/// and the current label so the agent can re-snapshot intelligently.
|
||||
/// On any CDP failure (e.g. node deleted), returns Ok(()) so the caller's
|
||||
/// existing fallback (`find_node_id_by_role_name`) takes over.
|
||||
async fn verify_ref_identity(
|
||||
client: &CdpClient,
|
||||
session_id: &str,
|
||||
backend_node_id: i64,
|
||||
ref_id: &str,
|
||||
expected_role: &str,
|
||||
expected_name: &str,
|
||||
) -> Result<(), String> {
|
||||
let params = serde_json::json!({
|
||||
"backendNodeId": backend_node_id,
|
||||
"fetchRelatives": false,
|
||||
});
|
||||
// Tight 1s timeout: this is a defensive guard, not a critical path.
|
||||
// The default 30s CDP timeout was the dominant factor in the
|
||||
// "click hangs 5+ minutes" report — three CDP calls (verify +
|
||||
// resolveNode + paint-settle) at 30s each, multiplied by parallel
|
||||
// click invocations queueing on the daemon, totalled multi-minute
|
||||
// user-visible hangs. Cap our own helper so a stuck AX query
|
||||
// doesn't make `click` worse than the no-guard version was.
|
||||
let resp: Result<GetFullAXTreeResult, String> = match tokio::time::timeout(
|
||||
std::time::Duration::from_secs(1),
|
||||
client.send_command_typed("Accessibility.getPartialAXTree", ¶ms, Some(session_id)),
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(r) => r,
|
||||
// Timeout: skip identity verification rather than block the click.
|
||||
Err(_) => return Ok(()),
|
||||
};
|
||||
let Ok(tree) = resp else {
|
||||
// Node likely gone; let the box-model call fail and trigger fallback.
|
||||
return Ok(());
|
||||
};
|
||||
// Find the AXNode for our backendNodeId. fetchRelatives=false still
|
||||
// returns ancestors; the target node has the matching backendNodeId.
|
||||
let Some(node) = tree
|
||||
.nodes
|
||||
.iter()
|
||||
.find(|n| n.backend_d_o_m_node_id == Some(backend_node_id))
|
||||
else {
|
||||
return Ok(());
|
||||
};
|
||||
let actual_role = extract_ax_string(&node.role);
|
||||
let actual_name = extract_ax_string(&node.name);
|
||||
if actual_role == expected_role && actual_name == expected_name {
|
||||
return Ok(());
|
||||
}
|
||||
Err(format!(
|
||||
"Ref {} no longer matches its snapshot. Was [{} \"{}\"], now [{} \"{}\"].\n\
|
||||
The DOM mutated between snapshot and interaction (typical with React/Vue \
|
||||
reusing nodes during re-render). Take a fresh snapshot, then re-target.\n\
|
||||
To bypass this guard set AGENT_BROWSER_VERIFY_REF=0.",
|
||||
ref_id, expected_role, expected_name, actual_role, actual_name,
|
||||
))
|
||||
}
|
||||
|
||||
/// At the moment we'd dispatch the click, ask the page itself which element
|
||||
/// occupies (x, y). If it's not our target (and not a descendant or
|
||||
/// ancestor), an overlay has appeared between snapshot and click — we'd
|
||||
/// silently click the overlay otherwise. Returns Err with details about
|
||||
/// the occluding element so the caller can wait + re-snapshot.
|
||||
///
|
||||
/// Implemented as a single Runtime.callFunctionOn: resolve the cached
|
||||
/// backendNodeId to a remote object, then run a function on it that
|
||||
/// compares with elementFromPoint. The function returns null when the
|
||||
/// click is safe and a JSON string with diagnostic info when it isn't.
|
||||
async fn verify_click_target(
|
||||
client: &CdpClient,
|
||||
session_id: &str,
|
||||
backend_node_id: i64,
|
||||
ref_id: &str,
|
||||
x: f64,
|
||||
y: f64,
|
||||
) -> Result<(), String> {
|
||||
use serde::Deserialize;
|
||||
|
||||
// Resolve once. backendNodeId is stable across renders; only the
|
||||
// element under (x, y) is what changes when an overlay flickers.
|
||||
let resolve_params = DomResolveNodeParams {
|
||||
backend_node_id: Some(backend_node_id),
|
||||
node_id: None,
|
||||
object_group: Some("agent-browser-occlusion".to_string()),
|
||||
};
|
||||
let resolve_fut = client.send_command_typed::<_, serde_json::Value>(
|
||||
"DOM.resolveNode",
|
||||
&resolve_params,
|
||||
Some(session_id),
|
||||
);
|
||||
let Ok(resolve_resp) =
|
||||
tokio::time::timeout(std::time::Duration::from_millis(500), resolve_fut).await
|
||||
else {
|
||||
return Ok(());
|
||||
};
|
||||
let Ok(resolved) = resolve_resp else { return Ok(()) };
|
||||
let Some(object_id) = resolved
|
||||
.get("object")
|
||||
.and_then(|o| o.get("objectId"))
|
||||
.and_then(|v| v.as_str())
|
||||
else {
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
// Auto-retry on transient occlusion. Many real-world overlays
|
||||
// (modal backdrops, focus rings, click-outside masks) blink in for
|
||||
// a frame or two during state transitions and clear on their own.
|
||||
// Without retries the user gets an "occluded" error and has to
|
||||
// wrap every click in their own retry loop. With retries the
|
||||
// common case is invisible — only persistent overlays surface.
|
||||
//
|
||||
// AGENT_BROWSER_OCCLUSION_RETRIES (default 3, 0 disables)
|
||||
// AGENT_BROWSER_OCCLUSION_RETRY_DELAY_MS (default 200)
|
||||
let max_retries: u32 = std::env::var("AGENT_BROWSER_OCCLUSION_RETRIES")
|
||||
.ok()
|
||||
.and_then(|v| v.parse().ok())
|
||||
.unwrap_or(3);
|
||||
let retry_delay_ms: u64 = std::env::var("AGENT_BROWSER_OCCLUSION_RETRY_DELAY_MS")
|
||||
.ok()
|
||||
.and_then(|v| v.parse().ok())
|
||||
.unwrap_or(200);
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct Occluder {
|
||||
tag: Option<String>,
|
||||
testid: Option<String>,
|
||||
role: Option<String>,
|
||||
#[serde(rename = "ariaLabel")]
|
||||
aria_label: Option<String>,
|
||||
text: Option<String>,
|
||||
reason: Option<String>,
|
||||
}
|
||||
|
||||
// function(x, y) { ... } where `this` is the target element.
|
||||
// Return null → click is safe.
|
||||
// Return JSON → describes the occluding element.
|
||||
let function_decl = "function(x, y) { \
|
||||
const at = document.elementFromPoint(x, y); \
|
||||
if (!at) return JSON.stringify({reason:'no-element-at-point'}); \
|
||||
if (at === this || this.contains(at) || at.contains(this)) return null; \
|
||||
return JSON.stringify({ \
|
||||
tag: at.tagName, \
|
||||
testid: (at.dataset && at.dataset.testid) || null, \
|
||||
role: at.getAttribute('role'), \
|
||||
ariaLabel: at.getAttribute('aria-label'), \
|
||||
text: ((at.textContent||'').trim().slice(0, 60)) \
|
||||
}); \
|
||||
}";
|
||||
|
||||
let mut last_occ: Option<Occluder> = None;
|
||||
for attempt in 0..=max_retries {
|
||||
if attempt > 0 {
|
||||
tokio::time::sleep(std::time::Duration::from_millis(retry_delay_ms)).await;
|
||||
}
|
||||
let call_params = serde_json::json!({
|
||||
"objectId": object_id,
|
||||
"functionDeclaration": function_decl,
|
||||
"arguments": [{"value": x}, {"value": y}],
|
||||
"returnByValue": true,
|
||||
});
|
||||
let call_fut = client.send_command_typed::<_, serde_json::Value>(
|
||||
"Runtime.callFunctionOn",
|
||||
&call_params,
|
||||
Some(session_id),
|
||||
);
|
||||
let Ok(call_resp) =
|
||||
tokio::time::timeout(std::time::Duration::from_millis(500), call_fut).await
|
||||
else {
|
||||
return Ok(()); // probe itself stalled — fall through to click
|
||||
};
|
||||
let Ok(call_result) = call_resp else {
|
||||
return Ok(());
|
||||
};
|
||||
let value = call_result.get("result").and_then(|r| r.get("value"));
|
||||
let json_str = match value {
|
||||
Some(serde_json::Value::String(s)) => s.clone(),
|
||||
// null / undefined → element at point IS our target. Safe.
|
||||
_ => return Ok(()),
|
||||
};
|
||||
let occ: Occluder = match serde_json::from_str(&json_str) {
|
||||
Ok(v) => v,
|
||||
Err(_) => return Ok(()),
|
||||
};
|
||||
last_occ = Some(occ);
|
||||
}
|
||||
|
||||
// All retries exhausted — overlay is sticky. Build the descriptive error.
|
||||
let occ = last_occ.expect("loop ran at least once");
|
||||
if let Some(reason) = occ.reason {
|
||||
return Err(format!(
|
||||
"Ref {} cannot be clicked at its computed position: {}. \
|
||||
The element may have moved off-screen — re-run snapshot.",
|
||||
ref_id, reason
|
||||
));
|
||||
}
|
||||
let mut desc = occ.tag.unwrap_or_else(|| "unknown".to_string());
|
||||
if let Some(t) = occ.testid {
|
||||
desc.push_str(&format!("[testid={}]", t));
|
||||
}
|
||||
if let Some(r) = occ.role {
|
||||
desc.push_str(&format!("[role={}]", r));
|
||||
}
|
||||
if let Some(a) = occ.aria_label {
|
||||
desc.push_str(&format!("[aria-label=\"{}\"]", a));
|
||||
}
|
||||
if let Some(t) = occ.text {
|
||||
if !t.is_empty() {
|
||||
desc.push_str(&format!(" text=\"{}\"", t));
|
||||
}
|
||||
}
|
||||
let waited_ms = (max_retries as u64) * retry_delay_ms;
|
||||
Err(format!(
|
||||
"Ref {} is occluded by {} at the click point (still occluded after \
|
||||
{} retries / {}ms). A persistent overlay is in the way — \
|
||||
re-run snapshot, dismiss the overlay, or set \
|
||||
AGENT_BROWSER_VERIFY_CLICK_TARGET=0 to bypass.",
|
||||
ref_id, desc, max_retries, waited_ms,
|
||||
))
|
||||
}
|
||||
|
||||
/// Re-query the accessibility tree to find a node matching role+name+nth,
|
||||
/// returning its fresh backendDOMNodeId. This uses the same data source
|
||||
/// (Accessibility.getFullAXTree) that built the ref map during snapshot,
|
||||
|
||||
@@ -15,7 +15,111 @@ pub async fn click(
|
||||
click_count: i32,
|
||||
iframe_sessions: &HashMap<String, String>,
|
||||
) -> Result<(), String> {
|
||||
let (x, y, effective_session_id) = resolve_element_center(
|
||||
// AGENT_BROWSER_CLICK_MODE: "" (default) = coordinate click with a DOM
|
||||
// fallback; "coord" = strict coordinate only (no fallback); "dom" = always
|
||||
// dispatch through the DOM.
|
||||
let mode = std::env::var("AGENT_BROWSER_CLICK_MODE").unwrap_or_default();
|
||||
|
||||
// (A) Scroll the target into view first so the computed coordinates land
|
||||
// inside the viewport. Without this, an element below the fold (or revealed
|
||||
// after scroll/popup) yields off-viewport coordinates and the click lands on
|
||||
// whatever currently occupies that point. Best-effort: ignore failures.
|
||||
scroll_into_view_if_needed(client, session_id, ref_map, selector_or_ref, iframe_sessions).await;
|
||||
|
||||
if mode == "dom" {
|
||||
return dom_click(client, session_id, ref_map, selector_or_ref, iframe_sessions).await;
|
||||
}
|
||||
|
||||
let resolved = resolve_element_center(
|
||||
client,
|
||||
session_id,
|
||||
ref_map,
|
||||
selector_or_ref,
|
||||
iframe_sessions,
|
||||
)
|
||||
.await;
|
||||
|
||||
match resolved {
|
||||
Ok((x, y, effective_session_id)) => {
|
||||
dispatch_click(client, &effective_session_id, x, y, button, click_count).await
|
||||
}
|
||||
Err(e) => {
|
||||
// (B) The coordinate path failed — typically a persistent overlay
|
||||
// failing the occlusion guard, or coordinates that won't resolve.
|
||||
// Fall back to a DOM-dispatched `.click()` on the intended element,
|
||||
// which targets the element directly instead of a screen point.
|
||||
// Skipped for strict "coord" mode and for non-left / multi-clicks
|
||||
// (a DOM `.click()` can't express right/middle/double semantics).
|
||||
if mode == "coord" || button != "left" || click_count != 1 {
|
||||
return Err(e);
|
||||
}
|
||||
eprintln!(
|
||||
"[click] coordinate click failed ({e}); falling back to DOM dispatch \
|
||||
(set AGENT_BROWSER_CLICK_MODE=coord to disable)"
|
||||
);
|
||||
dom_click(client, session_id, ref_map, selector_or_ref, iframe_sessions)
|
||||
.await
|
||||
.map_err(|dom_err| format!("{e}\n(DOM-dispatch fallback also failed: {dom_err})"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Best-effort scroll-into-view before a coordinate click. Uses Chrome's
|
||||
/// `scrollIntoViewIfNeeded` (only scrolls when not already fully visible),
|
||||
/// falling back to centered `scrollIntoView`. Resolution failures are ignored —
|
||||
/// the subsequent resolve will surface a real "not found" error.
|
||||
async fn scroll_into_view_if_needed(
|
||||
client: &CdpClient,
|
||||
session_id: &str,
|
||||
ref_map: &RefMap,
|
||||
selector_or_ref: &str,
|
||||
iframe_sessions: &HashMap<String, String>,
|
||||
) {
|
||||
let Ok((object_id, effective_session_id)) = resolve_element_object_id(
|
||||
client,
|
||||
session_id,
|
||||
ref_map,
|
||||
selector_or_ref,
|
||||
iframe_sessions,
|
||||
)
|
||||
.await
|
||||
else {
|
||||
return;
|
||||
};
|
||||
let js = "function() { try { \
|
||||
if (typeof this.scrollIntoViewIfNeeded === 'function') { this.scrollIntoViewIfNeeded(true); } \
|
||||
else { this.scrollIntoView({ block: 'center', inline: 'center' }); } \
|
||||
} catch (e) {} }";
|
||||
let _ = client
|
||||
.send_command_typed::<_, Value>(
|
||||
"Runtime.callFunctionOn",
|
||||
&CallFunctionOnParams {
|
||||
function_declaration: js.to_string(),
|
||||
object_id: Some(object_id),
|
||||
arguments: None,
|
||||
return_by_value: Some(true),
|
||||
await_promise: Some(false),
|
||||
},
|
||||
Some(&effective_session_id),
|
||||
)
|
||||
.await;
|
||||
// Let the scroll settle so the following getBoxModel sees final coordinates.
|
||||
wait_for_paint_settled(client, &effective_session_id).await;
|
||||
}
|
||||
|
||||
/// Dispatch a click through the DOM (`element.click()`) instead of via screen
|
||||
/// coordinates. Targets the intended element directly, so it works when a
|
||||
/// floating layer occludes the click point or the element sits in a portal that
|
||||
/// confuses `elementFromPoint`. Used as the fallback for `click` and when
|
||||
/// `AGENT_BROWSER_CLICK_MODE=dom`.
|
||||
async fn dom_click(
|
||||
client: &CdpClient,
|
||||
session_id: &str,
|
||||
ref_map: &RefMap,
|
||||
selector_or_ref: &str,
|
||||
iframe_sessions: &HashMap<String, String>,
|
||||
) -> Result<(), String> {
|
||||
let (object_id, effective_session_id) = resolve_element_object_id(
|
||||
client,
|
||||
session_id,
|
||||
ref_map,
|
||||
@@ -23,7 +127,21 @@ pub async fn click(
|
||||
iframe_sessions,
|
||||
)
|
||||
.await?;
|
||||
dispatch_click(client, &effective_session_id, x, y, button, click_count).await
|
||||
client
|
||||
.send_command_typed::<_, Value>(
|
||||
"Runtime.callFunctionOn",
|
||||
&CallFunctionOnParams {
|
||||
function_declaration: "function() { this.click(); }".to_string(),
|
||||
object_id: Some(object_id),
|
||||
arguments: None,
|
||||
return_by_value: Some(true),
|
||||
await_promise: Some(false),
|
||||
},
|
||||
Some(&effective_session_id),
|
||||
)
|
||||
.await?;
|
||||
wait_for_paint_settled(client, &effective_session_id).await;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn dblclick(
|
||||
@@ -884,6 +1002,46 @@ pub async fn tap_touch(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// After a click is dispatched, give the page two animation frames + a
|
||||
/// microtask boundary to let React/Vue/Svelte commit any state update
|
||||
/// scheduled by the click handler. Without this wait, follow-up commands
|
||||
/// (e.g. `inserttext` against the textbox the click was supposed to mount)
|
||||
/// race the renderer and can land on stale or wrong elements.
|
||||
///
|
||||
/// The wait is bounded to ~33ms in the common case (two RAFs at 60fps) and
|
||||
/// returns immediately on any error — never an exception path.
|
||||
///
|
||||
/// Set `AGENT_BROWSER_CLICK_WAIT_STABLE=0` to disable for perf-sensitive
|
||||
/// scripts that don't drive SPA UIs.
|
||||
async fn wait_for_paint_settled(client: &CdpClient, session_id: &str) {
|
||||
if std::env::var("AGENT_BROWSER_CLICK_WAIT_STABLE").as_deref() == Ok("0") {
|
||||
return;
|
||||
}
|
||||
let script = "new Promise(resolve => \
|
||||
requestAnimationFrame(() => \
|
||||
requestAnimationFrame(() => \
|
||||
queueMicrotask(() => resolve(true)))))";
|
||||
// Tight 500ms timeout. RAF normally fires at 16ms, two RAFs total ~33ms.
|
||||
// If the tab is hidden / throttled / page is doing something pathological
|
||||
// and RAF doesn't fire in 500ms, we'd rather return now than stall the
|
||||
// user's click. Without this cap, a stuck RAF inherited the default 30s
|
||||
// CDP timeout and was the main contributor to the "click hangs 5+ min"
|
||||
// user report.
|
||||
let _ = tokio::time::timeout(
|
||||
std::time::Duration::from_millis(500),
|
||||
client.send_command_typed::<_, Value>(
|
||||
"Runtime.evaluate",
|
||||
&EvaluateParams {
|
||||
expression: script.to_string(),
|
||||
return_by_value: Some(true),
|
||||
await_promise: Some(true),
|
||||
},
|
||||
Some(session_id),
|
||||
),
|
||||
)
|
||||
.await;
|
||||
}
|
||||
|
||||
async fn dispatch_click(
|
||||
client: &CdpClient,
|
||||
session_id: &str,
|
||||
@@ -955,6 +1113,7 @@ async fn dispatch_click(
|
||||
)
|
||||
.await?;
|
||||
|
||||
wait_for_paint_settled(client, session_id).await;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
#[allow(dead_code)]
|
||||
pub mod actions;
|
||||
#[allow(dead_code)]
|
||||
pub mod adaptive;
|
||||
#[allow(dead_code)]
|
||||
pub mod auth;
|
||||
#[allow(dead_code)]
|
||||
pub mod browser;
|
||||
|
||||
@@ -6,6 +6,7 @@ use super::cdp::client::CdpClient;
|
||||
use super::cdp::types::{
|
||||
AXNode, AXProperty, AXValue, EvaluateParams, EvaluateResult, GetFullAXTreeResult,
|
||||
};
|
||||
use super::adaptive::ElementFingerprint;
|
||||
use super::element::{resolve_ax_session, RefMap};
|
||||
|
||||
const INTERACTIVE_ROLES: &[&str] = &[
|
||||
@@ -148,6 +149,122 @@ impl TreeNode {
|
||||
}
|
||||
}
|
||||
|
||||
/// Build an AX fingerprint for a tree node, used by adaptive @ref relocation.
|
||||
/// Pulls only data already in the AX tree (no extra CDP calls): role as `tag`,
|
||||
/// accessible name as `text`, a few discriminating AX properties as `attrs`, and
|
||||
/// the ancestor/parent/sibling structure from the tree links.
|
||||
fn build_ax_fingerprint(tree_nodes: &[TreeNode], idx: usize) -> ElementFingerprint {
|
||||
let node = &tree_nodes[idx];
|
||||
|
||||
let mut attrs = std::collections::BTreeMap::new();
|
||||
if let Some(v) = &node.value_text {
|
||||
if !v.is_empty() {
|
||||
attrs.insert("value".to_string(), v.clone());
|
||||
}
|
||||
}
|
||||
if let Some(u) = &node.url {
|
||||
if !u.is_empty() {
|
||||
attrs.insert("url".to_string(), u.clone());
|
||||
}
|
||||
}
|
||||
if let Some(l) = node.level {
|
||||
attrs.insert("level".to_string(), l.to_string());
|
||||
}
|
||||
if let Some(c) = &node.checked {
|
||||
attrs.insert("checked".to_string(), c.clone());
|
||||
}
|
||||
|
||||
// Ancestor roles, nearest first, capped to keep the signature stable.
|
||||
let mut ancestors = Vec::new();
|
||||
let mut cur = node.parent_idx;
|
||||
while let Some(pidx) = cur {
|
||||
if ancestors.len() >= 6 {
|
||||
break;
|
||||
}
|
||||
let role = tree_nodes[pidx].role.clone();
|
||||
if !role.is_empty() {
|
||||
ancestors.push(role);
|
||||
}
|
||||
cur = tree_nodes[pidx].parent_idx;
|
||||
}
|
||||
|
||||
let (parent_tag, parent_text) = node
|
||||
.parent_idx
|
||||
.map(|pidx| (tree_nodes[pidx].role.clone(), tree_nodes[pidx].name.clone()))
|
||||
.unwrap_or_default();
|
||||
|
||||
// Position among same-role siblings under the same parent.
|
||||
let (sibling_index, sibling_count) = match node.parent_idx {
|
||||
Some(pidx) => {
|
||||
let mut count = 0u32;
|
||||
let mut index = 0u32;
|
||||
for &child in &tree_nodes[pidx].children {
|
||||
if tree_nodes[child].role == node.role {
|
||||
if child == idx {
|
||||
index = count;
|
||||
}
|
||||
count += 1;
|
||||
}
|
||||
}
|
||||
(index, count)
|
||||
}
|
||||
None => (0, 0),
|
||||
};
|
||||
|
||||
ElementFingerprint {
|
||||
tag: node.role.clone(),
|
||||
text: node.name.clone(),
|
||||
attrs,
|
||||
ancestors,
|
||||
parent_tag,
|
||||
parent_text,
|
||||
sibling_index,
|
||||
sibling_count,
|
||||
}
|
||||
}
|
||||
|
||||
/// Collect AX fingerprints for every node that has a backend node id, used as the
|
||||
/// candidate set when relocating a stale @ref. Reuses the same extraction as the
|
||||
/// baseline so the two are scored in the same space.
|
||||
fn collect_fingerprints(tree_nodes: &[TreeNode]) -> Vec<(i64, ElementFingerprint)> {
|
||||
tree_nodes
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter_map(|(idx, n)| {
|
||||
n.backend_node_id
|
||||
.map(|bid| (bid, build_ax_fingerprint(tree_nodes, idx)))
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Fetch a fresh AX tree for the given frame and return `(backend_node_id,
|
||||
/// fingerprint)` for every node — the candidate set for adaptive @ref
|
||||
/// relocation. One `getFullAXTree` call, no per-element work.
|
||||
pub(super) async fn collect_current_fingerprints(
|
||||
client: &CdpClient,
|
||||
session_id: &str,
|
||||
frame_id: Option<&str>,
|
||||
iframe_sessions: &HashMap<String, String>,
|
||||
) -> Result<Vec<(i64, ElementFingerprint)>, String> {
|
||||
let (ax_params, effective_session_id) =
|
||||
resolve_ax_session(frame_id, session_id, iframe_sessions);
|
||||
let _ = client
|
||||
.send_command_no_params("DOM.enable", Some(effective_session_id))
|
||||
.await;
|
||||
let _ = client
|
||||
.send_command_no_params("Accessibility.enable", Some(effective_session_id))
|
||||
.await;
|
||||
let ax_tree: GetFullAXTreeResult = client
|
||||
.send_command_typed(
|
||||
"Accessibility.getFullAXTree",
|
||||
&ax_params,
|
||||
Some(effective_session_id),
|
||||
)
|
||||
.await?;
|
||||
let (tree_nodes, _roots) = build_tree(&ax_tree.nodes);
|
||||
Ok(collect_fingerprints(&tree_nodes))
|
||||
}
|
||||
|
||||
/// The type of a hidden form input found inside a cursor-interactive element.
|
||||
#[derive(Clone, Copy)]
|
||||
enum HiddenInputKind {
|
||||
@@ -397,6 +514,7 @@ pub async fn take_snapshot(
|
||||
actual_nth,
|
||||
frame_id,
|
||||
);
|
||||
ref_map.set_fingerprint(&ref_id, build_ax_fingerprint(&tree_nodes, *idx));
|
||||
|
||||
tree_nodes[*idx].has_ref = true;
|
||||
tree_nodes[*idx].ref_id = Some(ref_id);
|
||||
|
||||
+139
-2
@@ -51,13 +51,18 @@ pub fn build_stealth_script(mode: StealthMode, locale: Option<&str>) -> String {
|
||||
vec![locale, base_lang]
|
||||
};
|
||||
let config_line = format!(
|
||||
r#"const __abStealth = {{ locale: "{}", languages: {}, allowWebGLContextFallback: false }};"#,
|
||||
r#"const __abStealth = {{ locale: "{}", languages: {}, allowWebGLContextFallback: false, hideCanvas: {}, canvasSeed: {} }};"#,
|
||||
locale,
|
||||
serde_json::to_string(&languages).unwrap_or_else(|_| r#"["en-US","en"]"#.to_string()),
|
||||
hide_canvas_enabled(),
|
||||
canvas_noise_seed(),
|
||||
);
|
||||
|
||||
// NB: this prefix MUST match the first line of stealth_scripts.js verbatim,
|
||||
// otherwise the fallback below prepends a SECOND `const __abStealth`
|
||||
// declaration and the whole script dies with a redeclaration SyntaxError.
|
||||
if let Some(rest) = STEALTH_SCRIPTS_RAW.strip_prefix(
|
||||
r#"const __abStealth = { locale: "en-US", languages: ["en-US", "en"], allowWebGLContextFallback: false };"#,
|
||||
r#"const __abStealth = { locale: "en-US", languages: ["en-US", "en"], allowWebGLContextFallback: false, hideCanvas: false, canvasSeed: 0 };"#,
|
||||
) {
|
||||
format!("{}{}", config_line, rest)
|
||||
} else {
|
||||
@@ -65,6 +70,35 @@ pub fn build_stealth_script(mode: StealthMode, locale: Option<&str>) -> String {
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether canvas/audio fingerprint noise is opted into (FullLaunch only).
|
||||
/// OFF by default: injecting noise is a deliberate "lie" that can itself be a
|
||||
/// tell, so it's reserved for users who explicitly want it via
|
||||
/// `AGENT_BROWSER_HIDE_CANVAS=1`.
|
||||
fn hide_canvas_enabled() -> bool {
|
||||
std::env::var("AGENT_BROWSER_HIDE_CANVAS")
|
||||
.ok()
|
||||
.map(|v| v == "1" || v.eq_ignore_ascii_case("true"))
|
||||
.unwrap_or(false)
|
||||
}
|
||||
|
||||
/// A per-process seed so canvas/audio noise is STABLE within a session (a real
|
||||
/// device returns the same hash on repeated reads) but differs from the
|
||||
/// headless-stable default. 0 is avoided so the JS can treat it as "unset".
|
||||
fn canvas_noise_seed() -> u32 {
|
||||
use std::sync::OnceLock;
|
||||
static SEED: OnceLock<u32> = OnceLock::new();
|
||||
*SEED.get_or_init(|| {
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
let nanos = SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.map(|d| d.subsec_nanos())
|
||||
.unwrap_or(0x9e3779b9);
|
||||
// mix the bits a little, then force non-zero
|
||||
let mixed = nanos ^ nanos.rotate_left(13).wrapping_mul(2654435761);
|
||||
mixed | 1
|
||||
})
|
||||
}
|
||||
|
||||
/// Apply stealth patches to a browser session.
|
||||
///
|
||||
/// In `CdpAttach` mode (user's real Chrome): only removes `navigator.webdriver`.
|
||||
@@ -118,11 +152,74 @@ pub async fn apply_stealth(
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
|
||||
// Align the timezone for fresh launches when explicitly requested.
|
||||
// Headless/launched Chrome often reports UTC (or the host's zone), which
|
||||
// can contradict a proxy's geolocation or a spoofed locale.
|
||||
// `Emulation.setTimezoneOverride` is a NATIVE override — Intl.DateTimeFormat
|
||||
// and Date both follow it with no detectable JS lie. Opt-in only:
|
||||
// AGENT_BROWSER_TIMEZONE=<IANA id> -> use that zone (e.g. align to proxy)
|
||||
// AGENT_BROWSER_TIMEZONE=auto -> derive a default from the locale
|
||||
// (unset) -> leave the real timezone untouched
|
||||
if let Some(tz) = resolve_timezone(locale) {
|
||||
let _ = client
|
||||
.send_command(
|
||||
"Emulation.setTimezoneOverride",
|
||||
Some(json!({ "timezoneId": tz })),
|
||||
Some(session_id),
|
||||
)
|
||||
.await;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Resolve the timezone to emulate for a fresh-launch session, if any.
|
||||
/// Controlled by `AGENT_BROWSER_TIMEZONE`: an explicit IANA id, or `auto` to
|
||||
/// derive a sensible default from the locale. Returns `None` (leave the real
|
||||
/// timezone) when unset, empty, or when `auto` can't map the locale.
|
||||
fn resolve_timezone(locale: Option<&str>) -> Option<String> {
|
||||
let raw = std::env::var("AGENT_BROWSER_TIMEZONE").ok()?;
|
||||
let raw = raw.trim();
|
||||
if raw.is_empty() {
|
||||
return None;
|
||||
}
|
||||
if raw.eq_ignore_ascii_case("auto") {
|
||||
return locale
|
||||
.and_then(locale_default_timezone)
|
||||
.map(str::to_string);
|
||||
}
|
||||
Some(raw.to_string())
|
||||
}
|
||||
|
||||
/// Best-effort IANA timezone for a locale. Used only for
|
||||
/// `AGENT_BROWSER_TIMEZONE=auto`; unknown locales return `None` so the real
|
||||
/// timezone is left untouched rather than guessing a wrong one.
|
||||
fn locale_default_timezone(locale: &str) -> Option<&'static str> {
|
||||
let tz = match locale.to_ascii_lowercase().as_str() {
|
||||
"en-us" => "America/New_York",
|
||||
"en-ca" => "America/Toronto",
|
||||
"en-gb" => "Europe/London",
|
||||
"en-au" => "Australia/Sydney",
|
||||
"ja" | "ja-jp" => "Asia/Tokyo",
|
||||
"ko" | "ko-kr" => "Asia/Seoul",
|
||||
"zh-cn" | "zh-hans" | "zh-hans-cn" => "Asia/Shanghai",
|
||||
"zh-tw" | "zh-hant" | "zh-hant-tw" => "Asia/Taipei",
|
||||
"zh-hk" => "Asia/Hong_Kong",
|
||||
"de" | "de-de" => "Europe/Berlin",
|
||||
"fr" | "fr-fr" => "Europe/Paris",
|
||||
"es" | "es-es" => "Europe/Madrid",
|
||||
"it" | "it-it" => "Europe/Rome",
|
||||
"nl" | "nl-nl" => "Europe/Amsterdam",
|
||||
"pt-br" => "America/Sao_Paulo",
|
||||
"pt" | "pt-pt" => "Europe/Lisbon",
|
||||
"ru" | "ru-ru" => "Europe/Moscow",
|
||||
_ => return None,
|
||||
};
|
||||
Some(tz)
|
||||
}
|
||||
|
||||
/// Get the browser's User-Agent string via CDP.
|
||||
async fn get_browser_user_agent(client: &CdpClient, session_id: &str) -> Option<String> {
|
||||
let result = client
|
||||
@@ -235,3 +332,43 @@ fn build_ua_metadata(ua: &str, locale: Option<&str>) -> serde_json::Value {
|
||||
"wow64": false,
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod timezone_tests {
|
||||
use super::{locale_default_timezone, resolve_timezone};
|
||||
|
||||
#[test]
|
||||
fn maps_common_locales_case_insensitively() {
|
||||
assert_eq!(locale_default_timezone("en-US"), Some("America/New_York"));
|
||||
assert_eq!(locale_default_timezone("ja-JP"), Some("Asia/Tokyo"));
|
||||
assert_eq!(locale_default_timezone("zh-CN"), Some("Asia/Shanghai"));
|
||||
assert_eq!(locale_default_timezone("ZH-TW"), Some("Asia/Taipei"));
|
||||
assert_eq!(locale_default_timezone("ja"), Some("Asia/Tokyo"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unknown_locale_returns_none() {
|
||||
assert_eq!(locale_default_timezone("xx-YY"), None);
|
||||
assert_eq!(locale_default_timezone(""), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn resolve_timezone_honors_env() {
|
||||
// Serialized via a single test to avoid cross-test env races on this key.
|
||||
std::env::remove_var("AGENT_BROWSER_TIMEZONE");
|
||||
assert_eq!(resolve_timezone(Some("en-US")), None);
|
||||
|
||||
std::env::set_var("AGENT_BROWSER_TIMEZONE", "Europe/Berlin");
|
||||
assert_eq!(resolve_timezone(None), Some("Europe/Berlin".to_string()));
|
||||
|
||||
std::env::set_var("AGENT_BROWSER_TIMEZONE", " ");
|
||||
assert_eq!(resolve_timezone(Some("en-US")), None);
|
||||
|
||||
std::env::set_var("AGENT_BROWSER_TIMEZONE", "auto");
|
||||
assert_eq!(resolve_timezone(Some("ja-JP")), Some("Asia/Tokyo".to_string()));
|
||||
assert_eq!(resolve_timezone(Some("xx-YY")), None);
|
||||
assert_eq!(resolve_timezone(None), None);
|
||||
|
||||
std::env::remove_var("AGENT_BROWSER_TIMEZONE");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,29 @@
|
||||
const __abStealth = { locale: "en-US", languages: ["en-US", "en"], allowWebGLContextFallback: false };
|
||||
const __abStealth = { locale: "en-US", languages: ["en-US", "en"], allowWebGLContextFallback: false, hideCanvas: false, canvasSeed: 0 };
|
||||
(function(){
|
||||
const removeWebdriver = (target) => {
|
||||
// Prefer the CDP-level automation override (Emulation.setAutomationOverride),
|
||||
// which makes navigator.webdriver report `false` NATIVELY — undetectable by
|
||||
// lie-detection (creepjs). Only intervene when webdriver is still truthy
|
||||
// (e.g. older Chrome without that override) and force it to FALSE.
|
||||
//
|
||||
// Never `delete` webdriver: real Chrome reports `false`, so `undefined` is
|
||||
// itself a tell, and deleting it removes the native `false` the override set.
|
||||
const forceWebdriverFalse = (target) => {
|
||||
if (!target) return;
|
||||
try { delete target.webdriver; } catch {}
|
||||
try {
|
||||
if (target.webdriver === true) {
|
||||
Object.defineProperty(target, 'webdriver', {
|
||||
get: () => false,
|
||||
configurable: true,
|
||||
enumerable: false,
|
||||
});
|
||||
}
|
||||
} catch {}
|
||||
};
|
||||
removeWebdriver(navigator);
|
||||
removeWebdriver(Object.getPrototypeOf(navigator));
|
||||
removeWebdriver(Navigator.prototype);
|
||||
forceWebdriverFalse(navigator);
|
||||
forceWebdriverFalse(Object.getPrototypeOf(navigator));
|
||||
forceWebdriverFalse(Navigator.prototype);
|
||||
if (typeof WorkerNavigator !== 'undefined') {
|
||||
removeWebdriver(WorkerNavigator.prototype);
|
||||
forceWebdriverFalse(WorkerNavigator.prototype);
|
||||
}
|
||||
})();
|
||||
(function(){
|
||||
@@ -1260,3 +1275,126 @@ const __abStealth = { locale: "en-US", languages: ["en-US", "en"], allowWebGLCon
|
||||
}
|
||||
}
|
||||
})();
|
||||
// Canvas + audio fingerprint noise (OPT-IN, full-launch only).
|
||||
// Headless Chrome produces a stable canvas/audio hash that trackers use as a
|
||||
// device id. When __abStealth.hideCanvas is on we perturb readback APIs with a
|
||||
// SESSION-STABLE, sub-perceptual amount of noise: repeated reads on this page
|
||||
// return the same noised result (a real device is consistent too), but the
|
||||
// hash differs from the headless default. Off by default — noise is itself a
|
||||
// "lie", so it's reserved for users who explicitly enable it.
|
||||
(function(){
|
||||
if (!__abStealth || __abStealth.hideCanvas !== true) return;
|
||||
|
||||
// Deterministic PRNG keyed by the per-session seed plus a position, so the
|
||||
// same pixel/sample is perturbed identically every read within the session.
|
||||
const baseSeed = (__abStealth.canvasSeed >>> 0) || 0x9e3779b9;
|
||||
const noiseAt = (n) => {
|
||||
let t = (baseSeed ^ Math.imul(n | 0, 0x6d2b79f5)) >>> 0;
|
||||
t = Math.imul(t ^ (t >>> 15), t | 1) >>> 0;
|
||||
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
|
||||
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
|
||||
};
|
||||
|
||||
// Make a wrapped function masquerade as the native one (toString + name).
|
||||
const mask = (wrapped, native) => {
|
||||
try {
|
||||
Object.defineProperty(wrapped, 'name', {
|
||||
value: native.name,
|
||||
configurable: true,
|
||||
});
|
||||
Object.defineProperty(wrapped, 'toString', {
|
||||
value: () => native.toString(),
|
||||
configurable: true,
|
||||
writable: true,
|
||||
});
|
||||
} catch {}
|
||||
return wrapped;
|
||||
};
|
||||
|
||||
// ---- Canvas 2D readback ---------------------------------------------------
|
||||
const perturbImageData = (imageData) => {
|
||||
const data = imageData && imageData.data;
|
||||
if (!data || !data.length) return imageData;
|
||||
for (let i = 0; i < data.length; i += 4) {
|
||||
// Touch ~5% of pixels by +/-1 on each RGB channel; leave alpha alone.
|
||||
if (noiseAt(i) < 0.05) {
|
||||
const delta = noiseAt(i + 1) < 0.5 ? -1 : 1;
|
||||
data[i] = Math.max(0, Math.min(255, data[i] + delta));
|
||||
data[i + 1] = Math.max(0, Math.min(255, data[i + 1] + delta));
|
||||
data[i + 2] = Math.max(0, Math.min(255, data[i + 2] + delta));
|
||||
}
|
||||
}
|
||||
return imageData;
|
||||
};
|
||||
|
||||
try {
|
||||
const ctxProto = (typeof CanvasRenderingContext2D !== 'undefined')
|
||||
? CanvasRenderingContext2D.prototype : null;
|
||||
if (ctxProto && typeof ctxProto.getImageData === 'function') {
|
||||
const nativeGetImageData = ctxProto.getImageData;
|
||||
ctxProto.getImageData = mask(function(...args) {
|
||||
return perturbImageData(nativeGetImageData.apply(this, args));
|
||||
}, nativeGetImageData);
|
||||
}
|
||||
} catch {}
|
||||
|
||||
// For toDataURL/toBlob, draw the (already-rendered) canvas onto a scratch
|
||||
// canvas, perturb its pixels, then encode that — so the export hash shifts
|
||||
// without disturbing what the page sees on screen.
|
||||
const exportNoised = (canvas) => {
|
||||
try {
|
||||
const w = canvas.width, h = canvas.height;
|
||||
if (!w || !h) return null;
|
||||
const scratch = document.createElement('canvas');
|
||||
scratch.width = w; scratch.height = h;
|
||||
const sctx = scratch.getContext('2d');
|
||||
if (!sctx) return null;
|
||||
sctx.drawImage(canvas, 0, 0);
|
||||
const img = sctx.getImageData(0, 0, w, h);
|
||||
perturbImageData(img);
|
||||
sctx.putImageData(img, 0, 0);
|
||||
return scratch;
|
||||
} catch { return null; }
|
||||
};
|
||||
|
||||
try {
|
||||
const canvasProto = (typeof HTMLCanvasElement !== 'undefined')
|
||||
? HTMLCanvasElement.prototype : null;
|
||||
if (canvasProto && typeof canvasProto.toDataURL === 'function') {
|
||||
const nativeToDataURL = canvasProto.toDataURL;
|
||||
canvasProto.toDataURL = mask(function(...args) {
|
||||
const scratch = exportNoised(this);
|
||||
return nativeToDataURL.apply(scratch || this, args);
|
||||
}, nativeToDataURL);
|
||||
}
|
||||
if (canvasProto && typeof canvasProto.toBlob === 'function') {
|
||||
const nativeToBlob = canvasProto.toBlob;
|
||||
canvasProto.toBlob = mask(function(cb, ...rest) {
|
||||
const scratch = exportNoised(this);
|
||||
return nativeToBlob.call(scratch || this, cb, ...rest);
|
||||
}, nativeToBlob);
|
||||
}
|
||||
} catch {}
|
||||
|
||||
// ---- AudioBuffer readback -------------------------------------------------
|
||||
// Perturb time-domain samples by a tiny, seed-stable amount so the audio
|
||||
// fingerprint (sum/hash of channel data) shifts without audible effect.
|
||||
try {
|
||||
const audioProto = (typeof AudioBuffer !== 'undefined') ? AudioBuffer.prototype : null;
|
||||
if (audioProto && typeof audioProto.getChannelData === 'function') {
|
||||
const nativeGetChannelData = audioProto.getChannelData;
|
||||
const seen = new WeakSet();
|
||||
audioProto.getChannelData = mask(function(...args) {
|
||||
const channel = nativeGetChannelData.apply(this, args);
|
||||
// Only perturb once per buffer to keep reads consistent.
|
||||
if (channel && !seen.has(channel)) {
|
||||
seen.add(channel);
|
||||
for (let i = 0; i < channel.length; i += 100) {
|
||||
channel[i] = channel[i] + (noiseAt(i) - 0.5) * 1e-7;
|
||||
}
|
||||
}
|
||||
return channel;
|
||||
}, nativeGetChannelData);
|
||||
}
|
||||
} catch {}
|
||||
})();
|
||||
|
||||
@@ -33,14 +33,141 @@ pub(super) fn cors_headers_for_origin(origin: Option<&str>) -> String {
|
||||
)
|
||||
}
|
||||
|
||||
fn request_headers(request: &str) -> &str {
|
||||
request
|
||||
.find("\r\n\r\n")
|
||||
.or_else(|| request.find("\n\n"))
|
||||
.map(|header_end| &request[..header_end])
|
||||
.unwrap_or(request)
|
||||
}
|
||||
|
||||
fn request_header_value<'a>(request: &'a str, name: &str) -> Option<&'a str> {
|
||||
request_headers(request).lines().find_map(|line| {
|
||||
let (header_name, value) = line.split_once(':')?;
|
||||
if header_name.trim().eq_ignore_ascii_case(name) {
|
||||
Some(value.trim())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn parse_origin(peeked: &[u8]) -> Option<String> {
|
||||
let header_str = std::str::from_utf8(peeked).ok()?;
|
||||
for line in header_str.lines() {
|
||||
if line.len() > 8 && line[..8].eq_ignore_ascii_case("origin: ") {
|
||||
return Some(line[8..].trim().to_string());
|
||||
request_header_value(header_str, "origin").map(ToString::to_string)
|
||||
}
|
||||
|
||||
fn normalize_origin_authority(origin: &str) -> Option<String> {
|
||||
let url = url::Url::parse(origin).ok()?;
|
||||
let host = url.host_str()?.to_ascii_lowercase();
|
||||
let host = if host.contains(':') {
|
||||
format!("[{host}]")
|
||||
} else {
|
||||
host
|
||||
};
|
||||
let default_port = (url.scheme() == "http" && url.port() == Some(80))
|
||||
|| (url.scheme() == "https" && url.port() == Some(443));
|
||||
Some(match url.port() {
|
||||
Some(port) if !default_port => format!("{host}:{port}"),
|
||||
_ => host,
|
||||
})
|
||||
}
|
||||
|
||||
fn normalize_host_authority(host: &str) -> String {
|
||||
let host = host.trim().to_ascii_lowercase();
|
||||
|
||||
if let Some(bracket_end) = host.rfind(']') {
|
||||
if bracket_end == host.len() - 1 {
|
||||
return host;
|
||||
}
|
||||
|
||||
if host.as_bytes().get(bracket_end + 1) == Some(&b':') {
|
||||
let port = &host[bracket_end + 2..];
|
||||
if port == "80" || port == "443" {
|
||||
return host[..=bracket_end].to_string();
|
||||
}
|
||||
}
|
||||
|
||||
return host;
|
||||
}
|
||||
|
||||
if let Some((name, port)) = host.rsplit_once(':') {
|
||||
if !name.contains(':') && (port == "80" || port == "443") {
|
||||
return name.to_string();
|
||||
}
|
||||
}
|
||||
None
|
||||
|
||||
host
|
||||
}
|
||||
|
||||
fn authority_host(authority: &str) -> &str {
|
||||
if let Some(stripped) = authority.strip_prefix('[') {
|
||||
if let Some(bracket_end) = stripped.find(']') {
|
||||
return &authority[..=bracket_end + 1];
|
||||
}
|
||||
}
|
||||
|
||||
if let Some((host, _port)) = authority.rsplit_once(':') {
|
||||
if !host.contains(':') {
|
||||
return host;
|
||||
}
|
||||
}
|
||||
|
||||
authority
|
||||
}
|
||||
|
||||
fn is_loopback_authority(authority: &str) -> bool {
|
||||
matches!(
|
||||
authority_host(authority),
|
||||
"localhost" | "127.0.0.1" | "::1" | "[::1]"
|
||||
)
|
||||
}
|
||||
|
||||
fn header_authority_matches_host(request: &str, header_name: &str) -> bool {
|
||||
let Some(authority) =
|
||||
request_header_value(request, header_name).and_then(normalize_origin_authority)
|
||||
else {
|
||||
return false;
|
||||
};
|
||||
let Some(host) = request_header_value(request, "host").map(normalize_host_authority) else {
|
||||
return false;
|
||||
};
|
||||
authority == host && is_loopback_authority(&authority) && is_loopback_authority(&host)
|
||||
}
|
||||
|
||||
/// Protects the command relay by requiring same-origin browser metadata.
|
||||
fn is_same_origin_command_request(request: &str) -> bool {
|
||||
if request_header_value(request, "origin").is_some() {
|
||||
header_authority_matches_host(request, "origin")
|
||||
} else {
|
||||
header_authority_matches_host(request, "referer")
|
||||
}
|
||||
}
|
||||
|
||||
fn command_cors_headers(request: &str) -> String {
|
||||
match request_header_value(request, "origin") {
|
||||
Some(origin) if is_same_origin_command_request(request) => format!(
|
||||
"Access-Control-Allow-Origin: {origin}\r\nAccess-Control-Allow-Methods: POST, OPTIONS\r\nAccess-Control-Allow-Headers: Content-Type\r\nVary: Origin\r\n"
|
||||
),
|
||||
_ => String::new(),
|
||||
}
|
||||
}
|
||||
|
||||
async fn write_json_error_response_no_cors(
|
||||
stream: &mut tokio::net::TcpStream,
|
||||
status: &str,
|
||||
error: &str,
|
||||
) {
|
||||
let body = format!(
|
||||
r#"{{"success":false,"error":{}}}"#,
|
||||
serde_json::to_string(error).unwrap_or_else(|_| format!("\"{}\"", error))
|
||||
);
|
||||
let response = format!(
|
||||
"HTTP/1.1 {status}\r\nContent-Type: application/json; charset=utf-8\r\nContent-Length: {}\r\nConnection: close\r\n\r\n",
|
||||
body.len()
|
||||
);
|
||||
let _ = stream.write_all(response.as_bytes()).await;
|
||||
let _ = stream.write_all(body.as_bytes()).await;
|
||||
}
|
||||
|
||||
pub(super) async fn handle_http_request(
|
||||
@@ -61,6 +188,25 @@ pub(super) async fn handle_http_request(
|
||||
let origin = parse_origin(peeked);
|
||||
|
||||
if method == "OPTIONS" {
|
||||
if path == "/api/command" {
|
||||
if !is_same_origin_command_request(&request) {
|
||||
write_json_error_response_no_cors(
|
||||
&mut stream,
|
||||
"403 Forbidden",
|
||||
"Origin or Referer does not match Host header.",
|
||||
)
|
||||
.await;
|
||||
return;
|
||||
}
|
||||
|
||||
let cors_headers = command_cors_headers(&request);
|
||||
let response = format!(
|
||||
"HTTP/1.1 204 No Content\r\n{cors_headers}Access-Control-Max-Age: 86400\r\nContent-Length: 0\r\nConnection: close\r\n\r\n"
|
||||
);
|
||||
let _ = stream.write_all(response.as_bytes()).await;
|
||||
return;
|
||||
}
|
||||
|
||||
let response = format!(
|
||||
"HTTP/1.1 204 No Content\r\n{CORS_HEADERS}Access-Control-Max-Age: 86400\r\nContent-Length: 0\r\nConnection: close\r\n\r\n"
|
||||
);
|
||||
@@ -69,13 +215,28 @@ pub(super) async fn handle_http_request(
|
||||
}
|
||||
|
||||
if method == "POST" {
|
||||
if path == "/api/command" && !is_same_origin_command_request(&request) {
|
||||
write_json_error_response_no_cors(
|
||||
&mut stream,
|
||||
"403 Forbidden",
|
||||
"Origin or Referer does not match Host header.",
|
||||
)
|
||||
.await;
|
||||
return;
|
||||
}
|
||||
|
||||
let full_body = read_full_body(&mut stream, peeked).await;
|
||||
if full_body.is_none()
|
||||
&& (path == "/api/chat" || path == "/api/sessions" || path == "/api/command")
|
||||
{
|
||||
let body = r#"{"error":"Request body too large"}"#;
|
||||
let cors_headers = if path == "/api/command" {
|
||||
command_cors_headers(&request)
|
||||
} else {
|
||||
CORS_HEADERS.to_string()
|
||||
};
|
||||
let response = format!(
|
||||
"HTTP/1.1 413 Payload Too Large\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n{CORS_HEADERS}\r\n",
|
||||
"HTTP/1.1 413 Payload Too Large\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n{cors_headers}\r\n",
|
||||
body.len()
|
||||
);
|
||||
let _ = stream.write_all(response.as_bytes()).await;
|
||||
@@ -117,8 +278,9 @@ pub(super) async fn handle_http_request(
|
||||
),
|
||||
),
|
||||
};
|
||||
let cors_headers = command_cors_headers(&request);
|
||||
let response = format!(
|
||||
"HTTP/1.1 {status}\r\nContent-Type: application/json; charset=utf-8\r\nContent-Length: {}\r\nConnection: close\r\n{CORS_HEADERS}\r\n",
|
||||
"HTTP/1.1 {status}\r\nContent-Type: application/json; charset=utf-8\r\nContent-Length: {}\r\nConnection: close\r\n{cors_headers}\r\n",
|
||||
resp_body.len()
|
||||
);
|
||||
let _ = stream.write_all(response.as_bytes()).await;
|
||||
@@ -313,3 +475,241 @@ pub(super) fn serve_embedded_file(url_path: &str) -> (&'static str, &'static str
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::test_utils::EnvGuard;
|
||||
use std::sync::Arc;
|
||||
use tokio::io::{AsyncBufReadExt, AsyncReadExt, AsyncWriteExt};
|
||||
use tokio::net::TcpListener;
|
||||
use tokio::sync::oneshot;
|
||||
|
||||
async fn send_request_to_handler(request: &str, session_name: &str) -> String {
|
||||
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let addr = listener.local_addr().unwrap();
|
||||
let peeked = request.as_bytes().to_vec();
|
||||
let last_tabs = Arc::new(RwLock::new(Vec::new()));
|
||||
let last_engine = Arc::new(RwLock::new("chrome".to_string()));
|
||||
let session_name = session_name.to_string();
|
||||
|
||||
let server = tokio::spawn(async move {
|
||||
let (stream, _) = listener.accept().await.unwrap();
|
||||
handle_http_request(stream, &peeked, &last_tabs, &last_engine, &session_name).await;
|
||||
});
|
||||
|
||||
let mut client = tokio::net::TcpStream::connect(addr).await.unwrap();
|
||||
client.write_all(request.as_bytes()).await.unwrap();
|
||||
client.shutdown().await.unwrap();
|
||||
|
||||
let mut response = Vec::new();
|
||||
client.read_to_end(&mut response).await.unwrap();
|
||||
server.await.unwrap();
|
||||
|
||||
String::from_utf8(response).unwrap()
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
async fn spawn_fake_daemon(
|
||||
socket_dir: &std::path::Path,
|
||||
session_name: &str,
|
||||
) -> oneshot::Receiver<String> {
|
||||
let socket_path = socket_dir.join(format!("{session_name}.sock"));
|
||||
let _ = std::fs::remove_file(&socket_path);
|
||||
let listener = tokio::net::UnixListener::bind(&socket_path).unwrap();
|
||||
let (tx, rx) = oneshot::channel();
|
||||
|
||||
tokio::spawn(async move {
|
||||
let (stream, _) = listener.accept().await.unwrap();
|
||||
let mut reader = tokio::io::BufReader::new(stream);
|
||||
let mut line = String::new();
|
||||
reader.read_line(&mut line).await.unwrap();
|
||||
|
||||
let mut stream = reader.into_inner();
|
||||
stream
|
||||
.write_all(br#"{"success":true,"data":{"ok":true}}"#)
|
||||
.await
|
||||
.unwrap();
|
||||
stream.write_all(b"\n").await.unwrap();
|
||||
let _ = tx.send(line);
|
||||
});
|
||||
|
||||
rx
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[tokio::test(flavor = "current_thread")]
|
||||
async fn cross_origin_command_post_is_rejected_without_relaying_to_daemon() {
|
||||
let temp_parent = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
|
||||
.join("target")
|
||||
.join("t");
|
||||
std::fs::create_dir_all(&temp_parent).unwrap();
|
||||
let socket_dir = tempfile::Builder::new()
|
||||
.prefix("ab-")
|
||||
.tempdir_in(temp_parent)
|
||||
.unwrap();
|
||||
let guard = EnvGuard::new(&["AGENT_BROWSER_SOCKET_DIR", "XDG_RUNTIME_DIR"]);
|
||||
guard.set(
|
||||
"AGENT_BROWSER_SOCKET_DIR",
|
||||
socket_dir.path().to_str().unwrap(),
|
||||
);
|
||||
guard.remove("XDG_RUNTIME_DIR");
|
||||
|
||||
let session_name = "x";
|
||||
let daemon_command = spawn_fake_daemon(socket_dir.path(), session_name).await;
|
||||
let body = r#"{"action":"tabs"}"#;
|
||||
let request = format!(
|
||||
"POST /api/command HTTP/1.1\r\nHost: localhost:7777\r\nOrigin: https://evil.example\r\nContent-Type: application/json\r\nContent-Length: {}\r\n\r\n{}",
|
||||
body.len(),
|
||||
body
|
||||
);
|
||||
|
||||
let response = send_request_to_handler(&request, session_name).await;
|
||||
|
||||
assert!(
|
||||
response.starts_with("HTTP/1.1 403 Forbidden"),
|
||||
"unexpected response: {response}"
|
||||
);
|
||||
assert!(
|
||||
tokio::time::timeout(std::time::Duration::from_millis(50), daemon_command)
|
||||
.await
|
||||
.is_err(),
|
||||
"cross-origin request reached daemon command relay"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "current_thread")]
|
||||
async fn cross_origin_command_preflight_is_rejected_without_wildcard_cors() {
|
||||
let request = concat!(
|
||||
"OPTIONS /api/command HTTP/1.1\r\n",
|
||||
"Host: localhost:7777\r\n",
|
||||
"Origin: https://evil.example\r\n",
|
||||
"Access-Control-Request-Method: POST\r\n",
|
||||
"Access-Control-Request-Headers: content-type\r\n",
|
||||
"\r\n"
|
||||
);
|
||||
|
||||
let response = send_request_to_handler(request, "x").await;
|
||||
|
||||
assert!(
|
||||
response.starts_with("HTTP/1.1 403 Forbidden"),
|
||||
"unexpected response: {response}"
|
||||
);
|
||||
assert!(
|
||||
!response.contains("Access-Control-Allow-Origin: *"),
|
||||
"forbidden command preflight exposed wildcard CORS: {response}"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "current_thread")]
|
||||
async fn command_post_without_origin_or_referer_is_rejected() {
|
||||
let body = r#"{"action":"tabs"}"#;
|
||||
let request = format!(
|
||||
"POST /api/command HTTP/1.1\r\nHost: localhost:7777\r\nContent-Type: application/json\r\nContent-Length: {}\r\n\r\n{}",
|
||||
body.len(),
|
||||
body
|
||||
);
|
||||
|
||||
let response = send_request_to_handler(&request, "x").await;
|
||||
|
||||
assert!(
|
||||
response.starts_with("HTTP/1.1 403 Forbidden"),
|
||||
"unexpected response: {response}"
|
||||
);
|
||||
assert!(
|
||||
!response.contains("Access-Control-Allow-Origin: *"),
|
||||
"forbidden command response exposed wildcard CORS: {response}"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "current_thread")]
|
||||
async fn command_post_with_dns_rebinding_host_is_rejected() {
|
||||
let body = r#"{"action":"tabs"}"#;
|
||||
let request = format!(
|
||||
"POST /api/command HTTP/1.1\r\nHost: attacker.example:7777\r\nOrigin: http://attacker.example:7777\r\nContent-Type: application/json\r\nContent-Length: {}\r\n\r\n{}",
|
||||
body.len(),
|
||||
body
|
||||
);
|
||||
|
||||
let response = send_request_to_handler(&request, "x").await;
|
||||
|
||||
assert!(
|
||||
response.starts_with("HTTP/1.1 403 Forbidden"),
|
||||
"unexpected response: {response}"
|
||||
);
|
||||
assert!(
|
||||
!response.contains("Access-Control-Allow-Origin: *"),
|
||||
"forbidden command response exposed wildcard CORS: {response}"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "current_thread")]
|
||||
async fn command_post_ignores_header_like_body_lines() {
|
||||
let body = "Referer: http://localhost:7777\r\n{\"action\":\"tabs\"}";
|
||||
let request = format!(
|
||||
"POST /api/command HTTP/1.1\r\nHost: localhost:7777\r\nContent-Type: application/json\r\nContent-Length: {}\r\n\r\n{}",
|
||||
body.len(),
|
||||
body
|
||||
);
|
||||
|
||||
let response = send_request_to_handler(&request, "x").await;
|
||||
|
||||
assert!(
|
||||
response.starts_with("HTTP/1.1 403 Forbidden"),
|
||||
"unexpected response: {response}"
|
||||
);
|
||||
assert!(
|
||||
!response.contains("Access-Control-Allow-Origin: *"),
|
||||
"forbidden command response exposed wildcard CORS: {response}"
|
||||
);
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[tokio::test(flavor = "current_thread")]
|
||||
async fn same_origin_command_post_relays_without_wildcard_cors() {
|
||||
let temp_parent = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
|
||||
.join("target")
|
||||
.join("t");
|
||||
std::fs::create_dir_all(&temp_parent).unwrap();
|
||||
let socket_dir = tempfile::Builder::new()
|
||||
.prefix("ab-")
|
||||
.tempdir_in(temp_parent)
|
||||
.unwrap();
|
||||
let guard = EnvGuard::new(&["AGENT_BROWSER_SOCKET_DIR", "XDG_RUNTIME_DIR"]);
|
||||
guard.set(
|
||||
"AGENT_BROWSER_SOCKET_DIR",
|
||||
socket_dir.path().to_str().unwrap(),
|
||||
);
|
||||
guard.remove("XDG_RUNTIME_DIR");
|
||||
|
||||
let session_name = "x";
|
||||
let daemon_command = spawn_fake_daemon(socket_dir.path(), session_name).await;
|
||||
let body = r#"{"action":"tabs"}"#;
|
||||
let request = format!(
|
||||
"POST /api/command HTTP/1.1\r\nHost: localhost:7777\r\nOrigin: http://localhost:7777\r\nContent-Type: application/json\r\nContent-Length: {}\r\n\r\n{}",
|
||||
body.len(),
|
||||
body
|
||||
);
|
||||
|
||||
let response = send_request_to_handler(&request, session_name).await;
|
||||
|
||||
assert!(
|
||||
response.starts_with("HTTP/1.1 200 OK"),
|
||||
"unexpected response: {response}"
|
||||
);
|
||||
assert!(
|
||||
response.contains("Access-Control-Allow-Origin: http://localhost:7777"),
|
||||
"same-origin command response did not reflect origin: {response}"
|
||||
);
|
||||
assert!(
|
||||
!response.contains("Access-Control-Allow-Origin: *"),
|
||||
"same-origin command response exposed wildcard CORS: {response}"
|
||||
);
|
||||
|
||||
let relayed = tokio::time::timeout(std::time::Duration::from_secs(1), daemon_command)
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
assert!(relayed.contains(r#""action":"tabs""#), "{relayed}");
|
||||
}
|
||||
}
|
||||
|
||||
+12
-1
@@ -1042,6 +1042,11 @@ pub fn print_response_with_opts(resp: &Response, action: Option<&str>, opts: &Ou
|
||||
|
||||
// Default success
|
||||
println!("{} Done", color::success_indicator());
|
||||
} else {
|
||||
// Success response with no data payload — still confirm the command ran
|
||||
// instead of printing nothing (a silent exit 0 looks like a no-op and
|
||||
// hides whether anything happened).
|
||||
println!("{} Done", color::success_indicator());
|
||||
}
|
||||
|
||||
print_warning(resp);
|
||||
@@ -1583,6 +1588,8 @@ Usage: agent-browser screenshot [selector] [path]
|
||||
|
||||
Captures a screenshot of the current page. If no path is provided,
|
||||
saves to a temporary directory with a generated filename.
|
||||
Headless Chromium screenshots hide native scrollbars for consistent image output.
|
||||
Pass --hide-scrollbars false when launching to keep native scrollbars visible.
|
||||
|
||||
Options:
|
||||
--full, -f Capture full page (not just viewport)
|
||||
@@ -3098,6 +3105,8 @@ Options:
|
||||
e.g., --proxy-bypass "localhost,*.internal.com"
|
||||
--ignore-https-errors Ignore HTTPS certificate errors
|
||||
--allow-file-access Allow file:// URLs to access local files (Chromium only)
|
||||
--hide-scrollbars <bool> Hide native scrollbars in headless Chromium screenshots (default: true)
|
||||
Use --hide-scrollbars false to keep scrollbars visible
|
||||
-p, --provider <name> Browser provider: ios, browserbase, kernel, browseruse, browserless, agentcore
|
||||
--device <name> iOS device name (e.g., "iPhone 15 Pro")
|
||||
--json JSON output
|
||||
@@ -3137,11 +3146,12 @@ Configuration:
|
||||
Boolean flags accept an optional true/false value to override config:
|
||||
--headed (same as --headed true)
|
||||
--headed false (disables "headed": true from config)
|
||||
--hide-scrollbars false (keeps native scrollbars visible in headless Chromium screenshots)
|
||||
|
||||
Extensions from user and project configs are merged (not replaced).
|
||||
|
||||
Example agent-browser.json:
|
||||
{{"headed": true, "proxy": "http://localhost:8080", "profile": "./browser-data"}}
|
||||
{{"headed": true, "hideScrollbars": false, "proxy": "http://localhost:8080"}}
|
||||
|
||||
Environment:
|
||||
AGENT_BROWSER_CONFIG Path to config file (or use --config)
|
||||
@@ -3161,6 +3171,7 @@ Environment:
|
||||
AGENT_BROWSER_PROVIDER Browser provider (ios, browserbase, kernel, browseruse, browserless, agentcore)
|
||||
AGENT_BROWSER_AUTO_CONNECT Auto-discover and connect to running Chrome
|
||||
AGENT_BROWSER_ALLOW_FILE_ACCESS Allow file:// URLs to access local files
|
||||
AGENT_BROWSER_HIDE_SCROLLBARS Hide scrollbars in headless Chromium screenshots (default: true)
|
||||
AGENT_BROWSER_COLOR_SCHEME Color scheme preference (dark, light, no-preference)
|
||||
AGENT_BROWSER_DOWNLOAD_PATH Default download directory for browser downloads
|
||||
AGENT_BROWSER_DEFAULT_TIMEOUT Default action timeout in ms (default: 25000)
|
||||
|
||||
+51
-8
@@ -1,3 +1,4 @@
|
||||
use include_dir::{include_dir, Dir};
|
||||
use serde_json::json;
|
||||
use std::env;
|
||||
use std::fs;
|
||||
@@ -6,6 +7,12 @@ use std::process::exit;
|
||||
|
||||
use crate::color;
|
||||
|
||||
/// Skill content compiled into the binary so `skills get` works on a
|
||||
/// single-binary install (GitHub Release / install.sh), where there is no
|
||||
/// adjacent `skills/` or `skill-data/` on disk the way an npm install has.
|
||||
static EMBEDDED_SKILLS: Dir = include_dir!("$CARGO_MANIFEST_DIR/../skills");
|
||||
static EMBEDDED_SKILL_DATA: Dir = include_dir!("$CARGO_MANIFEST_DIR/../skill-data");
|
||||
|
||||
struct SkillInfo {
|
||||
name: String,
|
||||
description: String,
|
||||
@@ -63,6 +70,29 @@ fn find_package_root() -> Option<PathBuf> {
|
||||
None
|
||||
}
|
||||
|
||||
/// Extract the binary-embedded skill content to a per-version cache dir on
|
||||
/// first use, returning a package root that contains `skills/` and
|
||||
/// `skill-data/`. Fallback for single-binary installs (GitHub Release /
|
||||
/// install.sh) that have no on-disk skill directories. Version-stamped so an
|
||||
/// upgraded binary re-extracts fresh content.
|
||||
fn embedded_skills_root() -> Option<PathBuf> {
|
||||
let base = dirs::cache_dir()?
|
||||
.join("agent-browser")
|
||||
.join(concat!("skills-", env!("CARGO_PKG_VERSION")));
|
||||
let marker = base.join(".extracted");
|
||||
if !marker.exists() {
|
||||
let _ = fs::create_dir_all(base.join("skills"));
|
||||
let _ = fs::create_dir_all(base.join("skill-data"));
|
||||
if EMBEDDED_SKILLS.extract(base.join("skills")).is_err()
|
||||
|| EMBEDDED_SKILL_DATA.extract(base.join("skill-data")).is_err()
|
||||
{
|
||||
return None;
|
||||
}
|
||||
let _ = fs::write(&marker, env!("CARGO_PKG_VERSION"));
|
||||
}
|
||||
base.join("skills").is_dir().then_some(base)
|
||||
}
|
||||
|
||||
/// Collect all skill directories to search, respecting the env var override.
|
||||
fn find_skills_dirs() -> Vec<PathBuf> {
|
||||
// Env var override: single directory, used as-is
|
||||
@@ -73,15 +103,28 @@ fn find_skills_dirs() -> Vec<PathBuf> {
|
||||
}
|
||||
}
|
||||
|
||||
let Some(root) = find_package_root() else {
|
||||
return vec![];
|
||||
};
|
||||
// On-disk package root (npm install layout, or dev build walking up to repo).
|
||||
if let Some(root) = find_package_root() {
|
||||
let dirs: Vec<PathBuf> = SKILL_DIRS
|
||||
.iter()
|
||||
.map(|d| root.join(d))
|
||||
.filter(|p| p.is_dir())
|
||||
.collect();
|
||||
if !dirs.is_empty() {
|
||||
return dirs;
|
||||
}
|
||||
}
|
||||
|
||||
SKILL_DIRS
|
||||
.iter()
|
||||
.map(|d| root.join(d))
|
||||
.filter(|p| p.is_dir())
|
||||
.collect()
|
||||
// Fallback: skill content compiled into the binary (single-binary install).
|
||||
if let Some(root) = embedded_skills_root() {
|
||||
return SKILL_DIRS
|
||||
.iter()
|
||||
.map(|d| root.join(d))
|
||||
.filter(|p| p.is_dir())
|
||||
.collect();
|
||||
}
|
||||
|
||||
vec![]
|
||||
}
|
||||
|
||||
/// Parse YAML frontmatter from a SKILL.md file. Returns (name, description, hidden).
|
||||
|
||||
+49
-263
@@ -1,284 +1,70 @@
|
||||
use crate::color;
|
||||
use std::path::Path;
|
||||
use std::process::{exit, Command, Stdio};
|
||||
use std::process::{exit, Command};
|
||||
|
||||
const CURRENT_VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||
const NPM_REGISTRY_URL: &str = "https://registry.npmjs.org/agent-browser/latest";
|
||||
|
||||
enum InstallMethod {
|
||||
Npm,
|
||||
Pnpm,
|
||||
Yarn,
|
||||
Bun,
|
||||
Homebrew,
|
||||
Cargo,
|
||||
Unknown,
|
||||
}
|
||||
|
||||
async fn fetch_latest_version() -> Result<String, String> {
|
||||
let resp = reqwest::get(NPM_REGISTRY_URL)
|
||||
.await
|
||||
.map_err(|e| format!("Failed to fetch version info: {}", e))?;
|
||||
|
||||
let body: serde_json::Value = resp
|
||||
.json()
|
||||
.await
|
||||
.map_err(|e| format!("Failed to parse version info: {}", e))?;
|
||||
|
||||
body.get("version")
|
||||
.and_then(|v| v.as_str())
|
||||
.map(|s| s.to_string())
|
||||
.ok_or_else(|| "No version field in registry response".to_string())
|
||||
}
|
||||
|
||||
/// Parse the `.install-method` marker written by postinstall.js.
|
||||
fn read_install_method_marker(exe_dir: &Path) -> Option<InstallMethod> {
|
||||
let contents = std::fs::read_to_string(exe_dir.join(".install-method")).ok()?;
|
||||
match contents.trim() {
|
||||
"npm" => Some(InstallMethod::Npm),
|
||||
"pnpm" => Some(InstallMethod::Pnpm),
|
||||
"yarn" => Some(InstallMethod::Yarn),
|
||||
"bun" => Some(InstallMethod::Bun),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn detect_install_method() -> InstallMethod {
|
||||
if let Ok(exe) = std::env::current_exe() {
|
||||
// Resolve symlinks to find the real binary location
|
||||
let real_path = exe.canonicalize().unwrap_or(exe);
|
||||
|
||||
// Preferred: read the marker file written at install time
|
||||
if let Some(dir) = real_path.parent() {
|
||||
if let Some(method) = read_install_method_marker(dir) {
|
||||
return method;
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback: infer from executable path
|
||||
let path_str = real_path.to_string_lossy();
|
||||
|
||||
if path_str.contains("/.cargo/bin/") || path_str.contains("\\.cargo\\bin\\") {
|
||||
return InstallMethod::Cargo;
|
||||
}
|
||||
|
||||
if path_str.contains("/Cellar/agent-browser/")
|
||||
|| path_str.contains("/homebrew/")
|
||||
|| path_str.contains("/linuxbrew/")
|
||||
{
|
||||
return InstallMethod::Homebrew;
|
||||
}
|
||||
|
||||
if path_str.contains("/pnpm/") || path_str.contains("/pnpm-global/") {
|
||||
return InstallMethod::Pnpm;
|
||||
}
|
||||
|
||||
if path_str.contains("/.yarn/") || path_str.contains("/yarn/global/") {
|
||||
return InstallMethod::Yarn;
|
||||
}
|
||||
|
||||
if path_str.contains("/.bun/") {
|
||||
return InstallMethod::Bun;
|
||||
}
|
||||
|
||||
if path_str.contains("node_modules/agent-browser")
|
||||
|| path_str.contains("node_modules\\agent-browser")
|
||||
{
|
||||
return InstallMethod::Npm;
|
||||
}
|
||||
}
|
||||
|
||||
// Last resort: probe package managers via subprocess
|
||||
|
||||
#[cfg(any(target_os = "macos", target_os = "linux"))]
|
||||
{
|
||||
if command_succeeds("brew", &["list", "agent-browser"]) {
|
||||
return InstallMethod::Homebrew;
|
||||
}
|
||||
}
|
||||
|
||||
if command_output_contains(
|
||||
"pnpm",
|
||||
&["list", "-g", "agent-browser", "--depth=0"],
|
||||
"agent-browser",
|
||||
) {
|
||||
return InstallMethod::Pnpm;
|
||||
}
|
||||
|
||||
if command_output_contains("yarn", &["global", "list", "--depth=0"], "agent-browser") {
|
||||
return InstallMethod::Yarn;
|
||||
}
|
||||
|
||||
if command_output_contains("bun", &["pm", "ls", "-g"], "agent-browser") {
|
||||
return InstallMethod::Bun;
|
||||
}
|
||||
|
||||
if command_succeeds("npm", &["list", "-g", "agent-browser", "--depth=0"]) {
|
||||
return InstallMethod::Npm;
|
||||
}
|
||||
|
||||
InstallMethod::Unknown
|
||||
}
|
||||
|
||||
fn command_succeeds(cmd: &str, args: &[&str]) -> bool {
|
||||
Command::new(cmd)
|
||||
.args(args)
|
||||
.stdout(Stdio::null())
|
||||
.stderr(Stdio::null())
|
||||
.status()
|
||||
.map(|s| s.success())
|
||||
.unwrap_or(false)
|
||||
}
|
||||
|
||||
fn command_output_contains(cmd: &str, args: &[&str], needle: &str) -> bool {
|
||||
Command::new(cmd)
|
||||
.args(args)
|
||||
.stderr(Stdio::null())
|
||||
.output()
|
||||
.map(|o| o.status.success() && String::from_utf8_lossy(&o.stdout).contains(needle))
|
||||
.unwrap_or(false)
|
||||
}
|
||||
|
||||
fn run_upgrade_command(method: &InstallMethod) -> bool {
|
||||
let (cmd, args, display): (&str, &[&str], &str) = match method {
|
||||
InstallMethod::Npm => (
|
||||
"npm",
|
||||
&["install", "-g", "agent-browser@latest"],
|
||||
"npm install -g agent-browser@latest",
|
||||
),
|
||||
InstallMethod::Pnpm => (
|
||||
"pnpm",
|
||||
&["add", "-g", "agent-browser@latest"],
|
||||
"pnpm add -g agent-browser@latest",
|
||||
),
|
||||
// NOTE: `yarn global` is Yarn Classic (v1) only; Yarn Berry (v2+) removed it.
|
||||
// Users on Yarn v2+ won't reach this path — detection falls through to Unknown.
|
||||
InstallMethod::Yarn => (
|
||||
"yarn",
|
||||
&["global", "add", "agent-browser@latest"],
|
||||
"yarn global add agent-browser@latest",
|
||||
),
|
||||
InstallMethod::Bun => (
|
||||
"bun",
|
||||
&["install", "-g", "agent-browser@latest"],
|
||||
"bun install -g agent-browser@latest",
|
||||
),
|
||||
InstallMethod::Homebrew => (
|
||||
"brew",
|
||||
&["upgrade", "agent-browser"],
|
||||
"brew upgrade agent-browser",
|
||||
),
|
||||
InstallMethod::Cargo => (
|
||||
"cargo",
|
||||
&["install", "agent-browser", "--force"],
|
||||
"cargo install agent-browser --force",
|
||||
),
|
||||
InstallMethod::Unknown => return false,
|
||||
};
|
||||
|
||||
println!("Running: {}", display);
|
||||
Command::new(cmd)
|
||||
.args(args)
|
||||
.status()
|
||||
.map(|s| s.success())
|
||||
.unwrap_or(false)
|
||||
}
|
||||
/// Canonical installer for the stealth fork. `upgrade` just re-runs it, so the
|
||||
/// upgrade path and the install path are identical (GitHub Release, no npm).
|
||||
const INSTALL_URL: &str =
|
||||
"https://raw.githubusercontent.com/leeguooooo/agent-browser-stealth/main/install.sh";
|
||||
|
||||
/// Upgrade to the latest GitHub Release.
|
||||
///
|
||||
/// The stealth fork ships as a prebuilt binary attached to a GitHub Release —
|
||||
/// NOT via the npm registry. Earlier this command (inherited from upstream)
|
||||
/// ran `npm/pnpm install -g agent-browser@latest`, which installed the
|
||||
/// UNRELATED upstream `agent-browser` package and clobbered the user's setup.
|
||||
/// Now `upgrade` simply re-runs install.sh into the same directory as the
|
||||
/// current binary, so it always tracks the freshest GitHub Release.
|
||||
pub fn run_upgrade() {
|
||||
let current = CURRENT_VERSION;
|
||||
println!(
|
||||
"{}",
|
||||
color::cyan(&format!(
|
||||
"Upgrading agent-browser-stealth (currently v{}) from the latest GitHub Release...",
|
||||
CURRENT_VERSION
|
||||
))
|
||||
);
|
||||
|
||||
let rt = tokio::runtime::Builder::new_current_thread()
|
||||
.enable_all()
|
||||
.build()
|
||||
.unwrap_or_else(|e| {
|
||||
eprintln!(
|
||||
"{} Failed to create runtime: {}",
|
||||
color::error_indicator(),
|
||||
e
|
||||
);
|
||||
exit(1);
|
||||
});
|
||||
|
||||
let latest = match rt.block_on(fetch_latest_version()) {
|
||||
Ok(v) => v,
|
||||
Err(e) => {
|
||||
eprintln!(
|
||||
"{} Could not check latest version: {}",
|
||||
color::warning_indicator(),
|
||||
e
|
||||
);
|
||||
String::new()
|
||||
}
|
||||
};
|
||||
|
||||
if !latest.is_empty() && current == latest.as_str() {
|
||||
println!(
|
||||
"{} agent-browser is already at the latest version (v{})",
|
||||
color::success_indicator(),
|
||||
current
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
let method = detect_install_method();
|
||||
|
||||
let method_name = match &method {
|
||||
InstallMethod::Npm => "npm",
|
||||
InstallMethod::Pnpm => "pnpm",
|
||||
InstallMethod::Yarn => "yarn",
|
||||
InstallMethod::Bun => "bun",
|
||||
InstallMethod::Homebrew => "Homebrew",
|
||||
InstallMethod::Cargo => "Cargo",
|
||||
InstallMethod::Unknown => "",
|
||||
};
|
||||
|
||||
if matches!(method, InstallMethod::Unknown) {
|
||||
#[cfg(windows)]
|
||||
{
|
||||
eprintln!(
|
||||
"{} Could not detect installation method.",
|
||||
color::error_indicator()
|
||||
"{} Automatic upgrade isn't supported on Windows.",
|
||||
color::warning_indicator()
|
||||
);
|
||||
eprintln!(" To update manually, run one of:");
|
||||
eprintln!(" npm install -g agent-browser@latest # npm");
|
||||
eprintln!(" pnpm add -g agent-browser@latest # pnpm");
|
||||
eprintln!(" yarn global add agent-browser@latest # yarn");
|
||||
eprintln!(" bun install -g agent-browser@latest # bun");
|
||||
eprintln!(" brew upgrade agent-browser # Homebrew");
|
||||
eprintln!(" cargo install agent-browser --force # Cargo");
|
||||
eprintln!(" Download the latest agent-browser-win32-x64.tar.gz from:");
|
||||
eprintln!(" https://github.com/leeguooooo/agent-browser-stealth/releases/latest");
|
||||
eprintln!(" and replace agent-browser.exe on your PATH.");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
println!("Detected installation via {}.", method_name);
|
||||
#[cfg(not(windows))]
|
||||
{
|
||||
// Install into the SAME directory as the running binary (in-place
|
||||
// upgrade), so we don't create a second copy elsewhere on PATH.
|
||||
let bin_dir = std::env::current_exe()
|
||||
.ok()
|
||||
.and_then(|p| p.canonicalize().ok())
|
||||
.and_then(|p| p.parent().map(|d| d.to_path_buf()));
|
||||
|
||||
if !latest.is_empty() {
|
||||
println!(
|
||||
"{}",
|
||||
color::cyan(&format!(
|
||||
"Upgrading agent-browser... v{} → v{}",
|
||||
current, latest
|
||||
))
|
||||
);
|
||||
} else {
|
||||
println!(
|
||||
"{}",
|
||||
color::cyan(&format!("Upgrading agent-browser (v{})...", current))
|
||||
);
|
||||
}
|
||||
let install_cmd = format!("curl -fsSL {} | sh", INSTALL_URL);
|
||||
println!("Running: {}", install_cmd);
|
||||
|
||||
let success = run_upgrade_command(&method);
|
||||
let mut cmd = Command::new("sh");
|
||||
cmd.arg("-c").arg(&install_cmd);
|
||||
if let Some(ref dir) = bin_dir {
|
||||
cmd.env("AGENT_BROWSER_BIN_DIR", dir);
|
||||
}
|
||||
|
||||
if success {
|
||||
if !latest.is_empty() {
|
||||
let ok = cmd.status().map(|s| s.success()).unwrap_or(false);
|
||||
if ok {
|
||||
println!(
|
||||
"{} Done! v{} → v{}",
|
||||
color::success_indicator(),
|
||||
current,
|
||||
latest
|
||||
"{} Upgrade complete — run `agent-browser-stealth --version` to confirm.",
|
||||
color::success_indicator()
|
||||
);
|
||||
} else {
|
||||
println!("{} Done!", color::success_indicator());
|
||||
eprintln!("{} Upgrade failed. Install manually:", color::error_indicator());
|
||||
eprintln!(" curl -fsSL {} | sh", INSTALL_URL);
|
||||
exit(1);
|
||||
}
|
||||
} else {
|
||||
eprintln!("{} Upgrade failed.", color::error_indicator());
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,13 +20,19 @@ services:
|
||||
|
||||
# Build both targets in parallel
|
||||
(echo "→ Linux x64" && cargo zigbuild --release --target x86_64-unknown-linux-gnu && cp /build/target/x86_64-unknown-linux-gnu/release/agent-browser /output/agent-browser-linux-x64 && chmod +x /output/agent-browser-linux-x64 && echo "✓ Linux x64 done") &
|
||||
PID1=$!
|
||||
PID1=$$!
|
||||
|
||||
(echo "→ Linux ARM64" && cargo zigbuild --release --target aarch64-unknown-linux-gnu && cp /build/target/aarch64-unknown-linux-gnu/release/agent-browser /output/agent-browser-linux-arm64 && chmod +x /output/agent-browser-linux-arm64 && echo "✓ Linux ARM64 done") &
|
||||
PID2=$!
|
||||
PID2=$$!
|
||||
|
||||
# Wait for both to complete
|
||||
wait $PID1 $PID2
|
||||
# Wait for both and check exit codes individually — without this
|
||||
# the outer script exits 0 even if one of the parallel builds
|
||||
# failed, silently leaving a stale binary in /output from the
|
||||
# previous release. Caused 0.27.0-fork.5 to ship with a stale
|
||||
# linux-x64 binary at the first publish attempt until caught
|
||||
# manually by checking the embedded version string.
|
||||
wait $$PID1 || { echo "✗ Linux x64 build failed"; exit 1; }
|
||||
wait $$PID2 || { echo "✗ Linux ARM64 build failed"; exit 1; }
|
||||
|
||||
echo ""
|
||||
echo "✓ Linux platforms built successfully!"
|
||||
@@ -65,10 +71,21 @@ services:
|
||||
environment:
|
||||
- TARGET=${TARGET:-x86_64-unknown-linux-gnu}
|
||||
- OUTPUT_NAME=${OUTPUT_NAME:-agent-browser-linux-x64}
|
||||
# NOTE: $$ escapes a literal $ for the in-container shell. A single $ is
|
||||
# interpolated by docker compose at YAML parse time against the *host*
|
||||
# environment, which silently drops script-local variables like SRC
|
||||
# (caused 0.27.0-fork.7 to ship with a stale linux-arm64 binary because
|
||||
# the cp command resolved to `cp "" "/output/"` after compose ate $SRC
|
||||
# and $OUTPUT_NAME). $TARGET / $OUTPUT_NAME are set via `environment:`
|
||||
# below — those are also passed into the container, so $$TARGET and
|
||||
# $$OUTPUT_NAME read them at script time.
|
||||
command: |
|
||||
-c '
|
||||
cargo zigbuild --release --target $TARGET
|
||||
cp /build/target/$TARGET/release/agent-browser* /output/$OUTPUT_NAME
|
||||
chmod +x /output/$OUTPUT_NAME 2>/dev/null || true
|
||||
echo "✓ Built $OUTPUT_NAME"
|
||||
set -e
|
||||
cargo zigbuild --release --target $$TARGET
|
||||
SRC="/build/target/$$TARGET/release/agent-browser"
|
||||
if [ -f "$$SRC.exe" ]; then SRC="$$SRC.exe"; fi
|
||||
cp "$$SRC" "/output/$$OUTPUT_NAME"
|
||||
chmod +x /output/$$OUTPUT_NAME 2>/dev/null || true
|
||||
echo "✓ Built $$OUTPUT_NAME"
|
||||
'
|
||||
|
||||
Executable
+111
@@ -0,0 +1,111 @@
|
||||
#!/bin/sh
|
||||
# agent-browser-stealth installer — downloads the prebuilt binary from the
|
||||
# GitHub Release (no npm, no auth for you or your users).
|
||||
#
|
||||
# curl -fsSL https://raw.githubusercontent.com/leeguooooo/agent-browser-stealth/main/install.sh | sh
|
||||
#
|
||||
# Env overrides:
|
||||
# AGENT_BROWSER_VERSION=v0.27.0-fork.11 pin a specific release tag
|
||||
# AGENT_BROWSER_BIN_DIR=/usr/local/bin install location (auto-detected otherwise)
|
||||
set -eu
|
||||
|
||||
REPO="leeguooooo/agent-browser-stealth"
|
||||
BIN_NAME="agent-browser"
|
||||
|
||||
err() { printf '\033[31merror:\033[0m %s\n' "$1" >&2; exit 1; }
|
||||
info() { printf '\033[36m==>\033[0m %s\n' "$1" >&2; }
|
||||
|
||||
command -v curl >/dev/null 2>&1 || err "curl is required"
|
||||
command -v tar >/dev/null 2>&1 || err "tar is required"
|
||||
|
||||
# --- detect platform -> release asset name -------------------------------
|
||||
os=$(uname -s)
|
||||
arch=$(uname -m)
|
||||
case "$os" in
|
||||
Darwin) plat="darwin" ;;
|
||||
Linux) plat="linux" ;;
|
||||
*) err "unsupported OS: $os (use the Windows .exe asset from the Releases page)" ;;
|
||||
esac
|
||||
case "$arch" in
|
||||
x86_64|amd64) cpu="x64" ;;
|
||||
arm64|aarch64) cpu="arm64" ;;
|
||||
*) err "unsupported architecture: $arch" ;;
|
||||
esac
|
||||
|
||||
# musl (Alpine etc.) gets the statically-linked Linux build
|
||||
libc=""
|
||||
if [ "$plat" = "linux" ] && ! ldd /bin/sh 2>/dev/null | grep -qi 'gnu\|glibc'; then
|
||||
if [ -e /lib/ld-musl-x86_64.so.1 ] || [ -e /lib/ld-musl-aarch64.so.1 ]; then
|
||||
libc="-musl"
|
||||
fi
|
||||
fi
|
||||
asset="agent-browser-${plat}${libc}-${cpu}"
|
||||
|
||||
# --- resolve release tag --------------------------------------------------
|
||||
tag="${AGENT_BROWSER_VERSION:-}"
|
||||
if [ -z "$tag" ]; then
|
||||
info "resolving latest release..."
|
||||
# Resolve via the releases/latest redirect on the github.com web host, NOT the
|
||||
# api.github.com JSON API (which rate-limits unauthenticated callers to 60/hr).
|
||||
# github.com/<repo>/releases/latest -> 302 -> github.com/<repo>/releases/tag/<TAG>
|
||||
loc=$(curl -fsSLI -o /dev/null -w '%{url_effective}' \
|
||||
"https://github.com/${REPO}/releases/latest" 2>/dev/null || true)
|
||||
case "$loc" in
|
||||
*/releases/tag/*) tag="${loc##*/releases/tag/}" ;;
|
||||
*) tag="" ;;
|
||||
esac
|
||||
[ -n "$tag" ] || err "could not resolve latest release (set AGENT_BROWSER_VERSION=vX.Y.Z)"
|
||||
fi
|
||||
|
||||
base="https://github.com/${REPO}/releases/download/${tag}"
|
||||
tgz_url="${base}/${asset}.tar.gz"
|
||||
sha_url="${tgz_url}.sha256"
|
||||
|
||||
# --- download + verify ----------------------------------------------------
|
||||
tmp=$(mktemp -d)
|
||||
trap 'rm -rf "$tmp"' EXIT
|
||||
info "downloading ${asset} (${tag})..."
|
||||
curl -fsSL "$tgz_url" -o "$tmp/pkg.tar.gz" \
|
||||
|| err "download failed: $tgz_url (is asset '${asset}.tar.gz' attached to release ${tag}?)"
|
||||
|
||||
if curl -fsSL "$sha_url" -o "$tmp/pkg.sha256" 2>/dev/null; then
|
||||
info "verifying checksum..."
|
||||
expected=$(awk '{print $1}' "$tmp/pkg.sha256")
|
||||
if command -v shasum >/dev/null 2>&1; then
|
||||
actual=$(shasum -a 256 "$tmp/pkg.tar.gz" | awk '{print $1}')
|
||||
elif command -v sha256sum >/dev/null 2>&1; then
|
||||
actual=$(sha256sum "$tmp/pkg.tar.gz" | awk '{print $1}')
|
||||
else
|
||||
actual=""; info "no sha256 tool found, skipping verification"
|
||||
fi
|
||||
[ -z "$actual" ] || [ "$expected" = "$actual" ] || err "checksum mismatch (expected $expected, got $actual)"
|
||||
else
|
||||
info "no .sha256 published, skipping verification"
|
||||
fi
|
||||
|
||||
tar -xzf "$tmp/pkg.tar.gz" -C "$tmp"
|
||||
[ -f "$tmp/${BIN_NAME}" ] || err "archive did not contain ${BIN_NAME}"
|
||||
chmod +x "$tmp/${BIN_NAME}"
|
||||
|
||||
# --- choose install dir ---------------------------------------------------
|
||||
bindir="${AGENT_BROWSER_BIN_DIR:-}"
|
||||
if [ -z "$bindir" ]; then
|
||||
if [ -w /usr/local/bin ] 2>/dev/null; then bindir="/usr/local/bin"; else bindir="$HOME/.local/bin"; fi
|
||||
fi
|
||||
mkdir -p "$bindir"
|
||||
|
||||
mv "$tmp/${BIN_NAME}" "$bindir/${BIN_NAME}"
|
||||
# Aliases pointing at the same binary: `abs` (short) and `agent-browser-stealth`
|
||||
# (the fork's package name). All three names work, and an upgrade refreshes
|
||||
# whichever name you actually run.
|
||||
for alias_name in abs agent-browser-stealth; do
|
||||
ln -sf "$bindir/${BIN_NAME}" "$bindir/${alias_name}" 2>/dev/null || true
|
||||
done
|
||||
|
||||
info "installed -> ${bindir}/ (agent-browser, agent-browser-stealth, abs)"
|
||||
"$bindir/${BIN_NAME}" --version 2>/dev/null || true
|
||||
|
||||
case ":$PATH:" in
|
||||
*":$bindir:"*) : ;;
|
||||
*) printf '\033[33mnote:\033[0m %s is not on your PATH. Add:\n export PATH="%s:$PATH"\n' "$bindir" "$bindir" >&2 ;;
|
||||
esac
|
||||
+4
-3
@@ -1,8 +1,9 @@
|
||||
{
|
||||
"name": "agent-browser-stealth",
|
||||
"version": "0.27.0-fork.2",
|
||||
"version": "0.27.0-fork.16",
|
||||
"description": "Browser automation CLI for AI agents — stealth fork with anti-detection",
|
||||
"type": "module",
|
||||
"packageManager": "pnpm@11.1.3",
|
||||
"files": [
|
||||
"bin",
|
||||
"scripts",
|
||||
@@ -21,9 +22,9 @@
|
||||
"version": "npm run version:sync && git add cli/Cargo.toml",
|
||||
"build:native": "npm run version:sync && cargo build --release --manifest-path cli/Cargo.toml && node scripts/copy-native.js",
|
||||
"build:linux": "npm run version:sync && docker compose -f docker/docker-compose.yml run --rm build-linux",
|
||||
"build:macos": "npm run version:sync && (cargo build --release --manifest-path cli/Cargo.toml --target aarch64-apple-darwin & cargo build --release --manifest-path cli/Cargo.toml --target x86_64-apple-darwin & wait) && cp cli/target/aarch64-apple-darwin/release/agent-browser bin/agent-browser-darwin-arm64 && cp cli/target/x86_64-apple-darwin/release/agent-browser bin/agent-browser-darwin-x64",
|
||||
"build:macos": "npm run version:sync && bash -c 'cargo build --release --manifest-path cli/Cargo.toml --target aarch64-apple-darwin & PID1=$!; cargo build --release --manifest-path cli/Cargo.toml --target x86_64-apple-darwin & PID2=$!; wait $PID1 || exit 1; wait $PID2 || exit 1' && cp cli/target/aarch64-apple-darwin/release/agent-browser bin/agent-browser-darwin-arm64 && cp cli/target/x86_64-apple-darwin/release/agent-browser bin/agent-browser-darwin-x64",
|
||||
"build:windows": "npm run version:sync && docker compose -f docker/docker-compose.yml run --rm build-windows",
|
||||
"build:all-platforms": "npm run version:sync && (npm run build:linux & npm run build:windows & wait) && npm run build:macos",
|
||||
"build:all-platforms": "npm run version:sync && npm run build:linux && npm run build:windows && npm run build:macos",
|
||||
"build:docker": "docker build -t agent-browser-builder -f docker/Dockerfile.build .",
|
||||
"release": "npm run version:sync && npm run build:all-platforms && npm publish --tag fork",
|
||||
"postinstall": "node scripts/postinstall.js"
|
||||
|
||||
@@ -1,2 +1,9 @@
|
||||
packages:
|
||||
- '.'
|
||||
minimumReleaseAge: 2880
|
||||
allowBuilds:
|
||||
'@mongodb-js/zstd': false
|
||||
msw: false
|
||||
node-liblzma: false
|
||||
sharp: false
|
||||
unrs-resolver: false
|
||||
|
||||
@@ -243,6 +243,9 @@ agent-browser screenshot --full full.png # full scroll height
|
||||
agent-browser screenshot --annotate map.png # numbered labels + legend keyed to snapshot refs
|
||||
```
|
||||
|
||||
Headless Chromium screenshots hide native scrollbars for consistent image output.
|
||||
Pass `--hide-scrollbars false` when launching to keep native scrollbars visible.
|
||||
|
||||
`--annotate` is designed for multimodal models: each label `[N]` maps to ref `@eN`.
|
||||
|
||||
### Handle multiple pages via tabs
|
||||
|
||||
@@ -103,6 +103,9 @@ agent-browser screenshot --full # Full page
|
||||
agent-browser pdf output.pdf # Save as PDF
|
||||
```
|
||||
|
||||
Headless Chromium screenshots hide native scrollbars for consistent image output.
|
||||
Pass `--hide-scrollbars false` when launching to keep native scrollbars visible.
|
||||
|
||||
## Video Recording
|
||||
|
||||
```bash
|
||||
@@ -309,6 +312,7 @@ agent-browser --headers <json> ... # HTTP headers scoped to URL's origin
|
||||
agent-browser --executable-path <p> # Custom browser executable
|
||||
agent-browser --extension <path> ... # Load browser extension (repeatable)
|
||||
agent-browser --ignore-https-errors # Ignore SSL certificate errors
|
||||
agent-browser --hide-scrollbars false # Keep native scrollbars visible in headless Chromium screenshots
|
||||
agent-browser --help # Show help (-h)
|
||||
agent-browser --version # Show version (-V)
|
||||
agent-browser <command> --help # Show detailed help for a command
|
||||
@@ -320,9 +324,9 @@ agent-browser <command> --help # Show detailed help for a command
|
||||
agent-browser --headed open example.com # Show browser window
|
||||
agent-browser --cdp 9222 snapshot # Connect via CDP port
|
||||
agent-browser connect 9222 # Alternative: connect command
|
||||
agent-browser console # View console messages
|
||||
agent-browser console # View console messages (needs AGENT_BROWSER_CAPTURE_CONSOLE=1)
|
||||
agent-browser console --clear # Clear console
|
||||
agent-browser errors # View page errors
|
||||
agent-browser errors # View page errors (needs AGENT_BROWSER_CAPTURE_CONSOLE=1)
|
||||
agent-browser errors --clear # Clear errors
|
||||
agent-browser highlight @e1 # Highlight element
|
||||
agent-browser inspect # Open Chrome DevTools for this session
|
||||
@@ -332,6 +336,25 @@ agent-browser profiler start # Start Chrome DevTools profiling
|
||||
agent-browser profiler stop trace.json # Stop and save profile
|
||||
```
|
||||
|
||||
### Debugging forms / hidden state with `eval`
|
||||
|
||||
The a11y `snapshot` shows visible, interactive elements — it does **not** show
|
||||
hidden inputs or a control's actual submitted value. When a form "looks filled"
|
||||
but submit-validation rejects it, go straight to the DOM with `eval` instead of
|
||||
guessing from the snapshot. This is usually the fastest way to find the real
|
||||
problem (e.g. a hidden `point_choice=none` that the visible UI never exposes):
|
||||
|
||||
```bash
|
||||
# Dump every field's name → value, including hidden inputs and unchecked radios
|
||||
agent-browser eval "JSON.stringify([...document.forms[0].elements].map(e=>({name:e.name,type:e.type,value:e.value,checked:e.checked})).filter(e=>e.name))"
|
||||
|
||||
# Inspect one hidden field directly
|
||||
agent-browser eval "document.querySelector('[name=point_choice]')?.value"
|
||||
|
||||
# Why won't it submit? Ask the browser's own validity API
|
||||
agent-browser eval "[...document.forms[0].elements].filter(e=>!e.validity?.valid).map(e=>e.name+': '+e.validationMessage)"
|
||||
```
|
||||
|
||||
## React / Web Vitals
|
||||
|
||||
Requires `--enable react-devtools` at launch for the `react ...` commands.
|
||||
@@ -383,7 +406,42 @@ AGENT_BROWSER_EXECUTABLE_PATH="/path/chrome" # Custom browser path
|
||||
AGENT_BROWSER_EXTENSIONS="/ext1,/ext2" # Comma-separated extension paths
|
||||
AGENT_BROWSER_INIT_SCRIPTS="/a.js,/b.js" # Comma-separated init script paths
|
||||
AGENT_BROWSER_ENABLE="react-devtools" # Comma-separated built-in init script features
|
||||
AGENT_BROWSER_HIDE_SCROLLBARS="false" # Keep native scrollbars visible in headless Chromium screenshots
|
||||
AGENT_BROWSER_PROVIDER="browserbase" # Cloud browser provider
|
||||
AGENT_BROWSER_STREAM_PORT="9223" # Override WebSocket streaming port (default: OS-assigned)
|
||||
AGENT_BROWSER_HOME="/path/to/agent-browser" # Custom install location
|
||||
AGENT_BROWSER_CLICK_MODE="dom" # Click strategy: "" (default: scroll-in + coordinate
|
||||
# click, DOM-dispatch fallback), "coord" (strict
|
||||
# coordinate only), "dom" (always element.click())
|
||||
```
|
||||
|
||||
### Click reliability
|
||||
|
||||
`click` auto-scrolls the target into view first, then dispatches a coordinate
|
||||
click. If that fails (a floating layer fails the occlusion guard, or the point
|
||||
won't resolve) it falls back to a DOM-dispatched `.click()` on the intended
|
||||
element. If a click *reports success but the page didn't react* — common for
|
||||
autocomplete/menu `<li>` items that close on the input's blur — retry that one
|
||||
with `AGENT_BROWSER_CLICK_MODE=dom` (a DOM dispatch doesn't move focus the way a
|
||||
real pointer press does, so the item still selects). `=coord` disables the
|
||||
fallback when you specifically want a hard failure on occlusion.
|
||||
|
||||
### Stealth / anti-detection knobs (fork)
|
||||
|
||||
```bash
|
||||
AGENT_BROWSER_CAPTURE_CONSOLE="1" # Enable `console`/`errors` capture. OFF by default:
|
||||
# a live CDP Runtime domain is a detectable bot signal,
|
||||
# so console/errors return empty (with a hint) until set.
|
||||
AGENT_BROWSER_TIMEZONE="Asia/Tokyo" # --launch only. Native timezone override (IANA id, or
|
||||
# "auto" to derive from locale). Aligns Intl+Date to a proxy.
|
||||
AGENT_BROWSER_BLOCK_WEBRTC="1" # --launch only. Hide local IP via WebRTC. Auto-forces WebRTC
|
||||
# through the proxy when one is set; "0" opts out.
|
||||
AGENT_BROWSER_HIDE_CANVAS="1" # --launch only. Session-stable canvas/audio fingerprint noise.
|
||||
AGENT_BROWSER_ADAPTIVE_REF="0" # Disable adaptive @ref relocation (on by default; relocates a
|
||||
# moved element by fingerprint when role/name re-query fails).
|
||||
```
|
||||
|
||||
> **Heads-up for `console` / `errors`:** capture is **off by default** in this stealth
|
||||
> fork. Both commands return `{"messages":[]}` / `{"errors":[]}` plus a `hint` until you
|
||||
> launch the session with `AGENT_BROWSER_CAPTURE_CONSOLE=1`. This keeps the CDP `Runtime`
|
||||
> domain disabled (a known bot signal) for the common automation path.
|
||||
|
||||
@@ -96,7 +96,7 @@ Read [references/issue-taxonomy.md](references/issue-taxonomy.md) for the full l
|
||||
- Within each section, test interactive elements: click buttons, fill forms, open dropdowns/modals.
|
||||
- Check edge cases: empty states, error handling, boundary inputs.
|
||||
- Try realistic end-to-end workflows (create, edit, delete flows).
|
||||
- Check the browser console for errors periodically.
|
||||
- Check the browser console for errors periodically. **Console/error capture is off by default in this stealth fork** — start the dogfood session with `AGENT_BROWSER_CAPTURE_CONSOLE=1` (e.g. `AGENT_BROWSER_CAPTURE_CONSOLE=1 agent-browser --session {SESSION} open <url>`) or `console`/`errors` will return empty.
|
||||
|
||||
**At each page:**
|
||||
|
||||
|
||||
@@ -230,6 +230,9 @@ agent-browser snapshot -i | grep -c "treeitem"
|
||||
|
||||
### Check console for errors
|
||||
|
||||
Console/error capture is off by default in this stealth fork — launch the session with
|
||||
`AGENT_BROWSER_CAPTURE_CONSOLE=1` first, or these return empty.
|
||||
|
||||
```bash
|
||||
agent-browser console
|
||||
agent-browser errors
|
||||
|
||||
@@ -10,7 +10,13 @@ hidden: true
|
||||
Fast browser automation CLI for AI agents. Chrome/Chromium via CDP with
|
||||
accessibility-tree snapshots and compact `@eN` element refs.
|
||||
|
||||
Install: `npm i -g agent-browser && agent-browser install`
|
||||
**Install / self-heal:** if the `agent-browser` (or `abs`) command is missing —
|
||||
e.g. a command errors with "command not found" — install it from the GitHub
|
||||
Release (no npm, no token), then retry. Do NOT fall back to other browser tools:
|
||||
|
||||
```bash
|
||||
curl -fsSL https://raw.githubusercontent.com/leeguooooo/agent-browser-stealth/main/install.sh | sh
|
||||
```
|
||||
|
||||
## Start here
|
||||
|
||||
|
||||
Reference in New Issue
Block a user