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