From 3546ea0fff44852f3aa12a2cbe1d06ab03145e16 Mon Sep 17 00:00:00 2001 From: Coff0xc <184964491+Coff0xc@users.noreply.github.com> Date: Wed, 18 Nov 2026 10:56:10 +0000 Subject: [PATCH] fix(mcp): rewrite npx -y and playwright-mcp rows missing --browser The stock seed was npx @playwright/mcp, but the UI mock and common invocations use npx -y, which the first pass left untouched. Also add --browser chromium to a leftover playwright-mcp --headless row, resolve the MCP's playwright-core CLI via require.resolve, and cover proxy-arg stripping so capture sync does not depend on args[0]. --- CHANGELOG.md | 2 +- Dockerfile | 2 +- db/browser_mcp.go | 66 ++++++++++++++++++----- db/browser_mcp_test.go | 86 ++++++++++++++++++++++++++++-- server/manager_browser_mcp_test.go | 24 +++++++++ 5 files changed, 161 insertions(+), 19 deletions(-) create mode 100644 server/manager_browser_mcp_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ad90aa..c5a2543 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -54,7 +54,7 @@ All notable changes to this project are documented in this file. The format is b - **Chat session directories**: `ChatAgent.Chat` accepts only `[A-Za-z0-9_-]` session ids (at most 64 bytes) before joining them under `/sessions`, so `..` and path separators cannot escape that directory. - **Change-password rate limit**: `POST /api/auth/change-password` uses the same limiter as login, keyed by client IP and by the session token, so a stolen session cannot guess the current password without backoff. - **Ingested traffic headers**: `RecordExternal` rejects a method that is not an HTTP token and header names or values that contain CR, LF, or NUL, matching Replay, so ingested exchanges cannot inject extra header lines into stored evidence. -- **Browser MCP starts offline** (#329): the seeded `browser` MCP ran `npx @playwright/mcp`, which contacts the npm registry on every start even with the package installed globally, and Playwright MCP defaulted to a Chrome channel the image does not ship. It now runs the image's `playwright-mcp` binary with `--headless --browser chromium`. Existing databases that still have the stock `npx` row are rewritten once; a row the operator has edited is left alone. The image also installs the Chromium build `@playwright/mcp` and `@playwright/cli` expect, which is not the same build `playwright install` puts in the cache. +- **Browser MCP starts offline** (#329): the seeded `browser` MCP ran `npx @playwright/mcp`, which contacts the npm registry on every start even with the package installed globally, and Playwright MCP defaulted to a Chrome channel the image does not ship. It now runs the image's `playwright-mcp` binary with `--headless --browser chromium`. Existing databases that still have a stock `npx @playwright/mcp` row (including the common `-y` form) or a `playwright-mcp` row with no `--browser` are rewritten once; any other command or package is left alone. The image also installs the Chromium build `@playwright/mcp` and `@playwright/cli` expect, which is not the same build `playwright install` puts in the cache. - **ask_operator image_path**: the screenshot path must resolve inside the task working directory (`/tasks/`). Absolute paths, `..` segments, and symlinks that leave that directory are refused, so the tool cannot copy an arbitrary server image into the operator transcript. - **insert_assets fail-closed**: when intercept or task allow/block rules cannot be loaded, `insert_assets` refuses the whole call instead of evaluating an empty rule set (which allowed every asset). The tool result does not include the underlying database error. - **BlobRange length no longer wraps int64**: `offset+length > size` overflowed when length was near MaxInt64, so the cap was skipped and `make([]byte, length)` could exhaust memory. The bound is now `length > size-offset`. diff --git a/Dockerfile b/Dockerfile index 80a83b2..48cbf37 100644 --- a/Dockerfile +++ b/Dockerfile @@ -184,7 +184,7 @@ ARG PLAYWRIGHT_VERSION=1.64.1 RUN npm install -g "@playwright/mcp@${PLAYWRIGHT_MCP_VERSION}" "@playwright/cli@${PLAYWRIGHT_CLI_VERSION}" "playwright@${PLAYWRIGHT_VERSION}" \ && playwright-cli --help \ && playwright install --with-deps chromium \ - && node /usr/local/lib/node_modules/@playwright/mcp/node_modules/playwright-core/cli.js install chromium \ + && node "$(node -p 'require.resolve("playwright-core/cli.js",{paths:["/usr/local/lib/node_modules/@playwright/mcp"]})')" install chromium \ && rm -rf /var/lib/apt/lists/* WORKDIR /app # Binary compiled from source in the build stage above. diff --git a/db/browser_mcp.go b/db/browser_mcp.go index 0f1afcd..3cee0d5 100644 --- a/db/browser_mcp.go +++ b/db/browser_mcp.go @@ -20,19 +20,44 @@ const ( // /opt/google/chrome, which the image does not ship. const browserMCPSeedArgs = `["--headless","--browser","chromium"]` -// rewriteStockNpxBrowserMCP converts the original seed -// `npx @playwright/mcp [--headless ...]` to the image's global playwright-mcp -// binary. ok is false when the row is not that stock invocation (the operator -// changed the command or the package), so those rows are left alone. +// npxMetaFlags are npx switches that do not take a value and that still leave +// the following argument as the package spec. `-y` is the form the UI mock +// used; `--` is a common separator. Flags that take a value (`-p`, `--package`) +// are treated as a custom invocation and left alone. +var npxMetaFlags = map[string]bool{ + "-y": true, + "--yes": true, + "--": true, + "-q": true, + "--quiet": true, +} + +func playwrightMCPPackageIndex(args []string) (int, bool) { + for i := 0; i < len(args); i++ { + a := args[i] + if npxMetaFlags[a] { + continue + } + if a == "@playwright/mcp" || strings.HasPrefix(a, "@playwright/mcp@") { + return i, true + } + return 0, false + } + return 0, false +} + +// rewriteStockNpxBrowserMCP converts `npx [@meta...] @playwright/mcp [...]` to +// the image's global playwright-mcp binary. ok is false when the row is not +// that invocation (the operator changed the command or the package). func rewriteStockNpxBrowserMCP(command string, args []string) (string, []string, bool) { - if command != "npx" || len(args) == 0 { + if command != "npx" { return command, args, false } - pkg := args[0] - if pkg != "@playwright/mcp" && !strings.HasPrefix(pkg, "@playwright/mcp@") { + idx, ok := playwrightMCPPackageIndex(args) + if !ok { return command, args, false } - rest := append([]string{}, args[1:]...) + rest := append([]string{}, args[idx+1:]...) if !hasCLIFlag(rest, "--headless") { rest = append(rest, "--headless") } @@ -42,6 +67,22 @@ func rewriteStockNpxBrowserMCP(command string, args []string) (string, []string, return browserMCPCommand, rest, true } +// rewriteBrowserMCPRow is the one-time row rewrite: stock npx @playwright/mcp +// becomes playwright-mcp, and a playwright-mcp row that still has no --browser +// (the MCP defaults to a Chrome channel the image does not ship) gets +// --browser chromium. Any other command or package is left alone. +func rewriteBrowserMCPRow(command string, args []string) (string, []string, bool) { + cmd, out, ok := rewriteStockNpxBrowserMCP(command, args) + if !ok { + cmd, out = command, append([]string{}, args...) + } + if cmd == browserMCPCommand && !hasCLIFlag(out, "--browser") { + out = append(out, "--browser", "chromium") + ok = true + } + return cmd, out, ok +} + func hasCLIFlag(args []string, flag string) bool { eq := flag + "=" for _, a := range args { @@ -53,9 +94,10 @@ func hasCLIFlag(args []string, flag string) bool { } // migrateStockBrowserMCP rewrites a leftover stock `npx @playwright/mcp` browser -// MCP row once. Gated by a settings flag so an operator who later puts npx back -// is not rewritten on the next start. A row whose command or first arg is no -// longer the stock seed is left as-is. +// MCP row once, and adds `--browser chromium` to a playwright-mcp row that +// still has none. Gated by a settings flag so an operator who later puts npx +// back is not rewritten on the next start. Any other command or package is +// left as-is. func (d *DB) migrateStockBrowserMCP() error { if v, _, _ := d.GetSetting(browserMCPDirectBinSetting); v == "done" { return nil @@ -75,7 +117,7 @@ func (d *DB) migrateStockBrowserMCP() error { return d.SetSetting(browserMCPDirectBinSetting, "done") } } - newCommand, newArgs, ok := rewriteStockNpxBrowserMCP(command, args) + newCommand, newArgs, ok := rewriteBrowserMCPRow(command, args) if ok { raw, err := json.Marshal(newArgs) if err != nil { diff --git a/db/browser_mcp_test.go b/db/browser_mcp_test.go index ea68742..5786c49 100644 --- a/db/browser_mcp_test.go +++ b/db/browser_mcp_test.go @@ -63,15 +63,31 @@ func TestRewriteStockNpxBrowserMCP(t *testing.T) { wantCommand: "playwright-mcp", wantArgs: []string{"--browser=chrome", "--headless"}, }, + { + name: "npx -y", + command: "npx", + args: []string{"-y", "@playwright/mcp", "--headless"}, + wantOK: true, + wantCommand: "playwright-mcp", + wantArgs: []string{"--headless", "--browser", "chromium"}, + }, + { + name: "npx --yes -- separator", + command: "npx", + args: []string{"--yes", "--", "@playwright/mcp"}, + wantOK: true, + wantCommand: "playwright-mcp", + wantArgs: []string{"--headless", "--browser", "chromium"}, + }, { name: "already playwright-mcp", command: "playwright-mcp", args: []string{"--headless"}, }, { - name: "npx -y is a user edit", + name: "npx -p is a custom invocation", command: "npx", - args: []string{"-y", "@playwright/mcp", "--headless"}, + args: []string{"-p", "@playwright/mcp", "playwright-mcp", "--headless"}, }, { name: "different package", @@ -108,6 +124,34 @@ func TestRewriteStockNpxBrowserMCP(t *testing.T) { } } +func TestRewriteBrowserMCPRow(t *testing.T) { + cmd, args, ok := rewriteBrowserMCPRow("playwright-mcp", []string{"--headless"}) + if !ok || cmd != browserMCPCommand || !slices.Equal(args, []string{"--headless", "--browser", "chromium"}) { + t.Fatalf("missing --browser: ok=%v %q %q", ok, cmd, args) + } + + cmd, args, ok = rewriteBrowserMCPRow("playwright-mcp", []string{"--headless", "--browser", "firefox"}) + if ok { + t.Fatalf("existing --browser rewritten: %q %q", cmd, args) + } + + cmd, args, ok = rewriteBrowserMCPRow("npx", []string{"-y", "@playwright/mcp", "--headless"}) + if !ok || cmd != browserMCPCommand || !slices.Equal(args, []string{"--headless", "--browser", "chromium"}) { + t.Fatalf("npx -y: ok=%v %q %q", ok, cmd, args) + } + + in := []string{"--headless"} + orig := slices.Clone(in) + _, out, ok := rewriteBrowserMCPRow("playwright-mcp", in) + if !ok { + t.Fatal("expected rewrite") + } + out[0] = "mutated" + if !slices.Equal(in, orig) { + t.Fatalf("input mutated: %q", in) + } +} + func TestRewriteStockNpxBrowserMCPDoesNotAliasInput(t *testing.T) { in := []string{"@playwright/mcp", "--headless"} orig := slices.Clone(in) @@ -163,8 +207,32 @@ func TestSeedAndMigrateBrowserMCP(t *testing.T) { } }) - t.Run("custom npx -y left alone", func(t *testing.T) { - custom := []string{"-y", "@playwright/mcp", "--headless"} + t.Run("npx -y rewritten", func(t *testing.T) { + putBrowserMCP(t, d, "npx", []string{"-y", "@playwright/mcp", "--headless"}) + cleanupExec(t, d, `DELETE FROM settings WHERE key=$1`, browserMCPDirectBinSetting) + if err := d.seedBuiltins(); err != nil { + t.Fatal(err) + } + cmd, args, _ := readBrowserMCP(t, d) + if cmd != browserMCPCommand || !slices.Equal(args, []string{"--headless", "--browser", "chromium"}) { + t.Fatalf("got %q %q", cmd, args) + } + }) + + t.Run("playwright-mcp missing --browser", func(t *testing.T) { + putBrowserMCP(t, d, "playwright-mcp", []string{"--headless"}) + cleanupExec(t, d, `DELETE FROM settings WHERE key=$1`, browserMCPDirectBinSetting) + if err := d.seedBuiltins(); err != nil { + t.Fatal(err) + } + cmd, args, _ := readBrowserMCP(t, d) + if cmd != browserMCPCommand || !slices.Equal(args, []string{"--headless", "--browser", "chromium"}) { + t.Fatalf("got %q %q", cmd, args) + } + }) + + t.Run("custom package left alone", func(t *testing.T) { + custom := []string{"@modelcontextprotocol/server-github"} putBrowserMCP(t, d, "npx", custom) cleanupExec(t, d, `DELETE FROM settings WHERE key=$1`, browserMCPDirectBinSetting) if err := d.seedBuiltins(); err != nil { @@ -196,9 +264,17 @@ func putBrowserMCP(t *testing.T, d *DB, command string, args []string) { if err != nil { t.Fatal(err) } - if _, err := d.Exec(`UPDATE mcp_servers SET command=$1, args=$2 WHERE name=$3`, command, string(raw), browserMCPName); err != nil { + res, err := d.Exec(`UPDATE mcp_servers SET command=$1, args=$2 WHERE name=$3`, command, string(raw), browserMCPName) + if err != nil { t.Fatal(err) } + n, err := res.RowsAffected() + if err != nil { + t.Fatal(err) + } + if n != 1 { + t.Fatalf("update browser mcp: %d rows", n) + } } func readBrowserMCP(t *testing.T, d *DB) (command string, args []string, raw []byte) { diff --git a/server/manager_browser_mcp_test.go b/server/manager_browser_mcp_test.go new file mode 100644 index 0000000..fa5eb27 --- /dev/null +++ b/server/manager_browser_mcp_test.go @@ -0,0 +1,24 @@ +package server + +import ( + "slices" + "testing" +) + +func TestStripProxyArgsLeavesBrowserMCPFlags(t *testing.T) { + in := []string{"--headless", "--browser", "chromium", "--proxy-server", "http://127.0.0.1:8788"} + got := stripProxyArgs(in) + want := []string{"--headless", "--browser", "chromium"} + if !slices.Equal(got, want) { + t.Fatalf("got %q, want %q", got, want) + } + if in[3] != "--proxy-server" { + t.Fatal("input mutated") + } + + equals := []string{"--headless", "--proxy-server=http://127.0.0.1:8788", "--browser", "chromium"} + got = stripProxyArgs(equals) + if !slices.Equal(got, want) { + t.Fatalf("equals form: got %q, want %q", got, want) + } +}