Revision 1139279 of CSP: form-action

  • Revision slug: Web/HTTP/Headers/Content-Security-Policy/form-action
  • Revision title: Content-Security-Policy: form-action
  • Revision id: 1139279
  • Created:
  • Creator: fscholz
  • Is current revision? No
  • Comment new page

Revision Content

{{HTTPSidebar}}

The HTTP {{HTTPHeader("Content-Security-Policy")}} form-action directive restricts the URLs which can be used as the target of a form submissions from a given context.

CSP version 2
Directive type {{Glossary("Navigation directive")}}
{{CSP("default-src")}} fallback No. Not setting this allows anything.

Syntax

One or more sources can be set for the form-action policy:

Content-Security-Policy: form-action <source>;
Content-Security-Policy: form-action <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 the http: 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 using https:.
<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 and filesystem 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="form-action 'none'">

Apache configuration

<IfModule mod_headers.c> 
Header set Content-Security-Policy "form-action 'none';
</IfModule>

Nginx configuration

add_header Content-Security-Policy "form-action 'none';"

Violation case

Using a {{HTMLElement("form")}} element with an action set to inline JavaScript will result in a CSP violation.

<meta http-equiv="Content-Security-Policy" content="form-action 'none'">

<form action="javascript:alert('Foo')" id="form1" method="post"> 
  <input type="text" name="fieldName" value="fieldValue"> 
  <input type="submit" id="submit" value="submit"> 
</form>

// Error: Refused to send form data because it violates the following 
// Content Security Policy directive: "form-action 'none'".

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

{{Compat}}

See also

  • {{HTTPheader("Content-Security-Policy")}}
  • {{HTMLElement("form")}}

Revision Source

<div>{{HTTPSidebar}}</div>

<p>The HTTP {{HTTPHeader("Content-Security-Policy")}} <code><strong>form</strong></code><strong><code>-action</code></strong> directive restricts the URLs which can be used as the target of a form submissions from a given context.</p>

<table class="properties">
 <tbody>
  <tr>
   <th scope="row">CSP version</th>
   <td>2</td>
  </tr>
  <tr>
   <th scope="row">Directive type</th>
   <td>{{Glossary("Navigation directive")}}</td>
  </tr>
  <tr>
   <th scope="row">{{CSP("default-src")}} fallback</th>
   <td>No. Not setting this allows anything.</td>
  </tr>
 </tbody>
</table>

<h2 id="Syntax">Syntax</h2>

<p>One or more sources can be set for the <code>form-action</code> policy:</p>

<pre class="syntaxbox">
Content-Security-Policy: form-action &lt;source&gt;;
Content-Security-Policy: form-action &lt;source&gt; &lt;source&gt;;
</pre>

<p>&lt;source&gt; can be one of the following:</p>

<dl>
 <dt>&lt;host-source&gt;</dt>
 <dd>Internet hosts by name or IP address, as well as an optional <a href="/en-US/docs/URIs_and_URLs">URL scheme</a> and/or port number. The site's address may include an optional leading wildcard (the asterisk character, <code>'*'</code>), and you may use a wildcard (again, <code>'*'</code>) as the port number, indicating that all legal ports are valid for the source.<br />
 Examples:
 <ul>
  <li><code>http://*.example.com</code>: Matches all attempts to load from any subdomain of example.com using the <code>http:</code> URL scheme.</li>
  <li><code>mail.example.com:443</code>: Matches all attempts to access port 443 on mail.example.com.</li>
  <li><code>https://store.example.com</code>: Matches all attempts to access store.example.com using <code>https:</code>.</li>
 </ul>
 </dd>
 <dt>&lt;scheme-source&gt;</dt>
 <dd>A schema such as 'http:' or 'https:'. The colon is required.</dd>
 <dt><code>'self'</code></dt>
 <dd>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 <code>blob</code> and <code>filesystem</code> from source directives. Sites needing to allow these content types can specify them using the Data attribute.</dd>
 <dt><code>'unsafe-inline'</code></dt>
 <dd>Allows the use of inline resources, such as inline {{HTMLElement("script")}} elements, <code>javascript:</code> URLs, inline event handlers, and inline {{HTMLElement("style")}} elements. You must include the single quotes.</dd>
 <dt><code>'unsafe-eval'</code></dt>
 <dd>Allows the use of <code>eval()</code> and similar methods for creating code from strings. You must include the single quotes.</dd>
 <dt><code>'none'</code></dt>
 <dd>Refers to the empty set; that is, no URLs match. The single quotes are required.</dd>
</dl>

<h2 id="Examples">Examples</h2>

<h3 id="Meta_tag_configuration">Meta tag configuration</h3>

<pre class="brush: html">
&lt;meta http-equiv="Content-Security-Policy" content="form-action 'none'"&gt;</pre>

<h3 id="Apache_configuration">Apache configuration</h3>

<pre class="brush: bash">
&lt;IfModule mod_headers.c&gt; 
Header set Content-Security-Policy "form-action 'none';
&lt;/IfModule&gt;</pre>

<h3 id="Nginx_configuration">Nginx configuration</h3>

<pre class="brush: bash">
add_header Content-Security-Policy "form-action 'none';"</pre>

<h3 id="Violation_case">Violation case</h3>

<p>Using a {{HTMLElement("form")}} element with an action set to inline JavaScript will result in a CSP violation.</p>

<pre class="brush: html; example-bad">
&lt;meta http-equiv="Content-Security-Policy" content="form-action 'none'"&gt;

&lt;form action="javascript:alert('Foo')" id="form1" method="post"&gt; 
  &lt;input type="text" name="fieldName" value="fieldValue"&gt; 
  &lt;input type="submit" id="submit" value="submit"&gt; 
&lt;/form&gt;

// Error: Refused to send form data because it violates the following 
// Content Security Policy directive: "form-action 'none'".</pre>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{specName("CSP 3.0")}}</td>
   <td>{{Spec2('CSP 3.0')}}</td>
   <td>No changes.</td>
  </tr>
  <tr>
   <td>{{specName("CSP 1.1")}}</td>
   <td>{{Spec2('CSP 1.1')}}</td>
   <td>Initial definition.</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<p class="hidden">The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</p>

<p>{{Compat}}</p>

<h2 id="See_also">See also</h2>

<ul>
 <li>{{HTTPheader("Content-Security-Policy")}}</li>
 <li>{{HTMLElement("form")}}</li>
</ul>
Revert to this revision