]>
Commit | Line | Data |
---|---|---|
1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ | |
2 | ||
3 | import 'mocha' | |
4 | import * as chai from 'chai' | |
5 | import { activityPubContextify, buildSignedActivity } from '@server/helpers/activitypub' | |
6 | import { buildDigest } from '@server/helpers/peertube-crypto' | |
7 | import { HTTP_SIGNATURE } from '@server/initializers/constants' | |
8 | import { buildGlobalHeaders } from '@server/lib/job-queue/handlers/utils/activitypub-http-utils' | |
9 | import { buildAbsoluteFixturePath, cleanupTests, createMultipleServers, killallServers, PeerTubeServer, wait } from '@shared/extra-utils' | |
10 | import { makeFollowRequest, makePOSTAPRequest } from '@shared/extra-utils/requests/activitypub' | |
11 | import { HttpStatusCode } from '@shared/models' | |
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])) | |
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])) | |
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])) | |
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])) | |
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', async function () { | |
150 | const body = activityPubContextify(getAnnounceWithoutContext(servers[1])) | |
151 | const headers = buildGlobalHeaders(body) | |
152 | ||
153 | const { statusCode } = await makePOSTAPRequest(url, body, baseHttpSignature(), headers) | |
154 | expect(statusCode).to.equal(HttpStatusCode.NO_CONTENT_204) | |
155 | }) | |
156 | ||
157 | it('Should refresh the actor keys', async function () { | |
158 | this.timeout(20000) | |
159 | ||
160 | // Update keys of server 2 to invalid keys | |
161 | // Server 1 should refresh the actor and fail | |
162 | await setKeysOfServer(servers[1], servers[1], invalidKeys.publicKey, invalidKeys.privateKey) | |
163 | await setUpdatedAtOfServer(servers[0], servers[1], '2015-07-17 22:00:00+00') | |
164 | ||
165 | // Invalid peertube actor cache | |
166 | await killallServers([ servers[1] ]) | |
167 | await servers[1].run() | |
168 | ||
169 | const body = activityPubContextify(getAnnounceWithoutContext(servers[1])) | |
170 | const headers = buildGlobalHeaders(body) | |
171 | ||
172 | try { | |
173 | await makePOSTAPRequest(url, body, baseHttpSignature(), headers) | |
174 | expect(true, 'Did not throw').to.be.false | |
175 | } catch (err) { | |
176 | console.error(err) | |
177 | expect(err.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403) | |
178 | } | |
179 | }) | |
180 | }) | |
181 | ||
182 | describe('When checking Linked Data Signature', function () { | |
183 | before(async function () { | |
184 | this.timeout(10000) | |
185 | ||
186 | await setKeysOfServer(servers[0], servers[1], keys.publicKey, keys.privateKey) | |
187 | await setKeysOfServer(servers[1], servers[1], keys.publicKey, keys.privateKey) | |
188 | await setKeysOfServer(servers[2], servers[2], keys.publicKey, keys.privateKey) | |
189 | ||
190 | const to = { url: 'http://localhost:' + servers[0].port + '/accounts/peertube' } | |
191 | const by = { url: 'http://localhost:' + servers[2].port + '/accounts/peertube', privateKey: keys.privateKey } | |
192 | await makeFollowRequest(to, by) | |
193 | }) | |
194 | ||
195 | it('Should fail with bad keys', async function () { | |
196 | this.timeout(10000) | |
197 | ||
198 | await setKeysOfServer(servers[0], servers[2], invalidKeys.publicKey, invalidKeys.privateKey) | |
199 | await setKeysOfServer(servers[2], servers[2], invalidKeys.publicKey, invalidKeys.privateKey) | |
200 | ||
201 | const body = getAnnounceWithoutContext(servers[1]) | |
202 | body.actor = 'http://localhost:' + servers[2].port + '/accounts/peertube' | |
203 | ||
204 | const signer: any = { privateKey: invalidKeys.privateKey, url: 'http://localhost:' + servers[2].port + '/accounts/peertube' } | |
205 | const signedBody = await buildSignedActivity(signer, body) | |
206 | ||
207 | const headers = buildGlobalHeaders(signedBody) | |
208 | ||
209 | try { | |
210 | await makePOSTAPRequest(url, signedBody, baseHttpSignature(), headers) | |
211 | expect(true, 'Did not throw').to.be.false | |
212 | } catch (err) { | |
213 | expect(err.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403) | |
214 | } | |
215 | }) | |
216 | ||
217 | it('Should fail with an altered body', async function () { | |
218 | this.timeout(10000) | |
219 | ||
220 | await setKeysOfServer(servers[0], servers[2], keys.publicKey, keys.privateKey) | |
221 | await setKeysOfServer(servers[0], servers[2], keys.publicKey, keys.privateKey) | |
222 | ||
223 | const body = getAnnounceWithoutContext(servers[1]) | |
224 | body.actor = 'http://localhost:' + servers[2].port + '/accounts/peertube' | |
225 | ||
226 | const signer: any = { privateKey: keys.privateKey, url: 'http://localhost:' + servers[2].port + '/accounts/peertube' } | |
227 | const signedBody = await buildSignedActivity(signer, body) | |
228 | ||
229 | signedBody.actor = 'http://localhost:' + servers[2].port + '/account/peertube' | |
230 | ||
231 | const headers = buildGlobalHeaders(signedBody) | |
232 | ||
233 | try { | |
234 | await makePOSTAPRequest(url, signedBody, baseHttpSignature(), headers) | |
235 | expect(true, 'Did not throw').to.be.false | |
236 | } catch (err) { | |
237 | expect(err.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403) | |
238 | } | |
239 | }) | |
240 | ||
241 | it('Should succeed with a valid signature', async function () { | |
242 | this.timeout(10000) | |
243 | ||
244 | const body = getAnnounceWithoutContext(servers[1]) | |
245 | body.actor = 'http://localhost:' + servers[2].port + '/accounts/peertube' | |
246 | ||
247 | const signer: any = { privateKey: keys.privateKey, url: 'http://localhost:' + servers[2].port + '/accounts/peertube' } | |
248 | const signedBody = await buildSignedActivity(signer, body) | |
249 | ||
250 | const headers = buildGlobalHeaders(signedBody) | |
251 | ||
252 | const { statusCode } = await makePOSTAPRequest(url, signedBody, baseHttpSignature(), headers) | |
253 | expect(statusCode).to.equal(HttpStatusCode.NO_CONTENT_204) | |
254 | }) | |
255 | ||
256 | it('Should refresh the actor keys', async function () { | |
257 | this.timeout(20000) | |
258 | ||
259 | // Wait refresh invalidation | |
260 | await wait(10000) | |
261 | ||
262 | // Update keys of server 3 to invalid keys | |
263 | // Server 1 should refresh the actor and fail | |
264 | await setKeysOfServer(servers[2], servers[2], invalidKeys.publicKey, invalidKeys.privateKey) | |
265 | ||
266 | const body = getAnnounceWithoutContext(servers[1]) | |
267 | body.actor = 'http://localhost:' + servers[2].port + '/accounts/peertube' | |
268 | ||
269 | const signer: any = { privateKey: keys.privateKey, url: 'http://localhost:' + servers[2].port + '/accounts/peertube' } | |
270 | const signedBody = await buildSignedActivity(signer, body) | |
271 | ||
272 | const headers = buildGlobalHeaders(signedBody) | |
273 | ||
274 | try { | |
275 | await makePOSTAPRequest(url, signedBody, baseHttpSignature(), headers) | |
276 | expect(true, 'Did not throw').to.be.false | |
277 | } catch (err) { | |
278 | expect(err.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403) | |
279 | } | |
280 | }) | |
281 | }) | |
282 | ||
283 | after(async function () { | |
284 | this.timeout(10000) | |
285 | ||
286 | await cleanupTests(servers) | |
287 | }) | |
288 | }) |