]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/activitypub/security.ts
Add ability to cleanup remote AP interactions
[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 const { response } = await makePOSTAPRequest(url, body, baseHttpSignature(), headers)
83
84 expect(response.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
85 })
86
87 it('Should fail with an invalid date', async function () {
88 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]))
89 const headers = buildGlobalHeaders(body)
90 headers['date'] = 'Wed, 21 Oct 2015 07:28:00 GMT'
91
92 const { response } = await makePOSTAPRequest(url, body, baseHttpSignature(), headers)
93
94 expect(response.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
95 })
96
97 it('Should fail with bad keys', async function () {
98 await setKeysOfServer(servers[0], servers[1], invalidKeys.publicKey, invalidKeys.privateKey)
99 await setKeysOfServer(servers[1], servers[1], invalidKeys.publicKey, invalidKeys.privateKey)
100
101 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]))
102 const headers = buildGlobalHeaders(body)
103
104 const { response } = await makePOSTAPRequest(url, body, baseHttpSignature(), headers)
105
106 expect(response.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
107 })
108
109 it('Should reject requests without appropriate signed headers', async function () {
110 await setKeysOfServer(servers[0], servers[1], keys.publicKey, keys.privateKey)
111 await setKeysOfServer(servers[1], servers[1], keys.publicKey, keys.privateKey)
112
113 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]))
114 const headers = buildGlobalHeaders(body)
115
116 const signatureOptions = baseHttpSignature()
117 const badHeadersMatrix = [
118 [ '(request-target)', 'date', 'digest' ],
119 [ 'host', 'date', 'digest' ],
120 [ '(request-target)', 'host', 'digest' ]
121 ]
122
123 for (const badHeaders of badHeadersMatrix) {
124 signatureOptions.headers = badHeaders
125
126 const { response } = await makePOSTAPRequest(url, body, signatureOptions, headers)
127 expect(response.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
128 }
129 })
130
131 it('Should succeed with a valid HTTP signature', async function () {
132 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]))
133 const headers = buildGlobalHeaders(body)
134
135 const { response } = await makePOSTAPRequest(url, body, baseHttpSignature(), headers)
136
137 expect(response.statusCode).to.equal(HttpStatusCode.NO_CONTENT_204)
138 })
139
140 it('Should refresh the actor keys', async function () {
141 this.timeout(20000)
142
143 // Wait refresh invalidation
144 await wait(10000)
145
146 // Update keys of server 2 to invalid keys
147 // Server 1 should refresh the actor and fail
148 await setKeysOfServer(servers[1], servers[1], invalidKeys.publicKey, invalidKeys.privateKey)
149
150 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]))
151 const headers = buildGlobalHeaders(body)
152
153 const { response } = await makePOSTAPRequest(url, body, baseHttpSignature(), headers)
154
155 expect(response.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
156 })
157 })
158
159 describe('When checking Linked Data Signature', function () {
160 before(async function () {
161 this.timeout(10000)
162
163 await setKeysOfServer(servers[0], servers[1], keys.publicKey, keys.privateKey)
164 await setKeysOfServer(servers[1], servers[1], keys.publicKey, keys.privateKey)
165 await setKeysOfServer(servers[2], servers[2], keys.publicKey, keys.privateKey)
166
167 const to = { url: 'http://localhost:' + servers[0].port + '/accounts/peertube' }
168 const by = { url: 'http://localhost:' + servers[2].port + '/accounts/peertube', privateKey: keys.privateKey }
169 await makeFollowRequest(to, by)
170 })
171
172 it('Should fail with bad keys', async function () {
173 this.timeout(10000)
174
175 await setKeysOfServer(servers[0], servers[2], invalidKeys.publicKey, invalidKeys.privateKey)
176 await setKeysOfServer(servers[2], servers[2], invalidKeys.publicKey, invalidKeys.privateKey)
177
178 const body = getAnnounceWithoutContext(servers[1])
179 body.actor = 'http://localhost:' + servers[2].port + '/accounts/peertube'
180
181 const signer: any = { privateKey: invalidKeys.privateKey, url: 'http://localhost:' + servers[2].port + '/accounts/peertube' }
182 const signedBody = await buildSignedActivity(signer, body)
183
184 const headers = buildGlobalHeaders(signedBody)
185
186 const { response } = await makePOSTAPRequest(url, signedBody, baseHttpSignature(), headers)
187
188 expect(response.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
189 })
190
191 it('Should fail with an altered body', async function () {
192 this.timeout(10000)
193
194 await setKeysOfServer(servers[0], servers[2], keys.publicKey, keys.privateKey)
195 await setKeysOfServer(servers[0], servers[2], keys.publicKey, keys.privateKey)
196
197 const body = getAnnounceWithoutContext(servers[1])
198 body.actor = 'http://localhost:' + servers[2].port + '/accounts/peertube'
199
200 const signer: any = { privateKey: keys.privateKey, url: 'http://localhost:' + servers[2].port + '/accounts/peertube' }
201 const signedBody = await buildSignedActivity(signer, body)
202
203 signedBody.actor = 'http://localhost:' + servers[2].port + '/account/peertube'
204
205 const headers = buildGlobalHeaders(signedBody)
206
207 const { response } = await makePOSTAPRequest(url, signedBody, baseHttpSignature(), headers)
208
209 expect(response.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
210 })
211
212 it('Should succeed with a valid signature', async function () {
213 this.timeout(10000)
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 const headers = buildGlobalHeaders(signedBody)
222
223 const { response } = await makePOSTAPRequest(url, signedBody, baseHttpSignature(), headers)
224
225 expect(response.statusCode).to.equal(HttpStatusCode.NO_CONTENT_204)
226 })
227
228 it('Should refresh the actor keys', async function () {
229 this.timeout(20000)
230
231 // Wait refresh invalidation
232 await wait(10000)
233
234 // Update keys of server 3 to invalid keys
235 // Server 1 should refresh the actor and fail
236 await setKeysOfServer(servers[2], servers[2], invalidKeys.publicKey, invalidKeys.privateKey)
237
238 const body = getAnnounceWithoutContext(servers[1])
239 body.actor = 'http://localhost:' + servers[2].port + '/accounts/peertube'
240
241 const signer: any = { privateKey: keys.privateKey, url: 'http://localhost:' + servers[2].port + '/accounts/peertube' }
242 const signedBody = await buildSignedActivity(signer, body)
243
244 const headers = buildGlobalHeaders(signedBody)
245
246 const { response } = await makePOSTAPRequest(url, signedBody, baseHttpSignature(), headers)
247
248 expect(response.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
249 })
250 })
251
252 after(async function () {
253 this.timeout(10000)
254
255 await cleanupTests(servers)
256
257 await closeAllSequelize(servers)
258 })
259 })