1 import { NextFunction, Request, Response } from 'express'
2 import { isActorDeleteActivityValid } from '@server/helpers/custom-validators/activitypub/actor'
3 import { getAPId } from '@server/lib/activitypub/activity'
4 import { wrapWithSpanAndContext } from '@server/lib/opentelemetry/tracing'
5 import { ActivityDelete, ActivityPubSignature, HttpStatusCode } from '@shared/models'
6 import { logger } from '../helpers/logger'
7 import { isHTTPSignatureVerified, isJsonLDSignatureVerified, parseHTTPSignature } from '../helpers/peertube-crypto'
8 import { ACCEPT_HEADERS, ACTIVITY_PUB, HTTP_SIGNATURE } from '../initializers/constants'
9 import { getOrCreateAPActor, loadActorUrlOrGetFromWebfinger } from '../lib/activitypub/actors'
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.status(HttpStatusCode.NO_CONTENT_204).end()
34 logger.warn('Error in ActivityPub signature checker.', { err })
36 status: HttpStatusCode.FORBIDDEN_403,
37 message: 'ActivityPub signature could not be checked'
42 function executeIfActivityPub (req: Request, res: Response, next: NextFunction) {
43 const accepted = req.accepts(ACCEPT_HEADERS)
44 if (accepted === false || ACTIVITY_PUB.POTENTIAL_ACCEPT_HEADERS.includes(accepted) === false) {
49 logger.debug('ActivityPub request for %s.', req.url)
54 // ---------------------------------------------------------------------------
62 // ---------------------------------------------------------------------------
64 async function checkHttpSignature (req: Request, res: Response) {
65 return wrapWithSpanAndContext('peertube.activitypub.checkHTTPSignature', async () => {
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 }
124 async function checkJsonLDSignature (req: Request, res: Response) {
125 return wrapWithSpanAndContext('peertube.activitypub.JSONLDSignature', async () => {
126 const signatureObject: ActivityPubSignature = req.body.signature
128 if (!signatureObject?.creator) {
130 status: HttpStatusCode.FORBIDDEN_403,
131 message: 'Object and creator signature do not match'
136 const [ creator ] = signatureObject.creator.split('#')
138 logger.debug('Checking JsonLD signature of actor %s...', creator)
140 const actor = await getOrCreateAPActor(creator)
141 const verified = await isJsonLDSignatureVerified(actor, req.body)
143 if (verified !== true) {
144 logger.warn('Signature not verified.', req.body)
147 status: HttpStatusCode.FORBIDDEN_403,
148 message: 'Signature could not be verified'
153 res.locals.signature = { actor }