-
-
Notifications
You must be signed in to change notification settings - Fork 10.1k
fix(security): pin image fetch DNS resolution to prevent SSRF rebinding (GHSA-cmhj-wh2f-9cgx) #4634
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
diegosouzapw
merged 3 commits into
release/v3.8.36
from
fix/port-sec-c7d07448-image-dns-rebinding
Jun 24, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
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
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
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,121 @@ | ||
| import assert from "node:assert/strict"; | ||
| import test from "node:test"; | ||
|
|
||
| import { fetchRemoteImage } from "@/shared/network/remoteImageFetch"; | ||
|
|
||
| // GHSA-cmhj-wh2f-9cgx — DNS-rebinding SSRF: a public hostname whose DNS | ||
| // resolves to a private/loopback IP would otherwise bypass the string-only | ||
| // `parseAndValidatePublicUrl` guard. The fix is to (a) resolve the host once | ||
| // up-front, (b) reject if any resolved record is private, and (c) pin the | ||
| // connection to that resolved IP so a second DNS resolution at fetch-time | ||
| // cannot rebind to a different (private) address. | ||
|
|
||
| test("fetchRemoteImage rejects when DNS resolves a public hostname to loopback (rebinding)", async () => { | ||
| let fetchCalled = false; | ||
| await assert.rejects( | ||
| () => | ||
| fetchRemoteImage("https://attacker.example.com/image.png", { | ||
| fetchImpl: async () => { | ||
| fetchCalled = true; | ||
| return new Response(new Uint8Array([1, 2, 3]), { | ||
| status: 200, | ||
| headers: { "content-type": "image/png" }, | ||
| }); | ||
| }, | ||
| guard: "public-only", | ||
| // Inject a fake DNS resolver: attacker.example.com resolves to 127.0.0.1 | ||
| lookup: async () => [{ address: "127.0.0.1", family: 4 }], | ||
| }), | ||
| /blocked|private|rebind/i | ||
| ); | ||
| assert.equal(fetchCalled, false, "fetch must not be called when DNS resolves to a private IP"); | ||
| }); | ||
|
|
||
| test("fetchRemoteImage rejects when DNS resolves to cloud-metadata IP (169.254.169.254)", async () => { | ||
| let fetchCalled = false; | ||
| await assert.rejects( | ||
| () => | ||
| fetchRemoteImage("https://cdn.example.com/image.png", { | ||
| fetchImpl: async () => { | ||
| fetchCalled = true; | ||
| return new Response("unexpected"); | ||
| }, | ||
| guard: "public-only", | ||
| lookup: async () => [{ address: "169.254.169.254", family: 4 }], | ||
| }), | ||
| /blocked|private|rebind/i | ||
| ); | ||
| assert.equal(fetchCalled, false); | ||
| }); | ||
|
|
||
| test("fetchRemoteImage rejects when any of multiple resolved IPs is private (multi-A trick)", async () => { | ||
| let fetchCalled = false; | ||
| await assert.rejects( | ||
| () => | ||
| fetchRemoteImage("https://multi.example.com/image.png", { | ||
| fetchImpl: async () => { | ||
| fetchCalled = true; | ||
| return new Response("unexpected"); | ||
| }, | ||
| guard: "public-only", | ||
| lookup: async () => [ | ||
| { address: "203.0.113.5", family: 4 }, | ||
| { address: "10.0.0.1", family: 4 }, | ||
| ], | ||
| }), | ||
| /blocked|private|rebind/i | ||
| ); | ||
| assert.equal(fetchCalled, false); | ||
| }); | ||
|
|
||
| test("fetchRemoteImage allows a public hostname that resolves to a public IP", async () => { | ||
| const result = await fetchRemoteImage("https://cdn.example.com/image.png", { | ||
| fetchImpl: async () => | ||
| new Response(new Uint8Array([1, 2, 3]), { | ||
| status: 200, | ||
| headers: { "content-type": "image/png" }, | ||
| }), | ||
| guard: "public-only", | ||
| lookup: async () => [{ address: "203.0.113.5", family: 4 }], | ||
| }); | ||
| assert.equal(result.buffer.toString("base64"), "AQID"); | ||
| }); | ||
|
|
||
| test("fetchRemoteImage skips DNS resolution for IP-literal hosts (already string-validated)", async () => { | ||
| // IP literals are validated by parseAndValidatePublicUrl directly; the | ||
| // resolver injection should not be invoked. | ||
| let lookupCalled = false; | ||
| const result = await fetchRemoteImage("https://203.0.113.5/image.png", { | ||
| fetchImpl: async () => | ||
| new Response(new Uint8Array([1]), { | ||
| status: 200, | ||
| headers: { "content-type": "image/png" }, | ||
| }), | ||
| guard: "public-only", | ||
| lookup: async () => { | ||
| lookupCalled = true; | ||
| return [{ address: "203.0.113.5", family: 4 }]; | ||
| }, | ||
| }); | ||
| assert.equal(result.buffer.toString("base64"), "AQ=="); | ||
| assert.equal(lookupCalled, false, "IP-literal hosts must not trigger DNS lookup"); | ||
| }); | ||
|
|
||
| test("fetchRemoteImage rejects when DNS resolution fails entirely", async () => { | ||
| let fetchCalled = false; | ||
| await assert.rejects( | ||
| () => | ||
| fetchRemoteImage("https://nx.example.com/image.png", { | ||
| fetchImpl: async () => { | ||
| fetchCalled = true; | ||
| return new Response("unexpected"); | ||
| }, | ||
| guard: "public-only", | ||
| lookup: async () => { | ||
| throw new Error("ENOTFOUND"); | ||
| }, | ||
| }), | ||
| /resolve|dns|blocked/i | ||
| ); | ||
| assert.equal(fetchCalled, false); | ||
| }); |
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
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.
IPv4-compatible IPv6 addresses (e.g.,
::127.0.0.1or::7f00:1) and unspecified IPv6 addresses (e.g.,::) can bypass theisPrivateHostcheck because it lacks specific rules for these formats. SinceisIP(bare)returns6for these, they bypass the DNS resolution check entirely and are fetched directly, leading to a complete SSRF guard bypass.We can close this gap by explicitly checking for and rejecting any IPv6 address starting with
::(except IPv4-mapped addresses starting with::ffff:) both for IP literals and resolved addresses.