webfetch

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2026 License: AGPL-3.0 Imports: 25 Imported by: 0

Documentation

Overview

Package webfetch implements the SSRF-safe HTTP fetch primitive used by the web_fetch tool. It provides configurable options, an IP guard that blocks loopback/private/link-local addresses, a custom dialer, redirect validation, content-type gating, and a bounded body reader.

Index

Constants

View Source
const ToolName = "web_fetch"

ToolName is the model-facing name of the web_fetch tool.

Variables

View Source
var ApprovalDescriptor = approval.Descriptor{
	ID:              "web:fetch",
	CommandPrefix:   "",
	Notice:          "The model wants to fetch this URL.",
	HarnessVerb:     "fetch",
	EvaluatorPrompt: evaluatorPrompt,
}

ApprovalDescriptor describes how a web_fetch approval request is presented and evaluated. It is the single source of truth for this tool's approval metadata: the approval overlay, harness logger, and AI evaluator all read it instead of switching on a category enum.

View Source
var DefaultOptions = Options{
	Timeout:             15 * time.Second,
	MaxBodyBytes:        2 << 20,
	MaxRedirects:        5,
	AllowedContentTypes: []string{"text/html", "text/plain", "application/xhtml+xml"},
}

DefaultOptions are conservative defaults suitable for production use.

Functions

func ToolDef

func ToolDef() tooldef.Definition

ToolDef returns the jungi tool definition for web_fetch.

Types

type Extracted

type Extracted struct {
	// Title is the page title extracted by the readability parser.
	Title string
	// Markdown is the lean markdown conversion of the main content.
	Markdown string
}

Extracted holds the result of extracting and converting a web page.

func DefaultFetcher

func DefaultFetcher(ctx context.Context, rawURL string) (*Extracted, error)

DefaultFetcher performs a real SSRF-safe fetch followed by readability extraction and markdown conversion.

func ExtractMarkdown

func ExtractMarkdown(htmlBytes []byte, pageURL string) (*Extracted, error)

ExtractMarkdown extracts the main content from htmlBytes (the raw HTTP body fetched from pageURL) and converts it to lean markdown. It:

  • runs go-readability to isolate the main article content and title,
  • converts the resulting HTML to markdown (headings, lists, links, code, blockquote, tables only),
  • strips data: URIs, inline SVG blocks, and collapses redundant blank lines.

type Fetcher

type Fetcher func(ctx context.Context, rawURL string) (*Extracted, error)

Fetcher is the function signature for the HTTP fetch + extraction pipeline. It is injectable so tests can substitute a stub without hitting the network.

type Options

type Options struct {
	// Timeout is the total request timeout including reading the body.
	Timeout time.Duration
	// MaxBodyBytes caps the number of body bytes read. Content beyond this
	// limit is silently discarded (not an error); the caller receives a
	// partial body.
	MaxBodyBytes int64
	// MaxRedirects is the maximum number of redirects to follow. Exceeding
	// this limit returns an error.
	MaxRedirects int
	// AllowedContentTypes is the set of MIME type prefixes that are
	// accepted. Responses whose Content-Type does not start with one of
	// these values are rejected. A nil or empty slice falls back to
	// DefaultOptions.AllowedContentTypes; use a non-empty slice to narrow
	// the set. There is no way to accept all content types.
	AllowedContentTypes []string
	// OnBlocked, if non-nil, is called whenever a request or redirect is
	// blocked by the SSRF guard or the content-type gate. Use this to emit
	// diagnostic logs without importing a specific logger into this package.
	OnBlocked func(msg string)
	// contains filtered or unexported fields
}

Options configures the fetch behaviour.

type Result

type Result struct {
	// Body contains up to MaxBodyBytes of the response body.
	Body []byte
	// ContentType is the raw Content-Type header value.
	ContentType string
	// StatusCode is the HTTP response status code.
	StatusCode int
}

Result holds the response body and metadata from a successful fetch.

func Fetch

func Fetch(ctx context.Context, url string, opts Options) (*Result, error)

Fetch performs an SSRF-safe HTTP GET of url using opts. It:

  • rejects non-http/https schemes,
  • resolves the target host and rejects private/loopback/link-local IPs,
  • follows up to opts.MaxRedirects hops, re-validating each destination,
  • gates the response on opts.AllowedContentTypes,
  • reads at most opts.MaxBodyBytes of the response body.

type Tool

type Tool struct {
	// contains filtered or unexported fields
}

Tool implements tool.Tool for the web_fetch tool. It binds the sandbox (for the output directory), a lazy approval accessor, and an injectable fetcher.

func New

func New(sb sandbox.Sandbox, onApproval func() func(approval.Request), fetcher Fetcher) *Tool

New returns a web_fetch Tool bound to sb, the lazy approval accessor, and the fetcher. Pass DefaultFetcher for production; pass a stub in tests.

func (*Tool) Definition

func (*Tool) Definition() tooldef.Definition

Definition delegates to the package-level ToolDef.

func (*Tool) Execute

func (t *Tool) Execute(ctx context.Context, req message.ToolUseRequest) tool.Result

Execute approval-gates the fetch, runs the fetcher on approval, writes the result to the pages directory, and returns the path, title, line count, and a capped first-window preview.

func (*Tool) Preview added in v0.2.1

func (*Tool) Preview(req message.ToolUseRequest) (string, string)

Preview returns the URL subject from the raw request so the start history line matches completion.

Source Files

  • approval.go
  • content.go
  • fetch.go
  • tool.go

Jump to

Keyboard shortcuts

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