X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmiddlewares%2Factivitypub.ts;h=a1fdfafcfd0f4d3fa1f89c851fcb9f60e6ce87cf;hb=136d7efde798d3dc0ec0dd18aac674365f7d162e;hp=ce94a2129f84f56c275d3cfaf3e1c965b816a295;hpb=2d53be0267acc49cda46707b885096193a1f4e9c;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/middlewares/activitypub.ts b/server/middlewares/activitypub.ts index ce94a2129..a1fdfafcf 100644 --- a/server/middlewares/activitypub.ts +++ b/server/middlewares/activitypub.ts @@ -3,7 +3,7 @@ import { ActivityDelete, ActivityPubSignature } from '../../shared' import { logger } from '../helpers/logger' import { isHTTPSignatureVerified, isJsonLDSignatureVerified, parseHTTPSignature } from '../helpers/peertube-crypto' import { ACCEPT_HEADERS, ACTIVITY_PUB, HTTP_SIGNATURE } from '../initializers/constants' -import { getOrCreateActorAndServerAndModel } from '../lib/activitypub/actor' +import { getOrCreateAPActor } from '../lib/activitypub/actors' import { loadActorUrlOrGetFromWebfinger } from '../helpers/webfinger' import { isActorDeleteActivityValid } from '@server/helpers/custom-validators/activitypub/actor' import { getAPId } from '@server/helpers/activitypub' @@ -29,11 +29,14 @@ async function checkSignature (req: Request, res: Response, next: NextFunction) const activity: ActivityDelete = req.body if (isActorDeleteActivityValid(activity) && activity.object === activity.actor) { logger.debug('Handling signature error on actor delete activity', { err }) - return res.sendStatus(HttpStatusCode.NO_CONTENT_204) + return res.status(HttpStatusCode.NO_CONTENT_204).end() } logger.warn('Error in ActivityPub signature checker.', { err }) - return res.sendStatus(HttpStatusCode.FORBIDDEN_403) + return res.fail({ + status: HttpStatusCode.FORBIDDEN_403, + message: 'ActivityPub signature could not be checked' + }) } } @@ -71,13 +74,22 @@ async function checkHttpSignature (req: Request, res: Response) { } catch (err) { logger.warn('Invalid signature because of exception in signature parser', { reqBody: req.body, err }) - res.status(HttpStatusCode.FORBIDDEN_403).json({ error: err.message }) + res.fail({ + status: HttpStatusCode.FORBIDDEN_403, + message: err.message + }) return false } const keyId = parsed.keyId if (!keyId) { - res.sendStatus(HttpStatusCode.FORBIDDEN_403) + res.fail({ + status: HttpStatusCode.FORBIDDEN_403, + message: 'Invalid key ID', + data: { + keyId + } + }) return false } @@ -88,18 +100,23 @@ async function checkHttpSignature (req: Request, res: Response) { actorUrl = await loadActorUrlOrGetFromWebfinger(actorUrl.replace(/^acct:/, '')) } - const actor = await getOrCreateActorAndServerAndModel(actorUrl) + const actor = await getOrCreateAPActor(actorUrl) const verified = isHTTPSignatureVerified(parsed, actor) if (verified !== true) { logger.warn('Signature from %s is invalid', actorUrl, { parsed }) - res.sendStatus(HttpStatusCode.FORBIDDEN_403) + res.fail({ + status: HttpStatusCode.FORBIDDEN_403, + message: 'Invalid signature', + data: { + actorUrl + } + }) return false } res.locals.signature = { actor } - return true } @@ -107,7 +124,10 @@ async function checkJsonLDSignature (req: Request, res: Response) { const signatureObject: ActivityPubSignature = req.body.signature if (!signatureObject || !signatureObject.creator) { - res.sendStatus(HttpStatusCode.FORBIDDEN_403) + res.fail({ + status: HttpStatusCode.FORBIDDEN_403, + message: 'Object and creator signature do not match' + }) return false } @@ -115,17 +135,19 @@ async function checkJsonLDSignature (req: Request, res: Response) { logger.debug('Checking JsonLD signature of actor %s...', creator) - const actor = await getOrCreateActorAndServerAndModel(creator) + const actor = await getOrCreateAPActor(creator) const verified = await isJsonLDSignatureVerified(actor, req.body) if (verified !== true) { logger.warn('Signature not verified.', req.body) - res.sendStatus(HttpStatusCode.FORBIDDEN_403) + res.fail({ + status: HttpStatusCode.FORBIDDEN_403, + message: 'Signature could not be verified' + }) return false } res.locals.signature = { actor } - return true }