On this page
Content Security Policy source URL path matching can restrict which URLs a directive permits, but it is not a general-purpose route authorization mechanism. The distinction matters most for trailing slashes, redirects, and resources hosted alongside less-trusted content.
This article explains the matching model and provides a controlled browser experiment. The examples are untested: no browser execution or documentation retrieval was available for this article. The W3C CSP specification and current MDN documentation could not be consulted here, so verify the details and browser behavior against those sources and your supported browsers before relying on them in a policy.
Key Takeaways
- A source expression can constrain scheme, host, port, and path; a path ending in
/has different matching behavior from one without a trailing slash. - Path matching is not a string-prefix check:
/assets/is different from both/assetsand/assets-old/. - CSP's redirect matching model does not apply the path restriction to a redirected URL in the same way it applies it to the initial URL. Scheme and host restrictions still matter.
- A permitted host or path does not establish that its content is trustworthy. CSP is one layer, not a substitute for controlling what that host serves.
- Browser behavior and reporting should be checked in the environments you support; the examples below are not observed test results.
Read a source expression
Consider this response header:
Content-Security-Policy: script-src https://cdn.example.test/assets/
The directive applies to script loading. Its source expression describes HTTPS URLs on cdn.example.test, under the /assets/ path. The examples use fictional hosts; they are not public test services.
Expression in script-src | Intended scope |
|---|---|
https: | HTTPS script URLs, without restricting them to a particular host. |
https://cdn.example.test | The specified host over HTTPS, without a path restriction. |
https://*.example.test | Matching subdomains, not the bare example.test host. |
https://cdn.example.test/assets/ | The specified host and an /assets/ path restriction. |
https://cdn.example.test/assets/app.js | The specified host and an exact path restriction. |
A host source can also specify a port, such as https://cdn.example.test:8443/assets/, or a wildcard port with :*. Omitting the scheme makes matching depend on the protected page's scheme and CSP's scheme-matching rules. Scheme and port matching have upgrade and default-port subtleties; do not assume they are always simple text comparisons.
Multiple source expressions in a directive are alternatives. Separately enforced CSP policies, however, each have to permit a load. Other features, including nonces, hashes, and strict-dynamic, can affect how a script policy works; the examples here deliberately omit them.
Advertisement
Content Security Policy source URL path matching
For a source expression with a path, the important distinction is the final slash:
- A path without a trailing slash is an exact-path restriction.
/assetsis not the same path as/assets/app.js. - A path with a trailing slash acts as a directory-style prefix.
/assets/can match/assets/app.jsand deeper paths, but not/assetsor/assets-old/app.js. - A host source without a path does not limit the URL to a particular path on that host.
These are path rules, not query-parameter rules. A query string does not let a CSP source distinguish an otherwise matching resource by parameters such as ?version=1. A fragment is not a way to constrain the fetched resource either.
Path comparison also involves URL parsing and percent-decoding of path segments. Do not infer security from raw string prefixes or assume that an encoded separator is interchangeable with a literal path separator. If encoded paths matter to your application, test the precise URLs in each supported browser and compare them with the CSP specification.
Redirects change the path analysis
Suppose the policy allows https://cdn.example.test/assets/, and an initially permitted URL responds with a redirect:
https://cdn.example.test/assets/redirect.js
-> https://cdn.example.test/private/final.js
Under CSP's redirect matching model, the redirected URL is not subject to the source expression's path check in the same way as the initial request. The redirect is not a general exemption from CSP: applicable source restrictions, including scheme and host matching, still matter, as can other policies and browser security checks.
This means /assets/ should not be treated as a route-prefix authorization boundary. If an allowed endpoint can redirect to a sensitive or untrusted script on a permitted host, a path restriction alone may not prevent that script from being loaded. Whether a particular redirect chain works depends on the policy, responses, and browser; test it rather than assuming it does.
Try the rules in an isolated browser lab
The following is a test plan, not a report of observed results. Use only infrastructure you control. Set up a protected HTTPS page and a separate controlled HTTPS host named cdn.example.test, or substitute hostnames you control consistently throughout the policy and page. Ensure the browser trusts the lab certificates.
Configure the page response with this enforced, not report-only, header:
Content-Security-Policy: script-src https://cdn.example.test/assets/
Serve this HTML from the protected page:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>CSP path lab</title>
</head>
<body>
<script src="https://cdn.example.test/assets/app.js"></script>
<script src="https://cdn.example.test/assets"></script>
<script src="https://cdn.example.test/assets-old/app.js"></script>
<script src="https://cdn.example.test/assets/redirect.js"></script>
</body>
</html>
Configure the controlled CDN host as follows:
- Return valid JavaScript from
/assets/app.js,/assets, and/assets-old/app.js. Give each a distinct console message and an appropriate JavaScript content type. - Have
/assets/redirect.jsreturn a redirect tohttps://cdn.example.test/private/final.js. - Return valid JavaScript with a distinct console message from
/private/final.js.
The first script is intended to match the directory-style path rule. The next two are intended not to match it. The redirect case probes the redirect behavior described above; it should not be interpreted as proof of browser behavior until you run it.
In Chrome, Firefox, and Safari where relevant to your deployment, open developer tools, reload the page, and inspect the response CSP header, console messages, CSP violations, and network requests. Record browser versions and whether the initial and final redirect requests occurred. A missing console message alone is not conclusive: a network error, MIME-type issue, or JavaScript error can also prevent execution. CSP reports and browser consoles may expose only part of a blocked URL, so use controlled server logs as additional evidence.
Repeat with https://cdn.example.test/assets in the policy to examine exact-path behavior, and with a no-path host source to compare scope. Also test query strings, encoded paths, and redirect destinations on a different controlled host if those cases matter to your deployment.
Advertisement
Common Mistakes
- Treating
/assetsand/assets/as interchangeable. One is an exact path restriction; the other is directory-style matching. - Assuming a path rule is a general route-prefix allowlist. Redirect handling and URL parsing make that an unsafe authorization model.
- Assuming a wildcard includes the parent domain.
*.example.testandexample.testare distinct cases; list the latter separately if it is genuinely needed. - Assuming every resource on an allowed hostname is safe. User uploads, redirects, compromised dependencies, and endpoints that return attacker-controlled content can make an otherwise trusted host dangerous.
- Ignoring the directive and other policies.
script-srcconcerns scripts; allowing an image or connection requires examining the applicable directive. Multiple enforced policies can further restrict a request. - Mistaking a report-only result for enforcement. A report-only policy can help find violations, but it does not itself block the load.
Best Practices
- Control the content and redirect behavior of allowed hosts. Keep untrusted uploads and user-controlled responses away from hosts used to serve executable assets.
- Use narrow source expressions where they reduce exposure, but enforce sensitive route and resource access on the server.
- Consider nonce- or hash-based script policies when appropriate for the application, and evaluate their interaction with host sources and
strict-dynamicbefore deployment. - Roll out policy changes with browser testing and violation monitoring. Investigate unexpected reports without assuming every report represents a successful attack or a complete URL.
- Test redirects, encoded paths, and each browser engine your users rely on. Recheck the current W3C specification and MDN guidance when reviewing version-specific behavior.
Conclusion
CSP paths can narrow source matching, but their trailing-slash semantics and redirect behavior make them unsuitable as a standalone route security boundary. Control the resources served by allowed hosts, and validate the policy with a lab that reflects your actual browsers and redirect chains.
What would your domain sell for?
A free TidyURLs account unlocks the Domain Estimator. It prices a name from 191,011 real domain sales.