diff --git a/tools/browser/browser.go b/tools/browser/browser.go index 7db7101..9cddf95 100644 --- a/tools/browser/browser.go +++ b/tools/browser/browser.go @@ -36,6 +36,24 @@ const ( janitorInterval = 1 * time.Minute ) +// hostResolverRules forces Chrome to fail DNS resolution for private/internal +// IP ranges and localhost, so in-page fetch() and evaluate()'d JS cannot reach +// cloud-metadata endpoints or localhost services — the Go-side +// ValidateURLNotInternal check only sees the top-level URL, so this covers +// sub-resource and script-initiated requests. (S3) +// +// Residual gap: name-based rules do not stop a page that dials a *literal* +// private IP URL in some Chrome builds; the complete fix is an allowlisting +// proxy (deferred). See the security-deeper spec's limitations section. +const hostResolverRules = "MAP localhost ~NOTFOUND," + + "MAP *.localhost ~NOTFOUND," + + "MAP 127.0.0.0/8 ~NOTFOUND," + + "MAP 10.0.0.0/8 ~NOTFOUND," + + "MAP 172.16.0.0/12 ~NOTFOUND," + + "MAP 169.254.0.0/16 ~NOTFOUND," + + "MAP 192.168.0.0/16 ~NOTFOUND," + + "MAP [::1] ~NOTFOUND" + // stealthScript runs before any page script on every new document. It hides the // most common headless/automation tells that JS-heavy sites use to refuse to // render content (Cloudflare, Akamai, anti-bot scripts, "please enable JS" @@ -474,6 +492,7 @@ func launchBrowser(parent context.Context) (context.Context, context.CancelFunc, chromedp.UserAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36"), chromedp.Flag("disable-blink-features", "AutomationControlled"), chromedp.Flag("lang", "en-US"), + chromedp.Flag("host-resolver-rules", hostResolverRules), chromedp.UserDataDir(udd), )..., ) diff --git a/tools/browser/browser_ssrf_live_test.go b/tools/browser/browser_ssrf_live_test.go new file mode 100644 index 0000000..1c06c8d --- /dev/null +++ b/tools/browser/browser_ssrf_live_test.go @@ -0,0 +1,24 @@ +//go:build live + +package browser + +import ( + "context" + "testing" + "time" + + "github.com/stretchr/testify/require" +) + +// TestInPageFetchPrivateIPBlocked_Live verifies host-resolver-rules stops an +// in-page fetch to a metadata IP. Requires a real Chrome. Run: +// go test -tags live ./tools/browser/ -run TestInPageFetchPrivateIPBlocked_Live +func TestInPageFetchPrivateIPBlocked_Live(t *testing.T) { + tool := NewBrowserTool() + defer tool.Shutdown() + ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second) + defer cancel() + res, err := tool.Execute(ctx, []byte(`{"action":"evaluate","url":"https://example.com","session":"ssrf","script":"fetch('http://169.254.169.254/').then(()=>'REACHED').catch(e=>'BLOCKED:'+e)"}`)) + require.NoError(t, err) + require.NotContains(t, res.Output, "REACHED") +} diff --git a/tools/browser/browser_test.go b/tools/browser/browser_test.go index e36548c..7a78fd3 100644 --- a/tools/browser/browser_test.go +++ b/tools/browser/browser_test.go @@ -4,12 +4,23 @@ import ( "context" "encoding/json" "fmt" + "strings" "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) +func TestHostResolverRules_BlocksPrivateRanges(t *testing.T) { + require.Contains(t, hostResolverRules, "127.0.0.0/8") + require.Contains(t, hostResolverRules, "169.254.0.0/16") + require.Contains(t, hostResolverRules, "10.0.0.0/8") + require.Contains(t, hostResolverRules, "192.168.0.0/16") + require.Contains(t, hostResolverRules, "172.16.0.0/12") + require.Contains(t, hostResolverRules, "localhost") + require.True(t, strings.Contains(hostResolverRules, "~NOTFOUND")) +} + func TestBrowserToolName(t *testing.T) { tool := &BrowserTool{} assert.Equal(t, "browser", tool.Name())