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