]>
Commit | Line | Data |
---|---|---|
1 | import { NextFunction, Request, Response } from 'express' | |
2 | import { ActivityDelete, ActivityPubSignature } from '../../shared' | |
3 | import { logger } from '../helpers/logger' | |
4 | import { isHTTPSignatureVerified, isJsonLDSignatureVerified, parseHTTPSignature } from '../helpers/peertube-crypto' | |
5 | import { ACCEPT_HEADERS, ACTIVITY_PUB, HTTP_SIGNATURE } from '../initializers/constants' | |
6 | import { getOrCreateActorAndServerAndModel } from '../lib/activitypub/actor' | |
7 | import { loadActorUrlOrGetFromWebfinger } from '../helpers/webfinger' | |
8 | import { isActorDeleteActivityValid } from '@server/helpers/custom-validators/activitypub/actor' | |
9 | import { getAPId } from '@server/helpers/activitypub' | |
10 | ||
11 | async function checkSignature (req: Request, res: Response, next: NextFunction) { | |
12 | try { | |
13 | const httpSignatureChecked = await checkHttpSignature(req, res) | |
14 | if (httpSignatureChecked !== true) return | |
15 | ||
16 | const actor = res.locals.signature.actor | |
17 | ||
18 | // Forwarded activity | |
19 | const bodyActor = req.body.actor | |
20 | const bodyActorId = getAPId(bodyActor) | |
21 | if (bodyActorId && bodyActorId !== actor.url) { | |
22 | const jsonLDSignatureChecked = await checkJsonLDSignature(req, res) | |
23 | if (jsonLDSignatureChecked !== true) return | |
24 | } | |
25 | ||
26 | return next() | |
27 | } catch (err) { | |
28 | const activity: ActivityDelete = req.body | |
29 | if (isActorDeleteActivityValid(activity) && activity.object === activity.actor) { | |
30 | logger.debug('Handling signature error on actor delete activity', { err }) | |
31 | return res.sendStatus(204) | |
32 | } | |
33 | ||
34 | logger.warn('Error in ActivityPub signature checker.', { err }) | |
35 | return res.sendStatus(403) | |
36 | } | |
37 | } | |
38 | ||
39 | function executeIfActivityPub (req: Request, res: Response, next: NextFunction) { | |
40 | const accepted = req.accepts(ACCEPT_HEADERS) | |
41 | if (accepted === false || ACTIVITY_PUB.POTENTIAL_ACCEPT_HEADERS.includes(accepted) === false) { | |
42 | // Bypass this route | |
43 | return next('route') | |
44 | } | |
45 | ||
46 | logger.debug('ActivityPub request for %s.', req.url) | |
47 | ||
48 | return next() | |
49 | } | |
50 | ||
51 | // --------------------------------------------------------------------------- | |
52 | ||
53 | export { | |
54 | checkSignature, | |
55 | executeIfActivityPub, | |
56 | checkHttpSignature | |
57 | } | |
58 | ||
59 | // --------------------------------------------------------------------------- | |
60 | ||
61 | async function checkHttpSignature (req: Request, res: Response) { | |
62 | // FIXME: compatibility with http-signature < v1.3 | |
63 | const sig = req.headers[HTTP_SIGNATURE.HEADER_NAME] as string | |
64 | if (sig && sig.startsWith('Signature ') === true) req.headers[HTTP_SIGNATURE.HEADER_NAME] = sig.replace(/^Signature /, '') | |
65 | ||
66 | const parsed = parseHTTPSignature(req, HTTP_SIGNATURE.CLOCK_SKEW_SECONDS) | |
67 | ||
68 | const keyId = parsed.keyId | |
69 | if (!keyId) { | |
70 | res.sendStatus(403) | |
71 | return false | |
72 | } | |
73 | ||
74 | logger.debug('Checking HTTP signature of actor %s...', keyId) | |
75 | ||
76 | let [ actorUrl ] = keyId.split('#') | |
77 | if (actorUrl.startsWith('acct:')) { | |
78 | actorUrl = await loadActorUrlOrGetFromWebfinger(actorUrl.replace(/^acct:/, '')) | |
79 | } | |
80 | ||
81 | const actor = await getOrCreateActorAndServerAndModel(actorUrl) | |
82 | ||
83 | const verified = isHTTPSignatureVerified(parsed, actor) | |
84 | if (verified !== true) { | |
85 | logger.warn('Signature from %s is invalid', actorUrl, { parsed }) | |
86 | ||
87 | res.sendStatus(403) | |
88 | return false | |
89 | } | |
90 | ||
91 | res.locals.signature = { actor } | |
92 | ||
93 | return true | |
94 | } | |
95 | ||
96 | async function checkJsonLDSignature (req: Request, res: Response) { | |
97 | const signatureObject: ActivityPubSignature = req.body.signature | |
98 | ||
99 | if (!signatureObject || !signatureObject.creator) { | |
100 | res.sendStatus(403) | |
101 | return false | |
102 | } | |
103 | ||
104 | const [ creator ] = signatureObject.creator.split('#') | |
105 | ||
106 | logger.debug('Checking JsonLD signature of actor %s...', creator) | |
107 | ||
108 | const actor = await getOrCreateActorAndServerAndModel(creator) | |
109 | const verified = await isJsonLDSignatureVerified(actor, req.body) | |
110 | ||
111 | if (verified !== true) { | |
112 | logger.warn('Signature not verified.', req.body) | |
113 | ||
114 | res.sendStatus(403) | |
115 | return false | |
116 | } | |
117 | ||
118 | res.locals.signature = { actor } | |
119 | ||
120 | return true | |
121 | } |