aboutsummaryrefslogtreecommitdiff
path: root/src/lib/Utility/pushEndpoint.test.ts
blob: 5a2e90c824f09877952eecb40cfd4ea8f55ee88d (plain) (blame)
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
import { describe, expect, it } from "vitest";
import { isAllowedPushEndpoint } from "./pushEndpoint";

describe("isAllowedPushEndpoint", () => {
	// Behaviour gate: real subscriptions minted by the browser must keep working.
	it("allows genuine vendor push endpoints", () => {
		expect(
			isAllowedPushEndpoint("https://fcm.googleapis.com/fcm/send/abc123"),
		).toBe(true);
		expect(
			isAllowedPushEndpoint(
				"https://updates.push.services.mozilla.com/wpush/v2/abc",
			),
		).toBe(true);
		expect(isAllowedPushEndpoint("https://web.push.apple.com/QABC/def")).toBe(
			true,
		);
		expect(
			isAllowedPushEndpoint("https://db5p.notify.windows.com/w/?token=abc"),
		).toBe(true);
	});

	// The fix: arbitrary / internal / non-https endpoints must not be reachable.
	it("blocks SSRF and non-vendor endpoints", () => {
		expect(isAllowedPushEndpoint("http://fcm.googleapis.com/fcm/send/x")).toBe(
			false,
		); // not https
		expect(isAllowedPushEndpoint("https://evil.example.com/collect")).toBe(
			false,
		);
		expect(
			isAllowedPushEndpoint("http://169.254.169.254/latest/meta-data"),
		).toBe(false); // cloud metadata
		expect(isAllowedPushEndpoint("http://localhost:8080/internal")).toBe(false);
		expect(isAllowedPushEndpoint("https://127.0.0.1/internal")).toBe(false);
		expect(isAllowedPushEndpoint("http://10.0.0.5/admin")).toBe(false);
	});

	it("rejects look-alike hostnames", () => {
		expect(isAllowedPushEndpoint("https://fcm.googleapis.com.evil.com/x")).toBe(
			false,
		);
		expect(isAllowedPushEndpoint("https://notfcm.googleapis.com/x")).toBe(
			false,
		);
		expect(isAllowedPushEndpoint("not a url")).toBe(false);
		expect(isAllowedPushEndpoint("")).toBe(false);
	});
});