Merge branch 'develop' of https://github.com/Chocobozzz/PeerTube into move-utils...
[oweals/peertube.git] / server / tests / api / activitypub / security.ts
1 /* tslint:disable:no-unused-expression */
2
3 import 'mocha'
4
5 import {
6   flushAndRunMultipleServers,
7   flushTests,
8   killallServers,
9   ServerInfo
10 } from '../../../../shared/utils'
11 import {
12   makePOSTAPRequest,
13   makeFollowRequest,
14 } from '../../utils/requests/activitypub'
15 import { HTTP_SIGNATURE } from '../../../initializers'
16 import { buildDigest, buildGlobalHeaders } from '../../../lib/job-queue/handlers/utils/activitypub-http-utils'
17 import * as chai from 'chai'
18 import { setActorField } from '../../utils/miscs/sql'
19 import { activityPubContextify, buildSignedActivity } from '../../../helpers/activitypub'
20
21 const expect = chai.expect
22
23 function setKeysOfServer2 (serverNumber: number, publicKey: string, privateKey: string) {
24   return Promise.all([
25     setActorField(serverNumber, 'http://localhost:9002/accounts/peertube', 'publicKey', publicKey),
26     setActorField(serverNumber, 'http://localhost:9002/accounts/peertube', 'privateKey', privateKey)
27   ])
28 }
29
30 function setKeysOfServer3 (serverNumber: number, publicKey: string, privateKey: string) {
31   return Promise.all([
32     setActorField(serverNumber, 'http://localhost:9003/accounts/peertube', 'publicKey', publicKey),
33     setActorField(serverNumber, 'http://localhost:9003/accounts/peertube', 'privateKey', privateKey)
34   ])
35 }
36
37 describe('Test ActivityPub security', function () {
38   let servers: ServerInfo[]
39   let url: string
40
41   const keys = require('./json/peertube/keys.json')
42   const invalidKeys = require('./json/peertube/invalid-keys.json')
43   const baseHttpSignature = {
44     algorithm: HTTP_SIGNATURE.ALGORITHM,
45     authorizationHeaderName: HTTP_SIGNATURE.HEADER_NAME,
46     keyId: 'acct:peertube@localhost:9002',
47     key: keys.privateKey,
48     headers: HTTP_SIGNATURE.HEADERS_TO_SIGN
49   }
50
51   // ---------------------------------------------------------------
52
53   before(async function () {
54     this.timeout(60000)
55
56     servers = await flushAndRunMultipleServers(3)
57
58     url = servers[0].url + '/inbox'
59
60     await setKeysOfServer2(1, keys.publicKey, keys.privateKey)
61
62     const to = { url: 'http://localhost:9001/accounts/peertube' }
63     const by = { url: 'http://localhost:9002/accounts/peertube', privateKey: keys.privateKey }
64     await makeFollowRequest(to, by)
65   })
66
67   describe('When checking HTTP signature', function () {
68
69     it('Should fail with an invalid digest', async function () {
70       const body = activityPubContextify(require('./json/peertube/announce-without-context.json'))
71       const headers = {
72         Digest: buildDigest({ hello: 'coucou' })
73       }
74
75       const { response } = await makePOSTAPRequest(url, body, baseHttpSignature, headers)
76
77       expect(response.statusCode).to.equal(403)
78     })
79
80     it('Should fail with an invalid date', async function () {
81       const body = activityPubContextify(require('./json/peertube/announce-without-context.json'))
82       const headers = buildGlobalHeaders(body)
83       headers['date'] = 'Wed, 21 Oct 2015 07:28:00 GMT'
84
85       const { response } = await makePOSTAPRequest(url, body, baseHttpSignature, headers)
86
87       expect(response.statusCode).to.equal(403)
88     })
89
90     it('Should fail with bad keys', async function () {
91       await setKeysOfServer2(1, invalidKeys.publicKey, invalidKeys.privateKey)
92       await setKeysOfServer2(2, invalidKeys.publicKey, invalidKeys.privateKey)
93
94       const body = activityPubContextify(require('./json/peertube/announce-without-context.json'))
95       const headers = buildGlobalHeaders(body)
96
97       const { response } = await makePOSTAPRequest(url, body, baseHttpSignature, headers)
98
99       expect(response.statusCode).to.equal(403)
100     })
101
102     it('Should succeed with a valid HTTP signature', async function () {
103       await setKeysOfServer2(1, keys.publicKey, keys.privateKey)
104       await setKeysOfServer2(2, keys.publicKey, keys.privateKey)
105
106       const body = activityPubContextify(require('./json/peertube/announce-without-context.json'))
107       const headers = buildGlobalHeaders(body)
108
109       const { response } = await makePOSTAPRequest(url, body, baseHttpSignature, headers)
110
111       expect(response.statusCode).to.equal(204)
112     })
113   })
114
115   describe('When checking Linked Data Signature', function () {
116     before(async () => {
117       await setKeysOfServer3(3, keys.publicKey, keys.privateKey)
118
119       const to = { url: 'http://localhost:9001/accounts/peertube' }
120       const by = { url: 'http://localhost:9003/accounts/peertube', privateKey: keys.privateKey }
121       await makeFollowRequest(to, by)
122     })
123
124     it('Should fail with bad keys', async function () {
125       this.timeout(10000)
126
127       await setKeysOfServer3(1, invalidKeys.publicKey, invalidKeys.privateKey)
128       await setKeysOfServer3(3, invalidKeys.publicKey, invalidKeys.privateKey)
129
130       const body = require('./json/peertube/announce-without-context.json')
131       body.actor = 'http://localhost:9003/accounts/peertube'
132
133       const signer: any = { privateKey: invalidKeys.privateKey, url: 'http://localhost:9003/accounts/peertube' }
134       const signedBody = await buildSignedActivity(signer, body)
135
136       const headers = buildGlobalHeaders(signedBody)
137
138       const { response } = await makePOSTAPRequest(url, signedBody, baseHttpSignature, headers)
139
140       expect(response.statusCode).to.equal(403)
141     })
142
143     it('Should fail with an altered body', async function () {
144       this.timeout(10000)
145
146       await setKeysOfServer3(1, keys.publicKey, keys.privateKey)
147       await setKeysOfServer3(3, keys.publicKey, keys.privateKey)
148
149       const body = require('./json/peertube/announce-without-context.json')
150       body.actor = 'http://localhost:9003/accounts/peertube'
151
152       const signer: any = { privateKey: keys.privateKey, url: 'http://localhost:9003/accounts/peertube' }
153       const signedBody = await buildSignedActivity(signer, body)
154
155       signedBody.actor = 'http://localhost:9003/account/peertube'
156
157       const headers = buildGlobalHeaders(signedBody)
158
159       const { response } = await makePOSTAPRequest(url, signedBody, baseHttpSignature, headers)
160
161       expect(response.statusCode).to.equal(403)
162     })
163
164     it('Should succeed with a valid signature', async function () {
165       this.timeout(10000)
166
167       const body = require('./json/peertube/announce-without-context.json')
168       body.actor = 'http://localhost:9003/accounts/peertube'
169
170       const signer: any = { privateKey: keys.privateKey, url: 'http://localhost:9003/accounts/peertube' }
171       const signedBody = await buildSignedActivity(signer, body)
172
173       const headers = buildGlobalHeaders(signedBody)
174
175       const { response } = await makePOSTAPRequest(url, signedBody, baseHttpSignature, headers)
176
177       expect(response.statusCode).to.equal(204)
178     })
179   })
180
181   after(async function () {
182     killallServers(servers)
183
184     // Keep the logs if the test failed
185     if (this['ok']) {
186       await flushTests()
187     }
188   })
189 })