From 73d727d07615df786306ae547c7f59e7c6a20177 Mon Sep 17 00:00:00 2001 From: Will Murphy Date: Thu, 29 Dec 2022 08:30:59 -0600 Subject: [PATCH] domain blocking --- index.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/index.js b/index.js index 98b1360..7f71700 100644 --- a/index.js +++ b/index.js @@ -74,6 +74,28 @@ async function actorOnDemand (req, res, next) { } catch (err) { return next(err) } next() } +// Do not boost posts from servers who abuse the service. +apex.net.inbox.post.splice( + // Blocked domain check is inserted into apex inbox route right after the sender is verified + apex.net.inbox.post.indexOf(apex.net.security.verifySignature) + 1, + 0, + async function rejectBlockedDomains (req, res, next) { + try { + const url = new URL(res.locals.apex.sender.id) + const domain = await req.app.locals.apex.store.db.collection('servers').findOne({ + hostname: url.hostname + }) + if (domain?.blocked) { + console.log(`Ignoring post from ${url}:`, req.body) + return res.sendStatus(200) + } + } catch (err) { + console.error('Error checking domain blocks:', err) + } + next() + } +) + // define routes using prepacakged middleware collections app.route(routes.inbox) .post(actorOnDemand, apex.net.inbox.post) @@ -198,6 +220,12 @@ client.connect() const { default: AutoEncrypt } = await import('@small-tech/auto-encrypt') apex.store.db = client.db(DB_NAME) await apex.store.setup() + await apex.store.db.collection('servers').createIndex({ + hostname: 1 + }, { + name: 'servers-primary', + unique: true + }) apex.systemUser = await apex.store.getObject(apex.utils.usernameToIRI('system_service'), true) if (!apex.systemUser) { const systemUser = await apex.createActor('system_service', `${DOMAIN} system service`, `${DOMAIN} system service`, icon, 'Service')