Skip to content

Node.js based forum software built for the modern web

fix(activitypub): remove mutex lock from fetchPublicKey rate limiter and raise default to 256

Julian Lam7f11768be70c
diff --git a/install/data/defaults.json b/install/data/defaults.json
index 194c752ee0..d94a83f0b5 100644
--- a/install/data/defaults.json
+++ b/install/data/defaults.json
@@ -207,7 +207,7 @@
"activitypubEnabled": 1,
"activitypubAllowLoopback": 0,
"activitypubParentTraversalDepth": 50,
- "activitypubPublicKeyFetchRateLimit": 0,
+ "activitypubPublicKeyFetchRateLimit": 256,
"activitypubProbe": 1,
"activitypubProbeTimeout": 2000,
"activitypubContentPruneDays": 30,
diff --git a/public/language/en-GB/admin/settings/activitypub.json b/public/language/en-GB/admin/settings/activitypub.json
index 4bc07e19a2..b708f5642b 100644
--- a/public/language/en-GB/admin/settings/activitypub.json
+++ b/public/language/en-GB/admin/settings/activitypub.json
@@ -10,7 +10,7 @@
"enabled": "Enable Federation",
"enabled-help": "If enabled, will allow this NodeBB will be able to communicate with all Activitypub-enabled clients on the wider fediverse.",
"publicKeyFetchRateLimit": "Public Key Fetch Rate Limit",
- "publicKeyFetchRateLimit-help": "Maximum number of public key fetch requests allowed per minute per IP. Disabled by default; enable if you need to guard against outbound request flooding, but may impact federation. A value of 512 is a reasonable starting point for active instances.",
+ "publicKeyFetchRateLimit-help": "Maximum number of public key fetch requests allowed per minute per IP. (Default: 256)",
"allowLoopback": "Allow loopback processing",
"allowLoopback-help": "Useful for debugging purposes only. You should probably leave this disabled.",
"parentTraversalDepth": "Parent traversal depth",
diff --git a/src/activitypub/index.js b/src/activitypub/index.js
index d3a6595b98..cc717c4101 100644
--- a/src/activitypub/index.js
+++ b/src/activitypub/index.js
@@ -236,16 +236,14 @@ ActivityPub.fetchPublicKey = async (uri, ip) => {
}
// Check rate limit for this IP (0 = disabled)
- let lockId;
- let currentCount;
if (meta.config.activitypubPublicKeyFetchRateLimit > 0) {
- lockId = `pubkey:${ip}`;
- currentCount = publicKeyFetchRateLimit.get(lockId) || 0;
- const lockStatus = await db.incrObjectField('locks', lockId);
- if (lockStatus > 1 || currentCount >= meta.config.activitypubPublicKeyFetchRateLimit) {
+ const lockId = `pubkey:${ip}`;
+ const currentCount = publicKeyFetchRateLimit.get(lockId) || 0;
+ if (currentCount >= meta.config.activitypubPublicKeyFetchRateLimit) {
winston.warn(`[activitypub/fetchPublicKey] Rate limit exceeded for IP ${ip}`);
throw new Error('[[error:activitypub.rate-limited]]');
}
+ publicKeyFetchRateLimit.set(lockId, currentCount + 1, 60 * 1000);
}
try {
@@ -290,11 +288,6 @@ ActivityPub.fetchPublicKey = async (uri, ip) => {
throw new Error('[[error:activitypub.invalid-id]]');
}
throw err;
- } finally {
- if (lockId) {
- await db.deleteObjectField('locks', lockId);
- publicKeyFetchRateLimit.set(lockId, currentCount + 1, 60 * 1000);
- }
}
};