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 { buildDigest, 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'
12 const expect = chai.expect
14 function setKeysOfServer (onServer: ServerInfo, ofServer: ServerInfo, publicKey: string, privateKey: string) {
16 setActorField(onServer.internalServerNumber, 'http://localhost:' + ofServer.port + '/accounts/peertube', 'publicKey', publicKey),
17 setActorField(onServer.internalServerNumber, 'http://localhost:' + ofServer.port + '/accounts/peertube', 'privateKey', privateKey)
21 function getAnnounceWithoutContext (server2: ServerInfo) {
22 const json = require('./json/peertube/announce-without-context.json')
23 const result: typeof json = {}
25 for (const key of Object.keys(json)) {
26 if (Array.isArray(json[key])) {
27 result[key] = json[key].map(v => v.replace(':9002', `:${server2.port}`))
29 result[key] = json[key].replace(':9002', `:${server2.port}`)
36 describe('Test ActivityPub security', function () {
37 let servers: ServerInfo[]
40 const keys = require('./json/peertube/keys.json')
41 const invalidKeys = require('./json/peertube/invalid-keys.json')
42 const baseHttpSignature = () => ({
43 algorithm: HTTP_SIGNATURE.ALGORITHM,
44 authorizationHeaderName: HTTP_SIGNATURE.HEADER_NAME,
45 keyId: 'acct:peertube@localhost:' + servers[1].port,
47 headers: HTTP_SIGNATURE.HEADERS_TO_SIGN
50 // ---------------------------------------------------------------
52 before(async function () {
55 servers = await flushAndRunMultipleServers(3)
57 url = servers[0].url + '/inbox'
59 await setKeysOfServer(servers[0], servers[1], keys.publicKey, keys.privateKey)
61 const to = { url: 'http://localhost:' + servers[0].port + '/accounts/peertube' }
62 const by = { url: 'http://localhost:' + servers[1].port + '/accounts/peertube', privateKey: keys.privateKey }
63 await makeFollowRequest(to, by)
66 describe('When checking HTTP signature', function () {
68 it('Should fail with an invalid digest', async function () {
69 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]))
71 Digest: buildDigest({ hello: 'coucou' })
74 const { response } = await makePOSTAPRequest(url, body, baseHttpSignature(), headers)
76 expect(response.statusCode).to.equal(403)
79 it('Should fail with an invalid date', async function () {
80 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]))
81 const headers = buildGlobalHeaders(body)
82 headers['date'] = 'Wed, 21 Oct 2015 07:28:00 GMT'
84 const { response } = await makePOSTAPRequest(url, body, baseHttpSignature(), headers)
86 expect(response.statusCode).to.equal(403)
89 it('Should fail with bad keys', async function () {
90 await setKeysOfServer(servers[0], servers[1], invalidKeys.publicKey, invalidKeys.privateKey)
91 await setKeysOfServer(servers[1], servers[1], invalidKeys.publicKey, invalidKeys.privateKey)
93 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]))
94 const headers = buildGlobalHeaders(body)
96 const { response } = await makePOSTAPRequest(url, body, baseHttpSignature(), headers)
98 expect(response.statusCode).to.equal(403)
101 it('Should succeed with a valid HTTP signature', async function () {
102 await setKeysOfServer(servers[0], servers[1], keys.publicKey, keys.privateKey)
103 await setKeysOfServer(servers[1], servers[1], keys.publicKey, keys.privateKey)
105 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]))
106 const headers = buildGlobalHeaders(body)
108 const { response } = await makePOSTAPRequest(url, body, baseHttpSignature(), headers)
110 expect(response.statusCode).to.equal(204)
114 describe('When checking Linked Data Signature', function () {
116 await setKeysOfServer(servers[2], servers[2], keys.publicKey, keys.privateKey)
118 const to = { url: 'http://localhost:' + servers[0].port + '/accounts/peertube' }
119 const by = { url: 'http://localhost:' + servers[2].port + '/accounts/peertube', privateKey: keys.privateKey }
120 await makeFollowRequest(to, by)
123 it('Should fail with bad keys', async function () {
126 await setKeysOfServer(servers[0], servers[2], invalidKeys.publicKey, invalidKeys.privateKey)
127 await setKeysOfServer(servers[2], servers[2], invalidKeys.publicKey, invalidKeys.privateKey)
129 const body = getAnnounceWithoutContext(servers[1])
130 body.actor = 'http://localhost:' + servers[2].port + '/accounts/peertube'
132 const signer: any = { privateKey: invalidKeys.privateKey, url: 'http://localhost:' + servers[2].port + '/accounts/peertube' }
133 const signedBody = await buildSignedActivity(signer, body)
135 const headers = buildGlobalHeaders(signedBody)
137 const { response } = await makePOSTAPRequest(url, signedBody, baseHttpSignature(), headers)
139 expect(response.statusCode).to.equal(403)
142 it('Should fail with an altered body', async function () {
145 await setKeysOfServer(servers[0], servers[2], keys.publicKey, keys.privateKey)
146 await setKeysOfServer(servers[0], servers[2], keys.publicKey, keys.privateKey)
148 const body = getAnnounceWithoutContext(servers[1])
149 body.actor = 'http://localhost:' + servers[2].port + '/accounts/peertube'
151 const signer: any = { privateKey: keys.privateKey, url: 'http://localhost:' + servers[2].port + '/accounts/peertube' }
152 const signedBody = await buildSignedActivity(signer, body)
154 signedBody.actor = 'http://localhost:' + servers[2].port + '/account/peertube'
156 const headers = buildGlobalHeaders(signedBody)
158 const { response } = await makePOSTAPRequest(url, signedBody, baseHttpSignature(), headers)
160 expect(response.statusCode).to.equal(403)
163 it('Should succeed with a valid signature', async function () {
166 const body = getAnnounceWithoutContext(servers[1])
167 body.actor = 'http://localhost:' + servers[2].port + '/accounts/peertube'
169 const signer: any = { privateKey: keys.privateKey, url: 'http://localhost:' + servers[2].port + '/accounts/peertube' }
170 const signedBody = await buildSignedActivity(signer, body)
172 const headers = buildGlobalHeaders(signedBody)
174 const { response } = await makePOSTAPRequest(url, signedBody, baseHttpSignature(), headers)
176 expect(response.statusCode).to.equal(204)
180 after(async function () {
183 await cleanupTests(servers)
185 await closeAllSequelize(servers)