@contember/engine-common 2.2.0-alpha.17 → 2.2.0-alpha.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/development/index.cjs +7 -0
- package/dist/development/index.cjs.map +1 -1
- package/dist/development/index.js +8 -1
- package/dist/development/index.js.map +1 -1
- package/dist/development/src/egress/EgressGuard.cjs +170 -0
- package/dist/development/src/egress/EgressGuard.cjs.map +1 -0
- package/dist/development/src/egress/EgressGuard.js +170 -0
- package/dist/development/src/egress/EgressGuard.js.map +1 -0
- package/dist/development/src/egress/EgressPolicy.cjs +110 -0
- package/dist/development/src/egress/EgressPolicy.cjs.map +1 -0
- package/dist/development/src/egress/EgressPolicy.js +110 -0
- package/dist/development/src/egress/EgressPolicy.js.map +1 -0
- package/dist/development/src/egress/createEgressGuard.cjs +7 -0
- package/dist/development/src/egress/createEgressGuard.cjs.map +1 -0
- package/dist/development/src/egress/createEgressGuard.js +7 -0
- package/dist/development/src/egress/createEgressGuard.js.map +1 -0
- package/dist/production/index.cjs +7 -0
- package/dist/production/index.cjs.map +1 -1
- package/dist/production/index.js +8 -1
- package/dist/production/index.js.map +1 -1
- package/dist/production/src/egress/EgressGuard.cjs +170 -0
- package/dist/production/src/egress/EgressGuard.cjs.map +1 -0
- package/dist/production/src/egress/EgressGuard.js +170 -0
- package/dist/production/src/egress/EgressGuard.js.map +1 -0
- package/dist/production/src/egress/EgressPolicy.cjs +110 -0
- package/dist/production/src/egress/EgressPolicy.cjs.map +1 -0
- package/dist/production/src/egress/EgressPolicy.js +110 -0
- package/dist/production/src/egress/EgressPolicy.js.map +1 -0
- package/dist/production/src/egress/createEgressGuard.cjs +7 -0
- package/dist/production/src/egress/createEgressGuard.cjs.map +1 -0
- package/dist/production/src/egress/createEgressGuard.js +7 -0
- package/dist/production/src/egress/createEgressGuard.js.map +1 -0
- package/dist/types/egress/EgressGuard.d.ts +37 -0
- package/dist/types/egress/EgressGuard.d.ts.map +1 -0
- package/dist/types/egress/EgressPolicy.d.ts +25 -0
- package/dist/types/egress/EgressPolicy.d.ts.map +1 -0
- package/dist/types/egress/createEgressGuard.d.ts +9 -0
- package/dist/types/egress/createEgressGuard.d.ts.map +1 -0
- package/dist/types/egress/index.d.ts +4 -0
- package/dist/types/egress/index.d.ts.map +1 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +2 -2
- package/src/egress/EgressGuard.ts +197 -0
- package/src/egress/EgressPolicy.ts +129 -0
- package/src/egress/createEgressGuard.ts +14 -0
- package/src/egress/index.ts +3 -0
- package/src/index.ts +1 -0
- package/tests/cases/egress/egressGuard.test.ts +147 -0
- package/tests/cases/egress/egressPolicy.test.ts +78 -0
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { BlockList, isIP, isIPv4 } from 'node:net'
|
|
2
|
+
|
|
3
|
+
// IPv4 special-purpose ranges (IANA): unspecified, private, CGNAT, loopback, link-local, protocol assignments,
|
|
4
|
+
// documentation, 6to4 relay, benchmarking, multicast, reserved and broadcast.
|
|
5
|
+
const SPECIAL_PURPOSE_IPV4: readonly [string, number][] = [
|
|
6
|
+
['0.0.0.0', 8],
|
|
7
|
+
['10.0.0.0', 8],
|
|
8
|
+
['100.64.0.0', 10],
|
|
9
|
+
['127.0.0.0', 8],
|
|
10
|
+
['169.254.0.0', 16],
|
|
11
|
+
['172.16.0.0', 12],
|
|
12
|
+
['192.0.0.0', 24],
|
|
13
|
+
['192.0.2.0', 24],
|
|
14
|
+
['192.88.99.0', 24],
|
|
15
|
+
['192.168.0.0', 16],
|
|
16
|
+
['198.18.0.0', 15],
|
|
17
|
+
['198.51.100.0', 24],
|
|
18
|
+
['203.0.113.0', 24],
|
|
19
|
+
['224.0.0.0', 4],
|
|
20
|
+
['240.0.0.0', 4],
|
|
21
|
+
]
|
|
22
|
+
|
|
23
|
+
// Only global unicast (2000::/3) is reachable; within it, protocol assignments (incl. Teredo), documentation and
|
|
24
|
+
// 6to4 (which embeds an arbitrary IPv4 address) are refused too. Everything outside 2000::/3 — unspecified, loopback,
|
|
25
|
+
// IPv4-mapped, NAT64, unique-local, link-local, multicast — never passes.
|
|
26
|
+
const GLOBAL_UNICAST_IPV6: readonly [string, number] = ['2000::', 3]
|
|
27
|
+
const SPECIAL_PURPOSE_IPV6: readonly [string, number][] = [
|
|
28
|
+
['2001::', 23],
|
|
29
|
+
['2001:db8::', 32],
|
|
30
|
+
['2002::', 16],
|
|
31
|
+
['3fff::', 20],
|
|
32
|
+
]
|
|
33
|
+
|
|
34
|
+
const normalizeHost = (host: string): string => host.toLowerCase().replace(/^\[(.*)]$/, '$1').replace(/\.$/, '')
|
|
35
|
+
|
|
36
|
+
const ipFamily = (address: string): 'ipv4' | 'ipv6' | null => {
|
|
37
|
+
const version = isIP(address)
|
|
38
|
+
return version === 4 ? 'ipv4' : version === 6 ? 'ipv6' : null
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export class EgressError extends Error {}
|
|
42
|
+
|
|
43
|
+
export interface EgressPolicyConfig {
|
|
44
|
+
/** Allows `http://` URLs; otherwise only `https://` is fetched. */
|
|
45
|
+
readonly allowHttp: boolean
|
|
46
|
+
/** Host names, addresses and CIDR ranges exempt from the non-public address check. */
|
|
47
|
+
readonly allowlist: readonly string[]
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Which URLs the engine may fetch on behalf of a tenant: `https://` only (unless `allowHttp`), never a special-purpose
|
|
52
|
+
* address unless allow-listed. Checked per connection (on the resolved address), so DNS rebinding cannot bypass it.
|
|
53
|
+
*/
|
|
54
|
+
export class EgressPolicy {
|
|
55
|
+
private readonly specialPurpose = new BlockList()
|
|
56
|
+
private readonly globalUnicastIpv6 = new BlockList()
|
|
57
|
+
private readonly allowedNetworks = new BlockList()
|
|
58
|
+
private readonly allowedHosts: ReadonlySet<string>
|
|
59
|
+
private readonly allowHttp: boolean
|
|
60
|
+
|
|
61
|
+
constructor(config: EgressPolicyConfig) {
|
|
62
|
+
for (const [network, prefix] of SPECIAL_PURPOSE_IPV4) {
|
|
63
|
+
this.specialPurpose.addSubnet(network, prefix, 'ipv4')
|
|
64
|
+
}
|
|
65
|
+
for (const [network, prefix] of SPECIAL_PURPOSE_IPV6) {
|
|
66
|
+
this.specialPurpose.addSubnet(network, prefix, 'ipv6')
|
|
67
|
+
}
|
|
68
|
+
this.globalUnicastIpv6.addSubnet(GLOBAL_UNICAST_IPV6[0], GLOBAL_UNICAST_IPV6[1], 'ipv6')
|
|
69
|
+
|
|
70
|
+
const allowedHosts = new Set<string>()
|
|
71
|
+
for (const entry of config.allowlist) {
|
|
72
|
+
const [network, prefixText, ...rest] = entry.trim().split('/')
|
|
73
|
+
const family = ipFamily(network)
|
|
74
|
+
if (family === null) {
|
|
75
|
+
if (prefixText !== undefined || network === '') {
|
|
76
|
+
throw new EgressError(`Invalid egress allowlist entry ${entry}`)
|
|
77
|
+
}
|
|
78
|
+
allowedHosts.add(normalizeHost(network))
|
|
79
|
+
continue
|
|
80
|
+
}
|
|
81
|
+
const maxPrefix = family === 'ipv4' ? 32 : 128
|
|
82
|
+
if (rest.length > 0 || (prefixText !== undefined && !/^\d{1,3}$/.test(prefixText))) {
|
|
83
|
+
throw new EgressError(`Invalid egress allowlist entry ${entry}`)
|
|
84
|
+
}
|
|
85
|
+
const prefix = prefixText === undefined ? maxPrefix : Number(prefixText)
|
|
86
|
+
if (prefix > maxPrefix) {
|
|
87
|
+
throw new EgressError(`Invalid egress allowlist entry ${entry}`)
|
|
88
|
+
}
|
|
89
|
+
this.allowedNetworks.addSubnet(network, prefix, family)
|
|
90
|
+
}
|
|
91
|
+
this.allowedHosts = allowedHosts
|
|
92
|
+
this.allowHttp = config.allowHttp
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** Throws unless the URL may be fetched; an IP-literal host is checked here because no lookup will run for it. */
|
|
96
|
+
public checkUrl(url: URL): void {
|
|
97
|
+
const host = normalizeHost(url.hostname)
|
|
98
|
+
if (url.username !== '' || url.password !== '') {
|
|
99
|
+
throw new EgressError('Outgoing request URLs must not carry credentials')
|
|
100
|
+
}
|
|
101
|
+
if (url.protocol !== 'https:' && !(url.protocol === 'http:' && this.allowHttp)) {
|
|
102
|
+
throw new EgressError(`Only https:// URLs are allowed, got ${url.protocol}//${host}`)
|
|
103
|
+
}
|
|
104
|
+
if (ipFamily(host) !== null) {
|
|
105
|
+
this.checkAddress(host, host)
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** Throws unless a connection to `address`, resolved from `host`, is allowed. */
|
|
110
|
+
public checkAddress(host: string, address: string): void {
|
|
111
|
+
const normalizedHost = normalizeHost(host)
|
|
112
|
+
if (this.allowedHosts.has(normalizedHost)) {
|
|
113
|
+
return
|
|
114
|
+
}
|
|
115
|
+
const family = ipFamily(address)
|
|
116
|
+
if (family === null) {
|
|
117
|
+
throw new EgressError(`Invalid address ${address} for ${normalizedHost}`)
|
|
118
|
+
}
|
|
119
|
+
if (this.allowedNetworks.check(address, family)) {
|
|
120
|
+
return
|
|
121
|
+
}
|
|
122
|
+
const isPublic = isIPv4(address)
|
|
123
|
+
? !this.specialPurpose.check(address, 'ipv4')
|
|
124
|
+
: this.globalUnicastIpv6.check(address, 'ipv6') && !this.specialPurpose.check(address, 'ipv6')
|
|
125
|
+
if (!isPublic) {
|
|
126
|
+
throw new EgressError(`${normalizedHost} resolves to the non-public address ${address}`)
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { EgressGuard } from './EgressGuard.js'
|
|
2
|
+
import { EgressPolicy } from './EgressPolicy.js'
|
|
3
|
+
|
|
4
|
+
export interface EgressConfig {
|
|
5
|
+
/** Opt-in: unset, outgoing requests to tenant-chosen URLs are not restricted. */
|
|
6
|
+
readonly guard?: boolean
|
|
7
|
+
readonly allowHttp?: boolean
|
|
8
|
+
readonly allowlist?: readonly string[]
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export const createEgressGuard = (config: EgressConfig | undefined): EgressGuard | null =>
|
|
12
|
+
config?.guard === true
|
|
13
|
+
? new EgressGuard(new EgressPolicy({ allowHttp: config.allowHttp ?? false, allowlist: config.allowlist ?? [] }))
|
|
14
|
+
: null
|
package/src/index.ts
CHANGED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { afterAll, beforeAll, describe, expect, test } from 'bun:test'
|
|
2
|
+
import http from 'node:http'
|
|
3
|
+
import { EgressGuard, EgressPolicy } from '../../../src/index.js'
|
|
4
|
+
|
|
5
|
+
type RecordedRequest = { method: string; url: string; headers: http.IncomingHttpHeaders; body: string }
|
|
6
|
+
|
|
7
|
+
const startServer = async (handler: (req: http.IncomingMessage, res: http.ServerResponse) => void) => {
|
|
8
|
+
const requests: RecordedRequest[] = []
|
|
9
|
+
const server = http.createServer((req, res) => {
|
|
10
|
+
let body = ''
|
|
11
|
+
req.on('data', chunk => (body += chunk))
|
|
12
|
+
req.on('end', () => {
|
|
13
|
+
requests.push({ method: req.method ?? '', url: req.url ?? '', headers: req.headers, body })
|
|
14
|
+
handler(req, res)
|
|
15
|
+
})
|
|
16
|
+
})
|
|
17
|
+
await new Promise<void>(resolve => server.listen(0, '127.0.0.1', resolve))
|
|
18
|
+
const address = server.address()
|
|
19
|
+
if (address === null || typeof address === 'string') {
|
|
20
|
+
throw new Error('expected a TCP address')
|
|
21
|
+
}
|
|
22
|
+
return { server, port: address.port, requests }
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const guardWith = (allowlist: string[]) => new EgressGuard(new EgressPolicy({ allowHttp: true, allowlist }))
|
|
26
|
+
|
|
27
|
+
describe('EgressGuard.fetch', () => {
|
|
28
|
+
let target: Awaited<ReturnType<typeof startServer>>
|
|
29
|
+
let redirector: Awaited<ReturnType<typeof startServer>>
|
|
30
|
+
|
|
31
|
+
beforeAll(async () => {
|
|
32
|
+
target = await startServer((req, res) => {
|
|
33
|
+
res.writeHead(201, 'Created', { 'content-type': 'application/json', 'x-test': 'yes' })
|
|
34
|
+
res.end(JSON.stringify({ ok: true }))
|
|
35
|
+
})
|
|
36
|
+
redirector = await startServer((req, res) => {
|
|
37
|
+
const status = Number(new URL(req.url ?? '/', 'http://x').searchParams.get('status') ?? 307)
|
|
38
|
+
res.writeHead(status, { location: `http://127.0.0.1:${target.port}/landed` })
|
|
39
|
+
res.end()
|
|
40
|
+
})
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
afterAll(() => {
|
|
44
|
+
target.server.close()
|
|
45
|
+
redirector.server.close()
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
test('a host name that resolves to a non-public address is refused before connecting', async () => {
|
|
49
|
+
const before = target.requests.length
|
|
50
|
+
const guard = guardWith([])
|
|
51
|
+
await expect(guard.fetch(`http://localhost:${target.port}/hook`, { method: 'POST', body: '{}' })).rejects.toThrow('non-public address')
|
|
52
|
+
expect(target.requests.length).toBe(before)
|
|
53
|
+
guard.dispose()
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
test('http is refused unless allowed', async () => {
|
|
57
|
+
const guard = new EgressGuard(new EgressPolicy({ allowHttp: false, allowlist: ['localhost'] }))
|
|
58
|
+
await expect(guard.fetch(`http://localhost:${target.port}/hook`, {})).rejects.toThrow('Only https:// URLs are allowed')
|
|
59
|
+
guard.dispose()
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
test('an allow-listed local host is reached and its response returned', async () => {
|
|
63
|
+
const guard = guardWith(['localhost'])
|
|
64
|
+
const response = await guard.fetch(`http://localhost:${target.port}/hook`, {
|
|
65
|
+
method: 'POST',
|
|
66
|
+
headers: { 'content-type': 'application/json', 'x-token': 'secret' },
|
|
67
|
+
body: '{"a":1}',
|
|
68
|
+
})
|
|
69
|
+
expect(response.status).toBe(201)
|
|
70
|
+
expect(response.statusText).toBe('Created')
|
|
71
|
+
expect(response.headers.get('x-test')).toBe('yes')
|
|
72
|
+
expect(await response.json()).toEqual({ ok: true })
|
|
73
|
+
const received = target.requests.at(-1)
|
|
74
|
+
expect(received?.method).toBe('POST')
|
|
75
|
+
expect(received?.body).toBe('{"a":1}')
|
|
76
|
+
expect(received?.headers['x-token']).toBe('secret')
|
|
77
|
+
guard.dispose()
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
test('a redirect to a non-public address is refused', async () => {
|
|
81
|
+
const before = target.requests.length
|
|
82
|
+
const guard = guardWith(['localhost'])
|
|
83
|
+
await expect(guard.fetch(`http://localhost:${redirector.port}/`, { method: 'POST', body: '{}' })).rejects.toThrow('non-public address')
|
|
84
|
+
expect(target.requests.length).toBe(before)
|
|
85
|
+
guard.dispose()
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
test('an allowed redirect keeps a 307 POST and drops credentials across origins', async () => {
|
|
89
|
+
const guard = guardWith(['localhost', '127.0.0.1'])
|
|
90
|
+
const response = await guard.fetch(`http://localhost:${redirector.port}/?status=307`, {
|
|
91
|
+
method: 'POST',
|
|
92
|
+
headers: { 'authorization': 'Bearer secret', 'x-other': 'kept' },
|
|
93
|
+
body: '{"a":1}',
|
|
94
|
+
})
|
|
95
|
+
expect(response.status).toBe(201)
|
|
96
|
+
const received = target.requests.at(-1)
|
|
97
|
+
expect(received?.url).toBe('/landed')
|
|
98
|
+
expect(received?.method).toBe('POST')
|
|
99
|
+
expect(received?.body).toBe('{"a":1}')
|
|
100
|
+
expect(received?.headers.authorization).toBeUndefined()
|
|
101
|
+
expect(received?.headers['x-other']).toBe('kept')
|
|
102
|
+
guard.dispose()
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
test('a 302 after a POST continues as a GET without a body', async () => {
|
|
106
|
+
const guard = guardWith(['localhost', '127.0.0.1'])
|
|
107
|
+
await guard.fetch(`http://localhost:${redirector.port}/?status=302`, {
|
|
108
|
+
method: 'POST',
|
|
109
|
+
headers: { 'content-type': 'application/json' },
|
|
110
|
+
body: '{}',
|
|
111
|
+
})
|
|
112
|
+
const received = target.requests.at(-1)
|
|
113
|
+
expect(received?.method).toBe('GET')
|
|
114
|
+
expect(received?.body).toBe('')
|
|
115
|
+
expect(received?.headers['content-type']).toBeUndefined()
|
|
116
|
+
guard.dispose()
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
test('a manual redirect is returned as is', async () => {
|
|
120
|
+
const guard = guardWith(['localhost'])
|
|
121
|
+
const response = await guard.fetch(`http://localhost:${redirector.port}/`, { redirect: 'manual' })
|
|
122
|
+
expect(response.status).toBe(307)
|
|
123
|
+
guard.dispose()
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
test('an aborted request rejects with the abort reason', async () => {
|
|
127
|
+
const hanging = await startServer(() => {})
|
|
128
|
+
const guard = guardWith(['localhost'])
|
|
129
|
+
const controller = new AbortController()
|
|
130
|
+
const pending = guard.fetch(`http://localhost:${hanging.port}/`, { signal: controller.signal })
|
|
131
|
+
setTimeout(() => controller.abort(new Error('timed out')), 50)
|
|
132
|
+
await expect(pending).rejects.toThrow('timed out')
|
|
133
|
+
guard.dispose()
|
|
134
|
+
hanging.server.closeAllConnections()
|
|
135
|
+
hanging.server.close()
|
|
136
|
+
})
|
|
137
|
+
})
|
|
138
|
+
|
|
139
|
+
describe('EgressGuard.httpOptions', () => {
|
|
140
|
+
test('refuses a disallowed URL before a client issues the request', () => {
|
|
141
|
+
const guard = new EgressGuard(new EgressPolicy({ allowHttp: false, allowlist: [] }))
|
|
142
|
+
expect(() => guard.httpOptions(new URL('http://example.com/'))).toThrow('Only https:// URLs are allowed')
|
|
143
|
+
expect(() => guard.httpOptions(new URL('https://169.254.169.254/'))).toThrow('non-public address')
|
|
144
|
+
expect(guard.httpOptions(new URL('https://example.com/')).lookup).toBeFunction()
|
|
145
|
+
guard.dispose()
|
|
146
|
+
})
|
|
147
|
+
})
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { describe, expect, test } from 'bun:test'
|
|
2
|
+
import { createEgressGuard, EgressError, EgressPolicy } from '../../../src/index.js'
|
|
3
|
+
|
|
4
|
+
const strict = new EgressPolicy({ allowHttp: false, allowlist: [] })
|
|
5
|
+
|
|
6
|
+
describe('EgressPolicy addresses', () => {
|
|
7
|
+
test.each([
|
|
8
|
+
'0.0.0.0',
|
|
9
|
+
'10.1.2.3',
|
|
10
|
+
'100.64.0.1',
|
|
11
|
+
'127.0.0.1',
|
|
12
|
+
'169.254.169.254',
|
|
13
|
+
'172.16.0.1',
|
|
14
|
+
'192.168.1.1',
|
|
15
|
+
'224.0.0.1',
|
|
16
|
+
'255.255.255.255',
|
|
17
|
+
'::',
|
|
18
|
+
'::1',
|
|
19
|
+
'::ffff:127.0.0.1',
|
|
20
|
+
'::ffff:169.254.169.254',
|
|
21
|
+
'64:ff9b::7f00:1',
|
|
22
|
+
'fc00::1',
|
|
23
|
+
'fd12:3456::1',
|
|
24
|
+
'fe80::1',
|
|
25
|
+
'ff02::1',
|
|
26
|
+
'2001:db8::1',
|
|
27
|
+
'2002:7f00:1::1',
|
|
28
|
+
])('refuses %s', address => {
|
|
29
|
+
expect(() => strict.checkAddress('example.com', address)).toThrow(EgressError)
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
test.each(['93.184.215.14', '1.1.1.1', '2606:4700:4700::1111'])('allows public %s', address => {
|
|
33
|
+
expect(() => strict.checkAddress('example.com', address)).not.toThrow()
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
test('an allow-listed network or host passes the address check', () => {
|
|
37
|
+
const policy = new EgressPolicy({ allowHttp: false, allowlist: ['10.0.0.0/8', 'internal.example', '::1'] })
|
|
38
|
+
expect(() => policy.checkAddress('hooks.example.com', '10.20.30.40')).not.toThrow()
|
|
39
|
+
expect(() => policy.checkAddress('Internal.Example.', '192.168.0.1')).not.toThrow()
|
|
40
|
+
expect(() => policy.checkAddress('hooks.example.com', '::1')).not.toThrow()
|
|
41
|
+
expect(() => policy.checkAddress('hooks.example.com', '192.168.0.1')).toThrow(EgressError)
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
test.each(['10.0.0.0/33', '10.0.0.0/8/1', 'host/8', '', '::1/x'])('rejects the invalid allowlist entry %p', entry => {
|
|
45
|
+
expect(() => new EgressPolicy({ allowHttp: false, allowlist: [entry] })).toThrow(EgressError)
|
|
46
|
+
})
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
describe('EgressPolicy URLs', () => {
|
|
50
|
+
test('only https unless http is allowed', () => {
|
|
51
|
+
expect(() => strict.checkUrl(new URL('https://example.com/hook'))).not.toThrow()
|
|
52
|
+
expect(() => strict.checkUrl(new URL('http://example.com/hook'))).toThrow('Only https:// URLs are allowed')
|
|
53
|
+
expect(() => strict.checkUrl(new URL('ftp://example.com/hook'))).toThrow(EgressError)
|
|
54
|
+
const withHttp = new EgressPolicy({ allowHttp: true, allowlist: [] })
|
|
55
|
+
expect(() => withHttp.checkUrl(new URL('http://example.com/hook'))).not.toThrow()
|
|
56
|
+
expect(() => withHttp.checkUrl(new URL('ftp://example.com/hook'))).toThrow(EgressError)
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
test('an IP-literal host is checked without a lookup', () => {
|
|
60
|
+
expect(() => strict.checkUrl(new URL('https://127.0.0.1/'))).toThrow('non-public address')
|
|
61
|
+
expect(() => strict.checkUrl(new URL('https://[::1]/'))).toThrow('non-public address')
|
|
62
|
+
expect(() => strict.checkUrl(new URL('https://[::ffff:a9fe:a9fe]/'))).toThrow('non-public address')
|
|
63
|
+
expect(() => strict.checkUrl(new URL('https://1.1.1.1/'))).not.toThrow()
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
test('URLs with credentials are refused', () => {
|
|
67
|
+
expect(() => strict.checkUrl(new URL('https://user:pass@example.com/'))).toThrow(EgressError)
|
|
68
|
+
})
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
describe('createEgressGuard', () => {
|
|
72
|
+
test('the guard is opt-in', () => {
|
|
73
|
+
expect(createEgressGuard(undefined)).toBeNull()
|
|
74
|
+
expect(createEgressGuard({})).toBeNull()
|
|
75
|
+
expect(createEgressGuard({ guard: false, allowHttp: true })).toBeNull()
|
|
76
|
+
expect(createEgressGuard({ guard: true })).not.toBeNull()
|
|
77
|
+
})
|
|
78
|
+
})
|