Skip to content

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

Add URL depth validation to crawl requests (ENG-2617) (#1753)

devin-ai-integration[bot]a9f4b1f20797
diff --git a/apps/api/src/__tests__/snips/crawl.test.ts b/apps/api/src/__tests__/snips/crawl.test.ts
index df85c29c3..584c8b14c 100644
--- a/apps/api/src/__tests__/snips/crawl.test.ts
+++ b/apps/api/src/__tests__/snips/crawl.test.ts
@@ -1,4 +1,4 @@
-import { asyncCrawl, asyncCrawlWaitForFinish, crawl, crawlOngoing, Identity, idmux, scrapeTimeout } from "./lib";
+import { asyncCrawl, asyncCrawlWaitForFinish, crawl, crawlOngoing, crawlStart, Identity, idmux, scrapeTimeout } from "./lib";
import { describe, it, expect } from "@jest/globals";
let identity: Identity;
@@ -179,4 +179,43 @@ describe("Crawl tests", () => {
}
}
}, 5 * scrapeTimeout);
+
+ it.concurrent("rejects crawl when URL depth exceeds maxDepth", async () => {
+ const response = await crawlStart({
+ url: "https://firecrawl.dev/blog/category/deep/nested/path",
+ maxDepth: 2,
+ limit: 5,
+ }, identity);
+
+ expect(response.statusCode).toBe(400);
+ expect(response.body.success).toBe(false);
+ expect(response.body.error).toBe("Bad Request");
+ expect(response.body.details).toBeDefined();
+ expect(response.body.details[0].message).toBe("URL depth exceeds the specified maxDepth");
+ expect(response.body.details[0].path).toEqual(["url"]);
+ });
+
+ it.concurrent("accepts crawl when URL depth equals maxDepth", async () => {
+ const response = await crawlStart({
+ url: "https://firecrawl.dev/blog/category",
+ maxDepth: 2,
+ limit: 5,
+ }, identity);
+
+ expect(response.statusCode).toBe(200);
+ expect(response.body.success).toBe(true);
+ expect(typeof response.body.id).toBe("string");
+ });
+
+ it.concurrent("accepts crawl when URL depth is less than maxDepth", async () => {
+ const response = await crawlStart({
+ url: "https://firecrawl.dev/blog",
+ maxDepth: 5,
+ limit: 5,
+ }, identity);
+
+ expect(response.statusCode).toBe(200);
+ expect(response.body.success).toBe(true);
+ expect(typeof response.body.id).toBe("string");
+ });
});
diff --git a/apps/api/src/__tests__/snips/lib.ts b/apps/api/src/__tests__/snips/lib.ts
index ebd34f822..9d1e9d81b 100644
--- a/apps/api/src/__tests__/snips/lib.ts
+++ b/apps/api/src/__tests__/snips/lib.ts
@@ -135,7 +135,7 @@ export async function scrapeStatus(jobId: string, identity: Identity): Promise<D
// Crawl API
// =========================================
-async function crawlStart(body: CrawlRequestInput, identity: Identity) {
+export async function crawlStart(body: CrawlRequestInput, identity: Identity) {
return await request(TEST_URL)
.post("/v1/crawl")
.set("Authorization", `Bearer ${identity.apiKey}`)
diff --git a/apps/api/src/controllers/v1/types.ts b/apps/api/src/controllers/v1/types.ts
index f918430a8..82a3e76b4 100644
--- a/apps/api/src/controllers/v1/types.ts
+++ b/apps/api/src/controllers/v1/types.ts
@@ -9,6 +9,7 @@ import {
Document as V0Document,
} from "../../lib/entities";
import { InternalOptions } from "../../scraper/scrapeURL";
+import { getURLDepth } from "../../scraper/WebScraper/utils/maxDepthUtils";
export enum IntegrationEnum {
DIFY = "dify",
@@ -671,6 +672,16 @@ export const crawlRequestSchema = crawlerOptions
.strict(strictMessage)
.refine((x) => extractRefine(x.scrapeOptions), extractRefineOpts)
.refine((x) => fire1Refine(x.scrapeOptions), fire1RefineOpts)
+ .refine(
+ (data) => {
+ const urlDepth = getURLDepth(data.url);
+ return urlDepth <= data.maxDepth;
+ },
+ {
+ message: "URL depth exceeds the specified maxDepth",
+ path: ["url"]
+ }
+ )
.transform((x) => {
if (x.crawlEntireDomain !== undefined) {
x.allowBackwardLinks = x.crawlEntireDomain;