]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/activitypub/security.ts
5e29b744ff28efd7f80f3e63b3a4f4a55c58394f
[github/Chocobozzz/PeerTube.git] / server / tests / api / activitypub / security.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import * as chai from 'chai'
4 import { buildDigest } from '@server/helpers/peertube-crypto'
5 import { HTTP_SIGNATURE } from '@server/initializers/constants'
6 import { activityPubContextify } from '@server/lib/activitypub/context'
7 import { buildGlobalHeaders, signAndContextify } from '@server/lib/activitypub/send'
8 import { makeFollowRequest, makePOSTAPRequest } from '@server/tests/shared'
9 import { buildAbsoluteFixturePath, wait } from '@shared/core-utils'
10 import { HttpStatusCode } from '@shared/models'
11 import { cleanupTests, createMultipleServers, killallServers, PeerTubeServer } from '@shared/server-commands'
12
13 const expect = chai.expect
14
15 function setKeysOfServer (onServer: PeerTubeServer, ofServer: PeerTubeServer, publicKey: string, privateKey: string) {
16 const url = 'http://localhost:' + ofServer.port + '/accounts/peertube'
17
18 return Promise.all([
19 onServer.sql.setActorField(url, 'publicKey', publicKey),
20 onServer.sql.setActorField(url, 'privateKey', privateKey)
21 ])
22 }
23
24 function setUpdatedAtOfServer (onServer: PeerTubeServer, ofServer: PeerTubeServer, updatedAt: string) {
25 const url = 'http://localhost:' + ofServer.port + '/accounts/peertube'
26
27 return Promise.all([
28 onServer.sql.setActorField(url, 'createdAt', updatedAt),
29 onServer.sql.setActorField(url, 'updatedAt', updatedAt)
30 ])
31 }
32
33 function getAnnounceWithoutContext (server: PeerTubeServer) {
34 const json = require(buildAbsoluteFixturePath('./ap-json/peertube/announce-without-context.json'))
35 const result: typeof json = {}
36
37 for (const key of Object.keys(json)) {
38 if (Array.isArray(json[key])) {
39 result[key] = json[key].map(v => v.replace(':9002', `:${server.port}`))
40 } else {
41 result[key] = json[key].replace(':9002', `:${server.port}`)
42 }
43 }
44
45 return result
46 }
47
48 describe('Test ActivityPub security', function () {
49 let servers: PeerTubeServer[]
50 let url: string
51
52 const keys = require(buildAbsoluteFixturePath('./ap-json/peertube/keys.json'))
53 const invalidKeys = require(buildAbsoluteFixturePath('./ap-json/peertube/invalid-keys.json'))
54 const baseHttpSignature = () => ({
55 algorithm: HTTP_SIGNATURE.ALGORITHM,
56 authorizationHeaderName: HTTP_SIGNATURE.HEADER_NAME,
57 keyId: 'acct:peertube@localhost:' + servers[1].port,
58 key: keys.privateKey,
59 headers: HTTP_SIGNATURE.HEADERS_TO_SIGN
60 })
61
62 // ---------------------------------------------------------------
63
64 before(async function () {
65 this.timeout(60000)
66
67 servers = await createMultipleServers(3)
68
69 url = servers[0].url + '/inbox'
70
71 await setKeysOfServer(servers[0], servers[1], keys.publicKey, null)
72 await setKeysOfServer(servers[1], servers[1], keys.publicKey, keys.privateKey)
73
74 const to = { url: 'http://localhost:' + servers[0].port + '/accounts/peertube' }
75 const by = { url: 'http://localhost:' + servers[1].port + '/accounts/peertube', privateKey: keys.privateKey }
76 await makeFollowRequest(to, by)
77 })
78
79 describe('When checking HTTP signature', function () {
80
81 it('Should fail with an invalid digest', async function () {
82 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]), 'Announce')
83 const headers = {
84 Digest: buildDigest({ hello: 'coucou' })
85 }
86
87 try {
88 await makePOSTAPRequest(url, body, baseHttpSignature(), headers)
89 expect(true, 'Did not throw').to.be.false
90 } catch (err) {
91 expect(err.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
92 }
93 })
94
95 it('Should fail with an invalid date', async function () {
96 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]), 'Announce')
97 const headers = buildGlobalHeaders(body)
98 headers['date'] = 'Wed, 21 Oct 2015 07:28:00 GMT'
99
100 try {
101 await makePOSTAPRequest(url, body, baseHttpSignature(), headers)
102 expect(true, 'Did not throw').to.be.false
103 } catch (err) {
104 expect(err.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
105 }
106 })
107
108 it('Should fail with bad keys', async function () {
109 await setKeysOfServer(servers[0], servers[1], invalidKeys.publicKey, invalidKeys.privateKey)
110 await setKeysOfServer(servers[1], servers[1], invalidKeys.publicKey, invalidKeys.privateKey)
111
112 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]), 'Announce')
113 const headers = buildGlobalHeaders(body)
114
115 try {
116 await makePOSTAPRequest(url, body, baseHttpSignature(), headers)
117 expect(true, 'Did not throw').to.be.false
118 } catch (err) {
119 expect(err.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
120 }
121 })
122
123 it('Should reject requests without appropriate signed headers', async function () {
124 await setKeysOfServer(servers[0], servers[1], keys.publicKey, keys.privateKey)
125 await setKeysOfServer(servers[1], servers[1], keys.publicKey, keys.privateKey)
126
127 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]), 'Announce')
128 const headers = buildGlobalHeaders(body)
129
130 const signatureOptions = baseHttpSignature()
131 const badHeadersMatrix = [
132 [ '(request-target)', 'date', 'digest' ],
133 [ 'host', 'date', 'digest' ],
134 [ '(request-target)', 'host', 'digest' ]
135 ]
136
137 for (const badHeaders of badHeadersMatrix) {
138 signatureOptions.headers = badHeaders
139
140 try {
141 await makePOSTAPRequest(url, body, signatureOptions, headers)
142 expect(true, 'Did not throw').to.be.false
143 } catch (err) {
144 expect(err.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
145 }
146 }
147 })
148
149 it('Should succeed with a valid HTTP signature draft 11 (without date but with (created))', async function () {
150 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]), 'Announce')
151 const headers = buildGlobalHeaders(body)
152
153 const signatureOptions = baseHttpSignature()
154 signatureOptions.headers = [ '(request-target)', '(created)', 'host', 'digest' ]
155
156 const { statusCode } = await makePOSTAPRequest(url, body, signatureOptions, headers)
157 expect(statusCode).to.equal(HttpStatusCode.NO_CONTENT_204)
158 })
159
160 it('Should succeed with a valid HTTP signature', async function () {
161 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]), 'Announce')
162 const headers = buildGlobalHeaders(body)
163
164 const { statusCode } = await makePOSTAPRequest(url, body, baseHttpSignature(), headers)
165 expect(statusCode).to.equal(HttpStatusCode.NO_CONTENT_204)
166 })
167
168 it('Should refresh the actor keys', async function () {
169 this.timeout(20000)
170
171 // Update keys of server 2 to invalid keys
172 // Server 1 should refresh the actor and fail
173 await setKeysOfServer(servers[1], servers[1], invalidKeys.publicKey, invalidKeys.privateKey)
174 await setUpdatedAtOfServer(servers[0], servers[1], '2015-07-17 22:00:00+00')
175
176 // Invalid peertube actor cache
177 await killallServers([ servers[1] ])
178 await servers[1].run()
179
180 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]), 'Announce')
181 const headers = buildGlobalHeaders(body)
182
183 try {
184 await makePOSTAPRequest(url, body, baseHttpSignature(), headers)
185 expect(true, 'Did not throw').to.be.false
186 } catch (err) {
187 console.error(err)
188 expect(err.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
189 }
190 })
191 })
192
193 describe('When checking Linked Data Signature', function () {
194 before(async function () {
195 this.timeout(10000)
196
197 await setKeysOfServer(servers[0], servers[1], keys.publicKey, keys.privateKey)
198 await setKeysOfServer(servers[1], servers[1], keys.publicKey, keys.privateKey)
199 await setKeysOfServer(servers[2], servers[2], keys.publicKey, keys.privateKey)
200
201 const to = { url: 'http://localhost:' + servers[0].port + '/accounts/peertube' }
202 const by = { url: 'http://localhost:' + servers[2].port + '/accounts/peertube', privateKey: keys.privateKey }
203 await makeFollowRequest(to, by)
204 })
205
206 it('Should fail with bad keys', async function () {
207 this.timeout(10000)
208
209 await setKeysOfServer(servers[0], servers[2], invalidKeys.publicKey, invalidKeys.privateKey)
210 await setKeysOfServer(servers[2], servers[2], invalidKeys.publicKey, invalidKeys.privateKey)
211
212 const body = getAnnounceWithoutContext(servers[1])
213 body.actor = 'http://localhost:' + servers[2].port + '/accounts/peertube'
214
215 const signer: any = { privateKey: invalidKeys.privateKey, url: 'http://localhost:' + servers[2].port + '/accounts/peertube' }
216 const signedBody = await signAndContextify(signer, body, 'Announce')
217
218 const headers = buildGlobalHeaders(signedBody)
219
220 try {
221 await makePOSTAPRequest(url, signedBody, baseHttpSignature(), headers)
222 expect(true, 'Did not throw').to.be.false
223 } catch (err) {
224 expect(err.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
225 }
226 })
227
228 it('Should fail with an altered body', async function () {
229 this.timeout(10000)
230
231 await setKeysOfServer(servers[0], servers[2], keys.publicKey, keys.privateKey)
232 await setKeysOfServer(servers[0], servers[2], keys.publicKey, keys.privateKey)
233
234 const body = getAnnounceWithoutContext(servers[1])
235 body.actor = 'http://localhost:' + servers[2].port + '/accounts/peertube'
236
237 const signer: any = { privateKey: keys.privateKey, url: 'http://localhost:' + servers[2].port + '/accounts/peertube' }
238 const signedBody = await signAndContextify(signer, body, 'Announce')
239
240 signedBody.actor = 'http://localhost:' + servers[2].port + '/account/peertube'
241
242 const headers = buildGlobalHeaders(signedBody)
243
244 try {
245 await makePOSTAPRequest(url, signedBody, baseHttpSignature(), headers)
246 expect(true, 'Did not throw').to.be.false
247 } catch (err) {
248 expect(err.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
249 }
250 })
251
252 it('Should succeed with a valid signature', async function () {
253 this.timeout(10000)
254
255 const body = getAnnounceWithoutContext(servers[1])
256 body.actor = 'http://localhost:' + servers[2].port + '/accounts/peertube'
257
258 const signer: any = { privateKey: keys.privateKey, url: 'http://localhost:' + servers[2].port + '/accounts/peertube' }
259 const signedBody = await signAndContextify(signer, body, 'Announce')
260
261 const headers = buildGlobalHeaders(signedBody)
262
263 const { statusCode } = await makePOSTAPRequest(url, signedBody, baseHttpSignature(), headers)
264 expect(statusCode).to.equal(HttpStatusCode.NO_CONTENT_204)
265 })
266
267 it('Should refresh the actor keys', async function () {
268 this.timeout(20000)
269
270 // Wait refresh invalidation
271 await wait(10000)
272
273 // Update keys of server 3 to invalid keys
274 // Server 1 should refresh the actor and fail
275 await setKeysOfServer(servers[2], servers[2], invalidKeys.publicKey, invalidKeys.privateKey)
276
277 const body = getAnnounceWithoutContext(servers[1])
278 body.actor = 'http://localhost:' + servers[2].port + '/accounts/peertube'
279
280 const signer: any = { privateKey: keys.privateKey, url: 'http://localhost:' + servers[2].port + '/accounts/peertube' }
281 const signedBody = await signAndContextify(signer, body, 'Announce')
282
283 const headers = buildGlobalHeaders(signedBody)
284
285 try {
286 await makePOSTAPRequest(url, signedBody, baseHttpSignature(), headers)
287 expect(true, 'Did not throw').to.be.false
288 } catch (err) {
289 expect(err.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
290 }
291 })
292 })
293
294 after(async function () {
295 this.timeout(10000)
296
297 await cleanupTests(servers)
298 })
299 })