Repository navigation
Expand file tree
/
Copy pathplugin_lan_test.go
More file actions
235 lines (225 loc) · 7.18 KB
/
Copy pathplugin_lan_test.go
File metadata and controls
235 lines (225 loc) · 7.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package plugin
import (
"context"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"fmt"
"io"
"math/big"
"net"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"slices"
"strings"
"sync"
"testing"
"time"
)
// lanAddr is a private IPv4 address of this computer: one on the user's
// own network, as a Remote magpie or a NAS is.
func lanAddr(t *testing.T) net.IP {
t.Helper()
addrs, _ := net.InterfaceAddrs()
for _, a := range addrs {
if n, ok := a.(*net.IPNet); ok && n.IP.To4() != nil && n.IP.IsPrivate() {
return n.IP
}
}
t.Skip("no private IPv4 address on this computer")
return nil
}
// askedProxy is an HTTP proxy that keeps the target of every request it
// is asked: an absolute-form one (http://) is answered "proxy <target>",
// a CONNECT (https://, HTTP/2) is turned away with 502.
func askedProxy(t *testing.T) (string, func() []string) {
t.Helper()
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { ln.Close() })
var mu sync.Mutex
var asked []string
go func() {
for {
c, err := ln.Accept()
if err != nil {
return
}
go func(c net.Conn) {
defer c.Close()
var head []byte
b := make([]byte, 1)
for !strings.HasSuffix(string(head), "\r\n\r\n") {
if _, err := c.Read(b); err != nil {
return
}
head = append(head, b[0])
}
f := strings.Fields(string(head))
if len(f) < 2 {
return
}
mu.Lock()
asked = append(asked, f[1])
mu.Unlock()
if f[0] == "CONNECT" {
fmt.Fprint(c, "HTTP/1.1 502 Bad Gateway\r\nContent-Length: 0\r\n\r\n")
return
}
body := "proxy " + f[1]
fmt.Fprintf(c, "HTTP/1.1 200 OK\r\nContent-Length: %d\r\nConnection: close\r\n\r\n%s", len(body), body)
}(c)
}
}()
return "http://" + ln.Addr().String(), func() []string {
mu.Lock()
defer mu.Unlock()
return slices.Clone(asked)
}
}
// certFor is a certificate for ip, and the PEM of it a client trusts.
func certFor(t *testing.T, ip net.IP) (tls.Certificate, []byte) {
t.Helper()
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
t.Fatal(err)
}
tmpl := &x509.Certificate{
SerialNumber: big.NewInt(1),
Subject: pkix.Name{CommonName: ip.String()},
IPAddresses: []net.IP{ip},
NotBefore: time.Now().Add(-time.Hour),
NotAfter: time.Now().Add(time.Hour),
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
BasicConstraintsValid: true,
IsCA: true,
}
der, err := x509.CreateCertificate(rand.Reader, tmpl, tmpl, &key.PublicKey, key)
if err != nil {
t.Fatal(err)
}
return tls.Certificate{Certificate: [][]byte{der}, PrivateKey: key}, pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: der})
}
// #1572: a plugin's requests to a host on the user's own network — a
// Remote magpie, an LLM relay on the NAS — went through the proxy magpie
// gave the host, which can't reach it (502 Bad Gateway), while magpie's
// own requests leave such a host direct (netproxy.onLAN, #982). The
// plugin's fetches, http:// (sent to the proxy in absolute form) and
// https:// (a CONNECT), and its HTTP/2 sessions now go direct too; a
// vendor's still go through the proxy.
func TestPluginLeavesTheUserNetworkDirect(t *testing.T) {
ip := lanAddr(t)
sandbox(t)
for _, k := range []string{"NO_PROXY", "no_proxy", "ALL_PROXY", "all_proxy", "https_proxy", "http_proxy", "FAKE_H2"} {
t.Setenv(k, "")
}
proxy, asked := askedProxy(t)
t.Setenv("HTTPS_PROXY", proxy)
t.Setenv("HTTP_PROXY", proxy)
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "lan %s %s", r.Proto, r.URL.Path)
})
listen := func() net.Listener {
ln, err := net.Listen("tcp", net.JoinHostPort(ip.String(), "0"))
if err != nil {
t.Skip("can't listen on", ip, err)
}
return ln
}
plain := httptest.NewUnstartedServer(handler)
plain.Listener.Close()
plain.Listener = listen()
plain.Start()
defer plain.Close()
cert, caPEM := certFor(t, ip)
secure := httptest.NewUnstartedServer(handler)
secure.Listener.Close()
secure.Listener = listen()
secure.EnableHTTP2 = true
secure.TLS = &tls.Config{Certificates: []tls.Certificate{cert}}
secure.StartTLS()
defer secure.Close()
// a name the computer resolves to itself: what decides is what it
// resolves to, as for a NAS's name on the router's DNS
local := httptest.NewServer(handler)
defer local.Close()
ca := filepath.Join(t.TempDir(), "lan-ca.pem")
if err := os.WriteFile(ca, caPEM, 0o644); err != nil {
t.Fatal(err)
}
t.Setenv("NODE_EXTRA_CA_CERTS", ca)
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
defer cancel()
abs, _ := filepath.Abs("testdata/fake/index.js")
if _, err := Add(ctx, abs); err != nil {
t.Fatal(err)
}
if _, err := Providers(ctx); err != nil {
t.Fatal(err)
}
if _, err := APIKey(ctx, "fakeco", 0, nil, "k1", NewAccount); err != nil {
t.Fatal(err)
}
fetch := func(url string) (int, string) {
fctx, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel()
res, err := Fetch(fctx, FetchRequest{Provider: "fakeco", Model: "fake-1", NPM: "@ai-sdk/openai-compatible",
URL: url, Method: "POST", Body: []byte(`{}`)})
if err != nil {
return 0, err.Error()
}
defer res.Body.Close()
b, _ := io.ReadAll(res.Body)
return res.StatusCode, string(b)
}
proxied := func(host string) bool {
for _, a := range asked() {
if strings.Contains(a, host) {
return true
}
}
return false
}
_, port, _ := net.SplitHostPort(local.Listener.Addr().String())
for _, url := range []string{
plain.URL + "/v1/chat/completions",
secure.URL + "/v1/chat/completions",
"http://localhost.:" + port + "/v1/chat/completions",
} {
code, b := fetch(url)
if code != 200 || b != "lan HTTP/1.1 /v1/chat/completions" {
t.Errorf("%s: %d %q, want it reached directly (the proxy was asked for %v)", url, code, b, asked())
}
}
if proxied(ip.String()) || proxied("localhost.") {
t.Fatalf("the proxy was asked for a host on the user's own network: %v", asked())
}
// a vendor still goes through it: an address of the internet in
// absolute form, and a name that resolves nowhere here as a CONNECT
if code, b := fetch("http://203.0.113.5:9/v1/chat/completions"); code != 200 || b != "proxy http://203.0.113.5:9/v1/chat/completions" {
t.Errorf("a public address: %d %q, want it through the proxy", code, b)
}
fetch("https://vendor.invalid/v1/chat/completions")
if !slices.Contains(asked(), "vendor.invalid:443") {
t.Errorf("a vendor's name: the proxy was asked for %v, want vendor.invalid:443", asked())
}
// a plugin's own HTTP/2 session (Cursor's chats), as #1035 made it take
// the proxy
t.Setenv("FAKE_H2", secure.URL)
Restart()
if code, b := fetch(secure.URL + "/v1/chat/completions"); code != 200 || b != "lan HTTP/2.0 /v1/chat/completions" {
t.Errorf("an HTTP/2 session to %s: %d %q, want it reached directly (the proxy was asked for %v)", secure.URL, code, b, asked())
}
if proxied(ip.String()) {
t.Fatalf("the proxy was asked for a host on the user's own network: %v", asked())
}
}