Skip to content

Node.js based forum software built for the modern web

refactor: blocklist tests and check/severity info methods

Julian Lam5faa853adeed
diff --git a/src/activitypub/blocklists.js b/src/activitypub/blocklists.js
index f41c64b50c..e270562369 100644
--- a/src/activitypub/blocklists.js
+++ b/src/activitypub/blocklists.js
@@ -90,21 +90,43 @@ Blocklists.check = async (domain) => {
let present = await db.isMemberOfSortedSets(blocklists.map(({ url }) => `blocklist:${url}`), domain);
present = present.reduce((memo, present) => memo || present, false);
- return {
- allowed: !present,
- severity: present ? await Blocklists.getSeverity(domain) : null,
- };
-};
+ if (!present) {
+ return { allowed: true, severity: null, listUrl: null };
+ }
-Blocklists.getSeverity = async (domain) => {
- const blocklists = await Blocklists.list();
const keys = blocklists.map(({ url }) => `blocklist:${url}:severity`);
const severityMaps = await db.getObjects(keys);
- for (const severityMap of severityMaps) {
+ let bestSeverity = null;
+ let bestListUrl = null;
+
+ blocklists.forEach(({ url }, idx) => {
+ const severityMap = severityMaps[idx];
if (severityMap && severityMap[domain]) {
- return severityScore[severityMap[domain]] ?? 1;
+ const score = severityScore[severityMap[domain]] ?? 1;
+ if (bestSeverity === null || score < bestSeverity) {
+ bestSeverity = score;
+ bestListUrl = url;
+ }
}
+ });
+
+ return {
+ allowed: false,
+ severity: bestSeverity,
+ listUrl: bestListUrl,
+ };
+};
+
+Blocklists.getSeverityInfo = async (domain) => {
+ const result = await Blocklists.check(domain);
+ if (result.severity === null) {
+ return null;
}
- return null;
+ return { severity: result.severity, listUrl: result.listUrl };
+};
+
+Blocklists.getSeverity = async (domain) => {
+ const result = await Blocklists.check(domain);
+ return result.severity;
};
diff --git a/src/activitypub/index.js b/src/activitypub/index.js
index 6e6e471c36..ad1e35dd3c 100644
--- a/src/activitypub/index.js
+++ b/src/activitypub/index.js
@@ -154,7 +154,8 @@ ActivityPub.resolveInboxes = async (ids) => {
let allowed = false;
try {
const { hostname } = new URL(inbox);
- allowed = await ActivityPub.instances.isAllowed(hostname);
+ const result = await ActivityPub.instances.isAllowed(hostname);
+ allowed = result.allowed;
if (!allowed) {
blocked.push(inbox);
}
@@ -397,8 +398,8 @@ ActivityPub.get = async (type, id, uri, options) => {
}
const { hostname } = new URL(uri);
- const allowed = await ActivityPub.instances.isAllowed(hostname);
- if (!allowed) {
+ const result = await ActivityPub.instances.isAllowed(hostname);
+ if (!result.allowed) {
ActivityPub.helpers.log(`[activitypub/get] Not retrieving ${uri}, domain is blocked.`);
const e = new Error(`[[error:activitypub.get-failed]]`);
e.code = `ap_get_domain_blocked`;
diff --git a/src/activitypub/instances.js b/src/activitypub/instances.js
index 39f0509bf1..891e9375d9 100644
--- a/src/activitypub/instances.js
+++ b/src/activitypub/instances.js
@@ -15,19 +15,20 @@ Instances.getCount = async () => db.sortedSetCard('instances:lastSeen');
Instances.list = async () => db.getSortedSetMembers('instances:lastSeen');
Instances.isAllowed = async (domain) => {
- /* eslint-disable-next-line no-unused-vars */
const { allowed, severity } = await activitypub.blocklists.check(domain);
+ const blocklistInfo = await activitypub.blocklists.getSeverityInfo(domain);
+
let { activitypubFilter: type, activitypubFilterList: list } = meta.config;
if (!allowed && !type) {
- return false;
+ return { allowed: false, severity, ...blocklistInfo };
}
list = new Set(String(list).split('\n'));
if (allowed) {
- return type ? list.has(domain) : true;
+ return { allowed: type ? list.has(domain) : true, severity, ...blocklistInfo };
}
- return type ? !list.has(domain) : false;
+ return { allowed: type ? !list.has(domain) : false, severity, ...blocklistInfo };
};
diff --git a/src/middleware/activitypub.js b/src/middleware/activitypub.js
index 92732a894c..65105859e2 100644
--- a/src/middleware/activitypub.js
+++ b/src/middleware/activitypub.js
@@ -105,12 +105,21 @@ middleware.assertPayload = helpers.try(async function (req, res, next) {
// Domain check
const { hostname } = new URL(actor);
- const allowed = await activitypub.instances.isAllowed(hostname);
- if (!allowed) {
+ const result = await activitypub.instances.isAllowed(hostname);
+ activitypub.instances.log(hostname);
+
+ if (!result.allowed) {
activitypub.helpers.log(`[middleware/activitypub] Blocked incoming activity from ${hostname}.`);
return res.sendStatus(403);
}
- activitypub.instances.log(hostname);
+
+ // Store blocklist info for severity-based handling (silence, filter)
+ if (result.severity !== null && result.severity >= 2) {
+ req.locals['ap:blocklist'] = {
+ severity: result.severity,
+ listUrl: result.listUrl,
+ };
+ }
// Origin checking
if (typeof object !== 'string' && object.hasOwnProperty('id')) {
diff --git a/test/activitypub/blocklists.js b/test/activitypub/blocklists.js
index f99277e066..d2254f9b17 100644
--- a/test/activitypub/blocklists.js
+++ b/test/activitypub/blocklists.js
@@ -198,8 +198,8 @@ describe('ActivityPub blocklists', () => {
await activitypub.blocklists.remove(u);
- const severity = await activitypub.blocklists.getSeverity('severed.com');
- assert.strictEqual(severity, null);
+ const result = await activitypub.blocklists.check('severed.com');
+ assert.strictEqual(result.severity, null);
});
});
@@ -302,7 +302,7 @@ describe('ActivityPub blocklists', () => {
it('should return { allowed: true, severity: null } when no blocklists exist', async () => {
const result = await activitypub.blocklists.check('any.example.com');
- assert.deepStrictEqual(result, { allowed: true, severity: null });
+ assert.deepStrictEqual(result, { allowed: true, severity: null, listUrl: null });
});
it('should return { allowed: false, severity: N } when domain is blocked', async () => {
diff --git a/test/activitypub/outbox.js b/test/activitypub/outbox.js
new file mode 100644
index 0000000000..1682f411fe
--- /dev/null
+++ b/test/activitypub/outbox.js
@@ -0,0 +1,179 @@
+'use strict';
+
+const assert = require('assert');
+const nconf = require('nconf');
+
+const db = require('../mocks/databasemock');
+const controllers = require('../../src/controllers');
+const middleware = require('../../src/middleware');
+const activitypub = require('../../src/activitypub');
+const utils = require('../../src/utils');
+const user = require('../../src/user');
+const categories = require('../../src/categories');
+const topics = require('../../src/topics');
+const posts = require('../../src/posts');
+
+describe('Outbox', () => {
+ let uid;
+ let cid;
+ let pid;
+ let resBody;
+
+ before(async () => {
+ nconf.set('runJobs', 1);
+ });
+
+ after(async () => {
+ nconf.set('runJobs', undefined);
+ });
+
+ beforeEach(async () => {
+ uid = await user.create({ username: utils.generateUUID().slice(0, 8) });
+ ({ cid } = await categories.create({ name: utils.generateUUID().slice(0, 8) }));
+ const { postData } = await topics.post({
+ uid,
+ cid,
+ title: utils.generateUUID(),
+ content: utils.generateUUID(),
+ });
+ pid = postData.pid;
+ });
+
+ afterEach(async () => {
+ resBody = null;
+ });
+
+ const collectResponse = (body) => {
+ resBody = body;
+ return {
+ status: (code) => ({ json: (b) => collectResponse(b) }),
+ };
+ };
+
+ describe('getOutbox', () => {
+ it('should return something for an existing user with posts', async () => {
+ const req = { params: { uid }, query: {} };
+ const res = collectResponse();
+
+ await controllers.activitypub.getOutbox(req, res);
+
+ assert(resBody);
+ assert(resBody.orderedItems);
+ assert(resBody.orderedItems.length > 0);
+ });
+
+ it('should return in the expected ActivityPub format', async () => {
+ const req = { params: { uid }, query: {} };
+ const res = collectResponse();
+
+ await controllers.activitypub.getOutbox(req, res);
+
+ assert.strictEqual(resBody['@context'], 'https://www.w3.org/ns/activitystreams');
+ assert.strictEqual(resBody.type, 'OrderedCollection');
+ assert.strictEqual(resBody.id, `${nconf.get('url')}/uid/${uid}/outbox`);
+ assert.strictEqual(resBody.totalItems, 1);
+ assert(Array.isArray(resBody.orderedItems));
+ assert.strictEqual(resBody.orderedItems.length, 1);
+
+ const activity = resBody.orderedItems[0];
+ assert.strictEqual(activity.type, 'Create');
+ assert(['Note', 'Article'].includes(activity.object.type));
+ assert.strictEqual(activity.actor, `${nconf.get('url')}/uid/${uid}`);
+ assert.strictEqual(activity.object.url, `${nconf.get('url')}/post/${pid}`);
+ });
+
+ it('should not return anything for a non-existent user via middleware assertion', async () => {
+ const nonExistentUid = '999999999';
+ const userExists = await user.exists(nonExistentUid);
+ assert(!userExists, 'User should not exist');
+
+ const req = { params: { uid: nonExistentUid }, query: {} };
+ let capturedCode;
+ const res = {
+ locals: {},
+ req: { method: 'GET' },
+ status: (code) => {
+ capturedCode = code;
+ return {
+ json: () => res,
+ };
+ },
+ };
+
+ await middleware.assert.user(req, res, () => {});
+
+ assert.strictEqual(capturedCode, 404);
+ });
+
+ it('should return an empty collection for a user with no activities', async () => {
+ const emptyUid = await user.create({ username: utils.generateUUID().slice(0, 8) });
+ const req = { params: { uid: emptyUid }, query: {} };
+ const res = collectResponse();
+
+ await controllers.activitypub.getOutbox(req, res);
+
+ assert(resBody);
+ assert.strictEqual(resBody.type, 'OrderedCollection');
+ assert.strictEqual(resBody.totalItems, 0);
+ assert(Array.isArray(resBody.orderedItems));
+ assert.strictEqual(resBody.orderedItems.length, 0);
+ });
+
+ it('should include upvote and downvote activities when present', async () => {
+ // Upvote the post
+ await posts.upvote(pid, uid);
+ // Downvote another post (create a second post to downvote)
+ const { postData: secondPost } = await topics.post({
+ uid,
+ cid,
+ title: utils.generateUUID(),
+ content: utils.generateUUID(),
+ });
+ await posts.downvote(secondPost.pid, uid);
+
+ const req = { params: { uid }, query: {} };
+ const res = collectResponse();
+
+ await controllers.activitypub.getOutbox(req, res);
+
+ assert(resBody.orderedItems.length >= 3);
+
+ const types = resBody.orderedItems.map((a) => a.type);
+ assert(types.includes('Create'));
+
+ const upvoteActivities = resBody.orderedItems.filter(
+ (a) => a.type === 'Like' && a.object.type === 'Note'
+ );
+ assert(upvoteActivities.length >= 1);
+
+ const dislikeActivities = resBody.orderedItems.filter(
+ (a) => a.type === 'Dislike' && a.object.type === 'Note'
+ );
+ assert(dislikeActivities.length >= 1);
+ });
+
+ it('should paginate when there are more than 20 items', async () => {
+ // Create 25 posts
+ for (let i = 0; i < 25; i++) {
+ await topics.post({
+ uid,
+ cid,
+ title: utils.generateUUID(),
+ content: utils.generateUUID(),
+ });
+ }
+
+ const req = { params: { uid }, query: {} };
+ const res = collectResponse();
+
+ await controllers.activitypub.getOutbox(req, res);
+
+ assert.strictEqual(resBody.type, 'OrderedCollectionPage');
+ assert.strictEqual(resBody.totalItems, 26);
+ assert(resBody.orderedItems.length <= 20);
+ assert(resBody.first);
+ assert(resBody.last);
+ assert(resBody.partOf);
+ });
+ });
+});