CSP: form-action

HTTP の Content-Security-Policy (CSP) における form-action ディレクティブは、指定のコンテキストからフォームの送信先として使用される URL を制限します。

警告: form-action がフォーム送信後のリダイレクトをブロックするかどうかは 議論の最中 であり、この点に関する実装はブラウザーによって異なります(例えば、 Firefox 57 はリダイレクトをブロックしませんが、 Chrome 63 はリダイレクトをブロックします。)

CSP バージョン 2
ディレクティブ種別 ナビゲーションディレクティブ
default-src による代替 なし。このディレクティブが設定されていない場合、すべてが許可されます。

構文

form-action ポリシーには、 1 つ以上のソースを設定することができます。

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

ソース

<source> には、 CSP ソース値にあるいずれかの値を取ることができます。

なお、この同じ値のセットはすべてのフェッチディレクティブ(と数々の他のディレクティブ)で使用できます。

Meta タグの設定

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

Apache の設定

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

Nginx の設定

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

違反している場合

インラインの JavaScript で action を設定した <form> 要素 は 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>

<!--
// エラー: 以下に違反したので、フォームデータの送信は拒否されました。
// Content Security Policy ディレクティブ: "form-action 'none'"
-->

仕様書

Specification
Content Security Policy Level 3
# directive-form-action

ブラウザーの互換性

BCD tables only load in the browser

関連情報