browsergate

package
v0.56.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 26, 2026 License: Apache-2.0 Imports: 26 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
const (
	MaxViewportWidth  = 1920
	MaxViewportHeight = 1080
)

The viewport ceiling browser_resize may ask for.

View Source
const ImageLimit = 512 << 10

ImageLimit is the size cap of one screenshot, in bytes.

View Source
const MaxBody = 4 << 20

MaxBody bounds a request the gate reads from the harness, the bound images/maku-opencode's MCP proxy keeps.

View Source
const ScreenshotTool = "browser_take_screenshot"

ScreenshotTool is the one allowed tool whose answer carries an image.

Variables

View Source
var (
	ErrTooLarge         = errors.New("image_too_large")
	ErrUnsupportedImage = errors.New("image_unsupported")
)

The refusals CapImage reports.

View Source
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.

View Source
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.

View Source
var ErrNoShot = errors.New("no such screenshot")

ErrNoShot reports a screenshot number the keep does not hold.

Functions

func CapImage

func CapImage(b []byte, mime string, limit int) ([]byte, string, error)

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

func CheckCall(name string, args map[string]any) error

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

func CheckURL(raw string) error

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

func FilterTools(tools []map[string]any) []map[string]any

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.

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

type Counter struct {
	Path string
	Max  int
	// contains filtered or unexported fields
}

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.

func (*Counter) Take

func (c *Counter) Take() (int, error)

Take reserves the next screenshot and returns its number, from 1, or ErrCapReached once Max are taken. A count file it cannot read fails closed.

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.

func NewKeep

func NewKeep(dir string) (*Keep, error)

NewKeep returns a keep in dir, creating it.

func (*Keep) List

func (k *Keep) List(after int) ([]Shot, error)

List returns the kept screenshots numbered above after, in order.

func (*Keep) Open

func (k *Keep) Open(n int) ([]byte, string, error)

Open returns screenshot n's bytes and MIME type, or ErrNoShot.

func (*Keep) Put

func (k *Keep) Put(s Shot, data []byte) error

Put stores data as screenshot s.N; s.Bytes is set from data. The image is renamed into place before its description, so a listed screenshot always has its bytes.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL