๐Ÿ“ฆ cloudflare / vinext

๐Ÿ“„ dev-origin-check.test.ts ยท 190 lines
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
190import { describe, it, expect } from "vitest";
import {
  isAllowedDevOrigin,
  isCrossSiteNoCorsRequest,
  validateDevRequest,
  generateDevOriginCheckCode,
} from "../packages/vinext/src/server/dev-origin-check.js";

describe("dev origin check", () => {
  // โ”€โ”€ isAllowedDevOrigin โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

  describe("isAllowedDevOrigin", () => {
    it("allows requests with no Origin header", () => {
      expect(isAllowedDevOrigin(null, "localhost:5173")).toBe(true);
      expect(isAllowedDevOrigin(undefined, "localhost:5173")).toBe(true);
    });

    it("allows requests with Origin 'null' (sandboxed iframe, privacy)", () => {
      expect(isAllowedDevOrigin("null", "localhost:5173")).toBe(true);
    });

    it("allows localhost origins (any port)", () => {
      expect(isAllowedDevOrigin("http://localhost:5173", "localhost:5173")).toBe(true);
      expect(isAllowedDevOrigin("http://localhost:3000", "localhost:5173")).toBe(true);
      expect(isAllowedDevOrigin("http://localhost", "localhost:5173")).toBe(true);
      expect(isAllowedDevOrigin("https://localhost:8443", "localhost:5173")).toBe(true);
    });

    it("allows 127.0.0.1 origins", () => {
      expect(isAllowedDevOrigin("http://127.0.0.1:5173", "localhost:5173")).toBe(true);
      expect(isAllowedDevOrigin("http://127.0.0.1:3000", "localhost:5173")).toBe(true);
      expect(isAllowedDevOrigin("http://127.0.0.1", "localhost:5173")).toBe(true);
    });

    it("allows [::1] (IPv6 loopback) origins", () => {
      expect(isAllowedDevOrigin("http://[::1]:5173", "localhost:5173")).toBe(true);
      expect(isAllowedDevOrigin("http://[::1]", "localhost:5173")).toBe(true);
    });

    it("allows subdomains of localhost", () => {
      expect(isAllowedDevOrigin("http://storybook.localhost:6006", "localhost:5173")).toBe(true);
      expect(isAllowedDevOrigin("http://foo.localhost", "localhost:5173")).toBe(true);
      expect(isAllowedDevOrigin("http://a.b.localhost:3000", "localhost:5173")).toBe(true);
    });

    it("blocks external origins", () => {
      expect(isAllowedDevOrigin("https://evil.com", "localhost:5173")).toBe(false);
      expect(isAllowedDevOrigin("http://external.io:5173", "localhost:5173")).toBe(false);
      expect(isAllowedDevOrigin("https://google.com", "localhost:5173")).toBe(false);
    });

    it("blocks origins that look like localhost but aren't", () => {
      // "notlocalhost" should not match
      expect(isAllowedDevOrigin("http://notlocalhost:5173", "localhost:5173")).toBe(false);
      // "localhost.evil.com" should not match
      expect(isAllowedDevOrigin("http://localhost.evil.com", "localhost:5173")).toBe(false);
    });

    it("blocks malformed origin headers", () => {
      expect(isAllowedDevOrigin("not-a-url", "localhost:5173")).toBe(false);
      expect(isAllowedDevOrigin("javascript:alert(1)", "localhost:5173")).toBe(false);
    });

    it("allows same-origin by matching Host header", () => {
      // Origin hostname matches Host hostname
      expect(isAllowedDevOrigin("http://myapp.local:5173", "myapp.local:5173")).toBe(true);
      expect(isAllowedDevOrigin("http://192.168.1.5:3000", "192.168.1.5:3000")).toBe(true);
    });

    it("same-origin check ignores port differences", () => {
      // Hostname matches even though port differs
      expect(isAllowedDevOrigin("http://myapp.local:3000", "myapp.local:5173")).toBe(true);
    });

    it("same-origin check handles comma-separated Host header", () => {
      expect(isAllowedDevOrigin("http://myapp.local:3000", "myapp.local:5173, proxy:8080")).toBe(true);
    });

    it("allows origins in the allowedDevOrigins list", () => {
      const allowed = ["custom-origin.com", "*.my-domain.com"];
      expect(isAllowedDevOrigin("http://custom-origin.com:3000", "localhost:5173", allowed)).toBe(true);
      expect(isAllowedDevOrigin("http://sub.my-domain.com", "localhost:5173", allowed)).toBe(true);
      expect(isAllowedDevOrigin("http://my-domain.com", "localhost:5173", allowed)).toBe(true);
      expect(isAllowedDevOrigin("http://a.b.my-domain.com", "localhost:5173", allowed)).toBe(true);
    });

    it("rejects origins not in the allowedDevOrigins list", () => {
      const allowed = ["custom-origin.com"];
      expect(isAllowedDevOrigin("https://other.com", "localhost:5173", allowed)).toBe(false);
    });

    it("handles case-insensitive hostnames", () => {
      expect(isAllowedDevOrigin("http://LOCALHOST:5173", "localhost:5173")).toBe(true);
      expect(isAllowedDevOrigin("http://LocalHost:5173", "localhost:5173")).toBe(true);
    });
  });

  // โ”€โ”€ isCrossSiteNoCorsRequest โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

  describe("isCrossSiteNoCorsRequest", () => {
    it("detects cross-site no-cors requests", () => {
      expect(isCrossSiteNoCorsRequest("cross-site", "no-cors")).toBe(true);
    });

    it("allows same-origin requests", () => {
      expect(isCrossSiteNoCorsRequest("same-origin", "cors")).toBe(false);
      expect(isCrossSiteNoCorsRequest("same-origin", "no-cors")).toBe(false);
    });

    it("allows same-site requests", () => {
      expect(isCrossSiteNoCorsRequest("same-site", "no-cors")).toBe(false);
    });

    it("allows cross-site CORS requests (fetch with mode:cors)", () => {
      expect(isCrossSiteNoCorsRequest("cross-site", "cors")).toBe(false);
    });

    it("handles missing headers", () => {
      expect(isCrossSiteNoCorsRequest(null, null)).toBe(false);
      expect(isCrossSiteNoCorsRequest(undefined, undefined)).toBe(false);
      expect(isCrossSiteNoCorsRequest("cross-site", null)).toBe(false);
    });
  });

  // โ”€โ”€ validateDevRequest โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

  describe("validateDevRequest", () => {
    it("allows requests with no Origin and no Sec-Fetch headers", () => {
      expect(validateDevRequest({ host: "localhost:5173" })).toBeNull();
    });

    it("allows localhost origin requests", () => {
      expect(validateDevRequest({
        origin: "http://localhost:5173",
        host: "localhost:5173",
      })).toBeNull();
    });

    it("blocks cross-site no-cors requests (script tag exfiltration)", () => {
      const result = validateDevRequest({
        origin: "https://evil.com",
        host: "localhost:5173",
        "sec-fetch-site": "cross-site",
        "sec-fetch-mode": "no-cors",
      });
      expect(result).not.toBeNull();
      expect(result).toContain("cross-site no-cors");
    });

    it("blocks cross-origin fetch requests", () => {
      const result = validateDevRequest({
        origin: "https://evil.com",
        host: "localhost:5173",
      });
      expect(result).not.toBeNull();
      expect(result).toContain("evil.com");
    });

    it("passes allowedDevOrigins to origin check", () => {
      expect(validateDevRequest(
        { origin: "http://custom.com", host: "localhost:5173" },
        ["custom.com"],
      )).toBeNull();
    });
  });

  // โ”€โ”€ generateDevOriginCheckCode โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

  describe("generateDevOriginCheckCode", () => {
    it("generates code with the validation function", () => {
      const code = generateDevOriginCheckCode();
      expect(code).toContain("__validateDevRequestOrigin");
      expect(code).toContain("__safeDevHosts");
      expect(code).toContain("sec-fetch-mode");
      expect(code).toContain("sec-fetch-site");
    });

    it("embeds allowed dev origins when provided", () => {
      const code = generateDevOriginCheckCode(["my-proxy.com", "*.staging.com"]);
      expect(code).toContain("my-proxy.com");
      expect(code).toContain("*.staging.com");
    });

    it("embeds empty array when no origins provided", () => {
      const code = generateDevOriginCheckCode();
      expect(code).toContain("__allowedDevOrigins = []");
    });
  });
});