forked from nodejs/undici
-
Notifications
You must be signed in to change notification settings - Fork 2
/
integrity.js
150 lines (118 loc) · 4.03 KB
/
integrity.js
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
'use strict'
const { test } = require('tap')
const { createServer } = require('http')
const { createHash, getHashes } = require('crypto')
const { gzipSync } = require('zlib')
const { fetch, setGlobalDispatcher, Agent } = require('../..')
const { once } = require('events')
const supportedHashes = getHashes()
setGlobalDispatcher(new Agent({
keepAliveTimeout: 1,
keepAliveMaxTimeout: 1
}))
test('request with correct integrity checksum', (t) => {
const body = 'Hello world!'
const hash = createHash('sha256').update(body).digest('hex')
const server = createServer((req, res) => {
res.end(body)
})
t.teardown(server.close.bind(server))
server.listen(0, async () => {
const response = await fetch(`http://localhost:${server.address().port}`, {
integrity: `sha256-${hash}`
})
t.strictSame(body, await response.text())
t.end()
})
})
test('request with wrong integrity checksum', (t) => {
const body = 'Hello world!'
const hash = 'c0535e4be2b79ffd93291305436bf889314e4a3faec05ecffcbb7df31ad9e51b'
const server = createServer((req, res) => {
res.end(body)
})
t.teardown(server.close.bind(server))
server.listen(0, () => {
fetch(`http://localhost:${server.address().port}`, {
integrity: `sha256-${hash}`
}).then(response => {
t.pass('request did not fail')
}).catch((err) => {
t.equal(err.cause.message, 'integrity mismatch')
}).finally(() => {
t.end()
})
})
})
test('request with integrity checksum on encoded body', (t) => {
const body = 'Hello world!'
const hash = createHash('sha256').update(body).digest('hex')
const server = createServer((req, res) => {
res.setHeader('content-encoding', 'gzip')
res.end(gzipSync(body))
})
t.teardown(server.close.bind(server))
server.listen(0, async () => {
const response = await fetch(`http://localhost:${server.address().port}`, {
integrity: `sha256-${hash}`
})
t.strictSame(body, await response.text())
t.end()
})
})
test('request with a totally incorrect integrity', async (t) => {
const server = createServer((req, res) => {
res.end()
}).listen(0)
t.teardown(server.close.bind(server))
await once(server, 'listening')
await t.resolves(fetch(`http://localhost:${server.address().port}`, {
integrity: 'what-integrityisthis'
}))
})
test('request with mixed in/valid integrities', async (t) => {
const body = 'Hello world!'
const hash = createHash('sha256').update(body).digest('base64')
const server = createServer((req, res) => {
res.end(body)
}).listen(0)
t.teardown(server.close.bind(server))
await once(server, 'listening')
await t.resolves(fetch(`http://localhost:${server.address().port}`, {
integrity: `invalid-integrity sha256-${hash}`
}))
})
test('request with sha384 hash', { skip: !supportedHashes.includes('sha384') }, async (t) => {
const body = 'Hello world!'
const hash = createHash('sha384').update(body).digest('base64')
const server = createServer((req, res) => {
res.end(body)
}).listen(0)
t.teardown(server.close.bind(server))
await once(server, 'listening')
// request should succeed
await t.resolves(fetch(`http://localhost:${server.address().port}`, {
integrity: `sha384-${hash}`
}))
// request should fail
await t.rejects(fetch(`http://localhost:${server.address().port}`, {
integrity: 'sha384-ypeBEsobvcr6wjGzmiPcTaeG7/gUfE5yuYB3ha/uSLs='
}))
})
test('request with sha512 hash', { skip: !supportedHashes.includes('sha512') }, async (t) => {
const body = 'Hello world!'
const hash = createHash('sha512').update(body).digest('base64')
const server = createServer((req, res) => {
res.end(body)
}).listen(0)
t.teardown(server.close.bind(server))
await once(server, 'listening')
// request should succeed
await t.resolves(fetch(`http://localhost:${server.address().port}`, {
integrity: `sha512-${hash}`
}))
// request should fail
await t.rejects(fetch(`http://localhost:${server.address().port}`, {
integrity: 'sha512-ypeBEsobvcr6wjGzmiPcTaeG7/gUfE5yuYB3ha/uSLs='
}))
})