Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions sdk/src/__tests__/read-url-dns-pinning.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,20 @@ import type { AddressInfo } from 'node:net'
let server: http.Server
let port = 0
let hits: { path: string; host: string | undefined }[] = []
let onHangingSocketClose: (() => void) | undefined
let destroyHangingSocket: (() => void) | undefined

beforeAll(async () => {
server = http.createServer((request, response) => {
hits.push({ path: request.url ?? '', host: request.headers.host })
if (request.url === '/unsupported-open') {
const socket = request.socket
destroyHangingSocket = () => socket.destroy()
socket.once('close', () => onHangingSocketClose?.())
response.writeHead(200, { 'content-type': 'application/octet-stream' })
response.write('an unsupported body that never ends')
return
}
if (request.url === '/redirect-internal') {
response.writeHead(302, {
location: `http://internal.example:${port}/secret`,
Expand Down Expand Up @@ -61,6 +71,36 @@ function flippingResolver(...answers: string[][]) {
const PUBLIC = '93.184.216.34'

describe('readUrl pins the connection to the address it validated', () => {
/** A real socket close can cross event-loop turns; the 1s deadline detects a leaked connection. */
it('cancels rejected response bodies before returning', async () => {
const closed = Promise.withResolvers<void>()
onHangingSocketClose = closed.resolve
let timer: ReturnType<typeof setTimeout> | undefined
try {
const url = `http://127.0.0.1:${port}/unsupported-open`
const result = await readUrl({
url,
isBlocked: (ip) => ip !== '127.0.0.1',
})
expect(result[0].value).toEqual({
url,
errorMessage: 'Unsupported content type: application/octet-stream',
})
const socketClosed = await Promise.race([
closed.promise.then(() => true),
new Promise<boolean>((resolve) => {
timer = setTimeout(() => resolve(false), 1_000)
}),
])
expect(socketClosed).toBe(true)
} finally {
if (timer) clearTimeout(timer)
onHangingSocketClose = undefined
destroyHangingSocket?.()
destroyHangingSocket = undefined
}
})

it('refuses a name that answers public for the check and private for the connection', async () => {
hits = []
// `localhost` so that a transport resolving the name on its own (the old
Expand Down
5 changes: 4 additions & 1 deletion sdk/src/tools/read-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,11 +396,11 @@ export async function readUrl({
const onExternalAbort = () => controller.abort()
signal?.addEventListener('abort', onExternalAbort, { once: true })

let response: Response | undefined
try {
// Follow redirects manually so every hop is re-validated against the SSRF
// policy — a public URL must not be able to 30x its way to an internal one.
let currentUrl = parsedUrl
let response: Response
for (let redirects = 0; ; redirects++) {
try {
// Refuses non-http(s) schemes and private/reserved addresses, literal
Expand Down Expand Up @@ -505,5 +505,8 @@ export async function readUrl({
} finally {
clearTimeout(timeout)
signal?.removeEventListener('abort', onExternalAbort)
if (response?.body && !response.body.locked) {
await response.body.cancel().catch(() => {})
}
}
}
Loading