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