|
| 1 | +import { eq } from "drizzle-orm"; |
| 2 | +import { idmux, map, scrape, scrapeRaw, scrapeTimeout } from "./lib"; |
| 3 | +import { createTestIdUrl, describeIf, Identity, TEST_PRODUCTION } from "../lib"; |
| 4 | +import { db } from "../../../db/connection"; |
| 5 | +import * as schema from "../../../db/schema"; |
| 6 | +import { parseApi } from "../../../lib/parseApi"; |
| 7 | + |
| 8 | +// Needs idmux (to create teams with the keyRestriction flag) and direct DB |
| 9 | +// access (to seed key_restriction_config), so production-suite only. |
| 10 | +describeIf(TEST_PRODUCTION)( |
| 11 | + "Key restriction (keyRestriction team flag)", |
| 12 | + () => { |
| 13 | + const seededKeyIds: number[] = []; |
| 14 | + |
| 15 | + async function seedRestriction( |
| 16 | + identity: Identity, |
| 17 | + restriction: { allowedFormats?: string[]; allowedEndpoints?: string[] }, |
| 18 | + ) { |
| 19 | + const [keyRow] = await db |
| 20 | + .select({ id: schema.api_keys.id }) |
| 21 | + .from(schema.api_keys) |
| 22 | + .where(eq(schema.api_keys.key, parseApi(identity.apiKey))) |
| 23 | + .limit(1); |
| 24 | + expect(keyRow).toBeDefined(); |
| 25 | + |
| 26 | + await db.insert(schema.key_restriction_config).values({ |
| 27 | + api_key_id: keyRow!.id, |
| 28 | + team_id: identity.teamId, |
| 29 | + allowed_formats: restriction.allowedFormats ?? [], |
| 30 | + allowed_endpoints: restriction.allowedEndpoints ?? [], |
| 31 | + }); |
| 32 | + seededKeyIds.push(keyRow!.id); |
| 33 | + } |
| 34 | + |
| 35 | + afterAll(async () => { |
| 36 | + for (const keyId of seededKeyIds) { |
| 37 | + await db |
| 38 | + .delete(schema.key_restriction_config) |
| 39 | + .where(eq(schema.key_restriction_config.api_key_id, keyId)); |
| 40 | + } |
| 41 | + }); |
| 42 | + |
| 43 | + it.concurrent( |
| 44 | + "allows markdown scrapes on a markdown-only key", |
| 45 | + async () => { |
| 46 | + const identity = await idmux({ |
| 47 | + name: "key-restriction/markdown-allowed", |
| 48 | + credits: 10000, |
| 49 | + flags: { keyRestriction: true }, |
| 50 | + }); |
| 51 | + await seedRestriction(identity, { allowedFormats: ["markdown"] }); |
| 52 | + |
| 53 | + // Explicit markdown and the implicit default must both pass. |
| 54 | + const doc = await scrape( |
| 55 | + { url: createTestIdUrl(), formats: ["markdown"] }, |
| 56 | + identity, |
| 57 | + ); |
| 58 | + expect(doc.markdown).toBeDefined(); |
| 59 | + |
| 60 | + const docDefault = await scrape({ url: createTestIdUrl() }, identity); |
| 61 | + expect(docDefault.markdown).toBeDefined(); |
| 62 | + }, |
| 63 | + scrapeTimeout * 2, |
| 64 | + ); |
| 65 | + |
| 66 | + it.concurrent( |
| 67 | + "rejects non-allowed formats on a markdown-only key", |
| 68 | + async () => { |
| 69 | + const identity = await idmux({ |
| 70 | + name: "key-restriction/format-blocked", |
| 71 | + credits: 10000, |
| 72 | + flags: { keyRestriction: true }, |
| 73 | + }); |
| 74 | + await seedRestriction(identity, { allowedFormats: ["markdown"] }); |
| 75 | + |
| 76 | + const response = await scrapeRaw( |
| 77 | + { url: createTestIdUrl(), formats: ["markdown", "rawHtml"] }, |
| 78 | + identity, |
| 79 | + ); |
| 80 | + |
| 81 | + expect(response.statusCode).toBe(403); |
| 82 | + expect(response.body.success).toBe(false); |
| 83 | + expect(response.body.error).toContain( |
| 84 | + "restricted to the following formats", |
| 85 | + ); |
| 86 | + }, |
| 87 | + scrapeTimeout, |
| 88 | + ); |
| 89 | + |
| 90 | + it.concurrent( |
| 91 | + "rejects content-returning actions on a format-restricted key", |
| 92 | + async () => { |
| 93 | + const identity = await idmux({ |
| 94 | + name: "key-restriction/action-blocked", |
| 95 | + credits: 10000, |
| 96 | + flags: { keyRestriction: true }, |
| 97 | + }); |
| 98 | + await seedRestriction(identity, { allowedFormats: ["markdown"] }); |
| 99 | + |
| 100 | + const response = await scrapeRaw( |
| 101 | + { |
| 102 | + url: createTestIdUrl(), |
| 103 | + formats: ["markdown"], |
| 104 | + actions: [{ type: "screenshot" }], |
| 105 | + }, |
| 106 | + identity, |
| 107 | + ); |
| 108 | + |
| 109 | + expect(response.statusCode).toBe(403); |
| 110 | + expect(response.body.success).toBe(false); |
| 111 | + expect(response.body.error).toContain("screenshot"); |
| 112 | + }, |
| 113 | + scrapeTimeout, |
| 114 | + ); |
| 115 | + |
| 116 | + it.concurrent( |
| 117 | + "enforces the endpoint allowlist", |
| 118 | + async () => { |
| 119 | + const identity = await idmux({ |
| 120 | + name: "key-restriction/endpoint", |
| 121 | + credits: 10000, |
| 122 | + flags: { keyRestriction: true }, |
| 123 | + }); |
| 124 | + await seedRestriction(identity, { allowedEndpoints: ["scrape"] }); |
| 125 | + |
| 126 | + // Allowed endpoint group works... |
| 127 | + await scrape({ url: createTestIdUrl() }, identity); |
| 128 | + |
| 129 | + // ...anything else is rejected at auth. |
| 130 | + const response = await map({ url: "https://firecrawl.dev" }, identity); |
| 131 | + expect(response.statusCode).toBe(403); |
| 132 | + expect(response.body.success).toBe(false); |
| 133 | + expect(response.body.error).toContain( |
| 134 | + "restricted to the following endpoints", |
| 135 | + ); |
| 136 | + }, |
| 137 | + scrapeTimeout * 2, |
| 138 | + ); |
| 139 | + |
| 140 | + it.concurrent( |
| 141 | + "does not restrict a flagged team before it configures a restriction", |
| 142 | + async () => { |
| 143 | + const identity = await idmux({ |
| 144 | + name: "key-restriction/no-config", |
| 145 | + credits: 10000, |
| 146 | + flags: { keyRestriction: true }, |
| 147 | + }); |
| 148 | + |
| 149 | + const doc = await scrape( |
| 150 | + { url: createTestIdUrl(), formats: ["rawHtml"] }, |
| 151 | + identity, |
| 152 | + ); |
| 153 | + expect(doc.rawHtml).toBeDefined(); |
| 154 | + }, |
| 155 | + scrapeTimeout, |
| 156 | + ); |
| 157 | + |
| 158 | + it.concurrent( |
| 159 | + "does not restrict when the flag is off even if a config exists", |
| 160 | + async () => { |
| 161 | + const identity = await idmux({ |
| 162 | + name: "key-restriction/no-flag", |
| 163 | + credits: 10000, |
| 164 | + }); |
| 165 | + await seedRestriction(identity, { allowedFormats: ["markdown"] }); |
| 166 | + |
| 167 | + const doc = await scrape( |
| 168 | + { url: createTestIdUrl(), formats: ["rawHtml"] }, |
| 169 | + identity, |
| 170 | + ); |
| 171 | + expect(doc.rawHtml).toBeDefined(); |
| 172 | + }, |
| 173 | + scrapeTimeout, |
| 174 | + ); |
| 175 | + }, |
| 176 | +); |
0 commit comments