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 { getOrCreateAPActor } from '../lib/activitypub/actors'
7 import { loadActorUrlOrGetFromWebfinger } from '../helpers/webfinger'
8 import { isActorDeleteActivityValid } from '@server/helpers/custom-validators/activitypub/actor'
9 import { getAPId } from '@server/helpers/activitypub'
10 import { HttpStatusCode } from '../../shared/core-utils/miscs/http-error-codes'
12 async function checkSignature (req: Request, res: Response, next: NextFunction) {
14 const httpSignatureChecked = await checkHttpSignature(req, res)
15 if (httpSignatureChecked !== true) return
17 const actor = res.locals.signature.actor
20 const bodyActor = req.body.actor
21 const bodyActorId = getAPId(bodyActor)
22 if (bodyActorId && bodyActorId !== actor.url) {
23 const jsonLDSignatureChecked = await checkJsonLDSignature(req, res)
24 if (jsonLDSignatureChecked !== true) return
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 })
32 return res.status(HttpStatusCode.NO_CONTENT_204).end()
35 logger.warn('Error in ActivityPub signature checker.', { err })
37 status: HttpStatusCode.FORBIDDEN_403,
38 message: 'ActivityPub signature could not be checked'
43 function executeIfActivityPub (req: Request, res: Response, next: NextFunction) {
44 const accepted = req.accepts(ACCEPT_HEADERS)
45 if (accepted === false || ACTIVITY_PUB.POTENTIAL_ACCEPT_HEADERS.includes(accepted) === false) {
50 logger.debug('ActivityPub request for %s.', req.url)
55 // ---------------------------------------------------------------------------
63 // ---------------------------------------------------------------------------
65 async function checkHttpSignature (req: Request, res: Response) {
66 // FIXME: compatibility with http-signature < v1.3
67 const sig = req.headers[HTTP_SIGNATURE.HEADER_NAME] as string
68 if (sig && sig.startsWith('Signature ') === true) req.headers[HTTP_SIGNATURE.HEADER_NAME] = sig.replace(/^Signature /, '')
73 parsed = parseHTTPSignature(req, HTTP_SIGNATURE.CLOCK_SKEW_SECONDS)
75 logger.warn('Invalid signature because of exception in signature parser', { reqBody: req.body, err })
78 status: HttpStatusCode.FORBIDDEN_403,
84 const keyId = parsed.keyId
87 status: HttpStatusCode.FORBIDDEN_403,
88 message: 'Invalid key ID',
96 logger.debug('Checking HTTP signature of actor %s...', keyId)
98 let [ actorUrl ] = keyId.split('#')
99 if (actorUrl.startsWith('acct:')) {
100 actorUrl = await loadActorUrlOrGetFromWebfinger(actorUrl.replace(/^acct:/, ''))
103 const actor = await getOrCreateAPActor(actorUrl)
105 const verified = isHTTPSignatureVerified(parsed, actor)
106 if (verified !== true) {
107 logger.warn('Signature from %s is invalid', actorUrl, { parsed })
110 status: HttpStatusCode.FORBIDDEN_403,
111 message: 'Invalid signature',
119 res.locals.signature = { actor }
123 async function checkJsonLDSignature (req: Request, res: Response) {
124 const signatureObject: ActivityPubSignature = req.body.signature
126 if (!signatureObject || !signatureObject.creator) {
128 status: HttpStatusCode.FORBIDDEN_403,
129 message: 'Object and creator signature do not match'
134 const [ creator ] = signatureObject.creator.split('#')
136 logger.debug('Checking JsonLD signature of actor %s...', creator)
138 const actor = await getOrCreateAPActor(creator)
139 const verified = await isJsonLDSignatureVerified(actor, req.body)
141 if (verified !== true) {
142 logger.warn('Signature not verified.', req.body)
145 status: HttpStatusCode.FORBIDDEN_403,
146 message: 'Signature could not be verified'
151 res.locals.signature = { actor }