Skip to content

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

Nick: block hits (#3564)

Nicolas2f407eba8c7c
diff --git a/apps/api/src/controllers/v0/crawl.ts b/apps/api/src/controllers/v0/crawl.ts
index 58b12d544..cf026605e 100644
--- a/apps/api/src/controllers/v0/crawl.ts
+++ b/apps/api/src/controllers/v0/crawl.ts
@@ -155,7 +155,12 @@ export async function crawlController(req: Request, res: Response) {
.json({ error: e.message ?? e });
}
- if (isUrlBlocked(url, auth.chunk?.flags ?? null)) {
+ if (
+ isUrlBlocked(url, auth.chunk?.flags ?? null, {
+ team_id: auth.chunk?.team_id ?? team_id,
+ origin: req.body?.origin ?? null,
+ })
+ ) {
return res.status(403).json({
error: UNSUPPORTED_SITE_MESSAGE,
});
diff --git a/apps/api/src/controllers/v0/scrape.ts b/apps/api/src/controllers/v0/scrape.ts
index c89484b99..da7ae3acf 100644
--- a/apps/api/src/controllers/v0/scrape.ts
+++ b/apps/api/src/controllers/v0/scrape.ts
@@ -62,7 +62,12 @@ async function scrapeHelper(
return { success: false, error: "Url is required", returnCode: 400 };
}
- if (isUrlBlocked(url, flags)) {
+ if (
+ isUrlBlocked(url, flags, {
+ team_id,
+ origin: req.body?.origin ?? null,
+ })
+ ) {
return {
success: false,
error: UNSUPPORTED_SITE_MESSAGE,
diff --git a/apps/api/src/controllers/v0/search.ts b/apps/api/src/controllers/v0/search.ts
index c43bd4162..79e175133 100644
--- a/apps/api/src/controllers/v0/search.ts
+++ b/apps/api/src/controllers/v0/search.ts
@@ -98,7 +98,13 @@ async function searchHelper(
return { success: true, data: res, returnCode: 200 };
}
- res = res.filter(r => !isUrlBlocked(r.url, flags));
+ res = res.filter(
+ r =>
+ !isUrlBlocked(r.url, flags, {
+ team_id,
+ origin: req.body?.origin ?? null,
+ }),
+ );
if (res.length > num_results) {
res = res.slice(0, num_results);
}
diff --git a/apps/api/src/controllers/v1/batch-scrape.ts b/apps/api/src/controllers/v1/batch-scrape.ts
index a27d8d304..d5bf86d37 100644
--- a/apps/api/src/controllers/v1/batch-scrape.ts
+++ b/apps/api/src/controllers/v1/batch-scrape.ts
@@ -75,7 +75,12 @@ export async function batchScrapeController(
for (const u of pendingURLs) {
try {
const nu = urlSchema.parse(u);
- if (!isUrlBlocked(nu, req.acuc?.flags ?? null)) {
+ if (
+ !isUrlBlocked(nu, req.acuc?.flags ?? null, {
+ team_id: req.auth.team_id,
+ origin: req.body.origin ?? null,
+ })
+ ) {
urls.push(nu);
unnormalizedURLs.push(u);
} else {
@@ -88,7 +93,10 @@ export async function batchScrapeController(
} else {
if (
req.body.urls?.some((url: string) =>
- isUrlBlocked(url, req.acuc?.flags ?? null),
+ isUrlBlocked(url, req.acuc?.flags ?? null, {
+ team_id: req.auth.team_id,
+ origin: req.body.origin ?? null,
+ }),
)
) {
if (!res.headersSent) {
diff --git a/apps/api/src/controllers/v1/extract.ts b/apps/api/src/controllers/v1/extract.ts
index f4882df0d..5c498aa8c 100644
--- a/apps/api/src/controllers/v1/extract.ts
+++ b/apps/api/src/controllers/v1/extract.ts
@@ -116,7 +116,10 @@ export async function extractController(
const invalidURLs: string[] =
req.body.urls?.filter((url: string) =>
- isUrlBlocked(url, req.acuc?.flags ?? null),
+ isUrlBlocked(url, req.acuc?.flags ?? null, {
+ team_id: req.auth.team_id,
+ origin: req.body.origin ?? null,
+ }),
) ?? [];
const createdAt = Date.now();
diff --git a/apps/api/src/controllers/v1/x402-search.ts b/apps/api/src/controllers/v1/x402-search.ts
index b1234257c..e599a048d 100644
--- a/apps/api/src/controllers/v1/x402-search.ts
+++ b/apps/api/src/controllers/v1/x402-search.ts
@@ -56,7 +56,12 @@ async function scrapeX402SearchResult(
applyZdrScope(zeroDataRetention);
try {
- if (isUrlBlocked(searchResult.url, flags)) {
+ if (
+ isUrlBlocked(searchResult.url, flags, {
+ team_id: options.teamId,
+ origin: options.origin,
+ })
+ ) {
throw new Error("Could not scrape url: " + UNSUPPORTED_SITE_MESSAGE);
}
logger.info("Adding scrape job [x402]", {
@@ -254,7 +259,11 @@ export async function x402SearchController(
if (req.body.ignoreInvalidURLs) {
searchResults = searchResults.filter(
- result => !isUrlBlocked(result.url, req.acuc?.flags ?? null),
+ result =>
+ !isUrlBlocked(result.url, req.acuc?.flags ?? null, {
+ team_id: req.auth.team_id,
+ origin: req.body.origin ?? null,
+ }),
);
}
diff --git a/apps/api/src/controllers/v2/batch-scrape.ts b/apps/api/src/controllers/v2/batch-scrape.ts
index b8c5af4d9..79351421f 100644
--- a/apps/api/src/controllers/v2/batch-scrape.ts
+++ b/apps/api/src/controllers/v2/batch-scrape.ts
@@ -95,7 +95,12 @@ export async function batchScrapeController(
for (const u of pendingURLs) {
try {
const nu = urlSchema.parse(u);
- if (!isUrlBlocked(nu, req.acuc?.flags ?? null)) {
+ if (
+ !isUrlBlocked(nu, req.acuc?.flags ?? null, {
+ team_id: req.auth.team_id,
+ origin: req.body.origin ?? null,
+ })
+ ) {
urls.push(nu);
unnormalizedURLs.push(u);
} else {
@@ -108,7 +113,10 @@ export async function batchScrapeController(
} else {
if (
req.body.urls?.some((url: string) =>
- isUrlBlocked(url, req.acuc?.flags ?? null),
+ isUrlBlocked(url, req.acuc?.flags ?? null, {
+ team_id: req.auth.team_id,
+ origin: req.body.origin ?? null,
+ }),
)
) {
if (!res.headersSent) {
diff --git a/apps/api/src/controllers/v2/extract.ts b/apps/api/src/controllers/v2/extract.ts
index d7155a312..5842dafd2 100644
--- a/apps/api/src/controllers/v2/extract.ts
+++ b/apps/api/src/controllers/v2/extract.ts
@@ -59,7 +59,10 @@ export async function extractController(
const invalidURLs: string[] =
req.body.urls?.filter((url: string) =>
- isUrlBlocked(url, req.acuc?.flags ?? null),
+ isUrlBlocked(url, req.acuc?.flags ?? null, {
+ team_id: req.auth.team_id,
+ origin: req.body.origin ?? null,
+ }),
) ?? [];
if (invalidURLs.length > 0 && !req.body.ignoreInvalidURLs) {
diff --git a/apps/api/src/controllers/v2/x402-search.ts b/apps/api/src/controllers/v2/x402-search.ts
index 098ac2ae8..ba54a96bb 100644
--- a/apps/api/src/controllers/v2/x402-search.ts
+++ b/apps/api/src/controllers/v2/x402-search.ts
@@ -396,10 +396,15 @@ export async function x402SearchController(
scrapeInput: ScrapeJobInput;
}> = [];
+ const blockContext = {
+ team_id: req.auth.team_id,
+ origin: req.body.origin ?? null,
+ };
+
// Add web results (skip blocked URLs)
if (searchResponse.web) {
searchResponse.web.forEach(item => {
- if (!isUrlBlocked(item.url, req.acuc?.flags ?? null)) {
+ if (!isUrlBlocked(item.url, req.acuc?.flags ?? null, blockContext)) {
itemsToScrape.push({
item,
type: "web",
@@ -420,7 +425,9 @@ export async function x402SearchController(
searchResponse.news
.filter(item => item.url)
.forEach(item => {
- if (!isUrlBlocked(item.url!, req.acuc?.flags ?? null)) {
+ if (
+ !isUrlBlocked(item.url!, req.acuc?.flags ?? null, blockContext)
+ ) {
itemsToScrape.push({
item,
type: "news",
@@ -441,7 +448,9 @@ export async function x402SearchController(
searchResponse.images
.filter(item => item.url)
.forEach(item => {
- if (!isUrlBlocked(item.url!, req.acuc?.flags ?? null)) {
+ if (
+ !isUrlBlocked(item.url!, req.acuc?.flags ?? null, blockContext)
+ ) {
itemsToScrape.push({
item,
type: "image",
diff --git a/apps/api/src/lib/extract/document-scraper.ts b/apps/api/src/lib/extract/document-scraper.ts
index 0beae14a9..dcd053ea8 100644
--- a/apps/api/src/lib/extract/document-scraper.ts
+++ b/apps/api/src/lib/extract/document-scraper.ts
@@ -37,7 +37,12 @@ export async function scrapeDocument(
trace.timing.scrapedAt = new Date().toISOString();
}
- if (isUrlBlocked(options.url, options.flags ?? null)) {
+ if (
+ isUrlBlocked(options.url, options.flags ?? null, {
+ team_id: options.teamId,
+ origin: options.origin,
+ })
+ ) {
return null;
}
diff --git a/apps/api/src/lib/extract/fire-0/document-scraper-f0.ts b/apps/api/src/lib/extract/fire-0/document-scraper-f0.ts
index 85d525f3b..a888aecf1 100644
--- a/apps/api/src/lib/extract/fire-0/document-scraper-f0.ts
+++ b/apps/api/src/lib/extract/fire-0/document-scraper-f0.ts
@@ -36,7 +36,12 @@ export async function scrapeDocument_F0(
trace.timing.scrapedAt = new Date().toISOString();
}
- if (isUrlBlocked(options.url, options.flags ?? null)) {
+ if (
+ isUrlBlocked(options.url, options.flags ?? null, {
+ team_id: options.teamId,
+ origin: options.origin,
+ })
+ ) {
return null;
}
diff --git a/apps/api/src/lib/extract/fire-0/url-processor-f0.ts b/apps/api/src/lib/extract/fire-0/url-processor-f0.ts
index 6c7098c10..650288a46 100644
--- a/apps/api/src/lib/extract/fire-0/url-processor-f0.ts
+++ b/apps/api/src/lib/extract/fire-0/url-processor-f0.ts
@@ -77,7 +77,12 @@ export async function processUrl_F0(
urlTraces.push(trace);
if (!options.url.includes("/*") && !options.allowExternalLinks) {
- if (!isUrlBlocked(options.url, teamFlags)) {
+ if (
+ !isUrlBlocked(options.url, teamFlags, {
+ team_id: options.teamId,
+ origin: options.origin ?? null,
+ })
+ ) {
trace.usedInCompletion = true;
return [options.url];
}
diff --git a/apps/api/src/lib/extract/url-processor.ts b/apps/api/src/lib/extract/url-processor.ts
index 0a4a2627e..e9c8b9096 100644
--- a/apps/api/src/lib/extract/url-processor.ts
+++ b/apps/api/src/lib/extract/url-processor.ts
@@ -160,7 +160,12 @@ export async function processUrl(
urlTraces.push(trace);
if (!options.url.includes("/*") && !options.allowExternalLinks) {
- if (!isUrlBlocked(options.url, teamFlags)) {
+ if (
+ !isUrlBlocked(options.url, teamFlags, {
+ team_id: options.teamId,
+ origin: options.origin ?? null,
+ })
+ ) {
trace.usedInCompletion = true;
return [options.url];
}
diff --git a/apps/api/src/routes/shared.ts b/apps/api/src/routes/shared.ts
index 174bacfdf..5724505b9 100644
--- a/apps/api/src/routes/shared.ts
+++ b/apps/api/src/routes/shared.ts
@@ -298,7 +298,10 @@ export function blocklistMiddleware(
) {
if (
typeof req.body.url === "string" &&
- isUrlBlocked(req.body.url, req.acuc?.flags ?? null)
+ isUrlBlocked(req.body.url, req.acuc?.flags ?? null, {
+ team_id: req.acuc?.team_id ?? null,
+ origin: typeof req.body.origin === "string" ? req.body.origin : null,
+ })
) {
if (!res.headersSent) {
return res.status(403).json({
diff --git a/apps/api/src/scraper/WebScraper/utils/blocklist.ts b/apps/api/src/scraper/WebScraper/utils/blocklist.ts
index 6809f3a9f..1b02d00e9 100644
--- a/apps/api/src/scraper/WebScraper/utils/blocklist.ts
+++ b/apps/api/src/scraper/WebScraper/utils/blocklist.ts
@@ -1,8 +1,12 @@
import { configDotenv } from "dotenv";
+import { v7 as uuidv7 } from "uuid";
import { config } from "../../../config";
import { parse } from "tldts";
import { TeamFlags } from "../../../controllers/v1/types";
-import { supabase_rr_service } from "../../../services/supabase";
+import {
+ supabase_rr_service,
+ supabase_service,
+} from "../../../services/supabase";
configDotenv();
@@ -11,6 +15,68 @@ type BlocklistBlob = {
allowedKeywords: string[];
};
+export type BlockContext = {
+ team_id?: string | null;
+ origin?: string | null;
+};
+
+type BlockHit = {
+ id: string;
+ domain: string;
+ url: string;
+ team_id: string | null;
+ origin: string | null;
+};
+
+const HIT_BUFFER_FLUSH_MS = 5000;
+const HIT_BUFFER_MAX_SIZE = 200;
+let hitBuffer: BlockHit[] = [];
+let hitFlushTimer: NodeJS.Timeout | null = null;
+
+async function flushHits(): Promise<void> {
+ if (hitBuffer.length === 0) return;
+ const batch = hitBuffer;
+ hitBuffer = [];
+ if (config.USE_DB_AUTHENTICATION !== true) return;
+ try {
+ await supabase_service.from("blocklist_hits").insert(batch);
+ } catch {}
+}
+
+function scheduleHitFlush(): void {
+ if (hitFlushTimer !== null) return;
+ hitFlushTimer = setTimeout(() => {
+ hitFlushTimer = null;
+ void flushHits();
+ }, HIT_BUFFER_FLUSH_MS);
+ if (typeof hitFlushTimer.unref === "function") hitFlushTimer.unref();
+}
+
+function recordHit(
+ url: string,
+ domain: string,
+ context: BlockContext | undefined,
+): void {
+ if (context === undefined) return;
+ if (config.USE_DB_AUTHENTICATION !== true) return;
+ hitBuffer.push({
+ id: uuidv7(),
+ domain,
+ url: url.length > 2048 ? url.slice(0, 2048) : url,
+ team_id: context.team_id ?? null,
+ origin: context.origin ?? null,
+ });
+ if (hitBuffer.length >= HIT_BUFFER_MAX_SIZE) {
+ if (hitFlushTimer !== null) {
+ clearTimeout(hitFlushTimer);
+ hitFlushTimer = null;
+ }
+ void flushHits();
+ } else {
+ scheduleHitFlush();
+ }
+}
+
function allowedKeywordMatches(url: string, allowedKeyword: string): boolean {
const keyword = allowedKeyword.trim();
if (!keyword) {
@@ -63,7 +129,11 @@ export async function initializeBlocklist() {
blob = data.data;
}
-export function isUrlBlocked(url: string, flags: TeamFlags): boolean {
+export function isUrlBlocked(
+ url: string,
+ flags: TeamFlags,
+ context?: BlockContext,
+): boolean {
if (blob === null) {
throw new Error("Blocklist not initialized");
}
@@ -106,11 +176,13 @@ export function isUrlBlocked(url: string, flags: TeamFlags): boolean {
// Block exact matches
if (blockedlist.includes(domain)) {
+ recordHit(url, domain, context);
return true;
}
// Block subdomains
if (blockedlist.some(blocked => domain.endsWith(`.${blocked}`))) {
+ recordHit(url, domain, context);
return true;
}
@@ -124,6 +196,7 @@ export function isUrlBlocked(url: string, flags: TeamFlags): boolean {
blocked => blocked.startsWith(baseDomain + ".") && blocked !== domain,
)
) {
+ recordHit(url, domain, context);
return true;
}
diff --git a/apps/api/src/search/execute.ts b/apps/api/src/search/execute.ts
index bf399f2af..789b4272d 100644
--- a/apps/api/src/search/execute.ts
+++ b/apps/api/src/search/execute.ts
@@ -150,7 +150,10 @@ export async function executeSearch(
scrapeOptions?.formats && scrapeOptions.formats.length > 0;
if (shouldScrape && scrapeOptions) {
- const itemsToScrape = getItemsToScrape(searchResponse, flags);
+ const itemsToScrape = getItemsToScrape(searchResponse, flags, {
+ team_id: teamId,
+ origin,
+ });
if (itemsToScrape.length > 0) {
const scrapeOpts = {
diff --git a/apps/api/src/search/scrape.ts b/apps/api/src/search/scrape.ts
index 298c60017..eb391eb75 100644
--- a/apps/api/src/search/scrape.ts
+++ b/apps/api/src/search/scrape.ts
@@ -142,12 +142,13 @@ async function scrapeSearchResultDirect(
export function getItemsToScrape(
searchResponse: SearchV2Response,
flags: TeamFlags,
+ context?: { team_id?: string | null; origin?: string | null },
): ScrapeItem[] {
const items: ScrapeItem[] = [];
if (searchResponse.web) {
for (const item of searchResponse.web) {
- if (!isUrlBlocked(item.url, flags)) {
+ if (!isUrlBlocked(item.url, flags, context)) {
items.push({
item,
type: "web",
@@ -163,7 +164,7 @@ export function getItemsToScrape(
if (searchResponse.news) {
for (const item of searchResponse.news) {
- if (item.url && !isUrlBlocked(item.url, flags)) {
+ if (item.url && !isUrlBlocked(item.url, flags, context)) {
items.push({
item,
type: "news",
@@ -179,7 +180,7 @@ export function getItemsToScrape(
if (searchResponse.images) {
for (const item of searchResponse.images) {
- if (item.url && !isUrlBlocked(item.url, flags)) {
+ if (item.url && !isUrlBlocked(item.url, flags, context)) {
items.push({
item,
type: "image",
diff --git a/apps/api/src/services/worker/scrape-worker.ts b/apps/api/src/services/worker/scrape-worker.ts
index 22707d59a..cfe39abae 100644
--- a/apps/api/src/services/worker/scrape-worker.ts
+++ b/apps/api/src/services/worker/scrape-worker.ts
@@ -365,6 +365,10 @@ async function processJob(job: NuQJob<ScrapeJobSingleUrls>) {
isUrlBlocked(
doc.metadata.url,
(await getACUCTeam(job.data.team_id))?.flags ?? null,
+ {
+ team_id: job.data.team_id,
+ origin: job.data.origin,
+ },
)
) {
throw new CrawlDenialError(UNSUPPORTED_SITE_MESSAGE); // TODO: make this its own error type that is ignored by error tracking