]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/activitypub.ts
Refactor requests
[github/Chocobozzz/PeerTube.git] / server / middlewares / activitypub.ts
1 import { NextFunction, Request, Response } from 'express'
2 import { getAPId } from '@server/helpers/activitypub'
3 import { isActorDeleteActivityValid } from '@server/helpers/custom-validators/activitypub/actor'
4 import { ActivityDelete, ActivityPubSignature } from '../../shared'
5 import { HttpStatusCode } from '../../shared/models/http/http-error-codes'
6 import { logger } from '../helpers/logger'
7 import { isHTTPSignatureVerified, isJsonLDSignatureVerified, parseHTTPSignature } from '../helpers/peertube-crypto'
8 import { ACCEPT_HEADERS, ACTIVITY_PUB, HTTP_SIGNATURE } from '../initializers/constants'
9 import { getOrCreateAPActor, loadActorUrlOrGetFromWebfinger } from '../lib/activitypub/actors'
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.status(HttpStatusCode.NO_CONTENT_204).end()
32 }
33
34 logger.warn('Error in ActivityPub signature checker.', { err })
35 return res.fail({
36 status: HttpStatusCode.FORBIDDEN_403,
37 message: 'ActivityPub signature could not be checked'
38 })
39 }
40 }
41
42 function executeIfActivityPub (req: Request, res: Response, next: NextFunction) {
43 const accepted = req.accepts(ACCEPT_HEADERS)
44 if (accepted === false || ACTIVITY_PUB.POTENTIAL_ACCEPT_HEADERS.includes(accepted) === false) {
45 // Bypass this route
46 return next('route')
47 }
48
49 logger.debug('ActivityPub request for %s.', req.url)
50
51 return next()
52 }
53
54 // ---------------------------------------------------------------------------
55
56 export {
57 checkSignature,
58 executeIfActivityPub,
59 checkHttpSignature
60 }
61
62 // ---------------------------------------------------------------------------
63
64 async function checkHttpSignature (req: Request, res: Response) {
65 // FIXME: compatibility with http-signature < v1.3
66 const sig = req.headers[HTTP_SIGNATURE.HEADER_NAME] as string
67 if (sig && sig.startsWith('Signature ') === true) req.headers[HTTP_SIGNATURE.HEADER_NAME] = sig.replace(/^Signature /, '')
68
69 let parsed: any
70
71 try {
72 parsed = parseHTTPSignature(req, HTTP_SIGNATURE.CLOCK_SKEW_SECONDS)
73 } catch (err) {
74 logger.warn('Invalid signature because of exception in signature parser', { reqBody: req.body, err })
75
76 res.fail({
77 status: HttpStatusCode.FORBIDDEN_403,
78 message: err.message
79 })
80 return false
81 }
82
83 const keyId = parsed.keyId
84 if (!keyId) {
85 res.fail({
86 status: HttpStatusCode.FORBIDDEN_403,
87 message: 'Invalid key ID',
88 data: {
89 keyId
90 }
91 })
92 return false
93 }
94
95 logger.debug('Checking HTTP signature of actor %s...', keyId)
96
97 let [ actorUrl ] = keyId.split('#')
98 if (actorUrl.startsWith('acct:')) {
99 actorUrl = await loadActorUrlOrGetFromWebfinger(actorUrl.replace(/^acct:/, ''))
100 }
101
102 const actor = await getOrCreateAPActor(actorUrl)
103
104 const verified = isHTTPSignatureVerified(parsed, actor)
105 if (verified !== true) {
106 logger.warn('Signature from %s is invalid', actorUrl, { parsed })
107
108 res.fail({
109 status: HttpStatusCode.FORBIDDEN_403,
110 message: 'Invalid signature',
111 data: {
112 actorUrl
113 }
114 })
115 return false
116 }
117
118 res.locals.signature = { actor }
119 return true
120 }
121
122 async function checkJsonLDSignature (req: Request, res: Response) {
123 const signatureObject: ActivityPubSignature = req.body.signature
124
125 if (!signatureObject || !signatureObject.creator) {
126 res.fail({
127 status: HttpStatusCode.FORBIDDEN_403,
128 message: 'Object and creator signature do not match'
129 })
130 return false
131 }
132
133 const [ creator ] = signatureObject.creator.split('#')
134
135 logger.debug('Checking JsonLD signature of actor %s...', creator)
136
137 const actor = await getOrCreateAPActor(creator)
138 const verified = await isJsonLDSignatureVerified(actor, req.body)
139
140 if (verified !== true) {
141 logger.warn('Signature not verified.', req.body)
142
143 res.fail({
144 status: HttpStatusCode.FORBIDDEN_403,
145 message: 'Signature could not be verified'
146 })
147 return false
148 }
149
150 res.locals.signature = { actor }
151 return true
152 }