1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
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'
10 flushAndRunMultipleServers,
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'
22 const expect = chai.expect
24 function setKeysOfServer (onServer: ServerInfo, ofServer: ServerInfo, publicKey: string, privateKey: string) {
25 const url = 'http://localhost:' + ofServer.port + '/accounts/peertube'
28 setActorField(onServer.internalServerNumber, url, 'publicKey', publicKey),
29 setActorField(onServer.internalServerNumber, url, 'privateKey', privateKey)
33 function setUpdatedAtOfServer (onServer: ServerInfo, ofServer: ServerInfo, updatedAt: string) {
34 const url = 'http://localhost:' + ofServer.port + '/accounts/peertube'
37 setActorField(onServer.internalServerNumber, url, 'createdAt', updatedAt),
38 setActorField(onServer.internalServerNumber, url, 'updatedAt', updatedAt)
42 function getAnnounceWithoutContext (server: ServerInfo) {
43 const json = require('./json/peertube/announce-without-context.json')
44 const result: typeof json = {}
46 for (const key of Object.keys(json)) {
47 if (Array.isArray(json[key])) {
48 result[key] = json[key].map(v => v.replace(':9002', `:${server.port}`))
50 result[key] = json[key].replace(':9002', `:${server.port}`)
57 describe('Test ActivityPub security', function () {
58 let servers: ServerInfo[]
61 const keys = require('./json/peertube/keys.json')
62 const invalidKeys = require('./json/peertube/invalid-keys.json')
63 const baseHttpSignature = () => ({
64 algorithm: HTTP_SIGNATURE.ALGORITHM,
65 authorizationHeaderName: HTTP_SIGNATURE.HEADER_NAME,
66 keyId: 'acct:peertube@localhost:' + servers[1].port,
68 headers: HTTP_SIGNATURE.HEADERS_TO_SIGN
71 // ---------------------------------------------------------------
73 before(async function () {
76 servers = await flushAndRunMultipleServers(3)
78 url = servers[0].url + '/inbox'
80 await setKeysOfServer(servers[0], servers[1], keys.publicKey, null)
81 await setKeysOfServer(servers[1], servers[1], keys.publicKey, keys.privateKey)
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 }
85 await makeFollowRequest(to, by)
88 describe('When checking HTTP signature', function () {
90 it('Should fail with an invalid digest', async function () {
91 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]))
93 Digest: buildDigest({ hello: 'coucou' })
97 await makePOSTAPRequest(url, body, baseHttpSignature(), headers)
98 expect(true, 'Did not throw').to.be.false
100 expect(err.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
104 it('Should fail with an invalid date', async function () {
105 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]))
106 const headers = buildGlobalHeaders(body)
107 headers['date'] = 'Wed, 21 Oct 2015 07:28:00 GMT'
110 await makePOSTAPRequest(url, body, baseHttpSignature(), headers)
111 expect(true, 'Did not throw').to.be.false
113 expect(err.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
117 it('Should fail with bad keys', async function () {
118 await setKeysOfServer(servers[0], servers[1], invalidKeys.publicKey, invalidKeys.privateKey)
119 await setKeysOfServer(servers[1], servers[1], invalidKeys.publicKey, invalidKeys.privateKey)
121 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]))
122 const headers = buildGlobalHeaders(body)
125 await makePOSTAPRequest(url, body, baseHttpSignature(), headers)
126 expect(true, 'Did not throw').to.be.false
128 expect(err.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
132 it('Should reject requests without appropriate signed headers', async function () {
133 await setKeysOfServer(servers[0], servers[1], keys.publicKey, keys.privateKey)
134 await setKeysOfServer(servers[1], servers[1], keys.publicKey, keys.privateKey)
136 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]))
137 const headers = buildGlobalHeaders(body)
139 const signatureOptions = baseHttpSignature()
140 const badHeadersMatrix = [
141 [ '(request-target)', 'date', 'digest' ],
142 [ 'host', 'date', 'digest' ],
143 [ '(request-target)', 'host', 'digest' ]
146 for (const badHeaders of badHeadersMatrix) {
147 signatureOptions.headers = badHeaders
150 await makePOSTAPRequest(url, body, signatureOptions, headers)
151 expect(true, 'Did not throw').to.be.false
153 expect(err.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
158 it('Should succeed with a valid HTTP signature', async function () {
159 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]))
160 const headers = buildGlobalHeaders(body)
162 const { statusCode } = await makePOSTAPRequest(url, body, baseHttpSignature(), headers)
163 expect(statusCode).to.equal(HttpStatusCode.NO_CONTENT_204)
166 it('Should refresh the actor keys', async function () {
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)
172 await setUpdatedAtOfServer(servers[0], servers[1], '2015-07-17 22:00:00+00')
174 // Invalid peertube actor cache
175 killallServers([ servers[1] ])
176 await reRunServer(servers[1])
178 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]))
179 const headers = buildGlobalHeaders(body)
182 await makePOSTAPRequest(url, body, baseHttpSignature(), headers)
183 expect(true, 'Did not throw').to.be.false
186 expect(err.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
191 describe('When checking Linked Data Signature', function () {
192 before(async function () {
195 await setKeysOfServer(servers[0], servers[1], keys.publicKey, keys.privateKey)
196 await setKeysOfServer(servers[1], servers[1], keys.publicKey, keys.privateKey)
197 await setKeysOfServer(servers[2], servers[2], keys.publicKey, keys.privateKey)
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 }
201 await makeFollowRequest(to, by)
204 it('Should fail with bad keys', async function () {
207 await setKeysOfServer(servers[0], servers[2], invalidKeys.publicKey, invalidKeys.privateKey)
208 await setKeysOfServer(servers[2], servers[2], invalidKeys.publicKey, invalidKeys.privateKey)
210 const body = getAnnounceWithoutContext(servers[1])
211 body.actor = 'http://localhost:' + servers[2].port + '/accounts/peertube'
213 const signer: any = { privateKey: invalidKeys.privateKey, url: 'http://localhost:' + servers[2].port + '/accounts/peertube' }
214 const signedBody = await buildSignedActivity(signer, body)
216 const headers = buildGlobalHeaders(signedBody)
219 await makePOSTAPRequest(url, signedBody, baseHttpSignature(), headers)
220 expect(true, 'Did not throw').to.be.false
222 expect(err.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
226 it('Should fail with an altered body', async function () {
229 await setKeysOfServer(servers[0], servers[2], keys.publicKey, keys.privateKey)
230 await setKeysOfServer(servers[0], servers[2], keys.publicKey, keys.privateKey)
232 const body = getAnnounceWithoutContext(servers[1])
233 body.actor = 'http://localhost:' + servers[2].port + '/accounts/peertube'
235 const signer: any = { privateKey: keys.privateKey, url: 'http://localhost:' + servers[2].port + '/accounts/peertube' }
236 const signedBody = await buildSignedActivity(signer, body)
238 signedBody.actor = 'http://localhost:' + servers[2].port + '/account/peertube'
240 const headers = buildGlobalHeaders(signedBody)
243 await makePOSTAPRequest(url, signedBody, baseHttpSignature(), headers)
244 expect(true, 'Did not throw').to.be.false
246 expect(err.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
250 it('Should succeed with a valid signature', async function () {
253 const body = getAnnounceWithoutContext(servers[1])
254 body.actor = 'http://localhost:' + servers[2].port + '/accounts/peertube'
256 const signer: any = { privateKey: keys.privateKey, url: 'http://localhost:' + servers[2].port + '/accounts/peertube' }
257 const signedBody = await buildSignedActivity(signer, body)
259 const headers = buildGlobalHeaders(signedBody)
261 const { statusCode } = await makePOSTAPRequest(url, signedBody, baseHttpSignature(), headers)
262 expect(statusCode).to.equal(HttpStatusCode.NO_CONTENT_204)
265 it('Should refresh the actor keys', async function () {
268 // Wait refresh invalidation
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)
275 const body = getAnnounceWithoutContext(servers[1])
276 body.actor = 'http://localhost:' + servers[2].port + '/accounts/peertube'
278 const signer: any = { privateKey: keys.privateKey, url: 'http://localhost:' + servers[2].port + '/accounts/peertube' }
279 const signedBody = await buildSignedActivity(signer, body)
281 const headers = buildGlobalHeaders(signedBody)
284 await makePOSTAPRequest(url, signedBody, baseHttpSignature(), headers)
285 expect(true, 'Did not throw').to.be.false
287 expect(err.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
292 after(async function () {
295 await cleanupTests(servers)
297 await closeAllSequelize(servers)