|
| 1 | +import { describe, expect, it } from "vitest" |
| 2 | +import { createMisina } from "../src/index.ts" |
| 3 | + |
| 4 | +describe("allowedProtocols — URL scheme allowlist", () => { |
| 5 | + it("default: http + https allowed", async () => { |
| 6 | + const driver = { |
| 7 | + name: "p", |
| 8 | + request: async () => new Response("{}", { headers: { "content-type": "application/json" } }), |
| 9 | + } |
| 10 | + const m = createMisina({ driver, retry: 0 }) |
| 11 | + |
| 12 | + await m.get("https://api.test/") |
| 13 | + await m.get("http://api.test/") |
| 14 | + // both succeed |
| 15 | + }) |
| 16 | + |
| 17 | + it("default: rejects ftp:// with a clear error", async () => { |
| 18 | + const driver = { |
| 19 | + name: "p", |
| 20 | + request: async () => new Response("{}", { headers: { "content-type": "application/json" } }), |
| 21 | + } |
| 22 | + const m = createMisina({ driver, retry: 0 }) |
| 23 | + |
| 24 | + await expect(m.get("ftp://files.test/x")).rejects.toThrow( |
| 25 | + /protocol "ftp:" not in allowedProtocols/, |
| 26 | + ) |
| 27 | + }) |
| 28 | + |
| 29 | + it("opt-in: capacitor:// permitted when listed", async () => { |
| 30 | + let captured: string | undefined |
| 31 | + const driver = { |
| 32 | + name: "p", |
| 33 | + request: async (req: Request) => { |
| 34 | + captured = req.url |
| 35 | + return new Response("{}", { headers: { "content-type": "application/json" } }) |
| 36 | + }, |
| 37 | + } |
| 38 | + const m = createMisina({ |
| 39 | + driver, |
| 40 | + retry: 0, |
| 41 | + allowedProtocols: ["http", "https", "capacitor"], |
| 42 | + }) |
| 43 | + |
| 44 | + await m.get("capacitor://localhost/api/users") |
| 45 | + expect(captured).toBe("capacitor://localhost/api/users") |
| 46 | + }) |
| 47 | + |
| 48 | + it("per-request override: tauri:// allowed for one call only", async () => { |
| 49 | + let lastUrl: string | undefined |
| 50 | + const driver = { |
| 51 | + name: "p", |
| 52 | + request: async (req: Request) => { |
| 53 | + lastUrl = req.url |
| 54 | + return new Response("{}", { headers: { "content-type": "application/json" } }) |
| 55 | + }, |
| 56 | + } |
| 57 | + const m = createMisina({ driver, retry: 0 }) |
| 58 | + |
| 59 | + await m.get("tauri://localhost/x", { allowedProtocols: ["tauri"] }) |
| 60 | + expect(lastUrl).toBe("tauri://localhost/x") |
| 61 | + |
| 62 | + // The next call without the override falls back to defaults → rejected. |
| 63 | + await expect(m.get("tauri://localhost/y")).rejects.toThrow(/protocol "tauri:"/) |
| 64 | + }) |
| 65 | + |
| 66 | + it("relative paths are not validated (driver supplies origin)", async () => { |
| 67 | + let captured: string | undefined |
| 68 | + const driver = { |
| 69 | + name: "p", |
| 70 | + request: async (req: Request) => { |
| 71 | + captured = req.url |
| 72 | + return new Response("{}", { headers: { "content-type": "application/json" } }) |
| 73 | + }, |
| 74 | + } |
| 75 | + // No baseURL — input is taken as-is. fetch will resolve it against origin. |
| 76 | + const m = createMisina({ |
| 77 | + driver, |
| 78 | + retry: 0, |
| 79 | + // NB: relative paths only work when the driver/runtime accepts them. |
| 80 | + // The protocol check is silent for unparseable inputs. |
| 81 | + }) |
| 82 | + |
| 83 | + // Passing a parseable URL just to confirm we don't crash on the check. |
| 84 | + await m.get("https://api.test/v1") |
| 85 | + expect(captured).toBe("https://api.test/v1") |
| 86 | + }) |
| 87 | + |
| 88 | + it("allowAbsoluteUrls: false STILL applies under custom protocols", async () => { |
| 89 | + const driver = { |
| 90 | + name: "p", |
| 91 | + request: async () => new Response("{}", { headers: { "content-type": "application/json" } }), |
| 92 | + } |
| 93 | + const m = createMisina({ |
| 94 | + driver, |
| 95 | + retry: 0, |
| 96 | + baseURL: "capacitor://localhost", |
| 97 | + allowAbsoluteUrls: false, |
| 98 | + allowedProtocols: ["capacitor"], |
| 99 | + }) |
| 100 | + |
| 101 | + // Relative path resolved against baseURL — fine. |
| 102 | + await expect(m.get("capacitor://other.host/x")).rejects.toThrow(/allowAbsoluteUrls is false/) |
| 103 | + }) |
| 104 | +}) |
0 commit comments