activitypub.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import { eachSeries } from 'async'
  2. import { NextFunction, Request, RequestHandler, Response } from 'express'
  3. import { ActivityPubSignature } from '../../shared'
  4. import { logger } from '../helpers/logger'
  5. import { isHTTPSignatureVerified, isJsonLDSignatureVerified, parseHTTPSignature } from '../helpers/peertube-crypto'
  6. import { ACCEPT_HEADERS, ACTIVITY_PUB, HTTP_SIGNATURE } from '../initializers'
  7. import { getOrCreateActorAndServerAndModel } from '../lib/activitypub'
  8. import { ActorModel } from '../models/activitypub/actor'
  9. import { loadActorUrlOrGetFromWebfinger } from '../helpers/webfinger'
  10. async function checkSignature (req: Request, res: Response, next: NextFunction) {
  11. try {
  12. const httpSignatureChecked = await checkHttpSignature(req, res)
  13. if (httpSignatureChecked !== true) return
  14. const actor: ActorModel = res.locals.signature.actor
  15. // Forwarded activity
  16. const bodyActor = req.body.actor
  17. const bodyActorId = bodyActor && bodyActor.id ? bodyActor.id : bodyActor
  18. if (bodyActorId && bodyActorId !== actor.url) {
  19. const jsonLDSignatureChecked = await checkJsonLDSignature(req, res)
  20. if (jsonLDSignatureChecked !== true) return
  21. }
  22. return next()
  23. } catch (err) {
  24. logger.error('Error in ActivityPub signature checker.', err)
  25. return res.sendStatus(403)
  26. }
  27. }
  28. function executeIfActivityPub (fun: RequestHandler | RequestHandler[]) {
  29. return (req: Request, res: Response, next: NextFunction) => {
  30. const accepted = req.accepts(ACCEPT_HEADERS)
  31. if (accepted === false || ACTIVITY_PUB.POTENTIAL_ACCEPT_HEADERS.indexOf(accepted) === -1) {
  32. return next()
  33. }
  34. logger.debug('ActivityPub request for %s.', req.url)
  35. if (Array.isArray(fun) === true) {
  36. return eachSeries(fun as RequestHandler[], (f, cb) => {
  37. f(req, res, cb)
  38. }, next)
  39. }
  40. return (fun as RequestHandler)(req, res, next)
  41. }
  42. }
  43. // ---------------------------------------------------------------------------
  44. export {
  45. checkSignature,
  46. executeIfActivityPub
  47. }
  48. // ---------------------------------------------------------------------------
  49. async function checkHttpSignature (req: Request, res: Response) {
  50. // FIXME: mastodon does not include the Signature scheme
  51. const sig = req.headers[HTTP_SIGNATURE.HEADER_NAME] as string
  52. if (sig && sig.startsWith('Signature ') === false) req.headers[HTTP_SIGNATURE.HEADER_NAME] = 'Signature ' + sig
  53. const parsed = parseHTTPSignature(req)
  54. const keyId = parsed.keyId
  55. if (!keyId) {
  56. res.sendStatus(403)
  57. return false
  58. }
  59. logger.debug('Checking HTTP signature of actor %s...', keyId)
  60. let [ actorUrl ] = keyId.split('#')
  61. if (actorUrl.startsWith('acct:')) {
  62. actorUrl = await loadActorUrlOrGetFromWebfinger(actorUrl.replace(/^acct:/, ''))
  63. }
  64. const actor = await getOrCreateActorAndServerAndModel(actorUrl)
  65. const verified = isHTTPSignatureVerified(parsed, actor)
  66. if (verified !== true) {
  67. res.sendStatus(403)
  68. return false
  69. }
  70. res.locals.signature = { actor }
  71. return true
  72. }
  73. async function checkJsonLDSignature (req: Request, res: Response) {
  74. const signatureObject: ActivityPubSignature = req.body.signature
  75. if (!signatureObject.creator) {
  76. res.sendStatus(403)
  77. return false
  78. }
  79. const [ creator ] = signatureObject.creator.split('#')
  80. logger.debug('Checking JsonLD signature of actor %s...', creator)
  81. const actor = await getOrCreateActorAndServerAndModel(creator)
  82. const verified = await isJsonLDSignatureVerified(actor, req.body)
  83. if (verified !== true) {
  84. res.sendStatus(403)
  85. return false
  86. }
  87. res.locals.signature = { actor }
  88. return true
  89. }