-
Notifications
You must be signed in to change notification settings - Fork 5
feat: implement fetchRemoteAsset function for secure asset retrieval with DNS checks and size limits #183
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
feat: implement fetchRemoteAsset function for secure asset retrieval with DNS checks and size limits #183
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
68cfd80
feat: implement fetchRemoteAsset function for secure asset retrieval …
jakejarvis c3903f9
fix: update allowedHosts handling in fetchRemoteAsset to improve host…
jakejarvis f952d93
fix: enhance fetchRemoteAsset to support optional base URL for resolv…
jakejarvis 28c995b
fix: update fetchRemoteAsset mock to use mockRejectedValueOnce for si…
jakejarvis 9aa1f05
fix: refactor fetchWithTimeoutAndRetry to improve abort signal handli…
jakejarvis cb031dd
fix: update fetchFaviconInternal to ensure allNotFound is set correct…
jakejarvis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| /* @vitest-environment node */ | ||
| import { describe, expect, it, vi } from "vitest"; | ||
| import { | ||
| fetchRemoteAsset, | ||
| type RemoteAssetError, | ||
| } from "@/lib/fetch-remote-asset"; | ||
|
|
||
| // Each test replaces the global fetch/DNS lookup so we can simulate edge cases deterministically. | ||
| const fetchMock = vi.hoisted(() => vi.fn()); | ||
| const dnsLookupMock = vi.hoisted(() => | ||
| vi.fn(async () => [{ address: "93.184.216.34", family: 4 }]), | ||
| ); | ||
|
|
||
| vi.mock("node:dns/promises", () => ({ | ||
| lookup: dnsLookupMock, | ||
| })); | ||
|
|
||
| describe("fetchRemoteAsset", () => { | ||
| beforeEach(() => { | ||
| fetchMock.mockReset(); | ||
| dnsLookupMock.mockReset(); | ||
| dnsLookupMock.mockResolvedValue([{ address: "93.184.216.34", family: 4 }]); | ||
| globalThis.fetch = fetchMock as unknown as typeof fetch; | ||
| }); | ||
|
|
||
| it("returns buffer and content type for valid asset", async () => { | ||
| const body = new Uint8Array([1, 2, 3]); | ||
| fetchMock.mockResolvedValueOnce( | ||
| new Response(body, { | ||
| status: 200, | ||
| headers: { "content-type": "image/png" }, | ||
| }), | ||
| ); | ||
|
|
||
| const result = await fetchRemoteAsset({ | ||
| url: "https://example.com/image.png", | ||
| maxBytes: 1024, | ||
| }); | ||
|
|
||
| expect(Buffer.isBuffer(result.buffer)).toBe(true); | ||
| expect(result.contentType).toBe("image/png"); | ||
| expect(result.finalUrl).toBe("https://example.com/image.png"); | ||
| }); | ||
|
|
||
| it("rejects http URLs when allowHttp not set", async () => { | ||
| await expect( | ||
| fetchRemoteAsset({ url: "http://example.com/file.png" }), | ||
| ).rejects.toMatchObject({ | ||
| code: "protocol_not_allowed", | ||
| } satisfies Partial<RemoteAssetError>); | ||
| }); | ||
|
|
||
| it("allows http URLs when allowHttp is true", async () => { | ||
| fetchMock.mockResolvedValueOnce( | ||
| new Response(new Uint8Array([1]), { status: 200 }), | ||
| ); | ||
| const result = await fetchRemoteAsset({ | ||
| url: "http://example.com/icon.png", | ||
| allowHttp: true, | ||
| }); | ||
| expect(result.finalUrl).toBe("http://example.com/icon.png"); | ||
| }); | ||
|
|
||
| it("blocks hosts that resolve to private IPs", async () => { | ||
| dnsLookupMock.mockResolvedValueOnce([{ address: "10.0.0.5", family: 4 }]); | ||
| await expect( | ||
| fetchRemoteAsset({ url: "https://private.example/icon.png" }), | ||
| ).rejects.toMatchObject({ | ||
| code: "private_ip", | ||
| } satisfies Partial<RemoteAssetError>); | ||
| expect(fetchMock).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("follows redirects up to limit", async () => { | ||
| const redirectResponse = new Response(null, { | ||
| status: 302, | ||
| headers: { location: "https://cdn.example.com/img.png" }, | ||
| }); | ||
| const finalResponse = new Response(new Uint8Array([1, 2, 3]), { | ||
| status: 200, | ||
| headers: { "content-type": "image/png" }, | ||
| }); | ||
| fetchMock | ||
| .mockResolvedValueOnce(redirectResponse) | ||
| .mockResolvedValueOnce(finalResponse); | ||
|
|
||
| dnsLookupMock | ||
| .mockResolvedValueOnce([{ address: "93.184.216.34", family: 4 }]) | ||
| .mockResolvedValueOnce([{ address: "93.184.216.35", family: 4 }]); | ||
|
|
||
| const result = await fetchRemoteAsset({ | ||
| url: "https://example.com/img.png", | ||
| maxRedirects: 2, | ||
| }); | ||
| expect(result.finalUrl).toBe("https://cdn.example.com/img.png"); | ||
| expect(fetchMock).toHaveBeenCalledTimes(2); | ||
| }); | ||
|
|
||
| it("throws when asset exceeds configured size", async () => { | ||
| const largeBody = new Uint8Array(1024); | ||
| fetchMock.mockResolvedValueOnce( | ||
| new Response(largeBody, { | ||
| status: 200, | ||
| headers: { "content-type": "image/png" }, | ||
| }), | ||
| ); | ||
|
|
||
| await expect( | ||
| fetchRemoteAsset({ | ||
| url: "https://example.com/large.png", | ||
| maxBytes: 10, | ||
| }), | ||
| ).rejects.toMatchObject({ | ||
| code: "size_exceeded", | ||
| } satisfies Partial<RemoteAssetError>); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧹 Nitpick | 🔵 Trivial
Consider adding tests for remaining RemoteAssetError cases
Happy-path, protocol gating, redirect, private IP, and
size_exceededbehaviors are well covered. To lock in the error surface, consider adding tests forinvalid_url,host_not_allowed,host_blocked,dns_error, andredirect_limitso regressions on those branches are caught early.Also applies to: 45-52, 74-97, 99-116
🤖 Prompt for AI Agents