Skip to content

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

Scrape robots policy result handling (#4543)

Gergő Móricze3b72346c282
diff --git a/apps/api/src/__tests__/snips/v2/scrape-robots.test.ts b/apps/api/src/__tests__/snips/v2/scrape-robots.test.ts
new file mode 100644
index 000000000..9d43f98d5
--- /dev/null
+++ b/apps/api/src/__tests__/snips/v2/scrape-robots.test.ts
@@ -0,0 +1,99 @@
+import { randomUUID } from "node:crypto";
+import { scrapeOptions } from "../../../controllers/v2/types";
+import { CostTracking } from "../../../lib/cost-tracking";
+import { CrawlDenialError } from "../../../lib/error";
+import * as robots from "../../../lib/robots-txt";
+import { scrapeURL } from "../../../scraper/scrapeURL";
+import * as engines from "../../../scraper/scrapeURL/engines";
+import { TEST_SUITE_WEBSITE } from "../lib";
+import { scrapeTimeout } from "./lib";
+
+// Exercise the pipeline directly so team flags are covered in self-hosted
+// configurations too. Only robots.txt retrieval is stubbed; parsing, tracing,
+// and the fetch engine use their real implementations.
+describe("Scrape robots policy", () => {
+ beforeEach(() => {
+ vi.spyOn(robots, "fetchRobotsTxt").mockResolvedValue({
+ content: "User-agent: *\nDisallow: /\n",
+ url: new URL("/robots.txt", TEST_SUITE_WEBSITE).href,
+ });
+ vi.spyOn(engines, "scrapeURLWithEngine");
+ });
+
+ afterEach(() => {
+ vi.restoreAllMocks();
+ });
+
+ const runScrape = (checkRobotsOnScrape = true) =>
+ scrapeURL(
+ randomUUID(),
+ TEST_SUITE_WEBSITE,
+ scrapeOptions.parse({ formats: ["rawHtml"], maxAge: 0 }),
+ {
+ teamId: "scrape-robots-test",
+ orgId: null,
+ forceEngine: "fetch",
+ teamFlags: { checkRobotsOnScrape },
+ },
+ new CostTracking(),
+ );
+
+ it(
+ "returns a denial before invoking an engine for a disallowed URL",
+ async () => {
+ const result = await runScrape();
+
+ expect(robots.fetchRobotsTxt).toHaveBeenCalledOnce();
+ expect(result.success).toBe(false);
+ if (result.success) throw new Error("Expected a robots denial");
+ expect(result.error).toBeInstanceOf(CrawlDenialError);
+ expect(result.error.message).toBe("URL blocked by robots.txt");
+ expect(engines.scrapeURLWithEngine).not.toHaveBeenCalled();
+ },
+ scrapeTimeout,
+ );
+
+ it(
+ "scrapes an allowed URL",
+ async () => {
+ vi.mocked(robots.fetchRobotsTxt).mockResolvedValue({
+ content: "User-agent: *\nAllow: /\n",
+ url: new URL("/robots.txt", TEST_SUITE_WEBSITE).href,
+ });
+
+ const result = await runScrape();
+
+ expect(robots.fetchRobotsTxt).toHaveBeenCalledOnce();
+ expect(result.success).toBe(true);
+ expect(engines.scrapeURLWithEngine).toHaveBeenCalled();
+ },
+ scrapeTimeout,
+ );
+
+ it(
+ "scrapes without checking robots when the team flag is disabled",
+ async () => {
+ const result = await runScrape(false);
+
+ expect(result.success).toBe(true);
+ expect(robots.fetchRobotsTxt).not.toHaveBeenCalled();
+ expect(engines.scrapeURLWithEngine).toHaveBeenCalled();
+ },
+ scrapeTimeout,
+ );
+
+ it(
+ "allows scraping when robots.txt retrieval fails",
+ async () => {
+ vi.mocked(robots.fetchRobotsTxt).mockRejectedValue(
+ new Error("Fetch failed"),
+ );
+
+ const result = await runScrape();
+
+ expect(result.success).toBe(true);
+ expect(engines.scrapeURLWithEngine).toHaveBeenCalled();
+ },
+ scrapeTimeout,
+ );
+});
diff --git a/apps/api/src/scraper/scrapeURL/index.ts b/apps/api/src/scraper/scrapeURL/index.ts
index 270437228..dfe3bae08 100644
--- a/apps/api/src/scraper/scrapeURL/index.ts
+++ b/apps/api/src/scraper/scrapeURL/index.ts
@@ -1300,7 +1300,7 @@ export async function scrapeURL(
}
if (shouldCheckRobots(options, internalOptions)) {
- await withSpan("scrape.robots_check", async robotsSpan => {
+ const result = await withSpan("scrape.robots_check", async robotsSpan => {
const urlToCheck = meta.rewrittenUrl || meta.url;
meta.logger.info("Checking robots.txt", { url: urlToCheck });
@@ -1364,14 +1364,20 @@ export async function scrapeURL(
}
}
}).catch(error => {
- if (error.message === "URL blocked by robots.txt") {
+ if (error instanceof CrawlDenialError) {
return {
- success: false,
+ success: false as const,
error,
};
}
throw error;
});
+ if (result) {
+ return {
+ ...result,
+ threatDecisions: meta.threatDecisions,
+ };
+ }
}
// Initialize retry tracker with configured limits