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 { HTTP_SIGNATURE } from '@server/initializers/constants'
7 import { buildSignedActivity } from '@server/lib/activitypub/activity'
8 import { activityPubContextify } from '@server/lib/activitypub/context'
9 import { buildGlobalHeaders } from '@server/lib/job-queue/handlers/utils/activitypub-http-utils'
10 import { makeFollowRequest, makePOSTAPRequest } from '@server/tests/shared'
11 import { buildAbsoluteFixturePath, wait } from '@shared/core-utils'
12 import { HttpStatusCode } from '@shared/models'
13 import { cleanupTests, createMultipleServers, killallServers, PeerTubeServer } from '@shared/server-commands'
15 const expect = chai.expect
17 function setKeysOfServer (onServer: PeerTubeServer, ofServer: PeerTubeServer, publicKey: string, privateKey: string) {
18 const url = 'http://localhost:' + ofServer.port + '/accounts/peertube'
21 onServer.sql.setActorField(url, 'publicKey', publicKey),
22 onServer.sql.setActorField(url, 'privateKey', privateKey)
26 function setUpdatedAtOfServer (onServer: PeerTubeServer, ofServer: PeerTubeServer, updatedAt: string) {
27 const url = 'http://localhost:' + ofServer.port + '/accounts/peertube'
30 onServer.sql.setActorField(url, 'createdAt', updatedAt),
31 onServer.sql.setActorField(url, 'updatedAt', updatedAt)
35 function getAnnounceWithoutContext (server: PeerTubeServer) {
36 const json = require(buildAbsoluteFixturePath('./ap-json/peertube/announce-without-context.json'))
37 const result: typeof json = {}
39 for (const key of Object.keys(json)) {
40 if (Array.isArray(json[key])) {
41 result[key] = json[key].map(v => v.replace(':9002', `:${server.port}`))
43 result[key] = json[key].replace(':9002', `:${server.port}`)
50 describe('Test ActivityPub security', function () {
51 let servers: PeerTubeServer[]
54 const keys = require(buildAbsoluteFixturePath('./ap-json/peertube/keys.json'))
55 const invalidKeys = require(buildAbsoluteFixturePath('./ap-json/peertube/invalid-keys.json'))
56 const baseHttpSignature = () => ({
57 algorithm: HTTP_SIGNATURE.ALGORITHM,
58 authorizationHeaderName: HTTP_SIGNATURE.HEADER_NAME,
59 keyId: 'acct:peertube@localhost:' + servers[1].port,
61 headers: HTTP_SIGNATURE.HEADERS_TO_SIGN
64 // ---------------------------------------------------------------
66 before(async function () {
69 servers = await createMultipleServers(3)
71 url = servers[0].url + '/inbox'
73 await setKeysOfServer(servers[0], servers[1], keys.publicKey, null)
74 await setKeysOfServer(servers[1], servers[1], keys.publicKey, keys.privateKey)
76 const to = { url: 'http://localhost:' + servers[0].port + '/accounts/peertube' }
77 const by = { url: 'http://localhost:' + servers[1].port + '/accounts/peertube', privateKey: keys.privateKey }
78 await makeFollowRequest(to, by)
81 describe('When checking HTTP signature', function () {
83 it('Should fail with an invalid digest', async function () {
84 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]))
86 Digest: buildDigest({ hello: 'coucou' })
90 await makePOSTAPRequest(url, body, baseHttpSignature(), headers)
91 expect(true, 'Did not throw').to.be.false
93 expect(err.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
97 it('Should fail with an invalid date', async function () {
98 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]))
99 const headers = buildGlobalHeaders(body)
100 headers['date'] = 'Wed, 21 Oct 2015 07:28:00 GMT'
103 await makePOSTAPRequest(url, body, baseHttpSignature(), headers)
104 expect(true, 'Did not throw').to.be.false
106 expect(err.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
110 it('Should fail with bad keys', async function () {
111 await setKeysOfServer(servers[0], servers[1], invalidKeys.publicKey, invalidKeys.privateKey)
112 await setKeysOfServer(servers[1], servers[1], invalidKeys.publicKey, invalidKeys.privateKey)
114 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]))
115 const headers = buildGlobalHeaders(body)
118 await makePOSTAPRequest(url, body, baseHttpSignature(), headers)
119 expect(true, 'Did not throw').to.be.false
121 expect(err.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
125 it('Should reject requests without appropriate signed headers', async function () {
126 await setKeysOfServer(servers[0], servers[1], keys.publicKey, keys.privateKey)
127 await setKeysOfServer(servers[1], servers[1], keys.publicKey, keys.privateKey)
129 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]))
130 const headers = buildGlobalHeaders(body)
132 const signatureOptions = baseHttpSignature()
133 const badHeadersMatrix = [
134 [ '(request-target)', 'date', 'digest' ],
135 [ 'host', 'date', 'digest' ],
136 [ '(request-target)', 'host', 'digest' ]
139 for (const badHeaders of badHeadersMatrix) {
140 signatureOptions.headers = badHeaders
143 await makePOSTAPRequest(url, body, signatureOptions, headers)
144 expect(true, 'Did not throw').to.be.false
146 expect(err.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
151 it('Should succeed with a valid HTTP signature', async function () {
152 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]))
153 const headers = buildGlobalHeaders(body)
155 const { statusCode } = await makePOSTAPRequest(url, body, baseHttpSignature(), headers)
156 expect(statusCode).to.equal(HttpStatusCode.NO_CONTENT_204)
159 it('Should refresh the actor keys', async function () {
162 // Update keys of server 2 to invalid keys
163 // Server 1 should refresh the actor and fail
164 await setKeysOfServer(servers[1], servers[1], invalidKeys.publicKey, invalidKeys.privateKey)
165 await setUpdatedAtOfServer(servers[0], servers[1], '2015-07-17 22:00:00+00')
167 // Invalid peertube actor cache
168 await killallServers([ servers[1] ])
169 await servers[1].run()
171 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]))
172 const headers = buildGlobalHeaders(body)
175 await makePOSTAPRequest(url, body, baseHttpSignature(), headers)
176 expect(true, 'Did not throw').to.be.false
179 expect(err.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
184 describe('When checking Linked Data Signature', function () {
185 before(async function () {
188 await setKeysOfServer(servers[0], servers[1], keys.publicKey, keys.privateKey)
189 await setKeysOfServer(servers[1], servers[1], keys.publicKey, keys.privateKey)
190 await setKeysOfServer(servers[2], servers[2], keys.publicKey, keys.privateKey)
192 const to = { url: 'http://localhost:' + servers[0].port + '/accounts/peertube' }
193 const by = { url: 'http://localhost:' + servers[2].port + '/accounts/peertube', privateKey: keys.privateKey }
194 await makeFollowRequest(to, by)
197 it('Should fail with bad keys', async function () {
200 await setKeysOfServer(servers[0], servers[2], invalidKeys.publicKey, invalidKeys.privateKey)
201 await setKeysOfServer(servers[2], servers[2], invalidKeys.publicKey, invalidKeys.privateKey)
203 const body = getAnnounceWithoutContext(servers[1])
204 body.actor = 'http://localhost:' + servers[2].port + '/accounts/peertube'
206 const signer: any = { privateKey: invalidKeys.privateKey, url: 'http://localhost:' + servers[2].port + '/accounts/peertube' }
207 const signedBody = await buildSignedActivity(signer, body)
209 const headers = buildGlobalHeaders(signedBody)
212 await makePOSTAPRequest(url, signedBody, baseHttpSignature(), headers)
213 expect(true, 'Did not throw').to.be.false
215 expect(err.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
219 it('Should fail with an altered body', async function () {
222 await setKeysOfServer(servers[0], servers[2], keys.publicKey, keys.privateKey)
223 await setKeysOfServer(servers[0], servers[2], keys.publicKey, keys.privateKey)
225 const body = getAnnounceWithoutContext(servers[1])
226 body.actor = 'http://localhost:' + servers[2].port + '/accounts/peertube'
228 const signer: any = { privateKey: keys.privateKey, url: 'http://localhost:' + servers[2].port + '/accounts/peertube' }
229 const signedBody = await buildSignedActivity(signer, body)
231 signedBody.actor = 'http://localhost:' + servers[2].port + '/account/peertube'
233 const headers = buildGlobalHeaders(signedBody)
236 await makePOSTAPRequest(url, signedBody, baseHttpSignature(), headers)
237 expect(true, 'Did not throw').to.be.false
239 expect(err.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
243 it('Should succeed with a valid signature', async function () {
246 const body = getAnnounceWithoutContext(servers[1])
247 body.actor = 'http://localhost:' + servers[2].port + '/accounts/peertube'
249 const signer: any = { privateKey: keys.privateKey, url: 'http://localhost:' + servers[2].port + '/accounts/peertube' }
250 const signedBody = await buildSignedActivity(signer, body)
252 const headers = buildGlobalHeaders(signedBody)
254 const { statusCode } = await makePOSTAPRequest(url, signedBody, baseHttpSignature(), headers)
255 expect(statusCode).to.equal(HttpStatusCode.NO_CONTENT_204)
258 it('Should refresh the actor keys', async function () {
261 // Wait refresh invalidation
264 // Update keys of server 3 to invalid keys
265 // Server 1 should refresh the actor and fail
266 await setKeysOfServer(servers[2], servers[2], invalidKeys.publicKey, invalidKeys.privateKey)
268 const body = getAnnounceWithoutContext(servers[1])
269 body.actor = 'http://localhost:' + servers[2].port + '/accounts/peertube'
271 const signer: any = { privateKey: keys.privateKey, url: 'http://localhost:' + servers[2].port + '/accounts/peertube' }
272 const signedBody = await buildSignedActivity(signer, body)
274 const headers = buildGlobalHeaders(signedBody)
277 await makePOSTAPRequest(url, signedBody, baseHttpSignature(), headers)
278 expect(true, 'Did not throw').to.be.false
280 expect(err.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
285 after(async function () {
288 await cleanupTests(servers)