1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
5 import { cleanupTests, closeAllSequelize, flushAndRunMultipleServers, ServerInfo, setActorField } from '../../../../shared/extra-utils'
6 import { HTTP_SIGNATURE } from '../../../initializers/constants'
7 import { buildGlobalHeaders } from '../../../lib/job-queue/handlers/utils/activitypub-http-utils'
8 import * as chai from 'chai'
9 import { activityPubContextify, buildSignedActivity } from '../../../helpers/activitypub'
10 import { makeFollowRequest, makePOSTAPRequest } from '../../../../shared/extra-utils/requests/activitypub'
11 import { buildDigest } from '@server/helpers/peertube-crypto'
13 const expect = chai.expect
15 function setKeysOfServer (onServer: ServerInfo, ofServer: ServerInfo, publicKey: string, privateKey: string) {
17 setActorField(onServer.internalServerNumber, 'http://localhost:' + ofServer.port + '/accounts/peertube', 'publicKey', publicKey),
18 setActorField(onServer.internalServerNumber, 'http://localhost:' + ofServer.port + '/accounts/peertube', 'privateKey', privateKey)
22 function getAnnounceWithoutContext (server2: ServerInfo) {
23 const json = require('./json/peertube/announce-without-context.json')
24 const result: typeof json = {}
26 for (const key of Object.keys(json)) {
27 if (Array.isArray(json[key])) {
28 result[key] = json[key].map(v => v.replace(':9002', `:${server2.port}`))
30 result[key] = json[key].replace(':9002', `:${server2.port}`)
37 describe('Test ActivityPub security', function () {
38 let servers: ServerInfo[]
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:' + servers[1].port,
48 headers: HTTP_SIGNATURE.HEADERS_TO_SIGN
51 // ---------------------------------------------------------------
53 before(async function () {
56 servers = await flushAndRunMultipleServers(3)
58 url = servers[0].url + '/inbox'
60 await setKeysOfServer(servers[0], servers[1], keys.publicKey, keys.privateKey)
62 const to = { url: 'http://localhost:' + servers[0].port + '/accounts/peertube' }
63 const by = { url: 'http://localhost:' + servers[1].port + '/accounts/peertube', privateKey: keys.privateKey }
64 await makeFollowRequest(to, by)
67 describe('When checking HTTP signature', function () {
69 it('Should fail with an invalid digest', async function () {
70 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]))
72 Digest: buildDigest({ hello: 'coucou' })
75 const { response } = await makePOSTAPRequest(url, body, baseHttpSignature(), headers)
77 expect(response.statusCode).to.equal(403)
80 it('Should fail with an invalid date', async function () {
81 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]))
82 const headers = buildGlobalHeaders(body)
83 headers['date'] = 'Wed, 21 Oct 2015 07:28:00 GMT'
85 const { response } = await makePOSTAPRequest(url, body, baseHttpSignature(), headers)
87 expect(response.statusCode).to.equal(403)
90 it('Should fail with bad keys', async function () {
91 await setKeysOfServer(servers[0], servers[1], invalidKeys.publicKey, invalidKeys.privateKey)
92 await setKeysOfServer(servers[1], servers[1], invalidKeys.publicKey, invalidKeys.privateKey)
94 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]))
95 const headers = buildGlobalHeaders(body)
97 const { response } = await makePOSTAPRequest(url, body, baseHttpSignature(), headers)
99 expect(response.statusCode).to.equal(403)
102 it('Should succeed with a valid HTTP signature', async function () {
103 await setKeysOfServer(servers[0], servers[1], keys.publicKey, keys.privateKey)
104 await setKeysOfServer(servers[1], servers[1], keys.publicKey, keys.privateKey)
106 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]))
107 const headers = buildGlobalHeaders(body)
109 const { response } = await makePOSTAPRequest(url, body, baseHttpSignature(), headers)
111 expect(response.statusCode).to.equal(204)
115 describe('When checking Linked Data Signature', function () {
117 await setKeysOfServer(servers[2], servers[2], keys.publicKey, keys.privateKey)
119 const to = { url: 'http://localhost:' + servers[0].port + '/accounts/peertube' }
120 const by = { url: 'http://localhost:' + servers[2].port + '/accounts/peertube', privateKey: keys.privateKey }
121 await makeFollowRequest(to, by)
124 it('Should fail with bad keys', async function () {
127 await setKeysOfServer(servers[0], servers[2], invalidKeys.publicKey, invalidKeys.privateKey)
128 await setKeysOfServer(servers[2], servers[2], invalidKeys.publicKey, invalidKeys.privateKey)
130 const body = getAnnounceWithoutContext(servers[1])
131 body.actor = 'http://localhost:' + servers[2].port + '/accounts/peertube'
133 const signer: any = { privateKey: invalidKeys.privateKey, url: 'http://localhost:' + servers[2].port + '/accounts/peertube' }
134 const signedBody = await buildSignedActivity(signer, body)
136 const headers = buildGlobalHeaders(signedBody)
138 const { response } = await makePOSTAPRequest(url, signedBody, baseHttpSignature(), headers)
140 expect(response.statusCode).to.equal(403)
143 it('Should fail with an altered body', async function () {
146 await setKeysOfServer(servers[0], servers[2], keys.publicKey, keys.privateKey)
147 await setKeysOfServer(servers[0], servers[2], keys.publicKey, keys.privateKey)
149 const body = getAnnounceWithoutContext(servers[1])
150 body.actor = 'http://localhost:' + servers[2].port + '/accounts/peertube'
152 const signer: any = { privateKey: keys.privateKey, url: 'http://localhost:' + servers[2].port + '/accounts/peertube' }
153 const signedBody = await buildSignedActivity(signer, body)
155 signedBody.actor = 'http://localhost:' + servers[2].port + '/account/peertube'
157 const headers = buildGlobalHeaders(signedBody)
159 const { response } = await makePOSTAPRequest(url, signedBody, baseHttpSignature(), headers)
161 expect(response.statusCode).to.equal(403)
164 it('Should succeed with a valid signature', async function () {
167 const body = getAnnounceWithoutContext(servers[1])
168 body.actor = 'http://localhost:' + servers[2].port + '/accounts/peertube'
170 const signer: any = { privateKey: keys.privateKey, url: 'http://localhost:' + servers[2].port + '/accounts/peertube' }
171 const signedBody = await buildSignedActivity(signer, body)
173 const headers = buildGlobalHeaders(signedBody)
175 const { response } = await makePOSTAPRequest(url, signedBody, baseHttpSignature(), headers)
177 expect(response.statusCode).to.equal(204)
181 after(async function () {
184 await cleanupTests(servers)
186 await closeAllSequelize(servers)