Repository navigation
feat(images): imageToBuffer reads untrusted input from public addresses only - #211
Conversation
…es only
imageToBuffer reads any local path and fetches any http(s) URL with
redirects followed, so a URL from a tool call reached this machine, the
private network and the cloud metadata address. Checking the host string
first does not help: a public name can resolve to a private address, and
a public host can redirect to one.
imageToBuffer(input, { untrusted: true }) refuses file paths and file:
URLs, and fetches an http(s) URL only from public network addresses. The
address in the URL is checked before connecting; a host name's addresses
are all checked inside the connection's own lookup, so the connection
goes to an address the check passed. Up to five redirects are followed
by hand and checked the same way. The fetch stops at maxBytes (50 MiB)
and timeoutMs (30 s). isPublicNetworkAddress is exported on its own.
Also: a data URL without ;base64 is percent-decoded (RFC 2397) instead
of read as base64, a file: URL is read through fileURLToPath so escaped
characters resolve, and URL schemes match in any case.
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
|
Warning Review limit reached
This review includes 9 billable files and costs up to $2.25.
Reviews can continue after your included limit without a manual trigger. An admin must approve usage-based billing. Or wait 12 minutes for your next included review. View limit detailsLimit details: You’ve used all 4 included reviews currently available. Review configuration: ⚙️ Run configuration
📒 Files selected for processing (9)
Comment |
Reviewer's GuideIntroduces an opt-in untrusted image ingestion mode that blocks local files and SSRF targets, validates DNS results and redirects at connection time, limits fetch size and duration, fixes data/file URL decoding edge cases, and documents the safe Buffer-based usage pattern while leaving default fetching behavior unchanged. Sequence diagram for untrusted image URL ingestionsequenceDiagram
participant Caller
participant imageToBuffer
participant fetchUntrustedImage
participant DNS
participant PublicCheck as isPublicNetworkAddress
participant HTTP as HTTP_or_HTTPS_Server
Caller->>imageToBuffer: imageToBuffer(input, options)
alt untrusted local file
imageToBuffer-->>Caller: IMAGE_LOCAL_FILE_REFUSED
else untrusted HTTP(S) URL
imageToBuffer->>fetchUntrustedImage: fetchUntrustedImage(url, maxBytes, timeoutMs)
fetchUntrustedImage->>PublicCheck: Check IP literal
PublicCheck-->>fetchUntrustedImage: public or refusal
fetchUntrustedImage->>DNS: Resolve all host addresses
DNS-->>fetchUntrustedImage: Addresses
fetchUntrustedImage->>PublicCheck: Check every resolved address
PublicCheck-->>fetchUntrustedImage: public or refusal
fetchUntrustedImage->>HTTP: GET with agent:false
alt redirect
HTTP-->>fetchUntrustedImage: 3xx Location
fetchUntrustedImage->>PublicCheck: Check redirect target
fetchUntrustedImage->>DNS: Resolve redirect host
fetchUntrustedImage->>HTTP: Follow redirect up to five times
else successful response
HTTP-->>fetchUntrustedImage: 2xx response body
fetchUntrustedImage-->>imageToBuffer: Bounded Buffer
imageToBuffer-->>Caller: Buffer
end
end
Flow diagram for imageToBuffer input handlingflowchart TD
A[imageToBuffer input] --> B{Input format}
B -->|Buffer| C[Return Buffer]
B -->|Data URL| D{base64 marker}
D -->|Yes| E[Decode base64]
D -->|No| F[Percent-decode bytes]
B -->|file URL or local path| G{untrusted}
G -->|Yes| H[IMAGE_LOCAL_FILE_REFUSED]
G -->|No| I[Read from filesystem]
B -->|"HTTP(S) URL"| J{untrusted}
J -->|Yes| K[Public-address fetch with limits]
J -->|No| L[Default fetch any host]
B -->|Raw base64| M[Decode recognized image data]
C --> N[Image Buffer]
E --> N
F --> N
I --> N
K --> N
L --> N
M --> N
File-Level Changes
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
PR Summary by QodoAdd public-address-only fetching for untrusted image inputs
AI Description
Diagram
High-Level Assessment
Files changed (9)
|
Code Review by Qodo
1.
|
…sed bodies A maxBytes of NaN or Infinity made both size checks false, so an untrusted response could grow until the deadline. maxBytes and timeoutMs must be positive numbers, and timeoutMs at most Node's longest timer, or the fetch throws a RangeError before connecting. The response body was returned as it came, so a gzip-encoded image reached the provider compressed. The request asks for identity; a gzip, deflate or br body sent anyway is decoded, maxBytes counts the decoded bytes so a compression bomb stops at the limit, and any other coding is refused.
## [0.13.16](v0.13.15...v0.13.16) (2026-10-09) ### feat * **images:** imageToBuffer reads untrusted input from public addresses only (#211) ([31805b1](31805b1))
What this changes
imageToBuffertakes a second argument. With{ untrusted: true }, input from a tool call or a user is read without touching the file system or the private network:untrusted: trueBuffer, data URL, raw base64file:URLcode: 'IMAGE_LOCAL_FILE_REFUSED'fetch, redirects followed, any hostmaxBytes,timeoutMs); a refusal hascode: 'IMAGE_URL_REFUSED'Why
Every image API (
editImage,upscaleImage,variateImage,transferStyle,segment) reads its string input throughimageToBuffer, which fetched any URL. A URL from a model reachedhttp://169.254.169.254/,http://localhost:…and the rest of the private network. The image-editing and vision-pipeline packs check the host string before calling AgentOS, but a string check misses two cases:http://anything.attacker.example/with DNS answering169.254.169.254.302 Location: http://169.254.169.254/latest/meta-data/;fetchfollows it.How
src/io/media/images/untrustedImageFetch.tssends the request withnode:http/node:https. Itslookupresolves every address of the host and fails unless each one passes, so the connection goes to an address the check saw. A name that answers differently on a second lookup (DNS rebinding) gets no unchecked connection. An address in the URL is checked before connecting.agent: falsekeeps any pooled socket from an unchecked lookup from being reused.maxBytes; a declaredContent-Lengthover the limit is refused before reading. One deadline covers the redirects and the body. Error messages show the URL without its query or credentials.accept-encoding: identity. A server may send gzip, deflate or br anyway; that body is decoded, andmaxBytescounts the decoded bytes, so a compression bomb stops at the limit. Any other coding is refused.maxBytesandtimeoutMsmust be positive numbers, andtimeoutMsat most 2,147,483,647 ms, Node's longest timer (a longer one fires after 1 ms). Any other value throws aRangeErrorbefore connecting.src/io/media/images/networkAddress.tsexportsisPublicNetworkAddress. It covers IPv4 private, loopback, link-local, carrier-grade NAT, documentation, benchmarking, multicast and reserved ranges. For IPv6 it covers the same classes, and the IPv4 address carried by IPv4-mapped, IPv4-compatible, NAT64 and 6to4 forms. This is the same check the packs ship, reviewed there.Also in
imageToBuffer:;base64(data:image/svg+xml,%3Csvg…) was decoded as base64, which gives garbage. It is percent-decoded byte by byte (RFC 2397).file:URL was read throughnew URL().pathname, so%20stayed escaped and the read failed. It goes throughfileURLToPath.HTTPS://…).The default path is unchanged:
fetch, any host, files read.Tests
untrustedImageFetch.test.tsruns against a real HTTP server on 127.0.0.1. It stands in for a public host through theallowAddresstest seam, and a fake resolver plays a hostile DNS server. Cases:10.0.0.1, to a name resolving to169.254.169.254, and tofile:;NaN,Infinity, zero and negative limits refused before connecting;Through
imageToBuffer, it covers127.0.0.1andlocalhostrefused, paths andfile:URLs refused, and raw base64 still decoded.networkAddress.test.ts: a table of non-public and public addresses.imageToBuffer.test.ts: percent-encoded and upper-case data URLs; afile:URL with an escaped space.Not in this change
imageToBuffer(url, { untrusted: true })and pass theBuffer.