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' })
82 const { statusCode } = await makePOSTAPRequest(url, body, baseHttpSignature(), headers)
84 expect(statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
87 it('Should fail with an invalid date', async function () {
88 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]))
89 const headers = buildGlobalHeaders(body)
90 headers['date'] = 'Wed, 21 Oct 2015 07:28:00 GMT'
92 const { statusCode } = await makePOSTAPRequest(url, body, baseHttpSignature(), headers)
94 expect(statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
97 it('Should fail with bad keys', async function () {
98 await setKeysOfServer(servers[0], servers[1], invalidKeys.publicKey, invalidKeys.privateKey)
99 await setKeysOfServer(servers[1], servers[1], invalidKeys.publicKey, invalidKeys.privateKey)
101 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]))
102 const headers = buildGlobalHeaders(body)
104 const { statusCode } = await makePOSTAPRequest(url, body, baseHttpSignature(), headers)
106 expect(statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
109 it('Should reject requests without appropriate signed headers', async function () {
110 await setKeysOfServer(servers[0], servers[1], keys.publicKey, keys.privateKey)
111 await setKeysOfServer(servers[1], servers[1], keys.publicKey, keys.privateKey)
113 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]))
114 const headers = buildGlobalHeaders(body)
116 const signatureOptions = baseHttpSignature()
117 const badHeadersMatrix = [
118 [ '(request-target)', 'date', 'digest' ],
119 [ 'host', 'date', 'digest' ],
120 [ '(request-target)', 'host', 'digest' ]
123 for (const badHeaders of badHeadersMatrix) {
124 signatureOptions.headers = badHeaders
126 const { statusCode } = await makePOSTAPRequest(url, body, signatureOptions, headers)
127 expect(statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
131 it('Should succeed with a valid HTTP signature', async function () {
132 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]))
133 const headers = buildGlobalHeaders(body)
135 const { statusCode } = await makePOSTAPRequest(url, body, baseHttpSignature(), headers)
137 expect(statusCode).to.equal(HttpStatusCode.NO_CONTENT_204)
140 it('Should refresh the actor keys', async function () {
143 // Wait refresh invalidation
146 // Update keys of server 2 to invalid keys
147 // Server 1 should refresh the actor and fail
148 await setKeysOfServer(servers[1], servers[1], invalidKeys.publicKey, invalidKeys.privateKey)
150 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]))
151 const headers = buildGlobalHeaders(body)
153 const { statusCode } = await makePOSTAPRequest(url, body, baseHttpSignature(), headers)
155 expect(statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
159 describe('When checking Linked Data Signature', function () {
160 before(async function () {
163 await setKeysOfServer(servers[0], servers[1], keys.publicKey, keys.privateKey)
164 await setKeysOfServer(servers[1], servers[1], keys.publicKey, keys.privateKey)
165 await setKeysOfServer(servers[2], servers[2], keys.publicKey, keys.privateKey)
167 const to = { url: 'http://localhost:' + servers[0].port + '/accounts/peertube' }
168 const by = { url: 'http://localhost:' + servers[2].port + '/accounts/peertube', privateKey: keys.privateKey }
169 await makeFollowRequest(to, by)
172 it('Should fail with bad keys', async function () {
175 await setKeysOfServer(servers[0], servers[2], invalidKeys.publicKey, invalidKeys.privateKey)
176 await setKeysOfServer(servers[2], servers[2], invalidKeys.publicKey, invalidKeys.privateKey)
178 const body = getAnnounceWithoutContext(servers[1])
179 body.actor = 'http://localhost:' + servers[2].port + '/accounts/peertube'
181 const signer: any = { privateKey: invalidKeys.privateKey, url: 'http://localhost:' + servers[2].port + '/accounts/peertube' }
182 const signedBody = await buildSignedActivity(signer, body)
184 const headers = buildGlobalHeaders(signedBody)
186 const { statusCode } = await makePOSTAPRequest(url, signedBody, baseHttpSignature(), headers)
188 expect(statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
191 it('Should fail with an altered body', async function () {
194 await setKeysOfServer(servers[0], servers[2], keys.publicKey, keys.privateKey)
195 await setKeysOfServer(servers[0], servers[2], keys.publicKey, keys.privateKey)
197 const body = getAnnounceWithoutContext(servers[1])
198 body.actor = 'http://localhost:' + servers[2].port + '/accounts/peertube'
200 const signer: any = { privateKey: keys.privateKey, url: 'http://localhost:' + servers[2].port + '/accounts/peertube' }
201 const signedBody = await buildSignedActivity(signer, body)
203 signedBody.actor = 'http://localhost:' + servers[2].port + '/account/peertube'
205 const headers = buildGlobalHeaders(signedBody)
207 const { statusCode } = await makePOSTAPRequest(url, signedBody, baseHttpSignature(), headers)
209 expect(statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
212 it('Should succeed with a valid signature', async function () {
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 const headers = buildGlobalHeaders(signedBody)
223 const { statusCode } = await makePOSTAPRequest(url, signedBody, baseHttpSignature(), headers)
225 expect(statusCode).to.equal(HttpStatusCode.NO_CONTENT_204)
228 it('Should refresh the actor keys', async function () {
231 // Wait refresh invalidation
234 // Update keys of server 3 to invalid keys
235 // Server 1 should refresh the actor and fail
236 await setKeysOfServer(servers[2], servers[2], invalidKeys.publicKey, invalidKeys.privateKey)
238 const body = getAnnounceWithoutContext(servers[1])
239 body.actor = 'http://localhost:' + servers[2].port + '/accounts/peertube'
241 const signer: any = { privateKey: keys.privateKey, url: 'http://localhost:' + servers[2].port + '/accounts/peertube' }
242 const signedBody = await buildSignedActivity(signer, body)
244 const headers = buildGlobalHeaders(signedBody)
246 const { statusCode } = await makePOSTAPRequest(url, signedBody, baseHttpSignature(), headers)
248 expect(statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
252 after(async function () {
255 await cleanupTests(servers)
257 await closeAllSequelize(servers)