CSP:form-action

HTTP Content-Security-Policy (CSP) 的 form-action 指令限制了可用於作為給定上下文中的表單提交目標的 URL。

警告: form-action 是否應該阻止表單提交後的重定向,存在爭議,並且瀏覽器對此方面的實現不一致(例如,Firefox 57 不會阻止重定向,而 Chrome 63 則會阻止)。

CSP 版本 2
指令型別 導航指令
default-src 回退 否。不設定此項允許任何內容。

語法

可以為 form-action 策略設定一個或多個源

http
Content-Security-Policy: form-action <source>;
Content-Security-Policy: form-action <source> <source>;

來源

<source> 可以是 CSP 源值 中列出的任何值。

請注意,這套相同的值可用於所有 獲取指令(以及其他一些指令)。

示例

元標記配置

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

Apache 配置

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

Nginx 配置

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

違規情況

使用 <form> 元素,其操作設定為內聯 JavaScript,會導致 CSP 違規。

html
<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'".
-->

規範

規範
內容安全策略級別 3
# directive-form-action

瀏覽器相容性

BCD 表格僅在瀏覽器中載入

另請參閱