Chrome's --host-resolver-rules only glob-matches host strings, so the CIDR entries (127.0.0.0/8, 10.0.0.0/8, 172.16.0.0/12, 169.254.0.0/16, 192.168.0.0/16) never matched: pages could fetch private IP literals and names resolving to private IPs, and the live SSRF test hung dialing 169.254.169.254 until its deadline. Replace the rules with web.EgressProxy, a loopback HTTP proxy (CONNECT and absolute-URI HTTP) that resolves each destination, refuses it if any address is private, and dials the validated IP directly. Chrome is pointed at it with --proxy-server and <-loopback>, and WebRTC is limited to proxied UDP. Stagehand's local mode allows only Chrome's own debugging port through, which its extension needs. SafeHTTPClient and the proxy now share one safe dialer, which races the validated addresses happy-eyeballs style so dual-stack hosts with broken IPv6 don't stall. isPrivateIP also covers multicast, reserved, benchmark, Teredo and NAT64/6to4/IPv4-compatible addresses embedding a private IPv4.
88 lines
2.9 KiB
Go
88 lines
2.9 KiB
Go
//go:build live
|
|
|
|
package browser
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/chromedp/cdproto/runtime"
|
|
"github.com/chromedp/chromedp"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// TestInPageFetchPrivateIPBlocked_Live verifies the egress proxy stops
|
|
// in-page fetches and navigations to loopback, private-resolving names and the
|
|
// metadata IP, without any request reaching a local server. Requires a real
|
|
// Chrome and network access. Run:
|
|
//
|
|
// go test -tags live ./tools/browser/ -run TestInPageFetchPrivateIPBlocked_Live
|
|
func TestInPageFetchPrivateIPBlocked_Live(t *testing.T) {
|
|
var hits atomic.Int64
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
hits.Add(1)
|
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
|
fmt.Fprint(w, "secret")
|
|
}))
|
|
defer srv.Close()
|
|
port := srv.Listener.Addr().(interface{ String() string }).String()
|
|
port = port[strings.LastIndex(port, ":")+1:]
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 90*time.Second)
|
|
defer cancel()
|
|
|
|
taskCtx, ccancel, err := launchBrowser(ctx)
|
|
require.NoError(t, err)
|
|
defer ccancel()
|
|
|
|
// Positive control: public sites work through the proxy.
|
|
var title string
|
|
require.NoError(t, chromedp.Run(taskCtx, chromedp.Navigate("https://example.com"), chromedp.Title(&title)))
|
|
require.Contains(t, title, "Example")
|
|
|
|
targets := []string{
|
|
"http://127.0.0.1:" + port,
|
|
"http://localhost:" + port,
|
|
"http://127.0.0.1.nip.io:" + port,
|
|
"http://169.254.169.254/latest/meta-data/",
|
|
}
|
|
for _, target := range targets {
|
|
var result string
|
|
script := fmt.Sprintf(`(async () => {
|
|
const ac = new AbortController();
|
|
const timer = setTimeout(() => ac.abort(), 10000);
|
|
try {
|
|
await fetch(%q, {signal: ac.signal});
|
|
return 'REACHED';
|
|
} catch (e) {
|
|
return 'BLOCKED';
|
|
} finally { clearTimeout(timer); }
|
|
})()`, target)
|
|
start := time.Now()
|
|
err = chromedp.Run(taskCtx, chromedp.Evaluate(script, &result, func(p *runtime.EvaluateParams) *runtime.EvaluateParams {
|
|
return p.WithAwaitPromise(true)
|
|
}))
|
|
require.NoError(t, err, target)
|
|
require.Equal(t, "BLOCKED", result, "in-page fetch to %s must be blocked", target)
|
|
require.Less(t, time.Since(start), 8*time.Second, "%s should be refused quickly, not time out", target)
|
|
}
|
|
|
|
navCtx, navCancel := context.WithTimeout(taskCtx, 20*time.Second)
|
|
defer navCancel()
|
|
// The proxy answers 403, which Chrome renders as an ordinary page, so
|
|
// accept either a navigation error or the proxy's refusal text.
|
|
var body string
|
|
err = chromedp.Run(navCtx, chromedp.Navigate("http://127.0.0.1.nip.io:"+port), chromedp.Text("body", &body, chromedp.ByQuery))
|
|
if err == nil {
|
|
require.Contains(t, body, "blocked by egress policy", "direct navigation to a private-resolving name must be refused")
|
|
}
|
|
|
|
require.EqualValues(t, 0, hits.Load(), "local server must never be reached")
|
|
}
|