Node.js based forum software built for the modern web
fix: cache failed requests to fetchPublicKey
Julian Lam515361e09a19
diff --git a/src/activitypub/index.js b/src/activitypub/index.jsindex 2fdbbebd4d..b6e9314e6e 100644--- a/src/activitypub/index.js+++ b/src/activitypub/index.js@@ -205,7 +205,11 @@ ActivityPub.getPrivateKey = async (type, id) => {ActivityPub.fetchPublicKey = async (uri) => {const cached = publicKeyCache.get(uri);- if (cached) {+ if (cached !== undefined) {+ if (cached === null) {+ // Failed request was cached+ throw new Error('[[error:activitypub.pubKey-not-found]]');+ }return cached;}@@ -230,14 +234,19 @@ ActivityPub.fetchPublicKey = async (uri) => {// CryptographicKey returned (correct)publicKeyCache.set(uri, body.publicKeyPem);return body.publicKeyPem;- } else if (body.hasOwnProperty('publicKey') && body?.publicKey?.publicKeyPem) {+ } else if (body?.publicKey?.publicKeyPem) {// Actor object returned (less correct)publicKeyCache.set(uri, body.publicKey.publicKeyPem);return body.publicKey.publicKeyPem;}+ // Response didn't contain expected public key+ publicKeyCache.set(uri, null);throw new Error('[[error:activitypub.pubKey-not-found]]');} catch (err) {+ // Cache the failed request+ publicKeyCache.set(uri, null);+// Re-throw with context if neededif (err.message.includes('reserved-ip-address')) {throw new Error('[[error:activitypub.invalid-id]]');