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