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,
14 } from '../../../../shared/extra-utils'
15 import { makeFollowRequest, makePOSTAPRequest } from '../../../../shared/extra-utils/requests/activitypub'
16 import { activityPubContextify, buildSignedActivity } from '../../../helpers/activitypub'
17 import { HTTP_SIGNATURE } from '../../../initializers/constants'
18 import { buildGlobalHeaders } from '../../../lib/job-queue/handlers/utils/activitypub-http-utils'
20 const expect = chai.expect
22 function setKeysOfServer (onServer: ServerInfo, ofServer: ServerInfo, publicKey: string, privateKey: string) {
24 setActorField(onServer.internalServerNumber, 'http://localhost:' + ofServer.port + '/accounts/peertube', 'publicKey', publicKey),
25 setActorField(onServer.internalServerNumber, 'http://localhost:' + ofServer.port + '/accounts/peertube', 'privateKey', privateKey)
29 function getAnnounceWithoutContext (server2: ServerInfo) {
30 const json = require('./json/peertube/announce-without-context.json')
31 const result: typeof json = {}
33 for (const key of Object.keys(json)) {
34 if (Array.isArray(json[key])) {
35 result[key] = json[key].map(v => v.replace(':9002', `:${server2.port}`))
37 result[key] = json[key].replace(':9002', `:${server2.port}`)
44 describe('Test ActivityPub security', function () {
45 let servers: ServerInfo[]
48 const keys = require('./json/peertube/keys.json')
49 const invalidKeys = require('./json/peertube/invalid-keys.json')
50 const baseHttpSignature = () => ({
51 algorithm: HTTP_SIGNATURE.ALGORITHM,
52 authorizationHeaderName: HTTP_SIGNATURE.HEADER_NAME,
53 keyId: 'acct:peertube@localhost:' + servers[1].port,
55 headers: HTTP_SIGNATURE.HEADERS_TO_SIGN
58 // ---------------------------------------------------------------
60 before(async function () {
63 servers = await flushAndRunMultipleServers(3)
65 url = servers[0].url + '/inbox'
67 await setKeysOfServer(servers[0], servers[1], keys.publicKey, keys.privateKey)
69 const to = { url: 'http://localhost:' + servers[0].port + '/accounts/peertube' }
70 const by = { url: 'http://localhost:' + servers[1].port + '/accounts/peertube', privateKey: keys.privateKey }
71 await makeFollowRequest(to, by)
74 describe('When checking HTTP signature', function () {
76 it('Should fail with an invalid digest', async function () {
77 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]))
79 Digest: buildDigest({ hello: 'coucou' })
83 await makePOSTAPRequest(url, body, baseHttpSignature(), headers)
84 expect(true, 'Did not throw').to.be.false
86 expect(err.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
90 it('Should fail with an invalid date', async function () {
91 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]))
92 const headers = buildGlobalHeaders(body)
93 headers['date'] = 'Wed, 21 Oct 2015 07:28:00 GMT'
96 await makePOSTAPRequest(url, body, baseHttpSignature(), headers)
97 expect(true, 'Did not throw').to.be.false
99 expect(err.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
103 it('Should fail with bad keys', async function () {
104 await setKeysOfServer(servers[0], servers[1], invalidKeys.publicKey, invalidKeys.privateKey)
105 await setKeysOfServer(servers[1], servers[1], invalidKeys.publicKey, invalidKeys.privateKey)
107 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]))
108 const headers = buildGlobalHeaders(body)
111 await makePOSTAPRequest(url, body, baseHttpSignature(), headers)
112 expect(true, 'Did not throw').to.be.false
114 expect(err.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
118 it('Should reject requests without appropriate signed headers', async function () {
119 await setKeysOfServer(servers[0], servers[1], keys.publicKey, keys.privateKey)
120 await setKeysOfServer(servers[1], servers[1], keys.publicKey, keys.privateKey)
122 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]))
123 const headers = buildGlobalHeaders(body)
125 const signatureOptions = baseHttpSignature()
126 const badHeadersMatrix = [
127 [ '(request-target)', 'date', 'digest' ],
128 [ 'host', 'date', 'digest' ],
129 [ '(request-target)', 'host', 'digest' ]
132 for (const badHeaders of badHeadersMatrix) {
133 signatureOptions.headers = badHeaders
136 await makePOSTAPRequest(url, body, signatureOptions, headers)
137 expect(true, 'Did not throw').to.be.false
139 expect(err.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
144 it('Should succeed with a valid HTTP signature', async function () {
145 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]))
146 const headers = buildGlobalHeaders(body)
148 const { statusCode } = await makePOSTAPRequest(url, body, baseHttpSignature(), headers)
149 expect(statusCode).to.equal(HttpStatusCode.NO_CONTENT_204)
152 it('Should refresh the actor keys', async function () {
155 // Wait refresh invalidation
158 // Update keys of server 2 to invalid keys
159 // Server 1 should refresh the actor and fail
160 await setKeysOfServer(servers[1], servers[1], invalidKeys.publicKey, invalidKeys.privateKey)
162 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]))
163 const headers = buildGlobalHeaders(body)
166 await makePOSTAPRequest(url, body, baseHttpSignature(), headers)
167 expect(true, 'Did not throw').to.be.false
169 expect(err.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
174 describe('When checking Linked Data Signature', function () {
175 before(async function () {
178 await setKeysOfServer(servers[0], servers[1], keys.publicKey, keys.privateKey)
179 await setKeysOfServer(servers[1], servers[1], keys.publicKey, keys.privateKey)
180 await setKeysOfServer(servers[2], servers[2], keys.publicKey, keys.privateKey)
182 const to = { url: 'http://localhost:' + servers[0].port + '/accounts/peertube' }
183 const by = { url: 'http://localhost:' + servers[2].port + '/accounts/peertube', privateKey: keys.privateKey }
184 await makeFollowRequest(to, by)
187 it('Should fail with bad keys', async function () {
190 await setKeysOfServer(servers[0], servers[2], invalidKeys.publicKey, invalidKeys.privateKey)
191 await setKeysOfServer(servers[2], servers[2], invalidKeys.publicKey, invalidKeys.privateKey)
193 const body = getAnnounceWithoutContext(servers[1])
194 body.actor = 'http://localhost:' + servers[2].port + '/accounts/peertube'
196 const signer: any = { privateKey: invalidKeys.privateKey, url: 'http://localhost:' + servers[2].port + '/accounts/peertube' }
197 const signedBody = await buildSignedActivity(signer, body)
199 const headers = buildGlobalHeaders(signedBody)
202 await makePOSTAPRequest(url, signedBody, baseHttpSignature(), headers)
203 expect(true, 'Did not throw').to.be.false
205 expect(err.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
209 it('Should fail with an altered body', async function () {
212 await setKeysOfServer(servers[0], servers[2], keys.publicKey, keys.privateKey)
213 await setKeysOfServer(servers[0], servers[2], keys.publicKey, keys.privateKey)
215 const body = getAnnounceWithoutContext(servers[1])
216 body.actor = 'http://localhost:' + servers[2].port + '/accounts/peertube'
218 const signer: any = { privateKey: keys.privateKey, url: 'http://localhost:' + servers[2].port + '/accounts/peertube' }
219 const signedBody = await buildSignedActivity(signer, body)
221 signedBody.actor = 'http://localhost:' + servers[2].port + '/account/peertube'
223 const headers = buildGlobalHeaders(signedBody)
226 await makePOSTAPRequest(url, signedBody, baseHttpSignature(), headers)
227 expect(true, 'Did not throw').to.be.false
229 expect(err.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
233 it('Should succeed with a valid signature', async function () {
236 const body = getAnnounceWithoutContext(servers[1])
237 body.actor = 'http://localhost:' + servers[2].port + '/accounts/peertube'
239 const signer: any = { privateKey: keys.privateKey, url: 'http://localhost:' + servers[2].port + '/accounts/peertube' }
240 const signedBody = await buildSignedActivity(signer, body)
242 const headers = buildGlobalHeaders(signedBody)
244 const { statusCode } = await makePOSTAPRequest(url, signedBody, baseHttpSignature(), headers)
245 expect(statusCode).to.equal(HttpStatusCode.NO_CONTENT_204)
248 it('Should refresh the actor keys', async function () {
251 // Wait refresh invalidation
254 // Update keys of server 3 to invalid keys
255 // Server 1 should refresh the actor and fail
256 await setKeysOfServer(servers[2], servers[2], invalidKeys.publicKey, invalidKeys.privateKey)
258 const body = getAnnounceWithoutContext(servers[1])
259 body.actor = 'http://localhost:' + servers[2].port + '/accounts/peertube'
261 const signer: any = { privateKey: keys.privateKey, url: 'http://localhost:' + servers[2].port + '/accounts/peertube' }
262 const signedBody = await buildSignedActivity(signer, body)
264 const headers = buildGlobalHeaders(signedBody)
267 await makePOSTAPRequest(url, signedBody, baseHttpSignature(), headers)
268 expect(true, 'Did not throw').to.be.false
270 expect(err.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
275 after(async function () {
278 await cleanupTests(servers)
280 await closeAllSequelize(servers)