]>
Commit | Line | Data |
---|---|---|
e65c0c5b | 1 | import { NextFunction, Request, Response } from 'express' |
75ba887d | 2 | import { ActivityDelete, ActivityPubSignature } from '../../shared' |
da854ddd | 3 | import { logger } from '../helpers/logger' |
41f2ebae | 4 | import { isHTTPSignatureVerified, isJsonLDSignatureVerified, parseHTTPSignature } from '../helpers/peertube-crypto' |
74dc3bca | 5 | import { ACCEPT_HEADERS, ACTIVITY_PUB, HTTP_SIGNATURE } from '../initializers/constants' |
50d6de9c | 6 | import { getOrCreateActorAndServerAndModel } from '../lib/activitypub' |
41f2ebae | 7 | import { loadActorUrlOrGetFromWebfinger } from '../helpers/webfinger' |
75ba887d | 8 | import { isActorDeleteActivityValid } from '@server/helpers/custom-validators/activitypub/actor' |
a1587156 | 9 | import { getAPId } from '@server/helpers/activitypub' |
e4f97bab C |
10 | |
11 | async function checkSignature (req: Request, res: Response, next: NextFunction) { | |
41f2ebae C |
12 | try { |
13 | const httpSignatureChecked = await checkHttpSignature(req, res) | |
14 | if (httpSignatureChecked !== true) return | |
e4f97bab | 15 | |
dae86118 | 16 | const actor = res.locals.signature.actor |
e12a0092 | 17 | |
41f2ebae C |
18 | // Forwarded activity |
19 | const bodyActor = req.body.actor | |
a1587156 | 20 | const bodyActorId = getAPId(bodyActor) |
41f2ebae C |
21 | if (bodyActorId && bodyActorId !== actor.url) { |
22 | const jsonLDSignatureChecked = await checkJsonLDSignature(req, res) | |
23 | if (jsonLDSignatureChecked !== true) return | |
24 | } | |
e4f97bab | 25 | |
41f2ebae | 26 | return next() |
50d6de9c | 27 | } catch (err) { |
75ba887d C |
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 }) | |
50d6de9c | 35 | return res.sendStatus(403) |
e4f97bab | 36 | } |
e4f97bab C |
37 | } |
38 | ||
e65c0c5b C |
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.indexOf(accepted) === -1) { | |
42 | // Bypass this route | |
43 | return next('route') | |
44 | } | |
e4f97bab | 45 | |
e65c0c5b | 46 | logger.debug('ActivityPub request for %s.', req.url) |
165cdc75 | 47 | |
e65c0c5b | 48 | return next() |
e4f97bab C |
49 | } |
50 | ||
51 | // --------------------------------------------------------------------------- | |
52 | ||
53 | export { | |
54 | checkSignature, | |
df66d815 C |
55 | executeIfActivityPub, |
56 | checkHttpSignature | |
e4f97bab | 57 | } |
41f2ebae C |
58 | |
59 | // --------------------------------------------------------------------------- | |
60 | ||
61 | async function checkHttpSignature (req: Request, res: Response) { | |
e9226905 | 62 | // FIXME: compatibility with http-signature < v1.3 |
41f2ebae | 63 | const sig = req.headers[HTTP_SIGNATURE.HEADER_NAME] as string |
e9226905 | 64 | if (sig && sig.startsWith('Signature ') === true) req.headers[HTTP_SIGNATURE.HEADER_NAME] = sig.replace(/^Signature /, '') |
41f2ebae | 65 | |
f67d7574 | 66 | const parsed = parseHTTPSignature(req, HTTP_SIGNATURE.CLOCK_SKEW_SECONDS) |
41f2ebae C |
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) { | |
c28bcdd1 C |
85 | logger.warn('Signature from %s is invalid', actorUrl, { parsed }) |
86 | ||
41f2ebae C |
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 | ||
df66d815 | 99 | if (!signatureObject || !signatureObject.creator) { |
41f2ebae C |
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) { | |
ad513607 C |
112 | logger.warn('Signature not verified.', req.body) |
113 | ||
41f2ebae C |
114 | res.sendStatus(403) |
115 | return false | |
116 | } | |
117 | ||
118 | res.locals.signature = { actor } | |
119 | ||
120 | return true | |
121 | } |