Summary
WeasyPrint's default URL fetching logic can be abused to bypass server-side request forgery (SSRF) protections by following HTTP redirects without reapplying custom security checks.
When an application relies on a custom url_fetcher to block access to internal resources, attackers can still reach those resources if an initially allowed URL redirects to a forbidden target that is not revalidated against the developer's policy.
Affected context
This vulnerability appears in applications that:
- Use WeasyPrint to generate PDFs or other documents from web content
- Rely on
default_url_fetcherdirectly, or wrap/extend it within a customurl_fetcher - Intend to block internal or sensitive endpoints (e.g.,
localhost,169.254.169.254, in-cluster services, internal admin panels) - Process attacker-controlled or partially untrusted URLs inside WeasyPrint
The issue surfaces specifically when an initially permitted URL returns an HTTP redirect (3xx) to a second URL that would normally be blocked by the application's SSRF policy.
Exploit preconditions
- Attack surface: WeasyPrint's URL fetching mechanism, typically invoked when converting HTML/CSS that references external URLs (images, stylesheets, fonts, etc.).
- Required access: Ability to influence or supply URLs that WeasyPrint will fetch, such as via user-submitted HTML, templates with user-controlled attributes, or input parameters feeding into PDF generation.
- Trigger point: A crafted URL that:
- Points to an attacker-controlled endpoint or otherwise allowed host, and
- Responds with an HTTP redirect to a disallowed internal resource (e.g.,
http://127.0.0.1:..., cloud metadata URLs, or private services).
- Why exploitation is possible: The underlying
urllibclient automatically follows redirects and fetches the new location without re-validating the final destination against the application's customurl_fetchersecurity policy, effectively bypassing SSRF filtering logic.
Why this happens
The core issue stems from trusting automatic redirect handling in a lower-level HTTP library without enforcing security checks on the final redirect target.
In WeasyPrint's default_url_fetcher, the initial URL may be screened or constrained by a custom url_fetcher implementation, but once the request is handed off to urllib, subsequent redirects are followed implicitly. Because the redirected URL is not re-run through the developer's SSRF policy, an attacker can chain an allowed external host with a redirect to an internal or otherwise restricted host.
This behavior aligns with:
- CWE-918 (Server-Side Request Forgery): The server-side component (WeasyPrint) is induced to make network requests to attacker-chosen internal resources.
- CWE-601 (URL Redirection to Untrusted Site): Redirects are followed in a way that changes the ultimate target to one outside the intended trust boundary, without revalidation.
Conceptually, the bug is an insufficient validation of the redirected destination, allowing internal network access through automatic redirect following.
Potential impact
- Unauthorized access to internal HTTP services (e.g.,
localhost-bound admin APIs, debug interfaces). - Exposure of cloud or container metadata services (e.g.,
169.254.169.254), which can lead to credential leakage. - Interaction with internal microservices that were assumed to be unreachable from the outside, potentially influencing their state.
- Use of internal services as an oracle for network scanning or service discovery (e.g., timing differences, error responses).
- Potential pivot point for more complex attacks if internal endpoints expose sensitive operations or misconfigured admin panels.
General mitigation guidance
Effective mitigation focuses on constraining outbound requests, controlling redirect behavior, and centralizing URL validation:
- Enforce policy on the final URL after redirects:
- Treat the "effective URL" (post-redirect) as the one that must pass SSRF checks.
- Apply host, scheme, and IP-based allowlists/denylists to the resolved destination.
- Restrict or disable automatic redirect following for untrusted input:
- Use HTTP clients that expose hooks or callbacks on redirect events so the application can re-validate targets.
- Consider failing closed on unexpected or multi-hop redirects when processing user-influenced URLs.
- Normalize and resolve URLs before validation:
- Resolve hostnames to IP addresses and check against reserved ranges (localhost, RFC1918, link-local, metadata IPs).
- Canonicalize URLs before applying policy, so different representations of the same internal address can't bypass filters.
- Apply defense-in-depth at the network layer:
- Use network-level egress controls (e.g., firewall rules, security groups) to prevent application containers from reaching sensitive metadata endpoints and internal admin services.
- Segregate WeasyPrint-running workloads into network segments with limited outbound access.
- Design custom
url_fetcherimplementations to be secure by default: - Default to blocking all non-essential outbound requests and selectively allow specific domains and schemes.
- Log and monitor blocked attempts for anomaly detection and policy tuning.
How teams detect issues like this
Detection typically combines code-level review, configuration analysis, and behavioral monitoring:
- Static analysis and code review:
- Look for uses of WeasyPrint's
default_url_fetcheror wrappers that delegate to underlying HTTP libraries with automatic redirects enabled. - Flag any custom
url_fetcherthat validates only the initial URL but does not account for redirect targets. - Check for absence of explicit redirect policies (e.g., no maximum redirect hops, no validation of
Locationheaders). - Policy and configuration audits:
- Review SSRF defense policies to ensure they operate on the final resolved URL (including redirects and DNS resolution).
- Inspect network egress rules for applications using WeasyPrint to confirm that sensitive local ranges are blocked.
- Runtime and log-based monitoring:
- Instrument logging for all URLs fetched by WeasyPrint, including both initial and redirected URLs.
- Alert on requests targeting localhost, reserved IP ranges, or metadata services from application workloads.
- Use HTTP access logs or proxy logs to identify suspicious redirect chains originating from PDF generation workflows.
- Security testing and CI integration:
- Incorporate SSRF-focused tests in QA, using controlled redirect endpoints to verify that internal URLs cannot be reached through multi-step redirects.
- Leverage automated SAST/DAST tools that include SSRF and open-redirect patterns in their rule sets, while validating SSRF policies in CI pipelines.

