-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapi-helpers.test.ts
More file actions
127 lines (106 loc) · 4.32 KB
/
api-helpers.test.ts
File metadata and controls
127 lines (106 loc) · 4.32 KB
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
// src/lib/api-helpers.test.ts
import { describe, expect, it } from "vitest"
import { validateHttpUrl } from "./api-helpers"
describe("validateHttpUrl", () => {
it("accepts a public HTTPS URL", () => {
expect(validateHttpUrl("https://aither.cc/api/user")).toBeNull()
})
it("rejects loopback hostnames", async () => {
const response = validateHttpUrl("http://localhost:8080")
expect(response?.status).toBe(400)
await expect(response?.json()).resolves.toEqual({
error: "baseUrl must not target localhost or a private network address",
})
})
it("rejects loopback hostnames with trailing dots", async () => {
const response = validateHttpUrl("http://localhost.:8080")
expect(response?.status).toBe(400)
await expect(response?.json()).resolves.toEqual({
error: "baseUrl must not target localhost or a private network address",
})
})
it("rejects loopback IPv4 addresses", async () => {
const response = validateHttpUrl("http://127.0.0.1:8080")
expect(response?.status).toBe(400)
await expect(response?.json()).resolves.toEqual({
error: "baseUrl must not target localhost or a private network address",
})
})
it("rejects RFC1918 private IPv4 ranges", async () => {
const response = validateHttpUrl("http://192.168.1.25")
expect(response?.status).toBe(400)
await expect(response?.json()).resolves.toEqual({
error: "baseUrl must not target localhost or a private network address",
})
})
it("rejects link-local and metadata IPv4 ranges", async () => {
const response = validateHttpUrl("http://169.254.169.254/latest/meta-data")
expect(response?.status).toBe(400)
await expect(response?.json()).resolves.toEqual({
error: "baseUrl must not target localhost or a private network address",
})
})
it("rejects loopback IPv6 addresses", async () => {
const response = validateHttpUrl("http://[::1]/admin")
expect(response?.status).toBe(400)
await expect(response?.json()).resolves.toEqual({
error: "baseUrl must not target localhost or a private network address",
})
})
it("rejects IPv4-mapped IPv6 loopback addresses", async () => {
const response = validateHttpUrl("http://[::ffff:127.0.0.1]/admin")
expect(response?.status).toBe(400)
await expect(response?.json()).resolves.toEqual({
error: "baseUrl must not target localhost or a private network address",
})
})
it("rejects 10.x.x.x private range", async () => {
const response = validateHttpUrl("http://10.0.0.1/api")
expect(response?.status).toBe(400)
})
it("rejects 172.16.x.x private range", async () => {
const response = validateHttpUrl("http://172.16.0.1/api")
expect(response?.status).toBe(400)
})
it("rejects 0.0.0.0", async () => {
const response = validateHttpUrl("http://0.0.0.0/api")
expect(response?.status).toBe(400)
})
it("rejects IPv6 ULA (fc00::)", async () => {
const response = validateHttpUrl("http://[fc00::1]/api")
expect(response?.status).toBe(400)
})
it("rejects IPv6 link-local (fe80::)", async () => {
const response = validateHttpUrl("http://[fe80::1]/api")
expect(response?.status).toBe(400)
})
it("rejects .local mDNS domains", async () => {
const response = validateHttpUrl("http://tracker.local/api")
expect(response?.status).toBe(400)
})
it("rejects ip6-localhost", async () => {
const response = validateHttpUrl("http://ip6-localhost/api")
expect(response?.status).toBe(400)
})
it("rejects IPv4-mapped IPv6 hex notation", async () => {
const response = validateHttpUrl("http://[::ffff:7f00:1]/api")
expect(response?.status).toBe(400)
})
it("rejects shorthand IPv4 loopback (127.1)", async () => {
const response = validateHttpUrl("http://127.1/api")
expect(response?.status).toBe(400)
})
it("rejects shorthand IPv4 loopback (127.0.1)", async () => {
const response = validateHttpUrl("http://127.0.1/api")
expect(response?.status).toBe(400)
})
it("rejects single-octet loopback (2130706433)", async () => {
// 2130706433 = 0x7F000001 = 127.0.0.1
const response = validateHttpUrl("http://2130706433/api")
expect(response?.status).toBe(400)
})
it("rejects shorthand private range (10.1)", async () => {
const response = validateHttpUrl("http://10.1/api")
expect(response?.status).toBe(400)
})
})