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