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