]>
Commit | Line | Data |
---|---|---|
e65c0c5b | 1 | import { NextFunction, Request, Response } from 'express' |
10363c74 C |
2 | import { getAPId } from '@server/helpers/activitypub' |
3 | import { isActorDeleteActivityValid } from '@server/helpers/custom-validators/activitypub/actor' | |
d17c7b4e | 4 | import { ActivityDelete, ActivityPubSignature, HttpStatusCode } from '@shared/models' |
da854ddd | 5 | import { logger } from '../helpers/logger' |
41f2ebae | 6 | import { isHTTPSignatureVerified, isJsonLDSignatureVerified, parseHTTPSignature } from '../helpers/peertube-crypto' |
74dc3bca | 7 | import { ACCEPT_HEADERS, ACTIVITY_PUB, HTTP_SIGNATURE } from '../initializers/constants' |
10363c74 | 8 | import { getOrCreateAPActor, loadActorUrlOrGetFromWebfinger } from '../lib/activitypub/actors' |
e4f97bab C |
9 | |
10 | async function checkSignature (req: Request, res: Response, next: NextFunction) { | |
41f2ebae C |
11 | try { |
12 | const httpSignatureChecked = await checkHttpSignature(req, res) | |
13 | if (httpSignatureChecked !== true) return | |
e4f97bab | 14 | |
dae86118 | 15 | const actor = res.locals.signature.actor |
e12a0092 | 16 | |
41f2ebae C |
17 | // Forwarded activity |
18 | const bodyActor = req.body.actor | |
a1587156 | 19 | const bodyActorId = getAPId(bodyActor) |
41f2ebae C |
20 | if (bodyActorId && bodyActorId !== actor.url) { |
21 | const jsonLDSignatureChecked = await checkJsonLDSignature(req, res) | |
22 | if (jsonLDSignatureChecked !== true) return | |
23 | } | |
e4f97bab | 24 | |
41f2ebae | 25 | return next() |
50d6de9c | 26 | } catch (err) { |
75ba887d C |
27 | const activity: ActivityDelete = req.body |
28 | if (isActorDeleteActivityValid(activity) && activity.object === activity.actor) { | |
29 | logger.debug('Handling signature error on actor delete activity', { err }) | |
76148b27 | 30 | return res.status(HttpStatusCode.NO_CONTENT_204).end() |
75ba887d C |
31 | } |
32 | ||
33 | logger.warn('Error in ActivityPub signature checker.', { err }) | |
76148b27 RK |
34 | return res.fail({ |
35 | status: HttpStatusCode.FORBIDDEN_403, | |
36 | message: 'ActivityPub signature could not be checked' | |
37 | }) | |
e4f97bab | 38 | } |
e4f97bab C |
39 | } |
40 | ||
e65c0c5b C |
41 | function executeIfActivityPub (req: Request, res: Response, next: NextFunction) { |
42 | const accepted = req.accepts(ACCEPT_HEADERS) | |
bdd428a6 | 43 | if (accepted === false || ACTIVITY_PUB.POTENTIAL_ACCEPT_HEADERS.includes(accepted) === false) { |
e65c0c5b C |
44 | // Bypass this route |
45 | return next('route') | |
46 | } | |
e4f97bab | 47 | |
e65c0c5b | 48 | logger.debug('ActivityPub request for %s.', req.url) |
165cdc75 | 49 | |
e65c0c5b | 50 | return next() |
e4f97bab C |
51 | } |
52 | ||
53 | // --------------------------------------------------------------------------- | |
54 | ||
55 | export { | |
56 | checkSignature, | |
df66d815 C |
57 | executeIfActivityPub, |
58 | checkHttpSignature | |
e4f97bab | 59 | } |
41f2ebae C |
60 | |
61 | // --------------------------------------------------------------------------- | |
62 | ||
63 | async function checkHttpSignature (req: Request, res: Response) { | |
e9226905 | 64 | // FIXME: compatibility with http-signature < v1.3 |
41f2ebae | 65 | const sig = req.headers[HTTP_SIGNATURE.HEADER_NAME] as string |
e9226905 | 66 | if (sig && sig.startsWith('Signature ') === true) req.headers[HTTP_SIGNATURE.HEADER_NAME] = sig.replace(/^Signature /, '') |
41f2ebae | 67 | |
797d05bd C |
68 | let parsed: any |
69 | ||
70 | try { | |
71 | parsed = parseHTTPSignature(req, HTTP_SIGNATURE.CLOCK_SKEW_SECONDS) | |
72 | } catch (err) { | |
73 | logger.warn('Invalid signature because of exception in signature parser', { reqBody: req.body, err }) | |
74 | ||
76148b27 RK |
75 | res.fail({ |
76 | status: HttpStatusCode.FORBIDDEN_403, | |
77 | message: err.message | |
78 | }) | |
797d05bd C |
79 | return false |
80 | } | |
41f2ebae C |
81 | |
82 | const keyId = parsed.keyId | |
83 | if (!keyId) { | |
76148b27 RK |
84 | res.fail({ |
85 | status: HttpStatusCode.FORBIDDEN_403, | |
86 | message: 'Invalid key ID', | |
87 | data: { | |
88 | keyId | |
89 | } | |
90 | }) | |
41f2ebae C |
91 | return false |
92 | } | |
93 | ||
94 | logger.debug('Checking HTTP signature of actor %s...', keyId) | |
95 | ||
96 | let [ actorUrl ] = keyId.split('#') | |
97 | if (actorUrl.startsWith('acct:')) { | |
98 | actorUrl = await loadActorUrlOrGetFromWebfinger(actorUrl.replace(/^acct:/, '')) | |
99 | } | |
100 | ||
136d7efd | 101 | const actor = await getOrCreateAPActor(actorUrl) |
41f2ebae C |
102 | |
103 | const verified = isHTTPSignatureVerified(parsed, actor) | |
104 | if (verified !== true) { | |
c28bcdd1 C |
105 | logger.warn('Signature from %s is invalid', actorUrl, { parsed }) |
106 | ||
76148b27 RK |
107 | res.fail({ |
108 | status: HttpStatusCode.FORBIDDEN_403, | |
109 | message: 'Invalid signature', | |
110 | data: { | |
111 | actorUrl | |
112 | } | |
113 | }) | |
41f2ebae C |
114 | return false |
115 | } | |
116 | ||
117 | res.locals.signature = { actor } | |
41f2ebae C |
118 | return true |
119 | } | |
120 | ||
121 | async function checkJsonLDSignature (req: Request, res: Response) { | |
122 | const signatureObject: ActivityPubSignature = req.body.signature | |
123 | ||
df66d815 | 124 | if (!signatureObject || !signatureObject.creator) { |
76148b27 RK |
125 | res.fail({ |
126 | status: HttpStatusCode.FORBIDDEN_403, | |
127 | message: 'Object and creator signature do not match' | |
128 | }) | |
41f2ebae C |
129 | return false |
130 | } | |
131 | ||
132 | const [ creator ] = signatureObject.creator.split('#') | |
133 | ||
134 | logger.debug('Checking JsonLD signature of actor %s...', creator) | |
135 | ||
136d7efd | 136 | const actor = await getOrCreateAPActor(creator) |
41f2ebae C |
137 | const verified = await isJsonLDSignatureVerified(actor, req.body) |
138 | ||
139 | if (verified !== true) { | |
ad513607 C |
140 | logger.warn('Signature not verified.', req.body) |
141 | ||
76148b27 RK |
142 | res.fail({ |
143 | status: HttpStatusCode.FORBIDDEN_403, | |
144 | message: 'Signature could not be verified' | |
145 | }) | |
41f2ebae C |
146 | return false |
147 | } | |
148 | ||
149 | res.locals.signature = { actor } | |
41f2ebae C |
150 | return true |
151 | } |