|
1 | | -import { test, describe, expect } from "@jest/globals"; |
| 1 | +import { |
| 2 | + test, |
| 3 | + describe, |
| 4 | + expect, |
| 5 | + jest, |
| 6 | + beforeEach, |
| 7 | + afterEach, |
| 8 | +} from "@jest/globals"; |
2 | 9 | import { RecursiveUrlLoader } from "../web/recursive_url.js"; |
3 | 10 |
|
| 11 | +const _originalFetch = globalThis.fetch; |
| 12 | + |
| 13 | +describe("RecursiveUrlLoader - Redirect SSRF Protection", () => { |
| 14 | + afterEach(() => { |
| 15 | + globalThis.fetch = _originalFetch; |
| 16 | + }); |
| 17 | + |
| 18 | + test("blocks redirects to private IPs (localhost)", async () => { |
| 19 | + globalThis.fetch = jest.fn<typeof fetch>().mockResolvedValue( |
| 20 | + new Response(null, { |
| 21 | + status: 302, |
| 22 | + headers: { Location: "http://127.0.0.1/admin" }, |
| 23 | + }) |
| 24 | + ); |
| 25 | + |
| 26 | + const loader = new RecursiveUrlLoader("https://example.com/", { |
| 27 | + maxDepth: 0, |
| 28 | + }); |
| 29 | + const docs = await loader.load(); |
| 30 | + expect(docs).toHaveLength(0); |
| 31 | + }); |
| 32 | + |
| 33 | + test("blocks redirects to cloud metadata IPs", async () => { |
| 34 | + globalThis.fetch = jest.fn<typeof fetch>().mockResolvedValue( |
| 35 | + new Response(null, { |
| 36 | + status: 302, |
| 37 | + headers: { Location: "http://169.254.169.254/latest/meta-data/" }, |
| 38 | + }) |
| 39 | + ); |
| 40 | + |
| 41 | + const loader = new RecursiveUrlLoader("https://example.com/", { |
| 42 | + maxDepth: 0, |
| 43 | + }); |
| 44 | + const docs = await loader.load(); |
| 45 | + expect(docs).toHaveLength(0); |
| 46 | + }); |
| 47 | + |
| 48 | + test("blocks redirects to private network ranges", async () => { |
| 49 | + globalThis.fetch = jest.fn<typeof fetch>().mockResolvedValue( |
| 50 | + new Response(null, { |
| 51 | + status: 302, |
| 52 | + headers: { Location: "http://192.168.1.1/internal" }, |
| 53 | + }) |
| 54 | + ); |
| 55 | + |
| 56 | + const loader = new RecursiveUrlLoader("https://example.com/", { |
| 57 | + maxDepth: 0, |
| 58 | + }); |
| 59 | + const docs = await loader.load(); |
| 60 | + expect(docs).toHaveLength(0); |
| 61 | + }); |
| 62 | + |
| 63 | + test("follows safe redirects", async () => { |
| 64 | + globalThis.fetch = jest |
| 65 | + .fn<typeof fetch>() |
| 66 | + .mockResolvedValueOnce( |
| 67 | + new Response(null, { |
| 68 | + status: 301, |
| 69 | + headers: { Location: "https://www.example.com/" }, |
| 70 | + }) |
| 71 | + ) |
| 72 | + .mockResolvedValueOnce( |
| 73 | + new Response("<html><body>Hello</body></html>", { |
| 74 | + status: 200, |
| 75 | + headers: { "Content-Type": "text/html" }, |
| 76 | + }) |
| 77 | + ); |
| 78 | + |
| 79 | + const loader = new RecursiveUrlLoader("https://example.com/", { |
| 80 | + maxDepth: 0, |
| 81 | + }); |
| 82 | + const docs = await loader.load(); |
| 83 | + expect(docs).toHaveLength(1); |
| 84 | + expect(docs[0].pageContent).toContain("Hello"); |
| 85 | + }); |
| 86 | + |
| 87 | + test("throws on too many redirects", async () => { |
| 88 | + globalThis.fetch = jest.fn<typeof fetch>().mockResolvedValue( |
| 89 | + new Response(null, { |
| 90 | + status: 302, |
| 91 | + headers: { Location: "https://example.com/loop" }, |
| 92 | + }) |
| 93 | + ); |
| 94 | + |
| 95 | + const loader = new RecursiveUrlLoader("https://example.com/", { |
| 96 | + maxDepth: 0, |
| 97 | + }); |
| 98 | + const docs = await loader.load(); |
| 99 | + expect(docs).toHaveLength(0); |
| 100 | + }); |
| 101 | +}); |
| 102 | + |
4 | 103 | describe("RecursiveUrlLoader - URL Origin Validation", () => { |
5 | 104 | describe("preventOutside origin checking", () => { |
6 | 105 | test("allows URLs with same origin", async () => { |
|
0 commit comments