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'
9 import { getAPId } from '@server/helpers/activitypub'
11 async function checkSignature (req: Request, res: Response, next: NextFunction) {
13 const httpSignatureChecked = await checkHttpSignature(req, res)
14 if (httpSignatureChecked !== true) return
16 const actor = res.locals.signature.actor
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
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.sendStatus(204)
34 logger.warn('Error in ActivityPub signature checker.', { err })
35 return res.sendStatus(403)
39 function executeIfActivityPub (req: Request, res: Response, next: NextFunction) {
40 const accepted = req.accepts(ACCEPT_HEADERS)
41 if (accepted === false || ACTIVITY_PUB.POTENTIAL_ACCEPT_HEADERS.includes(accepted) === false) {
46 logger.debug('ActivityPub request for %s.', req.url)
51 // ---------------------------------------------------------------------------
59 // ---------------------------------------------------------------------------
61 async function checkHttpSignature (req: Request, res: Response) {
62 // FIXME: compatibility with http-signature < v1.3
63 const sig = req.headers[HTTP_SIGNATURE.HEADER_NAME] as string
64 if (sig && sig.startsWith('Signature ') === true) req.headers[HTTP_SIGNATURE.HEADER_NAME] = sig.replace(/^Signature /, '')
66 const parsed = parseHTTPSignature(req, HTTP_SIGNATURE.CLOCK_SKEW_SECONDS)
68 const keyId = parsed.keyId
74 logger.debug('Checking HTTP signature of actor %s...', keyId)
76 let [ actorUrl ] = keyId.split('#')
77 if (actorUrl.startsWith('acct:')) {
78 actorUrl = await loadActorUrlOrGetFromWebfinger(actorUrl.replace(/^acct:/, ''))
81 const actor = await getOrCreateActorAndServerAndModel(actorUrl)
83 const verified = isHTTPSignatureVerified(parsed, actor)
84 if (verified !== true) {
85 logger.warn('Signature from %s is invalid', actorUrl, { parsed })
91 res.locals.signature = { actor }
96 async function checkJsonLDSignature (req: Request, res: Response) {
97 const signatureObject: ActivityPubSignature = req.body.signature
99 if (!signatureObject || !signatureObject.creator) {
104 const [ creator ] = signatureObject.creator.split('#')
106 logger.debug('Checking JsonLD signature of actor %s...', creator)
108 const actor = await getOrCreateActorAndServerAndModel(creator)
109 const verified = await isJsonLDSignatureVerified(actor, req.body)
111 if (verified !== true) {
112 logger.warn('Signature not verified.', req.body)
118 res.locals.signature = { actor }