diff options
author | Chocobozzz <me@florianbigard.com> | 2018-10-19 11:41:19 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2018-10-19 12:26:37 +0200 |
commit | 41f2ebae4f970932fb62d2d8923b1f776f0b1494 (patch) | |
tree | 9e3f89e6363fc63e77d352f07956d1b6ff0cacda /server/middlewares/activitypub.ts | |
parent | d23e6a1c97a6ae3ca8d340a8c9adad268a5be57e (diff) | |
download | PeerTube-41f2ebae4f970932fb62d2d8923b1f776f0b1494.tar.gz PeerTube-41f2ebae4f970932fb62d2d8923b1f776f0b1494.tar.zst PeerTube-41f2ebae4f970932fb62d2d8923b1f776f0b1494.zip |
Add HTTP signature check before linked signature
It's faster, and will allow us to use RSA signature 2018 (with upstream
jsonld-signature module) without too much incompatibilities in the
peertube federation
Diffstat (limited to 'server/middlewares/activitypub.ts')
-rw-r--r-- | server/middlewares/activitypub.ts | 94 |
1 files changed, 76 insertions, 18 deletions
diff --git a/server/middlewares/activitypub.ts b/server/middlewares/activitypub.ts index d7f59be8c..1ec888477 100644 --- a/server/middlewares/activitypub.ts +++ b/server/middlewares/activitypub.ts | |||
@@ -2,34 +2,32 @@ import { eachSeries } from 'async' | |||
2 | import { NextFunction, Request, RequestHandler, Response } from 'express' | 2 | import { NextFunction, Request, RequestHandler, Response } from 'express' |
3 | import { ActivityPubSignature } from '../../shared' | 3 | import { ActivityPubSignature } from '../../shared' |
4 | import { logger } from '../helpers/logger' | 4 | import { logger } from '../helpers/logger' |
5 | import { isSignatureVerified } from '../helpers/peertube-crypto' | 5 | import { isHTTPSignatureVerified, isJsonLDSignatureVerified, parseHTTPSignature } from '../helpers/peertube-crypto' |
6 | import { ACCEPT_HEADERS, ACTIVITY_PUB } from '../initializers' | 6 | import { ACCEPT_HEADERS, ACTIVITY_PUB, HTTP_SIGNATURE } from '../initializers' |
7 | import { getOrCreateActorAndServerAndModel } from '../lib/activitypub' | 7 | import { getOrCreateActorAndServerAndModel } from '../lib/activitypub' |
8 | import { ActorModel } from '../models/activitypub/actor' | 8 | import { ActorModel } from '../models/activitypub/actor' |
9 | import { loadActorUrlOrGetFromWebfinger } from '../helpers/webfinger' | ||
9 | 10 | ||
10 | async function checkSignature (req: Request, res: Response, next: NextFunction) { | 11 | async function checkSignature (req: Request, res: Response, next: NextFunction) { |
11 | const signatureObject: ActivityPubSignature = req.body.signature | 12 | try { |
13 | const httpSignatureChecked = await checkHttpSignature(req, res) | ||
14 | if (httpSignatureChecked !== true) return | ||
12 | 15 | ||
13 | const [ creator ] = signatureObject.creator.split('#') | 16 | const actor: ActorModel = res.locals.signature.actor |
14 | 17 | ||
15 | logger.debug('Checking signature of actor %s...', creator) | 18 | // Forwarded activity |
19 | const bodyActor = req.body.actor | ||
20 | const bodyActorId = bodyActor && bodyActor.id ? bodyActor.id : bodyActor | ||
21 | if (bodyActorId && bodyActorId !== actor.url) { | ||
22 | const jsonLDSignatureChecked = await checkJsonLDSignature(req, res) | ||
23 | if (jsonLDSignatureChecked !== true) return | ||
24 | } | ||
16 | 25 | ||
17 | let actor: ActorModel | 26 | return next() |
18 | try { | ||
19 | actor = await getOrCreateActorAndServerAndModel(creator) | ||
20 | } catch (err) { | 27 | } catch (err) { |
21 | logger.warn('Cannot create remote actor %s and check signature.', creator, { err }) | 28 | logger.error('Error in ActivityPub signature checker.', err) |
22 | return res.sendStatus(403) | 29 | return res.sendStatus(403) |
23 | } | 30 | } |
24 | |||
25 | const verified = await isSignatureVerified(actor, req.body) | ||
26 | if (verified === false) return res.sendStatus(403) | ||
27 | |||
28 | res.locals.signature = { | ||
29 | actor | ||
30 | } | ||
31 | |||
32 | return next() | ||
33 | } | 31 | } |
34 | 32 | ||
35 | function executeIfActivityPub (fun: RequestHandler | RequestHandler[]) { | 33 | function executeIfActivityPub (fun: RequestHandler | RequestHandler[]) { |
@@ -57,3 +55,63 @@ export { | |||
57 | checkSignature, | 55 | checkSignature, |
58 | executeIfActivityPub | 56 | executeIfActivityPub |
59 | } | 57 | } |
58 | |||
59 | // --------------------------------------------------------------------------- | ||
60 | |||
61 | async function checkHttpSignature (req: Request, res: Response) { | ||
62 | // FIXME: mastodon does not include the Signature scheme | ||
63 | const sig = req.headers[HTTP_SIGNATURE.HEADER_NAME] as string | ||
64 | if (sig && sig.startsWith('Signature ') === false) req.headers[HTTP_SIGNATURE.HEADER_NAME] = 'Signature ' + sig | ||
65 | |||
66 | const parsed = parseHTTPSignature(req) | ||
67 | |||
68 | const keyId = parsed.keyId | ||
69 | if (!keyId) { | ||
70 | res.sendStatus(403) | ||
71 | return false | ||
72 | } | ||
73 | |||
74 | logger.debug('Checking HTTP signature of actor %s...', keyId) | ||
75 | |||
76 | let [ actorUrl ] = keyId.split('#') | ||
77 | if (actorUrl.startsWith('acct:')) { | ||
78 | actorUrl = await loadActorUrlOrGetFromWebfinger(actorUrl.replace(/^acct:/, '')) | ||
79 | } | ||
80 | |||
81 | const actor = await getOrCreateActorAndServerAndModel(actorUrl) | ||
82 | |||
83 | const verified = isHTTPSignatureVerified(parsed, actor) | ||
84 | if (verified !== true) { | ||
85 | res.sendStatus(403) | ||
86 | return false | ||
87 | } | ||
88 | |||
89 | res.locals.signature = { actor } | ||
90 | |||
91 | return true | ||
92 | } | ||
93 | |||
94 | async function checkJsonLDSignature (req: Request, res: Response) { | ||
95 | const signatureObject: ActivityPubSignature = req.body.signature | ||
96 | |||
97 | if (!signatureObject.creator) { | ||
98 | res.sendStatus(403) | ||
99 | return false | ||
100 | } | ||
101 | |||
102 | const [ creator ] = signatureObject.creator.split('#') | ||
103 | |||
104 | logger.debug('Checking JsonLD signature of actor %s...', creator) | ||
105 | |||
106 | const actor = await getOrCreateActorAndServerAndModel(creator) | ||
107 | const verified = await isJsonLDSignatureVerified(actor, req.body) | ||
108 | |||
109 | if (verified !== true) { | ||
110 | res.sendStatus(403) | ||
111 | return false | ||
112 | } | ||
113 | |||
114 | res.locals.signature = { actor } | ||
115 | |||
116 | return true | ||
117 | } | ||