Node.js based forum software built for the modern web
feat: implement additional checks to fetchPublicKey
Julian Lam6b3801fa9897
diff --git a/public/language/en-GB/error.json b/public/language/en-GB/error.jsonindex 867a3426c6..5dda7f0e68 100644--- a/public/language/en-GB/error.json+++ b/public/language/en-GB/error.json@@ -304,6 +304,7 @@"activitypub.not-enabled": "Federation is not enabled on this server","activitypub.invalid-id": "Unable to resolve the input id, likely as it is malformed.",+ "activitypub.invalid-content-type": "An unexpected content-type was associated with the returned content","activitypub.get-failed": "Unable to retrieve the specified resource.","activitypub.pubKey-not-found": "Unable to resolve public key, so payload verification cannot take place.","activitypub.origin-mismatch": "The received object's origin does not match the sender's origin",diff --git a/public/src/utils.common.js b/public/src/utils.common.jsindex dfb6f35c98..5052d109f7 100644--- a/public/src/utils.common.js+++ b/public/src/utils.common.js@@ -358,6 +358,16 @@ const utils = {return !isNaN(parseFloat(n)) && isFinite(n);},+ isPEM: function (pem) {+ if (!pem || typeof pem !== 'string') {+ return false;+ }+ // Basic PEM validation+ return pem.startsWith('-----BEGIN PUBLIC KEY-----') &&+ pem.endsWith('-----END PUBLIC KEY-----') &&+ pem.includes('MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA');+ },+languageKeyRegex: /\[\[[\w]+:.+\]\]/,hasLanguageKey: function (input) {return utils.languageKeyRegex.test(input);diff --git a/src/activitypub/index.js b/src/activitypub/index.jsindex da5405cd07..25dbc43b0c 100644--- a/src/activitypub/index.js+++ b/src/activitypub/index.js@@ -223,6 +223,13 @@ ActivityPub.fetchPublicKey = async (uri, ip) => {throw new Error('[[error:activitypub.invalid-id]]');}+ try {+ // Validate URI is well-formed+ new URL(uri);+ } catch (err) {+ throw new Error('[[error:activitypub.invalid-id]]');+ }+// Rate limiting check - only if not cachedconst lockId = `pubkey:${ip}`;let lockStatus = await db.incrObjectField('locks', lockId);@@ -235,20 +242,37 @@ ActivityPub.fetchPublicKey = async (uri, ip) => {try {// Use requests.get with built-in SSRF protections// Set reasonable timeout and response size limit- const { body } = await request.get(uri, {+ const { body, headers } = await request.get(uri, {timeout: 5000, // 5 secondsheaders: {'accept': ActivityPub._constants.acceptableTypes.at(1),},redirect: 'manual',+ maxBodyLength: 1024 * 1024, // 1MB limit});+ // Validate Content-Type header+ if (+ !headers['content-type'] ||+ ActivityPub._constants.acceptableTypes.includes(headers['content-type'])+ ) {+ throw new Error('[[error:activitypub.invalid-content-type]]');+ }+// Process response and cacheif (body.hasOwnProperty('publicKeyPem')) {+ // Validate public key+ if (!utils.isPEM(body.publicKeyPem)) {+ throw new Error('[[error:invalid-data]]');+ }// CryptographicKey returned (correct)publicKeyCache.set(uri, body.publicKeyPem);return body.publicKeyPem;} else if (body?.publicKey?.publicKeyPem) {+ // Validate public key+ if (!utils.isPEM(body.publicKey.publicKeyPem)) {+ throw new Error('[[error:invalid-data]]');+ }// Actor object returned (less correct)publicKeyCache.set(uri, body.publicKey.publicKeyPem);return body.publicKey.publicKeyPem;