1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { activityPubContextify, buildSignedActivity } from '@server/helpers/activitypub'
6 import { buildDigest } from '@server/helpers/peertube-crypto'
7 import { HTTP_SIGNATURE } from '@server/initializers/constants'
8 import { buildGlobalHeaders } from '@server/lib/job-queue/handlers/utils/activitypub-http-utils'
9 import { buildAbsoluteFixturePath, cleanupTests, createMultipleServers, killallServers, PeerTubeServer, wait } from '@shared/extra-utils'
10 import { makeFollowRequest, makePOSTAPRequest } from '@shared/extra-utils/requests/activitypub'
11 import { HttpStatusCode } from '@shared/models'
13 const expect = chai.expect
15 function setKeysOfServer (onServer: PeerTubeServer, ofServer: PeerTubeServer, publicKey: string, privateKey: string) {
16 const url = 'http://localhost:' + ofServer.port + '/accounts/peertube'
19 onServer.sql.setActorField(url, 'publicKey', publicKey),
20 onServer.sql.setActorField(url, 'privateKey', privateKey)
24 function setUpdatedAtOfServer (onServer: PeerTubeServer, ofServer: PeerTubeServer, updatedAt: string) {
25 const url = 'http://localhost:' + ofServer.port + '/accounts/peertube'
28 onServer.sql.setActorField(url, 'createdAt', updatedAt),
29 onServer.sql.setActorField(url, 'updatedAt', updatedAt)
33 function getAnnounceWithoutContext (server: PeerTubeServer) {
34 const json = require(buildAbsoluteFixturePath('./ap-json/peertube/announce-without-context.json'))
35 const result: typeof json = {}
37 for (const key of Object.keys(json)) {
38 if (Array.isArray(json[key])) {
39 result[key] = json[key].map(v => v.replace(':9002', `:${server.port}`))
41 result[key] = json[key].replace(':9002', `:${server.port}`)
48 describe('Test ActivityPub security', function () {
49 let servers: PeerTubeServer[]
52 const keys = require(buildAbsoluteFixturePath('./ap-json/peertube/keys.json'))
53 const invalidKeys = require(buildAbsoluteFixturePath('./ap-json/peertube/invalid-keys.json'))
54 const baseHttpSignature = () => ({
55 algorithm: HTTP_SIGNATURE.ALGORITHM,
56 authorizationHeaderName: HTTP_SIGNATURE.HEADER_NAME,
57 keyId: 'acct:peertube@localhost:' + servers[1].port,
59 headers: HTTP_SIGNATURE.HEADERS_TO_SIGN
62 // ---------------------------------------------------------------
64 before(async function () {
67 servers = await createMultipleServers(3)
69 url = servers[0].url + '/inbox'
71 await setKeysOfServer(servers[0], servers[1], keys.publicKey, null)
72 await setKeysOfServer(servers[1], servers[1], keys.publicKey, keys.privateKey)
74 const to = { url: 'http://localhost:' + servers[0].port + '/accounts/peertube' }
75 const by = { url: 'http://localhost:' + servers[1].port + '/accounts/peertube', privateKey: keys.privateKey }
76 await makeFollowRequest(to, by)
79 describe('When checking HTTP signature', function () {
81 it('Should fail with an invalid digest', async function () {
82 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]))
84 Digest: buildDigest({ hello: 'coucou' })
88 await makePOSTAPRequest(url, body, baseHttpSignature(), headers)
89 expect(true, 'Did not throw').to.be.false
91 expect(err.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
95 it('Should fail with an invalid date', async function () {
96 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]))
97 const headers = buildGlobalHeaders(body)
98 headers['date'] = 'Wed, 21 Oct 2015 07:28:00 GMT'
101 await makePOSTAPRequest(url, body, baseHttpSignature(), headers)
102 expect(true, 'Did not throw').to.be.false
104 expect(err.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
108 it('Should fail with bad keys', async function () {
109 await setKeysOfServer(servers[0], servers[1], invalidKeys.publicKey, invalidKeys.privateKey)
110 await setKeysOfServer(servers[1], servers[1], invalidKeys.publicKey, invalidKeys.privateKey)
112 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]))
113 const headers = buildGlobalHeaders(body)
116 await makePOSTAPRequest(url, body, baseHttpSignature(), headers)
117 expect(true, 'Did not throw').to.be.false
119 expect(err.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
123 it('Should reject requests without appropriate signed headers', async function () {
124 await setKeysOfServer(servers[0], servers[1], keys.publicKey, keys.privateKey)
125 await setKeysOfServer(servers[1], servers[1], keys.publicKey, keys.privateKey)
127 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]))
128 const headers = buildGlobalHeaders(body)
130 const signatureOptions = baseHttpSignature()
131 const badHeadersMatrix = [
132 [ '(request-target)', 'date', 'digest' ],
133 [ 'host', 'date', 'digest' ],
134 [ '(request-target)', 'host', 'digest' ]
137 for (const badHeaders of badHeadersMatrix) {
138 signatureOptions.headers = badHeaders
141 await makePOSTAPRequest(url, body, signatureOptions, headers)
142 expect(true, 'Did not throw').to.be.false
144 expect(err.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
149 it('Should succeed with a valid HTTP signature', async function () {
150 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]))
151 const headers = buildGlobalHeaders(body)
153 const { statusCode } = await makePOSTAPRequest(url, body, baseHttpSignature(), headers)
154 expect(statusCode).to.equal(HttpStatusCode.NO_CONTENT_204)
157 it('Should refresh the actor keys', async function () {
160 // Update keys of server 2 to invalid keys
161 // Server 1 should refresh the actor and fail
162 await setKeysOfServer(servers[1], servers[1], invalidKeys.publicKey, invalidKeys.privateKey)
163 await setUpdatedAtOfServer(servers[0], servers[1], '2015-07-17 22:00:00+00')
165 // Invalid peertube actor cache
166 await killallServers([ servers[1] ])
167 await servers[1].run()
169 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]))
170 const headers = buildGlobalHeaders(body)
173 await makePOSTAPRequest(url, body, baseHttpSignature(), headers)
174 expect(true, 'Did not throw').to.be.false
177 expect(err.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
182 describe('When checking Linked Data Signature', function () {
183 before(async function () {
186 await setKeysOfServer(servers[0], servers[1], keys.publicKey, keys.privateKey)
187 await setKeysOfServer(servers[1], servers[1], keys.publicKey, keys.privateKey)
188 await setKeysOfServer(servers[2], servers[2], keys.publicKey, keys.privateKey)
190 const to = { url: 'http://localhost:' + servers[0].port + '/accounts/peertube' }
191 const by = { url: 'http://localhost:' + servers[2].port + '/accounts/peertube', privateKey: keys.privateKey }
192 await makeFollowRequest(to, by)
195 it('Should fail with bad keys', async function () {
198 await setKeysOfServer(servers[0], servers[2], invalidKeys.publicKey, invalidKeys.privateKey)
199 await setKeysOfServer(servers[2], servers[2], invalidKeys.publicKey, invalidKeys.privateKey)
201 const body = getAnnounceWithoutContext(servers[1])
202 body.actor = 'http://localhost:' + servers[2].port + '/accounts/peertube'
204 const signer: any = { privateKey: invalidKeys.privateKey, url: 'http://localhost:' + servers[2].port + '/accounts/peertube' }
205 const signedBody = await buildSignedActivity(signer, body)
207 const headers = buildGlobalHeaders(signedBody)
210 await makePOSTAPRequest(url, signedBody, baseHttpSignature(), headers)
211 expect(true, 'Did not throw').to.be.false
213 expect(err.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
217 it('Should fail with an altered body', async function () {
220 await setKeysOfServer(servers[0], servers[2], keys.publicKey, keys.privateKey)
221 await setKeysOfServer(servers[0], servers[2], keys.publicKey, keys.privateKey)
223 const body = getAnnounceWithoutContext(servers[1])
224 body.actor = 'http://localhost:' + servers[2].port + '/accounts/peertube'
226 const signer: any = { privateKey: keys.privateKey, url: 'http://localhost:' + servers[2].port + '/accounts/peertube' }
227 const signedBody = await buildSignedActivity(signer, body)
229 signedBody.actor = 'http://localhost:' + servers[2].port + '/account/peertube'
231 const headers = buildGlobalHeaders(signedBody)
234 await makePOSTAPRequest(url, signedBody, baseHttpSignature(), headers)
235 expect(true, 'Did not throw').to.be.false
237 expect(err.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
241 it('Should succeed with a valid signature', async function () {
244 const body = getAnnounceWithoutContext(servers[1])
245 body.actor = 'http://localhost:' + servers[2].port + '/accounts/peertube'
247 const signer: any = { privateKey: keys.privateKey, url: 'http://localhost:' + servers[2].port + '/accounts/peertube' }
248 const signedBody = await buildSignedActivity(signer, body)
250 const headers = buildGlobalHeaders(signedBody)
252 const { statusCode } = await makePOSTAPRequest(url, signedBody, baseHttpSignature(), headers)
253 expect(statusCode).to.equal(HttpStatusCode.NO_CONTENT_204)
256 it('Should refresh the actor keys', async function () {
259 // Wait refresh invalidation
262 // Update keys of server 3 to invalid keys
263 // Server 1 should refresh the actor and fail
264 await setKeysOfServer(servers[2], servers[2], invalidKeys.publicKey, invalidKeys.privateKey)
266 const body = getAnnounceWithoutContext(servers[1])
267 body.actor = 'http://localhost:' + servers[2].port + '/accounts/peertube'
269 const signer: any = { privateKey: keys.privateKey, url: 'http://localhost:' + servers[2].port + '/accounts/peertube' }
270 const signedBody = await buildSignedActivity(signer, body)
272 const headers = buildGlobalHeaders(signedBody)
275 await makePOSTAPRequest(url, signedBody, baseHttpSignature(), headers)
276 expect(true, 'Did not throw').to.be.false
278 expect(err.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
283 after(async function () {
286 await cleanupTests(servers)