| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "net/http" |
| 6 | "net/http/httptest" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | func TestTailnetAuthorizationURL(t *testing.T) { |
| 12 | for _, u := range []string{"https://login.tailscale.com/a/abc", "https://login.tailscale.com/c/abc"} { |
| 13 | if !validTailnetAuthURL(u) { |
| 14 | t.Fatal(u) |
| 15 | } |
| 16 | } |
| 17 | for _, u := range []string{"http://login.tailscale.com/a/abc", "https://login.tailscale.com.evil.test/a/abc", "https://evil.test/a/abc", "https://[email protected]/a/abc", "https://login.tailscale.com:444/a/abc", "javascript:alert(1)", "https://login.tailscale.com/admin"} { |
| 18 | if validTailnetAuthURL(u) { |
| 19 | t.Fatal(u) |
| 20 | } |
| 21 | } |
| 22 | } |
| 23 | func TestTailnetBoundaries(t *testing.T) { |
| 24 | s := testServer(t) |
| 25 | if w := request(s, "POST", "/api/tailscale/connect", "https://evil.test", "{}", true); w.Code != 403 { |
| 26 | t.Fatal(w.Code) |
| 27 | } |
| 28 | if w := request(s, "GET", "/api/tailscale", "", "", false); w.Code != 401 { |
| 29 | t.Fatal(w.Code) |
| 30 | } |
| 31 | if w := request(s, "GET", "/api/tailscale", "", "", true); w.Code != 409 { |
| 32 | t.Fatal(w.Code) |
| 33 | } |
| 34 | for _, path := range []string{"/api/tailscale", "/api/tailscale/login", "/api/tailscale/add-key"} { |
| 35 | if !macOSWorkbenchRoute(path) || !windowsWorkbenchRoute(path) { |
| 36 | t.Fatal(path) |
| 37 | } |
| 38 | } |
| 39 | if !validWindowsBrowserRoute("POST", "/tailnet") || validWindowsBrowserRoute("GET", "/tailnet") { |
| 40 | t.Fatal("native guest route method changed") |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | func TestTailnetGuestContract(t *testing.T) { |
| 45 | s := testServer(t) |
| 46 | s.ready = true |
| 47 | s.credentials.Token = "test-tailnet-token" |
| 48 | guest := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 49 | if r.Method != "POST" || r.URL.Path != "/tailnet" || r.Header.Get("Authorization") != "Bearer test-tailnet-token" { |
| 50 | t.Error("wrong guest route or credentials") |
| 51 | } |
| 52 | var input map[string]string |
| 53 | if json.NewDecoder(r.Body).Decode(&input) != nil { |
| 54 | t.Error("invalid input") |
| 55 | } |
| 56 | if input["action"] == "add-key" { |
| 57 | _ = json.NewEncoder(w).Encode(map[string]string{"tailnet_error": "请粘贴一条有效的 SSH 公钥"}) |
| 58 | return |
| 59 | } |
| 60 | _ = json.NewEncoder(w).Encode(map[string]any{"installed": true, "backend": "NeedsLogin", "auth_url": "https://login.tailscale.com/a/test"}) |
| 61 | })) |
| 62 | defer guest.Close() |
| 63 | s.browserControl = localBrowserClient(t, s.lifetimeContext(), guest) |
| 64 | if w := request(s, "POST", "/api/tailscale/connect", s.origin, "{}", true); w.Code != 200 || !strings.Contains(w.Body.String(), "login.tailscale.com") { |
| 65 | t.Fatalf("connect: %d %s", w.Code, w.Body.String()) |
| 66 | } |
| 67 | if w := request(s, "POST", "/api/tailscale/add-key", s.origin, `{"key":"invalid"}`, true); w.Code != 502 || !strings.Contains(w.Body.String(), "有效的 SSH 公钥") { |
| 68 | t.Fatalf("key error: %d %s", w.Code, w.Body.String()) |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | func TestTailnetServicePortContract(t *testing.T) { |
| 73 | s := testServer(t) |
| 74 | s.ready = true |
| 75 | s.credentials.Token = "test-tailnet-token" |
| 76 | guest := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 77 | var input struct { |
| 78 | Action string `json:"action"` |
| 79 | Host string `json:"host"` |
| 80 | Port int `json:"port"` |
| 81 | } |
| 82 | if err := json.NewDecoder(r.Body).Decode(&input); err != nil { |
| 83 | t.Error(err) |
| 84 | } |
| 85 | if input.Action != "open-service" || input.Host != "100.88.2.4" || input.Port != 8080 { |
| 86 | t.Errorf("wrong service input: %+v", input) |
| 87 | } |
| 88 | _ = json.NewEncoder(w).Encode(map[string]any{"installed": true, "backend": "Running", "forwards": []map[string]any{{"host": input.Host, "port": input.Port, "address": "127.0.0.1:32123"}}}) |
| 89 | })) |
| 90 | defer guest.Close() |
| 91 | s.browserControl = localBrowserClient(t, s.lifetimeContext(), guest) |
| 92 | if w := request(s, "POST", "/api/tailscale/open-service", s.origin, `{"host":"100.88.2.4","port":8080}`, true); w.Code != 200 || !strings.Contains(w.Body.String(), "127.0.0.1:32123") { |
| 93 | t.Fatalf("service: %d %s", w.Code, w.Body.String()) |
| 94 | } |
| 95 | for _, body := range []string{`{"host":"100.88.2.4","port":0}`, `{"host":"100.88.2.4","port":65536}`, `{"host":"100.88.2.4","port":"80"}`, `{"port":80}`} { |
| 96 | if w := request(s, "POST", "/api/tailscale/open-service", s.origin, body, true); w.Code != 400 { |
| 97 | t.Fatalf("bad input accepted: %d %s", w.Code, body) |
| 98 | } |
| 99 | } |
| 100 | if !macOSWorkbenchRoute("/api/tailscale/open-service") || !windowsWorkbenchRoute("/api/tailscale/open-service") { |
| 101 | t.Fatal("native hosts must expose the same service route") |
| 102 | } |
| 103 | } |