diff --git a/docs/subsystems/gateway-routing.md b/docs/subsystems/gateway-routing.md index 4378a9653..9ced346c3 100644 --- a/docs/subsystems/gateway-routing.md +++ b/docs/subsystems/gateway-routing.md @@ -2506,7 +2506,7 @@ the row of the entry that id reaches, with its label, under the bare id. [`bare_ids_test.go`](../../internal/gateway/bare_ids_test.go), `TestBareModelIDsSetting` in `internal/gui`, and `internal/gui/tests/bare-ids.test.cjs`. -- A URL magpie downloads but didn't choose itself is downloaded from public addresses only, wherever the request came from ([`image_fetch_guard.go`](../../internal/gateway/image_fetch_guard.go)). That is an image a caller names (an `/v1/images/edits` or `/v1/images/generations` reference image, for an images API that takes files, Gemini's own API or Code Assist: `fetchPicture` in [`draw.go`](../../internal/gateway/draw.go), `publicClient`), a page `web_fetch` reads (`webFetchClient`), and an image or video a vendor answers by URL, which what the model was asked to draw or do can steer (the inline download for a `b64_json` caller in `draw.go` and `/v1/videos/{id}/content` in [`video.go`](../../internal/gateway/video.go), `vendorClient`; generate_image's own download in [`internal/imagemcp`](../../internal/imagemcp/imagemcp.go), `PublicClient`). Every address the host resolves to must be public (`publicAddr`, which is [`internal/netguard`](../../internal/netguard/netguard.go)'s `Public`, the rule a provider's icon uses too: not loopback, RFC 1918, fc00::/7, link-local with 169.254.169.254, CGNAT 100.64/10, unspecified, multicast, documentation, reserved, benchmarking, Teredo, nor an IPv4-mapped, NAT64 or 6to4 form of one; an AAAA record Loon synthesizes for an A answer in the same reply, `fd27:712::/96` with that IPv4 address inside, is left out and the A answer judged in its place, `dropLoon`), and the request goes to the address checked, so a name that resolves elsewhere the second time changes nothing; a direct connection is checked again on the address dialled (`guardDialer`). Through a proxy the proxy is sent that address, never the name (an `https://` CONNECT or SOCKS keeps the name for TLS and Host; a plain `http://` URL through an HTTP proxy is asked for by the address in Host too). A fake-IP resolver's answer is the one shape let past that (#2593; ggbdpq, with h4rk8s's and TomXPRIME's reports): Clash's TUN with fake-ip, Clash Verge, Surge's enhanced mode and Loon answer every name in 198.18.0.0/15 (`netguard.FakeIP`), so with one on every download was refused as a private address. A name (never an address literal) whose every answer is in that range is fetched: with no proxy (pure TUN) its fake address is pinned and dialled, the dial guard letting that one address in, and the TUN carries it; through a proxy on this computer (a loopback address or `localhost`, `onThisComputer`) the proxy is given the name, never the fake address, which a real proxy can't route (h4rk8s: a CONNECT by the name went through where one by its 198.18 address failed), and a plain `http://` request's Host is the checked name, never the caller's own; through a proxy elsewhere it is refused. An answer with any other address beside the fake ones (private or public) is judged as any, so a fake one beside 192.168.x is refused. The trust this asks for is the fake-IP resolver's: which host the name stands for, and so where the TUN or the proxy connects, is decided where magpie can't see, and a name that really points at the user's network is reached if that resolver's rules send it there. A provider's icon was already fetched this way (`publicIP` in [`internal/provider/icon.go`](../../internal/provider/icon.go), #252), and shares the rule. `TestFakeIPNameGoesToALocalProxyByName`, `TestFakeIPNameDialledThroughTheTUN`, `TestFakeIPRefusesTheRest` in [`fakeip_test.go`](../../internal/gateway/fakeip_test.go). Each redirect, up to 5, is checked the same, and a caller's image must sniff as an image. A request from this computer gets nothing more: a reverse proxy or relay in front of magpie on the same host makes every request it carries arrive from loopback, so where a request came from is never a reason to reach a private address. Two things are: settings.json's `fetchPrivate` (`settings.FetchPrivate`, none by default, no Settings page, this computer's own and never brought by a sync or restore), a list of names, `name:port`s, IPs and CIDR ranges the user lets magpie download from (`allowFor`; a settings.json that can't be read lets nothing extra in), and for a vendor's URL the `host:port` of that provider's own endpoints, which the user named themselves (a provider on the LAN that serves its images from itself). A vendor's image that can't be downloaded is answered by its URL, never dropped; its video is a 502 naming why. A URL left for the vendor to fetch (a chat model's message) is the vendor's to fetch. +- A URL magpie downloads but didn't choose itself is downloaded from public addresses only, wherever the request came from ([`image_fetch_guard.go`](../../internal/gateway/image_fetch_guard.go)). That is an image a caller names (an `/v1/images/edits` or `/v1/images/generations` reference image, for an images API that takes files, Gemini's own API or Code Assist: `fetchPicture` in [`draw.go`](../../internal/gateway/draw.go), `publicClient`), a page `web_fetch` reads (`webFetchClient`), and an image or video a vendor answers by URL, which what the model was asked to draw or do can steer (the inline download for a `b64_json` caller in `draw.go` and `/v1/videos/{id}/content` in [`video.go`](../../internal/gateway/video.go), `vendorClient`; generate_image's own download in [`internal/imagemcp`](../../internal/imagemcp/imagemcp.go), `PublicClient`). Every address the host resolves to must be public (`publicAddr`, which is [`internal/netguard`](../../internal/netguard/netguard.go)'s `Public`, the rule a provider's icon uses too: not loopback, RFC 1918, fc00::/7, link-local with 169.254.169.254, CGNAT 100.64/10, unspecified, multicast, documentation, reserved, benchmarking, Teredo, nor an IPv4-mapped, NAT64 or 6to4 form of one; an AAAA record Loon synthesizes for an A answer in the same reply, `fd27:712::/96` with that IPv4 address inside, is left out and the A answer judged in its place, `dropLoon`), and the request goes to the address checked, so a name that resolves elsewhere the second time changes nothing; a direct connection is checked again on the address dialled (`guardDialer`). Through a proxy the proxy is sent that address, never the name (an `https://` CONNECT or SOCKS keeps the name for TLS and Host; a plain `http://` URL through an HTTP proxy is asked for by the address in Host too). A fake-IP resolver's answer is the one shape let past that (#2593; ggbdpq, with h4rk8s's and TomXPRIME's reports): Clash's TUN with fake-ip, Clash Verge, Surge's enhanced mode and Loon answer every name in 198.18.0.0/15 (`netguard.FakeIP`), so with one on every download was refused as a private address. A name (never an address literal) whose every answer is in that range is fetched: with no proxy (pure TUN) its fake address is pinned and dialled, the dial guard letting that one address in, and the TUN carries it; through a proxy on this computer (a loopback address or `localhost`, `onThisComputer`) the proxy is given the name, never the fake address, which a real proxy can't route (h4rk8s: a CONNECT by the name went through where one by its 198.18 address failed), and a plain `http://` request's Host is the checked name, never the caller's own; through a proxy elsewhere it is refused. The fake-IP resolver's own AAAA beside its fake A answer (an IPv6 address in fc00::/7 or 2001:2::/48 while an A answer is in 198.18.0.0/15, `netguard.FakePaired`: mihomo's `fake-ip-range6`, which Clash Verge Rev turns on by default as `fdfe:dcba:9876::1/64` or fills in as `2001:2::/64`; jiezhengj, whose every `web_fetch` was refused) is left out by `dropLoon` and never dialled, the fake A answer judged and pinned in its place. An answer with any other address beside the fake ones (private or public) is judged as any, so a fake one beside 192.168.x is refused. The trust this asks for is the fake-IP resolver's: which host the name stands for, and so where the TUN or the proxy connects, is decided where magpie can't see, and a name that really points at the user's network is reached if that resolver's rules send it there. A provider's icon was already fetched this way (`publicIP` in [`internal/provider/icon.go`](../../internal/provider/icon.go), #252), and shares the rule. `TestFakeIPNameGoesToALocalProxyByName`, `TestFakeIPNameDialledThroughTheTUN`, `TestFakeIPWithItsIPv6Answer`, `TestFakeIPRefusesTheRest` in [`fakeip_test.go`](../../internal/gateway/fakeip_test.go). Each redirect, up to 5, is checked the same, and a caller's image must sniff as an image. A request from this computer gets nothing more: a reverse proxy or relay in front of magpie on the same host makes every request it carries arrive from loopback, so where a request came from is never a reason to reach a private address. Two things are: settings.json's `fetchPrivate` (`settings.FetchPrivate`, none by default, no Settings page, this computer's own and never brought by a sync or restore), a list of names, `name:port`s, IPs and CIDR ranges the user lets magpie download from (`allowFor`; a settings.json that can't be read lets nothing extra in), and for a vendor's URL the `host:port` of that provider's own endpoints, which the user named themselves (a provider on the LAN that serves its images from itself). A vendor's image that can't be downloaded is answered by its URL, never dropped; its video is a 502 naming why. A URL left for the vendor to fetch (a chat model's message) is the vendor's to fetch. ### Instruction roles on translated Responses turns diff --git a/internal/gateway/fakeip_test.go b/internal/gateway/fakeip_test.go index b6b38e787..a63c629d2 100644 --- a/internal/gateway/fakeip_test.go +++ b/internal/gateway/fakeip_test.go @@ -203,6 +203,47 @@ func TestFakeIPNameDialledThroughTheTUN(t *testing.T) { } } +// Clash Verge Rev turns on IPv6 with mihomo's fake-ip-range6 by default +// (fdfe:dcba:9876::1/64, or 2001:2::/64 it fills in), so the resolver +// answers a fake AAAA beside the fake A, and every web_fetch was refused +// as a private address (jiezhengj on Discord, mihomo 1.19.33's TUN). The +// AAAA is never dialled: the fake A is pinned, through the TUN, and a +// proxy on this computer is given the name. +func TestFakeIPWithItsIPv6Answer(t *testing.T) { + fresh(t) + useProxy(t, "direct") + page := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "text/plain") + w.Write([]byte("through the tun " + r.Host)) + })) + t.Cleanup(page.Close) + dialled := tun(t, page) + answers(t, map[string][]string{ + "example.com": {"fdfe:dcba:9876::7", "198.18.0.7"}, + "filled.test": {"198.18.0.8", "2001:2::8"}, + }) + h := lanGuard(New().Handler()) + got, isErr := fetchText_(t, h, fetchCall("http://example.com/", "")) + if isErr || !strings.Contains(got, "through the tun example.com") { + t.Fatalf("web_fetch of a name with a fake A and AAAA, no proxy: %v %q", isErr, got) + } + if err := guardedGet("http://filled.test/"); err != nil { + t.Fatalf("2001:2::/64 beside the fake A: %v", err) + } + if d := dialled(); len(d) != 2 || d[0] != "198.18.0.7:80" || d[1] != "198.18.0.8:80" { + t.Fatalf("dialled %q, want the fake A answers only", d) + } + proxy, asked := fakeProxy(t, "
fetched by name
") + useProxy(t, proxy.URL) + got, isErr = fetchText_(t, h, fetchCall("http://example.com/", "")) + if isErr || !strings.Contains(got, "fetched by name") { + t.Fatalf("web_fetch through a local proxy: %v %q", isErr, got) + } + if a := asked(); len(a) != 1 || a[0] != "http://example.com:80/ Host=example.com:80" { + t.Fatalf("the proxy was asked %q, want the name", a) + } +} + // What stays refused, with a proxy on this computer and without: a // private answer, a fake answer beside a private (or any real) one, a // fake address written as the URL, and a fake answer through a proxy @@ -224,12 +265,18 @@ func TestFakeIPRefusesTheRest(t *testing.T) { "mixedpub.test": {"198.18.0.9", "93.184.216.34"}, "loonlan.test": {"fd27:712::a00:1", "10.0.0.1"}, "loonbare.test": {"fd27:712::c612:139"}, + "ulabare.test": {"fdfe:dcba:9876::7"}, + "ulapub.test": {"fdfe:dcba:9876::7", "93.184.216.34"}, + "fake6lan.test": {"198.18.0.9", "fdfe:dcba:9876::7", "10.0.0.1"}, + "fake6lo.test": {"198.18.0.9", "fdfe:dcba:9876::7", "::1"}, }) refused := []string{ "http://lan.test/", "http://loop.test/", "http://meta.test/latest/meta-data/", "http://cgnat.test/", "http://ten.test/", "http://mixed.test/", "http://mixed6.test/", "http://mixedpub.test/", "http://loonlan.test/", "http://loonbare.test/", "http://198.18.0.7/", "https://198.18.0.7/", "http://[::ffff:198.18.0.7]/", + "http://ulabare.test/", "http://ulapub.test/", "http://fake6lan.test/", "http://fake6lo.test/", + "http://[fdfe:dcba:9876::7]/", "http://[2001:2::7]/", } proxy, asked := fakeProxy(t, "reached") for _, p := range []string{"direct", proxy.URL, "http://localhost:" + proxy.URL[strings.LastIndex(proxy.URL, ":")+1:]} { diff --git a/internal/gateway/image_fetch_guard.go b/internal/gateway/image_fetch_guard.go index 5584f552d..8bef0a053 100644 --- a/internal/gateway/image_fetch_guard.go +++ b/internal/gateway/image_fetch_guard.go @@ -59,7 +59,9 @@ import ( // the fake address is pinned and dialled, and the TUN carries it to the // host the name stands for. Through a proxy on this computer (loopback or // localhost), the proxy is given the name, never the fake address, which -// it can't route; through a proxy elsewhere it is refused. An address +// it can't route; through a proxy elsewhere it is refused. The fake-IP +// resolver's own AAAA beside its fake A (netguard.FakePaired, mihomo's +// fake-ip-range6) is left out and never dialled. An address // literal in the range, or an answer with any other address beside the // fake ones, is judged as ever. What the name stands for is then the // fake-IP resolver's to decide, where magpie can't see: a name that @@ -313,12 +315,14 @@ func fakeAnswer(addrs []netip.Addr) bool { } // dropLoon leaves out the AAAA records Loon synthesizes for an A answer -// in the same reply (netguard.LoonPaired): the A answer is judged, and -// pinned, in their place. +// in the same reply (netguard.LoonPaired), and the fake AAAA a fake-IP +// resolver answers beside its fake A (netguard.FakePaired: mihomo's +// fake-ip-range6, on by default in Clash Verge Rev): the A answer is +// judged, and pinned, in their place. func dropLoon(addrs []netip.Addr) []netip.Addr { var out []netip.Addr for _, a := range addrs { - if !netguard.LoonPaired(a, addrs) { + if !netguard.LoonPaired(a, addrs) && !netguard.FakePaired(a, addrs) { out = append(out, a) } } diff --git a/internal/netguard/netguard.go b/internal/netguard/netguard.go index 3f75d538d..15580d079 100644 --- a/internal/netguard/netguard.go +++ b/internal/netguard/netguard.go @@ -23,6 +23,7 @@ var specialNets = func() []netip.Prefix { "::/96", // IPv4-compatible (deprecated) "100::/64", // discard "2001::/32", // Teredo + "2001:2::/48", // benchmarking (RFC 5180); a fake-IP resolver's AAAA (FakePaired) "2001:db8::/32", // documentation "fec0::/10", // site-local (deprecated) "64:ff9b:1::/48", // local-use NAT64 @@ -36,6 +37,8 @@ var ( nat64 = netip.MustParsePrefix("64:ff9b::/96") fakeNet = netip.MustParsePrefix("198.18.0.0/15") loonNet = netip.MustParsePrefix("fd27:712::/96") + ulaNet = netip.MustParsePrefix("fc00::/7") + bench6 = netip.MustParsePrefix("2001:2::/48") ) // Public says a is on the open internet: not loopback, RFC 1918, fc00::/7, @@ -95,3 +98,27 @@ func LoonPaired(a netip.Addr, answers []netip.Addr) bool { } return false } + +// FakePaired says a is the AAAA answer a fake-IP resolver gives beside +// its fake A answer: an IPv6 address in fc00::/7 or 2001:2::/48 (RFC +// 5180's benchmarking range) while answers hold an IPv4 one in +// 198.18.0.0/15 (FakeIP). mihomo answers AAAA from its fake-ip-range6 +// when IPv6 is on, and Clash Verge Rev turns it on by default with +// fdfe:dcba:9876::1/64, or fills in 2001:2::/64; sing-box's inet6_range +// is a ULA as a rule (jiezhengj on Discord, 11-08: every web_fetch +// refused under Clash Verge Rev's TUN). Such an a is not dialled: the +// fake A answer is judged, and pinned, in its place, so it reaches +// nothing a fake A answer alone wouldn't. Without a fake A answer +// beside it, a ULA is private as ever. +func FakePaired(a netip.Addr, answers []netip.Addr) bool { + a = a.Unmap() + if !a.Is6() || !ulaNet.Contains(a) && !bench6.Contains(a) { + return false + } + for _, o := range answers { + if o = o.Unmap(); o.Is4() && FakeIP(o) { + return true + } + } + return false +} diff --git a/internal/netguard/netguard_test.go b/internal/netguard/netguard_test.go index f5e03900a..b60d86b7e 100644 --- a/internal/netguard/netguard_test.go +++ b/internal/netguard/netguard_test.go @@ -61,3 +61,36 @@ func TestLoonPaired(t *testing.T) { } } } + +func TestFakePaired(t *testing.T) { + addrs := func(ss ...string) []netip.Addr { + var out []netip.Addr + for _, s := range ss { + out = append(out, netip.MustParseAddr(s)) + } + return out + } + for _, c := range []struct { + name string + a string + answers []netip.Addr + want bool + }{ + {"Clash Verge Rev's default range6", "fdfe:dcba:9876::7", addrs("198.18.0.7", "fdfe:dcba:9876::7"), true}, + {"its filled-in 2001:2::/64", "2001:2::7", addrs("2001:2::7", "198.18.0.7"), true}, + {"a sing-box fc00::/18", "fc00::7", addrs("198.19.0.7"), true}, + {"the A IPv4-mapped", "fdfe:dcba:9876::7", addrs("::ffff:198.18.0.7"), true}, + {"beside a real A", "fdfe:dcba:9876::7", addrs("93.184.216.34"), false}, + {"beside a private A", "fdfe:dcba:9876::7", addrs("10.0.0.1"), false}, + {"alone", "fdfe:dcba:9876::7", addrs("fdfe:dcba:9876::7"), false}, + {"loopback", "::1", addrs("198.18.0.7"), false}, + {"link-local", "fe80::1", addrs("198.18.0.7"), false}, + {"outside 2001:2::/48", "2001:3::7", addrs("198.18.0.7"), false}, + {"an IPv4 address", "10.0.0.1", addrs("198.18.0.7"), false}, + {"the fake A itself", "198.18.0.7", addrs("198.18.0.7"), false}, + } { + if got := FakePaired(netip.MustParseAddr(c.a), c.answers); got != c.want { + t.Errorf("%s: %v, want %v", c.name, got, c.want) + } + } +} diff --git a/internal/provider/icon.go b/internal/provider/icon.go index dfc2d94d1..3e2076c51 100644 --- a/internal/provider/icon.go +++ b/internal/provider/icon.go @@ -138,8 +138,9 @@ func publicHost(ctx context.Context, host string) error { } // iconIPs is ips without the AAAA records Loon synthesizes for an A -// answer beside them (netguard.LoonPaired; that A answer is judged in -// their place), or an error when any other is not public. +// answer beside them (netguard.LoonPaired), nor a fake-IP resolver's +// fake AAAA beside its fake A (netguard.FakePaired); that A answer is +// judged in their place. It is an error when any other is not public. func iconIPs(host string, ips []net.IPAddr) ([]net.IPAddr, error) { all := make([]netip.Addr, len(ips)) for i, ip := range ips { @@ -151,7 +152,7 @@ func iconIPs(host string, ips []net.IPAddr) ([]net.IPAddr, error) { allowed = append(allowed, ip) continue } - if netguard.LoonPaired(all[i].Unmap(), all) { + if netguard.LoonPaired(all[i].Unmap(), all) || netguard.FakePaired(all[i], all) { continue } return nil, errorf("the icon host %s resolves to %s, which is not public", host, ip.IP) diff --git a/internal/provider/icon_test.go b/internal/provider/icon_test.go index 262056bd8..bba133dd6 100644 --- a/internal/provider/icon_test.go +++ b/internal/provider/icon_test.go @@ -147,6 +147,13 @@ func TestIconIPsLoon(t *testing.T) { {"loopback pair", []string{"fd27:712::7f00:1", "127.0.0.1"}, nil}, {"other ula", []string{"fd27:712:1::c600:1061", "198.0.16.97"}, nil}, {"mixed private", []string{"fd27:712::c600:1061", "198.0.16.97", "192.168.1.1"}, nil}, + // mihomo's fake-ip-range6 beside its fake A: Clash Verge Rev's + // default (jiezhengj); the fake A is dialled in its place + {"clash verge fake aaaa", []string{"fdfe:dcba:9876::5", "198.18.0.5"}, []string{"198.18.0.5"}}, + {"clash verge filled-in range6", []string{"198.18.0.5", "2001:2::5"}, []string{"198.18.0.5"}}, + {"fake aaaa, no fake a", []string{"fdfe:dcba:9876::5", "8.8.8.8"}, nil}, + {"fake aaaa alone", []string{"fdfe:dcba:9876::5"}, nil}, + {"fake pair beside private", []string{"fdfe:dcba:9876::5", "198.18.0.5", "10.0.0.1"}, nil}, } { t.Run(tt.name, func(t *testing.T) { var ips []net.IPAddr