(
hostname: string,
options: object,
callback: (
err: Error | null,
address: AxiosLookupAddress | AxiosLookupAddress[],
family?: AddressFamily,
) => void,
)
| 214 | * Signature matches axios's `lookup` config option (not Node's dns.lookup). |
| 215 | */ |
| 216 | export function ssrfGuardedLookup( |
| 217 | hostname: string, |
| 218 | options: object, |
| 219 | callback: ( |
| 220 | err: Error | null, |
| 221 | address: AxiosLookupAddress | AxiosLookupAddress[], |
| 222 | family?: AddressFamily, |
| 223 | ) => void, |
| 224 | ): void { |
| 225 | const wantsAll = 'all' in options && options.all === true |
| 226 | |
| 227 | // If hostname is already an IP literal, validate it directly. dns.lookup |
| 228 | // would short-circuit too, but checking here gives a clearer error and |
| 229 | // avoids any platform-specific lookup behavior for literals. |
| 230 | const ipVersion = isIP(hostname) |
| 231 | if (ipVersion !== 0) { |
| 232 | if (isBlockedAddress(hostname)) { |
| 233 | callback(ssrfError(hostname, hostname), '') |
| 234 | return |
| 235 | } |
| 236 | const family = ipVersion === 6 ? 6 : 4 |
| 237 | if (wantsAll) { |
| 238 | callback(null, [{ address: hostname, family }]) |
| 239 | } else { |
| 240 | callback(null, hostname, family) |
| 241 | } |
| 242 | return |
| 243 | } |
| 244 | |
| 245 | dnsLookup(hostname, { all: true }, (err, addresses) => { |
| 246 | if (err) { |
| 247 | callback(err, '') |
| 248 | return |
| 249 | } |
| 250 | |
| 251 | for (const { address } of addresses) { |
| 252 | if (isBlockedAddress(address)) { |
| 253 | callback(ssrfError(hostname, address), '') |
| 254 | return |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | const first = addresses[0] |
| 259 | if (!first) { |
| 260 | callback( |
| 261 | Object.assign(new Error(`ENOTFOUND ${hostname}`), { |
| 262 | code: 'ENOTFOUND', |
| 263 | hostname, |
| 264 | }), |
| 265 | '', |
| 266 | ) |
| 267 | return |
| 268 | } |
| 269 | |
| 270 | const family = first.family === 6 ? 6 : 4 |
| 271 | if (wantsAll) { |
| 272 | callback( |
| 273 | null, |
nothing calls this directly
no test coverage detected