1 import { NextFunction, Request, Response } from 'express'
2 import { ActivityDelete, ActivityPubSignature } from '../../shared'
3 import { logger } from '../helpers/logger'
4 import { isHTTPSignatureVerified, isJsonLDSignatureVerified, parseHTTPSignature } from '../helpers/peertube-crypto'
5 import { ACCEPT_HEADERS, ACTIVITY_PUB, HTTP_SIGNATURE } from '../initializers/constants'
6 import { getOrCreateActorAndServerAndModel } from '../lib/activitypub'
7 import { loadActorUrlOrGetFromWebfinger } from '../helpers/webfinger'
8 import { isActorDeleteActivityValid } from '@server/helpers/custom-validators/activitypub/actor'
10 async function checkSignature (req: Request, res: Response, next: NextFunction) {
12 const httpSignatureChecked = await checkHttpSignature(req, res)
13 if (httpSignatureChecked !== true) return
15 const actor = res.locals.signature.actor
18 const bodyActor = req.body.actor
19 const bodyActorId = bodyActor && bodyActor.id ? bodyActor.id : bodyActor
20 if (bodyActorId && bodyActorId !== actor.url) {
21 const jsonLDSignatureChecked = await checkJsonLDSignature(req, res)
22 if (jsonLDSignatureChecked !== true) return
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 })
30 return res.sendStatus(204)
33 logger.warn('Error in ActivityPub signature checker.', { err })
34 return res.sendStatus(403)
38 function executeIfActivityPub (req: Request, res: Response, next: NextFunction) {
39 const accepted = req.accepts(ACCEPT_HEADERS)
40 if (accepted === false || ACTIVITY_PUB.POTENTIAL_ACCEPT_HEADERS.indexOf(accepted) === -1) {
45 logger.debug('ActivityPub request for %s.', req.url)
50 // ---------------------------------------------------------------------------
58 // ---------------------------------------------------------------------------
60 async function checkHttpSignature (req: Request, res: Response) {
61 // FIXME: compatibility with http-signature < v1.3
62 const sig = req.headers[HTTP_SIGNATURE.HEADER_NAME] as string
63 if (sig && sig.startsWith('Signature ') === true) req.headers[HTTP_SIGNATURE.HEADER_NAME] = sig.replace(/^Signature /, '')
65 const parsed = parseHTTPSignature(req, HTTP_SIGNATURE.CLOCK_SKEW_SECONDS)
67 const keyId = parsed.keyId
73 logger.debug('Checking HTTP signature of actor %s...', keyId)
75 let [ actorUrl ] = keyId.split('#')
76 if (actorUrl.startsWith('acct:')) {
77 actorUrl = await loadActorUrlOrGetFromWebfinger(actorUrl.replace(/^acct:/, ''))
80 const actor = await getOrCreateActorAndServerAndModel(actorUrl)
82 const verified = isHTTPSignatureVerified(parsed, actor)
83 if (verified !== true) {
84 logger.warn('Signature from %s is invalid', actorUrl, { parsed })
90 res.locals.signature = { actor }
95 async function checkJsonLDSignature (req: Request, res: Response) {
96 const signatureObject: ActivityPubSignature = req.body.signature
98 if (!signatureObject || !signatureObject.creator) {
103 const [ creator ] = signatureObject.creator.split('#')
105 logger.debug('Checking JsonLD signature of actor %s...', creator)
107 const actor = await getOrCreateActorAndServerAndModel(creator)
108 const verified = await isJsonLDSignatureVerified(actor, req.body)
110 if (verified !== true) {
111 logger.warn('Signature not verified.', req.body)
117 res.locals.signature = { actor }