Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥
TypeScript aiai-agentsai-crawlerai-scrapingai-searchcrawlerdata-extractionhtml-to-markdownllmmarkdownscraperscrapingweb-crawlerweb-dataweb-data-extractionweb-scraperweb-scrapingweb-searchwebscraping
chore(threat-protection): hardcode zscaler limits, drop the env knobs (#4164)
Gergő Móriczc939363a8c02
diff --git a/apps/api/src/config.ts b/apps/api/src/config.tsindex 9f80645bd..1b8219f40 100644--- a/apps/api/src/config.ts+++ b/apps/api/src/config.ts@@ -86,25 +86,6 @@ const configSchema = z.object({// real endpoints derived from the org's vanity domain and cloud name.ZSCALER_TOKEN_URL_OVERRIDE: z.string().url().optional(),ZSCALER_API_URL_OVERRIDE: z.string().url().optional(),- // Per-org hourly budget for POST /urlLookup calls. ZIA allows 400/hour;- // the default keeps headroom for connection tests.- ZSCALER_LOOKUP_HOURLY_BUDGET: z.coerce.number().int().positive().default(380),- // Per-org hourly budget for GET /urlCategories calls (ZIA allows 1,000/hour).- ZSCALER_CATEGORIES_HOURLY_BUDGET: z.coerce- .number()- .int()- .positive()- .default(900),- // How long one queued urlLookup may wait for its batched verdict before the- // org's failurePolicy decides.- ZSCALER_LOOKUP_TIMEOUT_MS: z.coerce.number().int().positive().default(15000),- // Queued-URL depth per org above which new lookups fail fast into the- // failurePolicy instead of waiting in a line they cannot exit.- ZSCALER_LOOKUP_MAX_QUEUE_DEPTH: z.coerce- .number()- .int()- .positive()- .default(500),// Organization SIEM logging delivery. The encryption key must decode to// exactly 32 bytes; validation happens when a secret is encrypted/decrypteddiff --git a/apps/api/src/lib/threat-protection/index.ts b/apps/api/src/lib/threat-protection/index.tsindex 74b5e38b5..138206548 100644--- a/apps/api/src/lib/threat-protection/index.ts+++ b/apps/api/src/lib/threat-protection/index.ts@@ -1,8 +1,8 @@-import { config } from "../../config";import { logger } from "../logger";import { fetchGoogleWebRiskVerdict } from "./providers/google-web-risk";import { canonicalizeUrl } from "./providers/web-risk/canonicalize";import { fetchZscalerVerdict } from "./providers/zscaler";+import { ZSCALER_LOOKUP_TIMEOUT_MS } from "./providers/zscaler/lookup-queue";import { evaluateZscalerSyncedRules } from "./providers/zscaler/sync";import type {RawVerdict,@@ -102,7 +102,7 @@ async function fetchProviderVerdict(): Promise<RawVerdict> {const attempts = mode === "zscaler" ? 1 : PROVIDER_ATTEMPTS;const timeoutMs =- mode === "zscaler" ? config.ZSCALER_LOOKUP_TIMEOUT_MS : PROVIDER_TIMEOUT_MS;+ mode === "zscaler" ? ZSCALER_LOOKUP_TIMEOUT_MS : PROVIDER_TIMEOUT_MS;let lastError: unknown;for (let attempt = 1; attempt <= attempts; attempt++) {diff --git a/apps/api/src/lib/threat-protection/providers/zscaler/lookup-queue.ts b/apps/api/src/lib/threat-protection/providers/zscaler/lookup-queue.tsindex 1a5b4f62b..5ada6dd80 100644--- a/apps/api/src/lib/threat-protection/providers/zscaler/lookup-queue.ts+++ b/apps/api/src/lib/threat-protection/providers/zscaler/lookup-queue.ts@@ -1,6 +1,5 @@import { randomUUID } from "crypto";import { RateLimiterRedis, RateLimiterRes } from "rate-limiter-flexible";-import { config } from "../../../../config";import { logger as _logger } from "../../../logger";import { redisRateLimitClient } from "../../../../services/rate-limiter";import { redlock } from "../../../../services/redlock";@@ -49,6 +48,15 @@ const drainLockKey = (orgId: string) =>`threat-protection:zscaler:drain-lock:${orgId}`;const BATCH_SIZE = 100;+// ZIA's published urlLookup quota is 400 calls/hour per tenant; keep headroom+// for connection tests.+const LOOKUP_HOURLY_BUDGET = 380;+// How long one queued lookup may wait for its batched verdict before the+// org's failurePolicy decides.+export const ZSCALER_LOOKUP_TIMEOUT_MS = 15_000;+// Queued-URL depth per org above which new lookups fail fast into the+// failurePolicy instead of waiting in a line they cannot exit.+const MAX_QUEUE_DEPTH = 500;const REPLY_TTL_SECONDS = 120;const REPLY_POLL_INTERVAL_MS = 150;/** Re-attempt to become the drainer this often while waiting (drainer death recovery). */@@ -91,7 +99,7 @@ function getHourlyBudget(): RateLimiterRedis {hourlyBudget = new RateLimiterRedis({storeClient: redisRateLimitClient,keyPrefix: "threat-protection:zscaler:lookup-budget",- points: config.ZSCALER_LOOKUP_HOURLY_BUDGET,+ points: LOOKUP_HOURLY_BUDGET,duration: 3600,});}@@ -158,7 +166,7 @@ export async function enqueueZscalerLookup(// heuristic, and a brief overshoot from concurrent enqueuers is bounded// by their count and harmless.const depth = await redisRateLimitClient.llen(queueKey(orgId));- if (depth >= config.ZSCALER_LOOKUP_MAX_QUEUE_DEPTH) {+ if (depth >= MAX_QUEUE_DEPTH) {throw new ZscalerError("quota",`Zscaler lookup queue is over capacity (${depth} URLs waiting)`,@@ -173,10 +181,7 @@ export async function enqueueZscalerLookup(const budgetState = await getHourlyBudget().get(zscalerBudgetKey(credentials)).catch(() => null);- if (- budgetState &&- budgetState.consumedPoints >= config.ZSCALER_LOOKUP_HOURLY_BUDGET- ) {+ if (budgetState && budgetState.consumedPoints >= LOOKUP_HOURLY_BUDGET) {throw new ZscalerError("quota","Zscaler urlLookup hourly budget is exhausted",@@ -301,8 +306,7 @@ const ENTRY_EXPIRY_GRACE_MS = 2000;function isExpired(entry: QueueEntry): boolean {return (- Date.now() - entry.at >- config.ZSCALER_LOOKUP_TIMEOUT_MS + ENTRY_EXPIRY_GRACE_MS+ Date.now() - entry.at > ZSCALER_LOOKUP_TIMEOUT_MS + ENTRY_EXPIRY_GRACE_MS);}diff --git a/apps/api/src/lib/threat-protection/providers/zscaler/sync.ts b/apps/api/src/lib/threat-protection/providers/zscaler/sync.tsindex 32a4a5c4b..d8d417022 100644--- a/apps/api/src/lib/threat-protection/providers/zscaler/sync.ts+++ b/apps/api/src/lib/threat-protection/providers/zscaler/sync.ts@@ -1,6 +1,5 @@import { RateLimiterRedis, RateLimiterRes } from "rate-limiter-flexible";import { z } from "zod";-import { config } from "../../../../config";import { logger as _logger } from "../../../logger";import { redisRateLimitClient } from "../../../../services/rate-limiter";import { redlock } from "../../../../services/redlock";@@ -175,7 +174,8 @@ function getCategoriesBudget(): RateLimiterRedis {categoriesBudget = new RateLimiterRedis({storeClient: redisRateLimitClient,keyPrefix: "threat-protection:zscaler:categories-budget",- points: config.ZSCALER_CATEGORIES_HOURLY_BUDGET,+ // ZIA's published urlCategories quota is 1,000 calls/hour per tenant.+ points: 900,duration: 3600,});}