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'
12 import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
14 const expect = chai.expect
16 function setKeysOfServer (onServer: ServerInfo, ofServer: ServerInfo, publicKey: string, privateKey: string) {
18 setActorField(onServer.internalServerNumber, 'http://localhost:' + ofServer.port + '/accounts/peertube', 'publicKey', publicKey),
19 setActorField(onServer.internalServerNumber, 'http://localhost:' + ofServer.port + '/accounts/peertube', 'privateKey', privateKey)
23 function getAnnounceWithoutContext (server2: ServerInfo) {
24 const json = require('./json/peertube/announce-without-context.json')
25 const result: typeof json = {}
27 for (const key of Object.keys(json)) {
28 if (Array.isArray(json[key])) {
29 result[key] = json[key].map(v => v.replace(':9002', `:${server2.port}`))
31 result[key] = json[key].replace(':9002', `:${server2.port}`)
38 describe('Test ActivityPub security', function () {
39 let servers: ServerInfo[]
42 const keys = require('./json/peertube/keys.json')
43 const invalidKeys = require('./json/peertube/invalid-keys.json')
44 const baseHttpSignature = () => ({
45 algorithm: HTTP_SIGNATURE.ALGORITHM,
46 authorizationHeaderName: HTTP_SIGNATURE.HEADER_NAME,
47 keyId: 'acct:peertube@localhost:' + servers[1].port,
49 headers: HTTP_SIGNATURE.HEADERS_TO_SIGN
52 // ---------------------------------------------------------------
54 before(async function () {
57 servers = await flushAndRunMultipleServers(3)
59 url = servers[0].url + '/inbox'
61 await setKeysOfServer(servers[0], servers[1], keys.publicKey, keys.privateKey)
63 const to = { url: 'http://localhost:' + servers[0].port + '/accounts/peertube' }
64 const by = { url: 'http://localhost:' + servers[1].port + '/accounts/peertube', privateKey: keys.privateKey }
65 await makeFollowRequest(to, by)
68 describe('When checking HTTP signature', function () {
70 it('Should fail with an invalid digest', async function () {
71 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]))
73 Digest: buildDigest({ hello: 'coucou' })
76 const { response } = await makePOSTAPRequest(url, body, baseHttpSignature(), headers)
78 expect(response.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
81 it('Should fail with an invalid date', async function () {
82 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]))
83 const headers = buildGlobalHeaders(body)
84 headers['date'] = 'Wed, 21 Oct 2015 07:28:00 GMT'
86 const { response } = await makePOSTAPRequest(url, body, baseHttpSignature(), headers)
88 expect(response.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
91 it('Should fail with bad keys', async function () {
92 await setKeysOfServer(servers[0], servers[1], invalidKeys.publicKey, invalidKeys.privateKey)
93 await setKeysOfServer(servers[1], servers[1], invalidKeys.publicKey, invalidKeys.privateKey)
95 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]))
96 const headers = buildGlobalHeaders(body)
98 const { response } = await makePOSTAPRequest(url, body, baseHttpSignature(), headers)
100 expect(response.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
103 it('Should reject requests without appropriate signed headers', async function () {
104 await setKeysOfServer(servers[0], servers[1], keys.publicKey, keys.privateKey)
105 await setKeysOfServer(servers[1], servers[1], keys.publicKey, keys.privateKey)
107 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]))
108 const headers = buildGlobalHeaders(body)
110 const signatureOptions = baseHttpSignature()
111 const badHeadersMatrix = [
112 [ '(request-target)', 'date', 'digest' ],
113 [ 'host', 'date', 'digest' ],
114 [ '(request-target)', 'host', 'digest' ]
117 for (const badHeaders of badHeadersMatrix) {
118 signatureOptions.headers = badHeaders
120 const { response } = await makePOSTAPRequest(url, body, signatureOptions, headers)
121 expect(response.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
125 it('Should succeed with a valid HTTP signature', async function () {
126 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]))
127 const headers = buildGlobalHeaders(body)
129 const { response } = await makePOSTAPRequest(url, body, baseHttpSignature(), headers)
131 expect(response.statusCode).to.equal(HttpStatusCode.NO_CONTENT_204)
135 describe('When checking Linked Data Signature', function () {
137 await setKeysOfServer(servers[2], servers[2], keys.publicKey, keys.privateKey)
139 const to = { url: 'http://localhost:' + servers[0].port + '/accounts/peertube' }
140 const by = { url: 'http://localhost:' + servers[2].port + '/accounts/peertube', privateKey: keys.privateKey }
141 await makeFollowRequest(to, by)
144 it('Should fail with bad keys', async function () {
147 await setKeysOfServer(servers[0], servers[2], invalidKeys.publicKey, invalidKeys.privateKey)
148 await setKeysOfServer(servers[2], servers[2], invalidKeys.publicKey, invalidKeys.privateKey)
150 const body = getAnnounceWithoutContext(servers[1])
151 body.actor = 'http://localhost:' + servers[2].port + '/accounts/peertube'
153 const signer: any = { privateKey: invalidKeys.privateKey, url: 'http://localhost:' + servers[2].port + '/accounts/peertube' }
154 const signedBody = await buildSignedActivity(signer, body)
156 const headers = buildGlobalHeaders(signedBody)
158 const { response } = await makePOSTAPRequest(url, signedBody, baseHttpSignature(), headers)
160 expect(response.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
163 it('Should fail with an altered body', async function () {
166 await setKeysOfServer(servers[0], servers[2], keys.publicKey, keys.privateKey)
167 await setKeysOfServer(servers[0], servers[2], keys.publicKey, keys.privateKey)
169 const body = getAnnounceWithoutContext(servers[1])
170 body.actor = 'http://localhost:' + servers[2].port + '/accounts/peertube'
172 const signer: any = { privateKey: keys.privateKey, url: 'http://localhost:' + servers[2].port + '/accounts/peertube' }
173 const signedBody = await buildSignedActivity(signer, body)
175 signedBody.actor = 'http://localhost:' + servers[2].port + '/account/peertube'
177 const headers = buildGlobalHeaders(signedBody)
179 const { response } = await makePOSTAPRequest(url, signedBody, baseHttpSignature(), headers)
181 expect(response.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
184 it('Should succeed with a valid signature', async function () {
187 const body = getAnnounceWithoutContext(servers[1])
188 body.actor = 'http://localhost:' + servers[2].port + '/accounts/peertube'
190 const signer: any = { privateKey: keys.privateKey, url: 'http://localhost:' + servers[2].port + '/accounts/peertube' }
191 const signedBody = await buildSignedActivity(signer, body)
193 const headers = buildGlobalHeaders(signedBody)
195 const { response } = await makePOSTAPRequest(url, signedBody, baseHttpSignature(), headers)
197 expect(response.statusCode).to.equal(HttpStatusCode.NO_CONTENT_204)
201 after(async function () {
204 await cleanupTests(servers)
206 await closeAllSequelize(servers)