1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import * as chai from 'chai'
4 import { buildDigest } from '@server/helpers/peertube-crypto'
5 import { HTTP_SIGNATURE } from '@server/initializers/constants'
6 import { activityPubContextify } from '@server/lib/activitypub/context'
7 import { buildGlobalHeaders, signAndContextify } from '@server/lib/activitypub/send'
8 import { makeFollowRequest, makePOSTAPRequest } from '@server/tests/shared'
9 import { buildAbsoluteFixturePath, wait } from '@shared/core-utils'
10 import { HttpStatusCode } from '@shared/models'
11 import { cleanupTests, createMultipleServers, killallServers, PeerTubeServer } from '@shared/server-commands'
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]), 'Announce')
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]), 'Announce')
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]), 'Announce')
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]), 'Announce')
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 draft 11 (without date but with (created))', async function () {
150 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]), 'Announce')
151 const headers = buildGlobalHeaders(body)
153 const signatureOptions = baseHttpSignature()
154 signatureOptions.headers = [ '(request-target)', '(created)', 'host', 'digest' ]
156 const { statusCode } = await makePOSTAPRequest(url, body, signatureOptions, headers)
157 expect(statusCode).to.equal(HttpStatusCode.NO_CONTENT_204)
160 it('Should succeed with a valid HTTP signature', async function () {
161 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]), 'Announce')
162 const headers = buildGlobalHeaders(body)
164 const { statusCode } = await makePOSTAPRequest(url, body, baseHttpSignature(), headers)
165 expect(statusCode).to.equal(HttpStatusCode.NO_CONTENT_204)
168 it('Should refresh the actor keys', async function () {
171 // Update keys of server 2 to invalid keys
172 // Server 1 should refresh the actor and fail
173 await setKeysOfServer(servers[1], servers[1], invalidKeys.publicKey, invalidKeys.privateKey)
174 await setUpdatedAtOfServer(servers[0], servers[1], '2015-07-17 22:00:00+00')
176 // Invalid peertube actor cache
177 await killallServers([ servers[1] ])
178 await servers[1].run()
180 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]), 'Announce')
181 const headers = buildGlobalHeaders(body)
184 await makePOSTAPRequest(url, body, baseHttpSignature(), headers)
185 expect(true, 'Did not throw').to.be.false
188 expect(err.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
193 describe('When checking Linked Data Signature', function () {
194 before(async function () {
197 await setKeysOfServer(servers[0], servers[1], keys.publicKey, keys.privateKey)
198 await setKeysOfServer(servers[1], servers[1], keys.publicKey, keys.privateKey)
199 await setKeysOfServer(servers[2], servers[2], keys.publicKey, keys.privateKey)
201 const to = { url: 'http://localhost:' + servers[0].port + '/accounts/peertube' }
202 const by = { url: 'http://localhost:' + servers[2].port + '/accounts/peertube', privateKey: keys.privateKey }
203 await makeFollowRequest(to, by)
206 it('Should fail with bad keys', async function () {
209 await setKeysOfServer(servers[0], servers[2], invalidKeys.publicKey, invalidKeys.privateKey)
210 await setKeysOfServer(servers[2], servers[2], invalidKeys.publicKey, invalidKeys.privateKey)
212 const body = getAnnounceWithoutContext(servers[1])
213 body.actor = 'http://localhost:' + servers[2].port + '/accounts/peertube'
215 const signer: any = { privateKey: invalidKeys.privateKey, url: 'http://localhost:' + servers[2].port + '/accounts/peertube' }
216 const signedBody = await signAndContextify(signer, body, 'Announce')
218 const headers = buildGlobalHeaders(signedBody)
221 await makePOSTAPRequest(url, signedBody, baseHttpSignature(), headers)
222 expect(true, 'Did not throw').to.be.false
224 expect(err.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
228 it('Should fail with an altered body', async function () {
231 await setKeysOfServer(servers[0], servers[2], keys.publicKey, keys.privateKey)
232 await setKeysOfServer(servers[0], servers[2], keys.publicKey, keys.privateKey)
234 const body = getAnnounceWithoutContext(servers[1])
235 body.actor = 'http://localhost:' + servers[2].port + '/accounts/peertube'
237 const signer: any = { privateKey: keys.privateKey, url: 'http://localhost:' + servers[2].port + '/accounts/peertube' }
238 const signedBody = await signAndContextify(signer, body, 'Announce')
240 signedBody.actor = 'http://localhost:' + servers[2].port + '/account/peertube'
242 const headers = buildGlobalHeaders(signedBody)
245 await makePOSTAPRequest(url, signedBody, baseHttpSignature(), headers)
246 expect(true, 'Did not throw').to.be.false
248 expect(err.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
252 it('Should succeed with a valid signature', async function () {
255 const body = getAnnounceWithoutContext(servers[1])
256 body.actor = 'http://localhost:' + servers[2].port + '/accounts/peertube'
258 const signer: any = { privateKey: keys.privateKey, url: 'http://localhost:' + servers[2].port + '/accounts/peertube' }
259 const signedBody = await signAndContextify(signer, body, 'Announce')
261 const headers = buildGlobalHeaders(signedBody)
263 const { statusCode } = await makePOSTAPRequest(url, signedBody, baseHttpSignature(), headers)
264 expect(statusCode).to.equal(HttpStatusCode.NO_CONTENT_204)
267 it('Should refresh the actor keys', async function () {
270 // Wait refresh invalidation
273 // Update keys of server 3 to invalid keys
274 // Server 1 should refresh the actor and fail
275 await setKeysOfServer(servers[2], servers[2], invalidKeys.publicKey, invalidKeys.privateKey)
277 const body = getAnnounceWithoutContext(servers[1])
278 body.actor = 'http://localhost:' + servers[2].port + '/accounts/peertube'
280 const signer: any = { privateKey: keys.privateKey, url: 'http://localhost:' + servers[2].port + '/accounts/peertube' }
281 const signedBody = await signAndContextify(signer, body, 'Announce')
283 const headers = buildGlobalHeaders(signedBody)
286 await makePOSTAPRequest(url, signedBody, baseHttpSignature(), headers)
287 expect(true, 'Did not throw').to.be.false
289 expect(err.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
294 after(async function () {
297 await cleanupTests(servers)