The HTTP {{HTTPHeader("Content-Security-Policy")}} base-uri
directive restricts the URLs which can be used in a document's {{HTMLElement("base")}} element. If this value is absent, then any URI is allowed. If this directive is absent, the user agent will use the value in the {{HTMLElement("base")}} element.
CSP version | 2 |
---|---|
Directive type | {{Glossary("Document directive")}} |
{{CSP("default-src")}} fallback | No. Not setting this allows anything. |
Syntax
One or more sources can be allowed for the base-uri policy:
Content-Security-Policy: base-uri <source>; Content-Security-Policy: base-uri <source> <source>;
<source> can be one of the following:
- <host-source>
- Internet hosts by name or IP address, as well as an optional URL scheme and/or port number. The site's address may include an optional leading wildcard (the asterisk character,
'*'
), and you may use a wildcard (again,'*'
) as the port number, indicating that all legal ports are valid for the source.
Examples:http://*.example.com
: Matches all attempts to load from any subdomain of example.com using thehttp:
URL scheme.mail.example.com:443
: Matches all attempts to access port 443 on mail.example.com.https://store.example.com
: Matches all attempts to access store.example.com usinghttps:
.
- <scheme-source>
- A schema such as 'http:' or 'https:'. The colon is required.
'self'
- Refers to the origin from which the protected document is being served, including the same URL scheme and port number. You must include the single quotes. Some browsers specifically exclude
blob
andfilesystem
from source directives. Sites needing to allow these content types can specify them using the Data attribute. 'unsafe-inline'
- Allows the use of inline resources, such as inline {{HTMLElement("script")}} elements,
javascript:
URLs, inline event handlers, and inline {{HTMLElement("style")}} elements. You must include the single quotes. 'unsafe-eval'
- Allows the use of
eval()
and similar methods for creating code from strings. You must include the single quotes. 'none'
- Refers to the empty set; that is, no URLs match. The single quotes are required.
Examples
Meta tag configuration
<meta http-equiv="Content-Security-Policy" content="base-uri 'self'">
Apache configuration
<IfModule mod_headers.c> Header set Content-Security-Policy "base-uri 'self'; </IfModule>
Nginx configuration
add_header Content-Security-Policy "base-uri 'self';"
Violation case
Given your domain isn't example.com, using a {{HTMLElement("base")}} element with an href set to example.com will result in a CSP violation.
<meta http-equiv="Content-Security-Policy" content="base-uri 'self'"> <base href="http://example.com/"> // Error: Refused to set the document's base URI to 'http://example.com/' // because it violates the following Content Security Policy // directive: "base-uri 'self'"
Specifications
Specification | Status | Comment |
---|---|---|
{{specName("CSP 3.0")}} | {{Spec2('CSP 3.0')}} | No changes. |
{{specName("CSP 1.1")}} | {{Spec2('CSP 1.1')}} | Initial definition. |
Browser compatibility
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
{{Compat}}
See also
- {{HTTPheader("Content-Security-Policy")}}
- {{HTMLElement("base")}}
- {{domxref("Node.baseURI")}}