Skip to content

Node.js based forum software built for the modern web

refactor: notes.assert should call instances.isAllowed on each item prior to posting

Julian Lama696e103e040
diff --git a/src/activitypub/inbox.js b/src/activitypub/inbox.js
index b0cc260885..99cbe5eba7 100644
--- a/src/activitypub/inbox.js
+++ b/src/activitypub/inbox.js
@@ -55,7 +55,7 @@ inbox.create = async (req) => {
cid = Array.from(cids)[0];
}
- const asserted = await activitypub.notes.assert(0, object, { cid, blocklist: req?.res?.locals?.['ap:blocklist'] });
+ const asserted = await activitypub.notes.assert(0, object, { cid });
if (asserted) {
await activitypub.feps.announce(object.id, req.body);
// api.activitypub.add(req, { pid: object.id });
@@ -216,7 +216,7 @@ inbox.update = async (req) => {
cid = Array.from(cids)[0];
}
- const asserted = await activitypub.notes.assert(0, object.id, { cid, blocklist: req?.res?.locals?.['ap:blocklist'] });
+ const asserted = await activitypub.notes.assert(0, object.id, { cid });
if (asserted) {
activitypub.feps.announce(object.id, req.body);
}
@@ -363,7 +363,7 @@ inbox.like = async (req) => {
exists = await posts.exists(object.id);
if (!exists) {
// Proactively pull in the note
- const asserted = await activitypub.notes.assert(0, object.id, { skipChecks: 1, blocklist: req?.res?.locals?.['ap:blocklist'] });
+ const asserted = await activitypub.notes.assert(0, object.id, { skipChecks: 1 });
if (!asserted) {
throw new Error('[[error:invalid-pid]]');
}
@@ -540,7 +540,7 @@ inbox.announce = async (req) => {
return;
}
- const assertion = await activitypub.notes.assert(0, pid, { cid, skipChecks: true, blocklist: req?.res?.locals?.['ap:blocklist'] });
+ const assertion = await activitypub.notes.assert(0, pid, { cid, skipChecks: true });
if (!assertion) {
return;
}
diff --git a/src/activitypub/notes.js b/src/activitypub/notes.js
index 822dfbef70..0fea5875af 100644
--- a/src/activitypub/notes.js
+++ b/src/activitypub/notes.js
@@ -254,24 +254,33 @@ Notes.assert = async (uid, input, options = { skipChecks: false }) => {
const count = unprocessed.length;
activitypub.helpers.log(`[notes/assert] ${count} new note(s) found.`);
- const shouldQueue = options.blocklist?.severity === 3;
-
if (!hasTid) {
- if (shouldQueue) {
- activitypub.helpers.log(`[activitypub/notes.assert] Queuing main post (${mainPid}) due to blocklist severity 3`);
- await posts.addToQueue({
- uid: authorId,
- cid: options.cid || cid,
- pid: mainPid,
- title,
- timestamp,
- content: mainPost.content,
- sourceContent: mainPost.sourceContent,
- generatedTitle,
- _activitypub: mainPost._activitypub,
- });
- // Drop the rest of the chain — replies without OP don't make sense
- return { tid: null, queued: 1 };
+ activitypub.helpers.log(`[activitypub/notes.assert] hasTid=${hasTid}, skipChecks=${options.skipChecks}, mainPid=${mainPid}`);
+ if (!options.skipChecks) {
+ const { hostname: mainHostname } = new URL(mainPid);
+ const mainResult = await activitypub.instances.isAllowed(mainHostname);
+
+ if (!mainResult.allowed) {
+ activitypub.helpers.log(`[activitypub/notes.assert] Not asserting ${mainPid}, domain is blocked.`);
+ return null;
+ }
+
+ if (mainResult.severity === 3) {
+ activitypub.helpers.log(`[activitypub/notes.assert] Queuing main post (${mainPid}) due to blocklist severity 3`);
+ await posts.addToQueue({
+ uid: authorId,
+ cid: options.cid || cid,
+ pid: mainPid,
+ title,
+ timestamp,
+ content: mainPost.content,
+ sourceContent: mainPost.sourceContent,
+ generatedTitle,
+ _activitypub: mainPost._activitypub,
+ });
+ // Drop the rest of the chain — replies without OP don't make sense
+ return { tid: null, queued: 1 };
+ }
}
const { to, cc } = mainPost._activitypub;
@@ -326,19 +335,29 @@ Notes.assert = async (uid, input, options = { skipChecks: false }) => {
const { to, cc } = post._activitypub;
try {
- if (shouldQueue) {
- activitypub.helpers.log(`[activitypub/notes.assert] Queuing reply (${post.pid}) due to blocklist severity 3`);
- await posts.addToQueue({
- uid: post.uid,
- tid,
- pid: post.pid,
- content: post.content,
- sourceContent: post.sourceContent,
- timestamp: post.timestamp,
- _activitypub: post._activitypub,
- });
- queued += 1;
- return;
+ if (!options.skipChecks) {
+ const { hostname: postHostname } = new URL(post.pid);
+ const postResult = await activitypub.instances.isAllowed(postHostname);
+
+ if (!postResult.allowed) {
+ activitypub.helpers.log(`[activitypub/notes.assert] Not asserting ${post.pid}, domain is blocked.`);
+ return;
+ }
+
+ if (postResult.severity === 3) {
+ activitypub.helpers.log(`[activitypub/notes.assert] Queuing reply (${post.pid}) due to blocklist severity 3`);
+ await posts.addToQueue({
+ uid: post.uid,
+ tid,
+ pid: post.pid,
+ content: post.content,
+ sourceContent: post.sourceContent,
+ timestamp: post.timestamp,
+ _activitypub: post._activitypub,
+ });
+ queued += 1;
+ return;
+ }
}
const postData = await topics.reply(post);
diff --git a/test/activitypub/notes.js b/test/activitypub/notes.js
index a8be6a55f3..f5ac916f35 100644
--- a/test/activitypub/notes.js
+++ b/test/activitypub/notes.js
@@ -435,16 +435,45 @@ describe('Notes', () => {
await db.delete('post:queue');
}
+ /**
+ * Mock instances.isAllowed to return a specific severity for a domain.
+ */
+ function mockBlockedDomain(domain, severity) {
+ activitypub.instances.isAllowed = async (hostname) => {
+ if (hostname === domain) {
+ return {
+ allowed: false,
+ severity,
+ listUrl: 'https://example.org/blocklist.csv',
+ };
+ }
+ return activitypub.instances._originalIsAllowed(hostname);
+ };
+ }
+
+ before(async () => {
+ activitypub.instances._originalIsAllowed = activitypub.instances.isAllowed;
+ });
+
+ after(async () => {
+ if (activitypub.instances._originalIsAllowed) {
+ activitypub.instances.isAllowed = activitypub.instances._originalIsAllowed;
+ }
+ });
+
beforeEach(async () => {
await clearQueue();
});
describe('!hasTid — new topic', () => {
+ beforeEach(() => {
+ mockBlockedDomain('blocked.example.org', 3);
+ });
+
it('should queue the main post instead of creating it', async () => {
- const { id } = helpers.mocks.note();
- const assertion = await activitypub.notes.assert(0, id, {
+ const noteId = `https://blocked.example.org/object/${utils.generateUUID()}`;
+ const assertion = await activitypub.notes.assert(0, noteId, {
skipChecks: true,
- blocklist: { severity: 3 },
});
assert(assertion);
@@ -457,15 +486,12 @@ describe('Notes', () => {
});
it('should queue parent and also queue replies when hasTid is false', async () => {
- const { id: parentId } = helpers.mocks.note();
- const { id: replyId } = helpers.mocks.note({
- inReplyTo: parentId,
- });
+ const parentId = `https://blocked.example.org/object/${utils.generateUUID()}`;
+ const replyId = `https://blocked.example.org/object/${utils.generateUUID()}`;
// Assert the parent with severity 3 — should queue and return tid: null
const parentAssertion = await activitypub.notes.assert(0, parentId, {
skipChecks: true,
- blocklist: { severity: 3 },
});
assert(parentAssertion);
@@ -475,7 +501,6 @@ describe('Notes', () => {
// Assert the reply with severity 3 — parent has no tid, so hasTid is false
const replyAssertion = await activitypub.notes.assert(0, replyId, {
skipChecks: true,
- blocklist: { severity: 3 },
});
assert(replyAssertion);
@@ -509,13 +534,14 @@ describe('Notes', () => {
mainPid = postData.pid;
});
+ beforeEach(() => {
+ mockBlockedDomain('blocked.example.org', 3);
+ });
+
it('should queue replies when severity is 3', async () => {
- const { id } = helpers.mocks.note({
- inReplyTo: `${nconf.get('url')}/post/${mainPid}`,
- });
- const assertion = await activitypub.notes.assert(0, id, {
+ const replyId = `https://blocked.example.org/object/${utils.generateUUID()}`;
+ const assertion = await activitypub.notes.assert(0, replyId, {
skipChecks: true,
- blocklist: { severity: 3 },
});
assert(assertion);
@@ -527,20 +553,14 @@ describe('Notes', () => {
});
it('should queue multiple replies', async () => {
- const { id: id1 } = helpers.mocks.note({
- inReplyTo: `${nconf.get('url')}/post/${mainPid}`,
- });
- const { id: id2 } = helpers.mocks.note({
- inReplyTo: `${nconf.get('url')}/post/${mainPid}`,
- });
+ const id1 = `https://blocked.example.org/object/${utils.generateUUID()}`;
+ const id2 = `https://blocked.example.org/object/${utils.generateUUID()}`;
const assertion1 = await activitypub.notes.assert(0, id1, {
skipChecks: true,
- blocklist: { severity: 3 },
});
const assertion2 = await activitypub.notes.assert(0, id2, {
skipChecks: true,
- blocklist: { severity: 3 },
});
assert(assertion1);
@@ -556,9 +576,9 @@ describe('Notes', () => {
});
describe('null case', () => {
- it('should NOT queue posts when blocklist is undefined', async () => {
- const { id } = helpers.mocks.note();
- const assertion = await activitypub.notes.assert(0, id, {
+ it('should NOT queue posts when domain is not blocked', async () => {
+ const noteId = `https://allowed.example.org/object/${utils.generateUUID()}`;
+ const assertion = await activitypub.notes.assert(0, noteId, {
skipChecks: true,
});
@@ -573,10 +593,10 @@ describe('Notes', () => {
describe('Severity 1 and 2', () => {
it('should NOT queue posts with severity 1 (suspend)', async () => {
- const { id } = helpers.mocks.note();
- const assertion = await activitypub.notes.assert(0, id, {
+ mockBlockedDomain('blocked.example.org', 1);
+ const noteId = `https://blocked.example.org/object/${utils.generateUUID()}`;
+ const assertion = await activitypub.notes.assert(0, noteId, {
skipChecks: true,
- blocklist: { severity: 1 },
});
assert(assertion);
@@ -588,10 +608,10 @@ describe('Notes', () => {
});
it('should NOT queue posts with severity 2 (silence)', async () => {
- const { id } = helpers.mocks.note();
- const assertion = await activitypub.notes.assert(0, id, {
+ mockBlockedDomain('blocked.example.org', 2);
+ const noteId = `https://blocked.example.org/object/${utils.generateUUID()}`;
+ const assertion = await activitypub.notes.assert(0, noteId, {
skipChecks: true,
- blocklist: { severity: 2 },
});
assert(assertion);