]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/activitypub/security.ts
Add ability to sort by originallyPublishedAt
[github/Chocobozzz/PeerTube.git] / server / tests / api / activitypub / security.ts
CommitLineData
a1587156 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
df66d815
C
2
3import 'mocha'
4
a1587156 5import { cleanupTests, closeAllSequelize, flushAndRunMultipleServers, ServerInfo, setActorField } from '../../../../shared/extra-utils'
74dc3bca 6import { HTTP_SIGNATURE } from '../../../initializers/constants'
df66d815
C
7import { buildDigest, buildGlobalHeaders } from '../../../lib/job-queue/handlers/utils/activitypub-http-utils'
8import * as chai from 'chai'
df66d815 9import { activityPubContextify, buildSignedActivity } from '../../../helpers/activitypub'
94565d52 10import { makeFollowRequest, makePOSTAPRequest } from '../../../../shared/extra-utils/requests/activitypub'
df66d815
C
11
12const expect = chai.expect
13
48f07b4a 14function setKeysOfServer (onServer: ServerInfo, ofServer: ServerInfo, publicKey: string, privateKey: string) {
df66d815 15 return Promise.all([
48f07b4a
C
16 setActorField(onServer.internalServerNumber, 'http://localhost:' + ofServer.port + '/accounts/peertube', 'publicKey', publicKey),
17 setActorField(onServer.internalServerNumber, 'http://localhost:' + ofServer.port + '/accounts/peertube', 'privateKey', privateKey)
df66d815
C
18 ])
19}
20
48f07b4a
C
21function getAnnounceWithoutContext (server2: ServerInfo) {
22 const json = require('./json/peertube/announce-without-context.json')
23 const result: typeof json = {}
24
25 for (const key of Object.keys(json)) {
26 if (Array.isArray(json[key])) {
27 result[key] = json[key].map(v => v.replace(':9002', `:${server2.port}`))
28 } else {
a1587156 29 result[key] = json[key].replace(':9002', `:${server2.port}`)
48f07b4a
C
30 }
31 }
32
33 return result
df66d815
C
34}
35
36describe('Test ActivityPub security', function () {
37 let servers: ServerInfo[]
38 let url: string
39
40 const keys = require('./json/peertube/keys.json')
41 const invalidKeys = require('./json/peertube/invalid-keys.json')
48f07b4a 42 const baseHttpSignature = () => ({
df66d815
C
43 algorithm: HTTP_SIGNATURE.ALGORITHM,
44 authorizationHeaderName: HTTP_SIGNATURE.HEADER_NAME,
48f07b4a 45 keyId: 'acct:peertube@localhost:' + servers[1].port,
df66d815
C
46 key: keys.privateKey,
47 headers: HTTP_SIGNATURE.HEADERS_TO_SIGN
48f07b4a 48 })
df66d815
C
49
50 // ---------------------------------------------------------------
51
52 before(async function () {
53 this.timeout(60000)
54
55 servers = await flushAndRunMultipleServers(3)
56
57 url = servers[0].url + '/inbox'
58
48f07b4a 59 await setKeysOfServer(servers[0], servers[1], keys.publicKey, keys.privateKey)
df66d815 60
48f07b4a
C
61 const to = { url: 'http://localhost:' + servers[0].port + '/accounts/peertube' }
62 const by = { url: 'http://localhost:' + servers[1].port + '/accounts/peertube', privateKey: keys.privateKey }
df66d815
C
63 await makeFollowRequest(to, by)
64 })
65
66 describe('When checking HTTP signature', function () {
67
68 it('Should fail with an invalid digest', async function () {
48f07b4a 69 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]))
df66d815
C
70 const headers = {
71 Digest: buildDigest({ hello: 'coucou' })
72 }
73
48f07b4a 74 const { response } = await makePOSTAPRequest(url, body, baseHttpSignature(), headers)
df66d815
C
75
76 expect(response.statusCode).to.equal(403)
77 })
78
79 it('Should fail with an invalid date', async function () {
48f07b4a 80 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]))
df66d815
C
81 const headers = buildGlobalHeaders(body)
82 headers['date'] = 'Wed, 21 Oct 2015 07:28:00 GMT'
83
48f07b4a 84 const { response } = await makePOSTAPRequest(url, body, baseHttpSignature(), headers)
df66d815
C
85
86 expect(response.statusCode).to.equal(403)
87 })
88
89 it('Should fail with bad keys', async function () {
48f07b4a
C
90 await setKeysOfServer(servers[0], servers[1], invalidKeys.publicKey, invalidKeys.privateKey)
91 await setKeysOfServer(servers[1], servers[1], invalidKeys.publicKey, invalidKeys.privateKey)
df66d815 92
48f07b4a 93 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]))
df66d815
C
94 const headers = buildGlobalHeaders(body)
95
48f07b4a 96 const { response } = await makePOSTAPRequest(url, body, baseHttpSignature(), headers)
df66d815
C
97
98 expect(response.statusCode).to.equal(403)
99 })
100
101 it('Should succeed with a valid HTTP signature', async function () {
48f07b4a
C
102 await setKeysOfServer(servers[0], servers[1], keys.publicKey, keys.privateKey)
103 await setKeysOfServer(servers[1], servers[1], keys.publicKey, keys.privateKey)
df66d815 104
48f07b4a 105 const body = activityPubContextify(getAnnounceWithoutContext(servers[1]))
df66d815
C
106 const headers = buildGlobalHeaders(body)
107
48f07b4a 108 const { response } = await makePOSTAPRequest(url, body, baseHttpSignature(), headers)
df66d815
C
109
110 expect(response.statusCode).to.equal(204)
111 })
112 })
113
114 describe('When checking Linked Data Signature', function () {
115 before(async () => {
48f07b4a 116 await setKeysOfServer(servers[2], servers[2], keys.publicKey, keys.privateKey)
df66d815 117
48f07b4a
C
118 const to = { url: 'http://localhost:' + servers[0].port + '/accounts/peertube' }
119 const by = { url: 'http://localhost:' + servers[2].port + '/accounts/peertube', privateKey: keys.privateKey }
df66d815
C
120 await makeFollowRequest(to, by)
121 })
122
123 it('Should fail with bad keys', async function () {
124 this.timeout(10000)
125
48f07b4a
C
126 await setKeysOfServer(servers[0], servers[2], invalidKeys.publicKey, invalidKeys.privateKey)
127 await setKeysOfServer(servers[2], servers[2], invalidKeys.publicKey, invalidKeys.privateKey)
df66d815 128
48f07b4a
C
129 const body = getAnnounceWithoutContext(servers[1])
130 body.actor = 'http://localhost:' + servers[2].port + '/accounts/peertube'
df66d815 131
48f07b4a 132 const signer: any = { privateKey: invalidKeys.privateKey, url: 'http://localhost:' + servers[2].port + '/accounts/peertube' }
df66d815
C
133 const signedBody = await buildSignedActivity(signer, body)
134
135 const headers = buildGlobalHeaders(signedBody)
136
48f07b4a 137 const { response } = await makePOSTAPRequest(url, signedBody, baseHttpSignature(), headers)
df66d815
C
138
139 expect(response.statusCode).to.equal(403)
140 })
141
142 it('Should fail with an altered body', async function () {
143 this.timeout(10000)
144
48f07b4a
C
145 await setKeysOfServer(servers[0], servers[2], keys.publicKey, keys.privateKey)
146 await setKeysOfServer(servers[0], servers[2], keys.publicKey, keys.privateKey)
df66d815 147
48f07b4a
C
148 const body = getAnnounceWithoutContext(servers[1])
149 body.actor = 'http://localhost:' + servers[2].port + '/accounts/peertube'
df66d815 150
48f07b4a 151 const signer: any = { privateKey: keys.privateKey, url: 'http://localhost:' + servers[2].port + '/accounts/peertube' }
df66d815
C
152 const signedBody = await buildSignedActivity(signer, body)
153
48f07b4a 154 signedBody.actor = 'http://localhost:' + servers[2].port + '/account/peertube'
df66d815
C
155
156 const headers = buildGlobalHeaders(signedBody)
157
48f07b4a 158 const { response } = await makePOSTAPRequest(url, signedBody, baseHttpSignature(), headers)
df66d815
C
159
160 expect(response.statusCode).to.equal(403)
161 })
162
163 it('Should succeed with a valid signature', async function () {
164 this.timeout(10000)
165
48f07b4a
C
166 const body = getAnnounceWithoutContext(servers[1])
167 body.actor = 'http://localhost:' + servers[2].port + '/accounts/peertube'
df66d815 168
48f07b4a 169 const signer: any = { privateKey: keys.privateKey, url: 'http://localhost:' + servers[2].port + '/accounts/peertube' }
df66d815
C
170 const signedBody = await buildSignedActivity(signer, body)
171
172 const headers = buildGlobalHeaders(signedBody)
173
48f07b4a 174 const { response } = await makePOSTAPRequest(url, signedBody, baseHttpSignature(), headers)
df66d815
C
175
176 expect(response.statusCode).to.equal(204)
177 })
178 })
179
180 after(async function () {
48f07b4a
C
181 this.timeout(10000)
182
183 await cleanupTests(servers)
df66d815 184
c8000975 185 await closeAllSequelize(servers)
df66d815
C
186 })
187})