Skip to content

Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥

feat(api): admin endpoint to clear the IP restriction allowlist cache (#3957)

Gergő Móricz68904f013f59
diff --git a/apps/api/src/controllers/v0/admin/ip-restriction-cache-clear.ts b/apps/api/src/controllers/v0/admin/ip-restriction-cache-clear.ts
new file mode 100644
index 000000000..3b2fc20ab
--- /dev/null
+++ b/apps/api/src/controllers/v0/admin/ip-restriction-cache-clear.ts
@@ -0,0 +1,28 @@
+import { Request, Response } from "express";
+import { clearIpRestrictionCache } from "../../../lib/ip-restriction";
+import { logger } from "../../../lib/logger";
+
+// Called by the dashboard after it writes ip_restriction_config so allowlist
+// edits take effect immediately instead of after the 60s cache TTL.
+export async function ipRestrictionCacheClearController(
+ req: Request,
+ res: Response,
+) {
+ try {
+ const team_id = req.body?.team_id;
+
+ if (typeof team_id !== "string" || team_id.length === 0) {
+ return res.status(400).json({ error: "team_id is required" });
+ }
+
+ await clearIpRestrictionCache(team_id);
+
+ logger.info("IP restriction allowlist cache cleared", { team_id });
+ res.json({ ok: true });
+ } catch (error) {
+ logger.error("Error clearing IP restriction cache via API route", {
+ error,
+ });
+ res.status(500).json({ error: "Internal server error" });
+ }
+}
diff --git a/apps/api/src/lib/ip-restriction.ts b/apps/api/src/lib/ip-restriction.ts
index 44423f74d..48a0fa447 100644
--- a/apps/api/src/lib/ip-restriction.ts
+++ b/apps/api/src/lib/ip-restriction.ts
@@ -2,7 +2,7 @@ import { BlockList, isIP } from "node:net";
import { eq } from "drizzle-orm";
import { dbRr } from "../db/connection";
import * as schema from "../db/schema";
-import { getValue, setValue } from "../services/redis";
+import { deleteKey, getValue, setValue } from "../services/redis";
import { logger } from "./logger";
import type { TeamFlags } from "../controllers/v1/types";
@@ -12,6 +12,12 @@ const ALLOWLIST_CACHE_TTL_SECONDS = 60;
const allowlistCacheKey = (teamId: string) =>
`ip-restriction-allowlist:${teamId}`;
+// Invalidates the cached allowlist so dashboard edits apply immediately
+// (admin route ip-restriction-cache-clear).
+export async function clearIpRestrictionCache(teamId: string): Promise<void> {
+ await deleteKey(allowlistCacheKey(teamId));
+}
+
// Express hands IPv4 clients back as IPv4-mapped IPv6 (::ffff:1.2.3.4) when
// the socket is dual-stack; compare in IPv4 form so allowlist entries match.
export function normalizeIp(ip: string): string {
diff --git a/apps/api/src/routes/admin.ts b/apps/api/src/routes/admin.ts
index def358e82..b17205b78 100644
--- a/apps/api/src/routes/admin.ts
+++ b/apps/api/src/routes/admin.ts
@@ -4,6 +4,7 @@ import { redisHealthController } from "../controllers/v0/admin/redis-health";
import { autumnHealthController } from "../controllers/v0/admin/autumn-health";
import { authMiddleware, checkCreditsMiddleware, wrap } from "./shared";
import { acucCacheClearController } from "../controllers/v0/admin/acuc-cache-clear";
+import { ipRestrictionCacheClearController } from "../controllers/v0/admin/ip-restriction-cache-clear";
import { checkFireEngine } from "../controllers/v0/admin/check-fire-engine";
import { indexQueuePrometheus } from "../controllers/v0/admin/index-queue-prometheus";
import { triggerPrecrawl } from "../controllers/v0/admin/precrawl";
@@ -40,6 +41,11 @@ if (config.BULL_AUTH_KEY) {
wrap(acucCacheClearController),
);
+ adminRouter.post(
+ `/admin/${config.BULL_AUTH_KEY}/ip-restriction-cache-clear`,
+ wrap(ipRestrictionCacheClearController),
+ );
+
adminRouter.get(
`/admin/${config.BULL_AUTH_KEY}/feng-check`,
wrap(checkFireEngine),