aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/http/headers/content-security-policy/form-action/index.html
blob: 32a501d6225fa1fcca85bd9fcea0b5a91079848c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
---
title: 'CSP: form-action'
slug: Web/HTTP/Headers/Content-Security-Policy/form-action
translation_of: Web/HTTP/Headers/Content-Security-Policy/form-action
---
<div>{{HTTPSidebar}}</div>

<p>HTTP {{HTTPHeader("Content-Security-Policy")}} (CSP) 的 <code><strong>form</strong></code><strong><code>-action</code></strong> 指令能够限定当前页面中表单的提交地址。</p>

<div class="warning">
<p>在表单提交之后, <code>form-action</code> 指令是否应该阻止重定向仍有待讨论,各个浏览器对于此行为的实现也不尽相同(例如,Chrome 63会阻止重定向,而Firefox 57则不会)。</p>
</div>

<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. 未设定时允许任何值.</td>
  </tr>
 </tbody>
</table>

<h2 id="语法">语法</h2>

<p><code>form-action</code> 策略允许设定一个或多个源:</p>

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

<h2 id="示例">示例</h2>

<h3 id="meta标签配置">meta标签配置</h3>

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

<h3 id="Apache服务器配置">Apache服务器配置</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配置">Nginx配置</h3>

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

<h3 id="反例">反例</h3>

<p>{{HTMLElement("form")}} 元素的action设置为内联 JavaScript 会违反CSP规则。</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="规范">规范</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", "#directive-form-action", "form-action")}}</td>
   <td>{{Spec2('CSP 3.0')}}</td>
   <td>No changes.</td>
  </tr>
  <tr>
   <td>{{specName("CSP 1.1", "#directive-form-action", "form-action")}}</td>
   <td>{{Spec2('CSP 1.1')}}</td>
   <td>Initial definition.</td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>

<p>{{Compat("http.headers.csp.form-action")}}</p>

<h2 id="参见">参见</h2>

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