The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { describe, expect, it } from "vite-plus/test"; |
| 2 | |
| 3 | import { deriveAuthClientMetadata, resolveSessionCookieName } from "./utils.ts"; |
| 4 | |
| 5 | describe("deriveAuthClientMetadata", () => { |
| 6 | it("labels Electron user agents as Electron instead of Chrome", () => { |
| 7 | const metadata = deriveAuthClientMetadata({ |
| 8 | request: { |
| 9 | headers: { |
| 10 | "user-agent": |
| 11 | "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) t3code/0.0.15 Chrome/136.0.7103.93 Electron/36.3.2 Safari/537.36", |
| 12 | }, |
| 13 | source: { |
| 14 | remoteAddress: "::ffff:127.0.0.1", |
| 15 | }, |
| 16 | } as never, |
| 17 | }); |
| 18 | |
| 19 | expect(metadata).toMatchObject({ |
| 20 | browser: "Electron", |
| 21 | deviceType: "desktop", |
| 22 | ipAddress: "127.0.0.1", |
| 23 | os: "macOS", |
| 24 | }); |
| 25 | }); |
| 26 | |
| 27 | it("records the forwarded client address of a request proxied by Tailscale Serve", () => { |
| 28 | const metadata = deriveAuthClientMetadata({ |
| 29 | request: { |
| 30 | headers: { |
| 31 | "x-forwarded-for": "100.64.0.2", |
| 32 | "x-forwarded-host": "host-a.tailnet-name.ts.net", |
| 33 | "x-forwarded-proto": "https", |
| 34 | }, |
| 35 | source: { remoteAddress: "127.0.0.1" }, |
| 36 | } as never, |
| 37 | }); |
| 38 | |
| 39 | expect(metadata.ipAddress).toBe("100.64.0.2"); |
| 40 | |
| 41 | const direct = deriveAuthClientMetadata({ |
| 42 | request: { |
| 43 | headers: { "x-forwarded-for": "100.64.0.2" }, |
| 44 | source: { remoteAddress: "192.168.1.20" }, |
| 45 | } as never, |
| 46 | }); |
| 47 | expect(direct.ipAddress).toBe("192.168.1.20"); |
| 48 | }); |
| 49 | |
| 50 | it.each([ |
| 51 | [ |
| 52 | "Edge on Android", |
| 53 | "Mozilla/5.0 (Linux; Android 14; Pixel 8) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Mobile Safari/537.36 EdgA/129.0.0.0", |
| 54 | "Edge", |
| 55 | ], |
| 56 | [ |
| 57 | "Edge on iOS", |
| 58 | "Mozilla/5.0 (iPhone; CPU iPhone OS 18_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.0 EdgiOS/129.0.2792.84 Mobile/15E148 Safari/605.1.15", |
| 59 | "Edge", |
| 60 | ], |
| 61 | [ |
| 62 | "Firefox on iOS", |
| 63 | "Mozilla/5.0 (iPhone; CPU iPhone OS 18_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) FxiOS/131.0 Mobile/15E148 Safari/605.1.15", |
| 64 | "Firefox", |
| 65 | ], |
| 66 | [ |
| 67 | "Opera on iOS", |
| 68 | "Mozilla/5.0 (iPhone; CPU iPhone OS 18_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.0 OPiOS/5.0.0 Mobile/15E148 Safari/605.1.15", |
| 69 | "Opera", |
| 70 | ], |
| 71 | ])("labels %s by its own browser token", (_name, userAgent, browser) => { |
| 72 | const metadata = deriveAuthClientMetadata({ |
| 73 | request: { headers: { "user-agent": userAgent }, source: {} } as never, |
| 74 | }); |
| 75 | |
| 76 | expect(metadata.browser).toBe(browser); |
| 77 | }); |
| 78 | |
| 79 | it("applies client-presented display identity without replacing transport metadata", () => { |
| 80 | const metadata = deriveAuthClientMetadata({ |
| 81 | request: { |
| 82 | headers: { |
| 83 | "user-agent": |
| 84 | "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 Chrome/136.0.7103.93 Electron/36.3.2 Safari/537.36", |
| 85 | }, |
| 86 | source: { |
| 87 | remoteAddress: "::ffff:192.168.213.72", |
| 88 | }, |
| 89 | } as never, |
| 90 | presented: { |
| 91 | label: "T3 Code Mobile", |
| 92 | deviceType: "mobile", |
| 93 | os: "iOS", |
| 94 | }, |
| 95 | }); |
| 96 | |
| 97 | expect(metadata).toMatchObject({ |
| 98 | label: "T3 Code Mobile", |
| 99 | browser: "Electron", |
| 100 | deviceType: "mobile", |
| 101 | ipAddress: "192.168.213.72", |
| 102 | os: "iOS", |
| 103 | }); |
| 104 | expect(metadata.userAgent).toContain("Electron/36.3.2"); |
| 105 | }); |
| 106 | }); |
| 107 | |
| 108 | describe("session cookie isolation", () => { |
| 109 | it("isolates loopback web servers by port and server state", () => { |
| 110 | const first = resolveSessionCookieName({ |
| 111 | mode: "web", |
| 112 | port: 5775, |
| 113 | host: "127.0.0.1", |
| 114 | instanceKey: "/tmp/t3-agent-one", |
| 115 | environmentId: "environment-one", |
| 116 | development: true, |
| 117 | }); |
| 118 | const second = resolveSessionCookieName({ |
| 119 | mode: "web", |
| 120 | port: 5775, |
| 121 | host: "127.0.0.1", |
| 122 | instanceKey: "/tmp/t3-agent-two", |
| 123 | environmentId: "environment-two", |
| 124 | development: true, |
| 125 | }); |
| 126 | |
| 127 | expect(first).toMatch(/^t3_session_5775_[a-f0-9]{12}$/); |
| 128 | expect(second).toMatch(/^t3_session_5775_[a-f0-9]{12}$/); |
| 129 | expect(first).not.toBe(second); |
| 130 | }); |
| 131 | |
| 132 | // regression: #8085 isolate remote web session cookies |
| 133 | it("isolates remote web servers by server state", () => { |
| 134 | const first = resolveSessionCookieName({ |
| 135 | mode: "web", |
| 136 | port: 3773, |
| 137 | host: "192.168.1.50", |
| 138 | instanceKey: "/srv/t3-one", |
| 139 | environmentId: "environment-one", |
| 140 | development: false, |
| 141 | }); |
| 142 | const second = resolveSessionCookieName({ |
| 143 | mode: "web", |
| 144 | port: 5775, |
| 145 | host: "192.168.1.50", |
| 146 | instanceKey: "/srv/t3-two", |
| 147 | environmentId: "environment-two", |
| 148 | development: false, |
| 149 | }); |
| 150 | |
| 151 | expect(first).toMatch(/^t3_session_[a-f0-9]{12}$/); |
| 152 | expect(second).toMatch(/^t3_session_[a-f0-9]{12}$/); |
| 153 | expect(first).not.toBe(second); |
| 154 | }); |
| 155 | |
| 156 | it("keeps a remote web server cookie stable across port changes", () => { |
| 157 | const first = resolveSessionCookieName({ |
| 158 | mode: "web", |
| 159 | port: 8080, |
| 160 | host: "0.0.0.0", |
| 161 | instanceKey: "/srv/t3", |
| 162 | environmentId: "environment-one", |
| 163 | development: false, |
| 164 | }); |
| 165 | const second = resolveSessionCookieName({ |
| 166 | mode: "web", |
| 167 | port: 9090, |
| 168 | host: "app.example.com", |
| 169 | instanceKey: "/srv/t3", |
| 170 | environmentId: "environment-one", |
| 171 | development: false, |
| 172 | }); |
| 173 | |
| 174 | expect(first).toBe(second); |
| 175 | }); |
| 176 | }); |