Documentation
¶
Overview ¶
Package browsergate is the loopback gate in front of a run's Playwright MCP server: the one place that decides which browser tools the harness may call, which URLs the agent may open, how many screenshots a run takes and how large each one is, and that keeps a copy of every screenshot for the room. Everything it allows is an exact match against a short list; anything else is refused.
The gate is one of three layers that keep the browser on loopback. It refuses the agent's own requests; Chromium's proxy and resolver flags in the maku-browser image refuse what a page asks for (redirects, subresources). Neither stops a renderer exploit from reaching what the pod's network policy allows, which is the harness's own trust domain: no credential lives in the browser sidecar.
Index ¶
- Constants
- Variables
- func CapImage(b []byte, mime string, limit int) ([]byte, string, error)
- func CheckCall(name string, args map[string]any) error
- func CheckURL(raw string) error
- func FilterTools(tools []map[string]any) []map[string]any
- func NewProxy(upstream string, counter *Counter, keep *Keep, vision bool, log *slog.Logger) http.Handler
- type Counter
- type Keep
- type Shot
Constants ¶
const ( MaxViewportWidth = 1920 MaxViewportHeight = 1080 )
The viewport ceiling browser_resize may ask for.
const ImageLimit = 512 << 10
ImageLimit is the size cap of one screenshot, in bytes.
const MaxBody = 4 << 20
MaxBody bounds a request the gate reads from the harness, the bound images/maku-opencode's MCP proxy keeps.
const ScreenshotTool = "browser_take_screenshot"
ScreenshotTool is the one allowed tool whose answer carries an image.
Variables ¶
var ( ErrTooLarge = errors.New("image_too_large") ErrUnsupportedImage = errors.New("image_unsupported") )
The refusals CapImage reports.
var ( ErrToolNotAllowed = errors.New("tool_not_allowed") ErrURLRefused = errors.New("url_refused") ErrViewport = errors.New("viewport") ErrCapReached = errors.New("screenshot_cap_reached") )
The refusals CheckCall and Counter report. Each one's text is the code the model reads.
var Allowed = []string{
"browser_navigate", "browser_navigate_back", "browser_snapshot", "browser_take_screenshot",
"browser_click", "browser_hover", "browser_type", "browser_fill_form", "browser_select_option",
"browser_press_key", "browser_resize", "browser_emulate_media", "browser_handle_dialog",
"browser_wait_for", "browser_find", "browser_console_messages", "browser_network_requests",
"browser_tabs", "browser_close",
}
Allowed is the tool allow-list, @playwright/mcp 0.0.85's names. Anything else, a tool a later release adds or one a page registers, is neither listed nor served. browser_evaluate, browser_run_code_unsafe, browser_file_upload, browser_drop and browser_drag are left out on purpose: each runs page script or moves files.
var ErrNoShot = errors.New("no such screenshot")
ErrNoShot reports a screenshot number the keep does not hold.
Functions ¶
func CapImage ¶
CapImage returns b unchanged when it is a PNG or JPEG of at most limit bytes. A larger one is re-encoded as JPEG quality 70, then halved up to three times until it fits, else ErrTooLarge. Bytes that are not the PNG or JPEG mime names are ErrUnsupportedImage.
func CheckCall ¶
CheckCall refuses a tools/call the gate must not forward: a tool off the allow-list, a url argument that is not literal loopback (on any tool, browser_navigate requiring one), or a browser_resize past the viewport ceiling.
func CheckURL ¶
CheckURL accepts an http URL to a loopback host named literally, with no user info. It is the rule for every URL a tool argument carries.
func FilterTools ¶
FilterTools keeps the tools/list entries whose name is on the allow-list, in their order. browser_take_screenshot stays listed whatever the run's model: without vision the gate keeps the image for the room and answers the model with text. Its inputSchema drops any filename property so models do not ask Playwright MCP to write a sidecar-local file instead of returning the image.
func NewProxy ¶
func NewProxy(upstream string, counter *Counter, keep *Keep, vision bool, log *slog.Logger) http.Handler
NewProxy returns the gate's handler: /mcp (POST, GET, DELETE) proxied to Playwright MCP at upstream, /shots and /shots/{n} reading keep, and /healthz. Every tools/call passes CheckCall, and every screenshot Counter.Take and CapImage before it is kept and answered; with vision false the model gets text in the image's place. A request carrying an Origin header, or a Host that is not loopback, is refused: only the harness, never a page in the browser, talks to the gate.
Types ¶
type Counter ¶
Counter is a run's screenshot count, persisted in Path as a decimal so a restarted sidecar cannot hand out the cap again. Max is the run's cap.
type Keep ¶
type Keep struct {
Dir string
}
Keep is the gate's copy of every screenshot it served or stripped, for the room-bridge to relay: <n>.png or <n>.jpg and <n>.json in Dir. It is written only by the gate's own answer handling.
type Shot ¶
type Shot struct {
// N is the screenshot's number in the run, from 1, the Counter's.
N int `json:"n"`
// Mime is image/png or image/jpeg, after CapImage.
Mime string `json:"mime"`
// Bytes is the stored image's size.
Bytes int `json:"bytes"`
Width int `json:"width"`
Height int `json:"height"`
// URL is the page the browser last reported, loopback by construction; empty when none was.
URL string `json:"url"`
TakenAt time.Time `json:"takenAt"`
// Vision is whether the run's model received the image, or text in its place.
Vision bool `json:"vision"`
}
Shot describes one kept screenshot: the /shots list entry and the <n>.json beside its bytes.