|
| 1 | +/** |
| 2 | + * Federation blocklist routes. |
| 3 | + * |
| 4 | + * Operators can block specific peer nodes from federating with this node. |
| 5 | + * Blocked nodes cannot handshake, ping, sync, or appear in bootstrap. |
| 6 | + * |
| 7 | + * Routes: |
| 8 | + * GET /api/federation/blocks — list blocked nodes (admin) |
| 9 | + * POST /api/federation/blocks — add a block (admin) |
| 10 | + * DELETE /api/federation/blocks/:domain — remove a block (admin) |
| 11 | + * GET /api/federation/blocks/check — check if a domain is blocked (public — for peers) |
| 12 | + */ |
| 13 | + |
| 14 | +import { Router, type IRouter, type Request, type Response } from "express"; |
| 15 | +import { z } from "zod/v4"; |
| 16 | +import { db, federationBlocksTable, nodesTable } from "@workspace/db"; |
| 17 | +import { eq } from "drizzle-orm"; |
| 18 | +import { asyncHandler, AppError } from "../lib/errors"; |
| 19 | +import { requireAdmin } from "../middleware/requireAdmin"; |
| 20 | +import { writeLimiter } from "../middleware/rateLimiter"; |
| 21 | +import logger from "../lib/logger"; |
| 22 | + |
| 23 | +const router: IRouter = Router(); |
| 24 | + |
| 25 | +// In-memory Set for O(1) block checks on every incoming federation request. |
| 26 | +// Loaded at startup and kept in sync with DB mutations. |
| 27 | +export const blockedDomains = new Set<string>(); |
| 28 | + |
| 29 | +/** Load all blocked domains into the in-memory set at startup */ |
| 30 | +export async function loadBlocklist(): Promise<void> { |
| 31 | + try { |
| 32 | + const rows = await db.select({ nodeDomain: federationBlocksTable.nodeDomain }).from(federationBlocksTable); |
| 33 | + blockedDomains.clear(); |
| 34 | + for (const row of rows) blockedDomains.add(row.nodeDomain.toLowerCase()); |
| 35 | + logger.info({ count: blockedDomains.size }, "[blocklist] Loaded federation blocklist"); |
| 36 | + } catch (err) { |
| 37 | + logger.warn({ err }, "[blocklist] Failed to load blocklist — continuing without it"); |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +/** Returns true if the given domain is on the blocklist */ |
| 42 | +export function isBlocked(domain: string): boolean { |
| 43 | + return blockedDomains.has(domain.toLowerCase()); |
| 44 | +} |
| 45 | + |
| 46 | +// ── GET /api/federation/blocks ──────────────────────────────────────────────── |
| 47 | + |
| 48 | +router.get("/federation/blocks", requireAdmin, asyncHandler(async (req: Request, res: Response) => { |
| 49 | + const blocks = await db.select().from(federationBlocksTable).orderBy(federationBlocksTable.createdAt); |
| 50 | + res.json({ blocks, total: blocks.length }); |
| 51 | +})); |
| 52 | + |
| 53 | +// ── POST /api/federation/blocks ─────────────────────────────────────────────── |
| 54 | + |
| 55 | +router.post("/federation/blocks", requireAdmin, writeLimiter, asyncHandler(async (req: Request, res: Response) => { |
| 56 | + const { nodeDomain, reason } = z.object({ |
| 57 | + nodeDomain: z.string().min(1).max(253).toLowerCase(), |
| 58 | + reason: z.string().max(500).optional(), |
| 59 | + }).parse(req.body); |
| 60 | + |
| 61 | + // Check if already blocked |
| 62 | + const [existing] = await db.select({ id: federationBlocksTable.id }).from(federationBlocksTable) |
| 63 | + .where(eq(federationBlocksTable.nodeDomain, nodeDomain)); |
| 64 | + if (existing) throw AppError.conflict(`${nodeDomain} is already blocked`); |
| 65 | + |
| 66 | + const [block] = await db.insert(federationBlocksTable).values({ |
| 67 | + nodeDomain, |
| 68 | + reason: reason ?? null, |
| 69 | + blockedBy: req.user?.id ?? null, |
| 70 | + }).returning(); |
| 71 | + |
| 72 | + // Update in-memory set immediately |
| 73 | + blockedDomains.add(nodeDomain); |
| 74 | + |
| 75 | + // Mark the peer node as inactive if it exists in our nodes table |
| 76 | + await db.update(nodesTable) |
| 77 | + .set({ status: "inactive" }) |
| 78 | + .where(eq(nodesTable.domain, nodeDomain)); |
| 79 | + |
| 80 | + logger.info({ nodeDomain, reason, blockedBy: req.user?.id }, "[blocklist] Node blocked"); |
| 81 | + |
| 82 | + res.status(201).json({ block, message: `${nodeDomain} is now blocked from federating with this node.` }); |
| 83 | +})); |
| 84 | + |
| 85 | +// ── DELETE /api/federation/blocks/:domain ──────────────────────────────────── |
| 86 | + |
| 87 | +router.delete("/federation/blocks/:domain", requireAdmin, writeLimiter, asyncHandler(async (req: Request, res: Response) => { |
| 88 | + const domain = (req.params.domain as string).toLowerCase(); |
| 89 | + |
| 90 | + const [deleted] = await db.delete(federationBlocksTable) |
| 91 | + .where(eq(federationBlocksTable.nodeDomain, domain)) |
| 92 | + .returning(); |
| 93 | + |
| 94 | + if (!deleted) throw AppError.notFound(`No block found for ${domain}`); |
| 95 | + |
| 96 | + blockedDomains.delete(domain); |
| 97 | + |
| 98 | + logger.info({ domain, unblockedBy: req.user?.id }, "[blocklist] Node unblocked"); |
| 99 | + |
| 100 | + res.json({ message: `${domain} has been unblocked and can federate with this node again.` }); |
| 101 | +})); |
| 102 | + |
| 103 | +// ── GET /api/federation/blocks/check?domain= ───────────────────────────────── |
| 104 | +// Public endpoint — peers can check if they're blocked before attempting handshake. |
| 105 | +// Returns 200 with { blocked: true/false } rather than 403 so peers can handle it gracefully. |
| 106 | + |
| 107 | +router.get("/federation/blocks/check", asyncHandler(async (req: Request, res: Response) => { |
| 108 | + const domain = ((req.query.domain as string) ?? "").toLowerCase(); |
| 109 | + if (!domain) throw AppError.badRequest("domain query parameter required"); |
| 110 | + |
| 111 | + res.json({ domain, blocked: isBlocked(domain) }); |
| 112 | +})); |
| 113 | + |
| 114 | +export default router; |
0 commit comments