|
1 | 1 | import { afterEach, beforeEach, describe, expect, test, vi } from "vitest"; |
2 | | -import { createClient } from "../../src/index.ts"; |
| 2 | +import { createClient, createClientFromRequest } from "../../src/index.ts"; |
3 | 3 |
|
4 | 4 | const appId = "test-app-id"; |
5 | 5 | const origin = "https://my-app.base44.app"; |
@@ -191,3 +191,147 @@ describe("fetchWithAuth", () => { |
191 | 191 | expect(fetchMock).not.toHaveBeenCalled(); |
192 | 192 | }); |
193 | 193 | }); |
| 194 | + |
| 195 | +const apiUrl = "https://base44.app"; |
| 196 | + |
| 197 | +/** The header set the platform puts on a fullstack worker request. */ |
| 198 | +function inboundRequest( |
| 199 | + overrides: Record<string, string | undefined> = {} |
| 200 | +): Request { |
| 201 | + const headers: Record<string, string> = { |
| 202 | + Authorization: "Bearer caller-user-token", |
| 203 | + "Base44-Service-Authorization": "Bearer service-credential", |
| 204 | + "Base44-App-Id": appId, |
| 205 | + "Base44-Api-Url": apiUrl, |
| 206 | + "Base44-Functions-Version": "draft", |
| 207 | + "Base44-State": "signed-state-jwt", |
| 208 | + "X-Data-Env": "dev", |
| 209 | + host: "my-app.base44.app", |
| 210 | + cookie: "session=irrelevant", |
| 211 | + }; |
| 212 | + for (const [name, value] of Object.entries(overrides)) { |
| 213 | + if (value === undefined) delete headers[name]; |
| 214 | + else headers[name] = value; |
| 215 | + } |
| 216 | + return new Request(`${origin}/page`, { headers }); |
| 217 | +} |
| 218 | + |
| 219 | +describe("fetchWithAuth from a server route", () => { |
| 220 | + test("sends every header createClientFromRequest reads, so the callee rebuilds the same client", async () => { |
| 221 | + const base44 = createClientFromRequest(inboundRequest()); |
| 222 | + |
| 223 | + await base44.fetchWithAuth("/api/items", { fetch: fetchMock }); |
| 224 | + |
| 225 | + const { url, headers } = lastCall(); |
| 226 | + expect(url).toBe("/api/items"); |
| 227 | + expect(headers.get("Authorization")).toBe("Bearer caller-user-token"); |
| 228 | + expect(headers.get("Base44-App-Id")).toBe(appId); |
| 229 | + expect(headers.get("Base44-Api-Url")).toBe(apiUrl); |
| 230 | + expect(headers.get("Base44-Functions-Version")).toBe("draft"); |
| 231 | + expect(headers.get("Base44-State")).toBe("signed-state-jwt"); |
| 232 | + expect(headers.get("X-Data-Env")).toBe("dev"); |
| 233 | + }); |
| 234 | + |
| 235 | + test("carries the service credential, so asServiceRole works in the callee", async () => { |
| 236 | + const base44 = createClientFromRequest(inboundRequest()); |
| 237 | + |
| 238 | + await base44.fetchWithAuth("/api/items", { fetch: fetchMock }); |
| 239 | + |
| 240 | + expect(lastCall().headers.get("Base44-Service-Authorization")).toBe( |
| 241 | + "Bearer service-credential" |
| 242 | + ); |
| 243 | + }); |
| 244 | + |
| 245 | + test("does not forward host, which would repoint the sub-request's origin", async () => { |
| 246 | + const base44 = createClientFromRequest(inboundRequest()); |
| 247 | + |
| 248 | + await base44.fetchWithAuth("/api/items", { fetch: fetchMock }); |
| 249 | + |
| 250 | + expect(lastCall().headers.has("host")).toBe(false); |
| 251 | + }); |
| 252 | + |
| 253 | + test("forwards nothing from the inbound request beyond that set", async () => { |
| 254 | + const base44 = createClientFromRequest(inboundRequest()); |
| 255 | + |
| 256 | + await base44.fetchWithAuth("/api/items", { fetch: fetchMock }); |
| 257 | + |
| 258 | + expect(lastCall().headers.has("cookie")).toBe(false); |
| 259 | + }); |
| 260 | + |
| 261 | + test("stays anonymous when the caller is", async () => { |
| 262 | + const base44 = createClientFromRequest( |
| 263 | + inboundRequest({ Authorization: undefined }) |
| 264 | + ); |
| 265 | + |
| 266 | + await base44.fetchWithAuth("/api/items", { fetch: fetchMock }); |
| 267 | + |
| 268 | + const { headers } = lastCall(); |
| 269 | + expect(headers.has("Authorization")).toBe(false); |
| 270 | + expect(headers.get("Base44-Service-Authorization")).toBe( |
| 271 | + "Bearer service-credential" |
| 272 | + ); |
| 273 | + }); |
| 274 | + |
| 275 | + test("omits headers the inbound request did not carry", async () => { |
| 276 | + const base44 = createClientFromRequest( |
| 277 | + inboundRequest({ |
| 278 | + "Base44-State": undefined, |
| 279 | + "X-Data-Env": undefined, |
| 280 | + "Base44-Functions-Version": undefined, |
| 281 | + }) |
| 282 | + ); |
| 283 | + |
| 284 | + await base44.fetchWithAuth("/api/items", { fetch: fetchMock }); |
| 285 | + |
| 286 | + const { headers } = lastCall(); |
| 287 | + expect(headers.has("Base44-State")).toBe(false); |
| 288 | + expect(headers.has("X-Data-Env")).toBe(false); |
| 289 | + expect(headers.has("Base44-Functions-Version")).toBe(false); |
| 290 | + }); |
| 291 | + |
| 292 | + test("renders as anonymous when the caller drops Authorization on purpose", async () => { |
| 293 | + const base44 = createClientFromRequest(inboundRequest()); |
| 294 | + |
| 295 | + await base44.fetchWithAuth("/api/items", { |
| 296 | + fetch: fetchMock, |
| 297 | + headers: { Authorization: "" }, |
| 298 | + }); |
| 299 | + |
| 300 | + expect(lastCall().headers.get("Authorization")).toBe(""); |
| 301 | + }); |
| 302 | + |
| 303 | + test("uses the given transport and does not pass it on as request init", async () => { |
| 304 | + vi.stubGlobal("fetch", vi.fn()); |
| 305 | + const base44 = createClientFromRequest(inboundRequest()); |
| 306 | + |
| 307 | + await base44.fetchWithAuth("/api/items", { fetch: fetchMock }); |
| 308 | + |
| 309 | + expect(fetchMock).toHaveBeenCalledOnce(); |
| 310 | + expect(lastCall().init).not.toHaveProperty("fetch"); |
| 311 | + }); |
| 312 | + |
| 313 | + test("refuses to send the app's credentials to another origin", async () => { |
| 314 | + const base44 = createClientFromRequest(inboundRequest()); |
| 315 | + |
| 316 | + await expect( |
| 317 | + base44.fetchWithAuth("https://evil.example/steal", { fetch: fetchMock }) |
| 318 | + ).rejects.toThrow(/only sends requests to your app's own origin/); |
| 319 | + expect(fetchMock).not.toHaveBeenCalled(); |
| 320 | + }); |
| 321 | +}); |
| 322 | + |
| 323 | +describe("fetchWithAuth in a browser", () => { |
| 324 | + // The reason one method can serve both: a browser client is built without a |
| 325 | + // serviceToken, so there is no service credential for it to send. This is |
| 326 | + // what makes the wider header set safe to apply everywhere. |
| 327 | + test("sends no service credential, having none", async () => { |
| 328 | + stubBrowser(); |
| 329 | + const base44 = createTestClient("user-token"); |
| 330 | + |
| 331 | + await base44.fetchWithAuth("/api/orders"); |
| 332 | + |
| 333 | + const { headers } = lastCall(); |
| 334 | + expect(headers.get("Authorization")).toBe("Bearer user-token"); |
| 335 | + expect(headers.has("Base44-Service-Authorization")).toBe(false); |
| 336 | + }); |
| 337 | +}); |
0 commit comments