1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import { expect } 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 function setKeysOfServer (onServer: PeerTubeServer, ofServer: PeerTubeServer, publicKey: string, privateKey: string) {
14 const url = 'http://localhost:' + ofServer.port + '/accounts/peertube'
17 onServer.sql.setActorField(url, 'publicKey', publicKey),
18 onServer.sql.setActorField(url, 'privateKey', privateKey)
22 function setUpdatedAtOfServer (onServer: PeerTubeServer, ofServer: PeerTubeServer, updatedAt: string) {
23 const url = 'http://localhost:' + ofServer.port + '/accounts/peertube'
26 onServer.sql.setActorField(url, 'createdAt', updatedAt),
27 onServer.sql.setActorField(url, 'updatedAt', updatedAt)
31 function getAnnounceWithoutContext (server: PeerTubeServer) {
32 const json = require(buildAbsoluteFixturePath('./ap-json/peertube/announce-without-context.json'))
33 const result: typeof json = {}
35 for (const key of Object.keys(json)) {
36 if (Array.isArray(json[key])) {
37 result[key] = json[key].map(v => v.replace(':9002', `:${server.port}`))
39 result[key] = json[key].replace(':9002', `:${server.port}`)
46 describe('Test ActivityPub security', function () {
47 let servers: PeerTubeServer[]
50 const keys = require(buildAbsoluteFixturePath('./ap-json/peertube/keys.json'))
51 const invalidKeys = require(buildAbsoluteFixturePath('./ap-json/peertube/invalid-keys.json'))
52 const baseHttpSignature = () => ({
53 algorithm: HTTP_SIGNATURE.ALGORITHM,
54 authorizationHeaderName: HTTP_SIGNATURE.HEADER_NAME,
55 keyId: 'acct:peertube@localhost:' + servers[1].port,
57 headers: HTTP_SIGNATURE.HEADERS_TO_SIGN
60 // ---------------------------------------------------------------
62 before(async function () {
65 servers = await createMultipleServers(3)
67 url = servers[0].url + '/inbox'
69 await setKeysOfServer(servers[0], servers[1], keys.publicKey, null)
70 await setKeysOfServer(servers[1], servers[1], keys.publicKey, keys.privateKey)
72 const to = { url: 'http://localhost:' + servers[0].port + '/accounts/peertube' }
73 const by = { url: 'http://localhost:' + servers[1].port + '/accounts/peertube', privateKey: keys.privateKey }
74 await makeFollowRequest(to, by)
77 describe('When checking HTTP signature', function () {
79 it('Should fail with an invalid digest', async function () {
80 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]), 'Announce')
82 Digest: buildDigest({ hello: 'coucou' })
86 await makePOSTAPRequest(url, body, baseHttpSignature(), headers)
87 expect(true, 'Did not throw').to.be.false
89 expect(err.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
93 it('Should fail with an invalid date', async function () {
94 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]), 'Announce')
95 const headers = buildGlobalHeaders(body)
96 headers['date'] = 'Wed, 21 Oct 2015 07:28:00 GMT'
99 await makePOSTAPRequest(url, body, baseHttpSignature(), headers)
100 expect(true, 'Did not throw').to.be.false
102 expect(err.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
106 it('Should fail with bad keys', async function () {
107 await setKeysOfServer(servers[0], servers[1], invalidKeys.publicKey, invalidKeys.privateKey)
108 await setKeysOfServer(servers[1], servers[1], invalidKeys.publicKey, invalidKeys.privateKey)
110 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]), 'Announce')
111 const headers = buildGlobalHeaders(body)
114 await makePOSTAPRequest(url, body, baseHttpSignature(), headers)
115 expect(true, 'Did not throw').to.be.false
117 expect(err.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
121 it('Should reject requests without appropriate signed headers', async function () {
122 await setKeysOfServer(servers[0], servers[1], keys.publicKey, keys.privateKey)
123 await setKeysOfServer(servers[1], servers[1], keys.publicKey, keys.privateKey)
125 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]), 'Announce')
126 const headers = buildGlobalHeaders(body)
128 const signatureOptions = baseHttpSignature()
129 const badHeadersMatrix = [
130 [ '(request-target)', 'date', 'digest' ],
131 [ 'host', 'date', 'digest' ],
132 [ '(request-target)', 'host', 'digest' ]
135 for (const badHeaders of badHeadersMatrix) {
136 signatureOptions.headers = badHeaders
139 await makePOSTAPRequest(url, body, signatureOptions, headers)
140 expect(true, 'Did not throw').to.be.false
142 expect(err.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
147 it('Should succeed with a valid HTTP signature draft 11 (without date but with (created))', async function () {
148 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]), 'Announce')
149 const headers = buildGlobalHeaders(body)
151 const signatureOptions = baseHttpSignature()
152 signatureOptions.headers = [ '(request-target)', '(created)', 'host', 'digest' ]
154 const { statusCode } = await makePOSTAPRequest(url, body, signatureOptions, headers)
155 expect(statusCode).to.equal(HttpStatusCode.NO_CONTENT_204)
158 it('Should succeed with a valid HTTP signature', async function () {
159 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]), 'Announce')
160 const headers = buildGlobalHeaders(body)
162 const { statusCode } = await makePOSTAPRequest(url, body, baseHttpSignature(), headers)
163 expect(statusCode).to.equal(HttpStatusCode.NO_CONTENT_204)
166 it('Should refresh the actor keys', async function () {
169 // Update keys of server 2 to invalid keys
170 // Server 1 should refresh the actor and fail
171 await setKeysOfServer(servers[1], servers[1], invalidKeys.publicKey, invalidKeys.privateKey)
172 await setUpdatedAtOfServer(servers[0], servers[1], '2015-07-17 22:00:00+00')
174 // Invalid peertube actor cache
175 await killallServers([ servers[1] ])
176 await servers[1].run()
178 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]), 'Announce')
179 const headers = buildGlobalHeaders(body)
182 await makePOSTAPRequest(url, body, baseHttpSignature(), headers)
183 expect(true, 'Did not throw').to.be.false
186 expect(err.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
191 describe('When checking Linked Data Signature', function () {
192 before(async function () {
195 await setKeysOfServer(servers[0], servers[1], keys.publicKey, keys.privateKey)
196 await setKeysOfServer(servers[1], servers[1], keys.publicKey, keys.privateKey)
197 await setKeysOfServer(servers[2], servers[2], keys.publicKey, keys.privateKey)
199 const to = { url: 'http://localhost:' + servers[0].port + '/accounts/peertube' }
200 const by = { url: 'http://localhost:' + servers[2].port + '/accounts/peertube', privateKey: keys.privateKey }
201 await makeFollowRequest(to, by)
204 it('Should fail with bad keys', async function () {
207 await setKeysOfServer(servers[0], servers[2], invalidKeys.publicKey, invalidKeys.privateKey)
208 await setKeysOfServer(servers[2], servers[2], invalidKeys.publicKey, invalidKeys.privateKey)
210 const body = getAnnounceWithoutContext(servers[1])
211 body.actor = 'http://localhost:' + servers[2].port + '/accounts/peertube'
213 const signer: any = { privateKey: invalidKeys.privateKey, url: 'http://localhost:' + servers[2].port + '/accounts/peertube' }
214 const signedBody = await signAndContextify(signer, body, 'Announce')
216 const headers = buildGlobalHeaders(signedBody)
219 await makePOSTAPRequest(url, signedBody, baseHttpSignature(), headers)
220 expect(true, 'Did not throw').to.be.false
222 expect(err.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
226 it('Should fail with an altered body', async function () {
229 await setKeysOfServer(servers[0], servers[2], keys.publicKey, keys.privateKey)
230 await setKeysOfServer(servers[0], servers[2], keys.publicKey, keys.privateKey)
232 const body = getAnnounceWithoutContext(servers[1])
233 body.actor = 'http://localhost:' + servers[2].port + '/accounts/peertube'
235 const signer: any = { privateKey: keys.privateKey, url: 'http://localhost:' + servers[2].port + '/accounts/peertube' }
236 const signedBody = await signAndContextify(signer, body, 'Announce')
238 signedBody.actor = 'http://localhost:' + servers[2].port + '/account/peertube'
240 const headers = buildGlobalHeaders(signedBody)
243 await makePOSTAPRequest(url, signedBody, baseHttpSignature(), headers)
244 expect(true, 'Did not throw').to.be.false
246 expect(err.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
250 it('Should succeed with a valid signature', async function () {
253 const body = getAnnounceWithoutContext(servers[1])
254 body.actor = 'http://localhost:' + servers[2].port + '/accounts/peertube'
256 const signer: any = { privateKey: keys.privateKey, url: 'http://localhost:' + servers[2].port + '/accounts/peertube' }
257 const signedBody = await signAndContextify(signer, body, 'Announce')
259 const headers = buildGlobalHeaders(signedBody)
261 const { statusCode } = await makePOSTAPRequest(url, signedBody, baseHttpSignature(), headers)
262 expect(statusCode).to.equal(HttpStatusCode.NO_CONTENT_204)
265 it('Should refresh the actor keys', async function () {
268 // Wait refresh invalidation
271 // Update keys of server 3 to invalid keys
272 // Server 1 should refresh the actor and fail
273 await setKeysOfServer(servers[2], servers[2], invalidKeys.publicKey, invalidKeys.privateKey)
275 const body = getAnnounceWithoutContext(servers[1])
276 body.actor = 'http://localhost:' + servers[2].port + '/accounts/peertube'
278 const signer: any = { privateKey: keys.privateKey, url: 'http://localhost:' + servers[2].port + '/accounts/peertube' }
279 const signedBody = await signAndContextify(signer, body, 'Announce')
281 const headers = buildGlobalHeaders(signedBody)
284 await makePOSTAPRequest(url, signedBody, baseHttpSignature(), headers)
285 expect(true, 'Did not throw').to.be.false
287 expect(err.statusCode).to.equal(HttpStatusCode.FORBIDDEN_403)
292 after(async function () {
295 await cleanupTests(servers)