Node.js based forum software built for the modern web
fix(activitypub): make parent traversal depth configurable and add tests
Julian Lam73e3544cd48d
diff --git a/install/data/defaults.json b/install/data/defaults.jsonindex c1f89c217a..a1a6dfd28a 100644--- a/install/data/defaults.json+++ b/install/data/defaults.json@@ -206,6 +206,7 @@"tinyconBackground": "#ff0000","activitypubEnabled": 1,"activitypubAllowLoopback": 0,+ "activitypubParentTraversalDepth": 50,"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.jsonindex c54eddc96a..0bf6a05e49 100644--- a/public/language/en-GB/admin/settings/activitypub.json+++ b/public/language/en-GB/admin/settings/activitypub.json@@ -11,6 +11,8 @@"enabled-help": "If enabled, will allow this NodeBB will be able to communicate with all Activitypub-enabled clients on the wider fediverse.","allowLoopback": "Allow loopback processing","allowLoopback-help": "Useful for debugging purposes only. You should probably leave this disabled.",+ "parentTraversalDepth": "Parent traversal depth",+ "parentTraversalDepth-help": "Maximum number of times to traverse up the reply chain when building context. (Default: 50)","probe": "Open in App","probe-enabled": "Try to open ActivityPub-enabled resources in NodeBB",diff --git a/src/activitypub/notes.js b/src/activitypub/notes.jsindex a2afbda2a8..676100b31a 100644--- a/src/activitypub/notes.js+++ b/src/activitypub/notes.js@@ -618,7 +618,20 @@ Notes.getParentChain = async (uid, input) => {const id = activitypub.helpers.isUri(input) ? input : input.id;const chain = new Set();+ const visited = new Set();+ let depth = 0;const traverse = async (uid, id) => {+ const maxDepth = meta.config.activitypubParentTraversalDepth || 50;+ if (depth >= maxDepth) {+ activitypub.helpers.log(`[activitypub/notes/getParentChain] Depth limit reached (${maxDepth}), terminating.`);+ return;+ }+ if (visited.has(id)) {+ return;+ }+ visited.add(id);+ depth += 1;+// Handle remote reference to local postconst { type, id: localId } = await activitypub.helpers.resolveLocalId(id);if (type === 'post' && localId) {diff --git a/src/views/admin/federation/general.tpl b/src/views/admin/federation/general.tplindex ea10732c1e..3918c0c936 100644--- a/src/views/admin/federation/general.tpl+++ b/src/views/admin/federation/general.tpl@@ -22,6 +22,13 @@<label class="form-check-label">[[admin/settings/activitypub:allowLoopback]]</label><p class="form-text">[[admin/settings/activitypub:allowLoopback-help]]</p></div>+ <div class="mb-3">+ <label class="form-label" for="activitypubParentTraversalDepth">[[admin/settings/activitypub:parentTraversalDepth]]</label>+ <input type="number" id="activitypubParentTraversalDepth" name="activitypubParentTraversalDepth" data-field="activitypubParentTraversalDepth" title="[[admin/settings/activitypub:parentTraversalDepth]]" class="form-control" />+ <div class="form-text">+ [[admin/settings/activitypub:parentTraversalDepth-help]]+ </div>+ </div></form></div>diff --git a/test/activitypub/notes.js b/test/activitypub/notes.jsindex fdede98971..8637fef398 100644--- a/test/activitypub/notes.js+++ b/test/activitypub/notes.js@@ -660,6 +660,60 @@ describe('Notes', () => {});});+ describe('getParentChain', () => {+ it('should retrieve a two-note chain via inReplyTo', async () => {+ const { id: parentId } = helpers.mocks.note();+ const { id: childId } = helpers.mocks.note({ inReplyTo: parentId });++ const chain = await activitypub.notes.getParentChain(0, childId);++ assert(chain instanceof Set);+ assert.strictEqual(chain.size, 2);++ const pids = Array.from(chain).map((n) => n.pid);+ assert(pids.includes(parentId));+ assert(pids.includes(childId));+ });++ it('should stop at configured depth', async () => {+ meta.config.activitypubParentTraversalDepth = 30;++ const noteIds = [];+ let previousId = null;+ for (let i = 0; i < 55; i += 1) {+ const noteData = previousId ? { inReplyTo: previousId } : {};+ const { id } = helpers.mocks.note(noteData);+ noteIds.push(id);+ previousId = id;+ }++ const chain = await activitypub.notes.getParentChain(0, noteIds[noteIds.length - 1]);++ assert(chain instanceof Set);+ assert(chain.size <= 30);++ delete meta.config.activitypubParentTraversalDepth;+ });++ it('should use default depth of 50 when not configured', async () => {+ delete meta.config.activitypubParentTraversalDepth;++ const noteIds = [];+ let previousId = null;+ for (let i = 0; i < 55; i += 1) {+ const noteData = previousId ? { inReplyTo: previousId } : {};+ const { id } = helpers.mocks.note(noteData);+ noteIds.push(id);+ previousId = id;+ }++ const chain = await activitypub.notes.getParentChain(0, noteIds[noteIds.length - 1]);++ assert(chain instanceof Set);+ assert(chain.size <= 50);+ });+ });+describe('auto-categorization with queue rule', () => {let remoteCid;let targetCid;