(targetUrl, context = {})
| 271 | } |
| 272 | |
| 273 | async function validateOutboundUrl(targetUrl, context = {}) { |
| 274 | let parsedUrl; |
| 275 | try { |
| 276 | parsedUrl = new URL(targetUrl); |
| 277 | } catch (error) { |
| 278 | throw createPolicyError( |
| 279 | "invalid_url", |
| 280 | "Invalid outbound URL provided.", |
| 281 | targetUrl, |
| 282 | context |
| 283 | ); |
| 284 | } |
| 285 | |
| 286 | const protocol = (parsedUrl.protocol || "").toLowerCase(); |
| 287 | if (protocol !== "http:" && protocol !== "https:") { |
| 288 | throw createPolicyError( |
| 289 | "invalid_scheme", |
| 290 | "Only http and https outbound URLs are allowed.", |
| 291 | targetUrl, |
| 292 | context |
| 293 | ); |
| 294 | } |
| 295 | |
| 296 | const normalizedHostname = normalizeHostname(parsedUrl.hostname); |
| 297 | if (!normalizedHostname) { |
| 298 | throw createPolicyError( |
| 299 | "invalid_url", |
| 300 | "Outbound URL must include a hostname.", |
| 301 | targetUrl, |
| 302 | context |
| 303 | ); |
| 304 | } |
| 305 | |
| 306 | if (isMetadataHostname(normalizedHostname)) { |
| 307 | throw createPolicyError( |
| 308 | "metadata_endpoint", |
| 309 | "Requests to cloud metadata endpoints are blocked.", |
| 310 | targetUrl, |
| 311 | context, |
| 312 | { hostname: normalizedHostname } |
| 313 | ); |
| 314 | } |
| 315 | |
| 316 | const allowPrivateHost = resolveAllowPrivateHost(context); |
| 317 | const resolvedAddresses = await resolveTargetAddresses(normalizedHostname); |
| 318 | |
| 319 | let privateAddressMatched = false; |
| 320 | for (const resolvedAddress of resolvedAddresses) { |
| 321 | if (isMetadataAddress(resolvedAddress.address)) { |
| 322 | throw createPolicyError( |
| 323 | "metadata_endpoint", |
| 324 | "Requests to cloud metadata endpoints are blocked.", |
| 325 | targetUrl, |
| 326 | context, |
| 327 | { hostname: normalizedHostname, address: resolvedAddress.address } |
| 328 | ); |
| 329 | } |
| 330 |
no test coverage detected