1. 12 ウェブストレージ
    1. 12.1 導入
    2. 12.2 API
      1. 12.2.1 Storageインターフェイス
      2. 12.2.2 sessionStorageゲッター
      3. 12.2.3 localStorageゲッター
      4. 12.2.4 StorageEventインターフェイス
    3. 12.3 プライバシー
      1. 12.3.1 ユーザー追跡
      2. 12.3.2 Sensitivity of data
    4. 12.4 セキュリティ
      1. 12.4.1 DNSスプーフィング攻撃
      2. 12.4.2 クロスディレクトリ攻撃
      3. 12.4.3 Implementation risks

12 ウェブストレージ

Web_Storage_API

Support in all current engines.

Firefox3.5+Safari4+Chrome4+
Opera10.5+Edge79+
Edge (Legacy)12+Internet Explorer8+
Firefox Android6+Safari iOS?Chrome Android?WebView Android37+Samsung Internet?Opera Android11+

Web_Storage_API/Using_the_Web_Storage_API

12.1 導入

この節は非規範的である。

この仕様は、クライアント側で名前と値のペアを格納するための、HTTPセッションCookieに類似した2つの関連メカニズムを導入する。[COOKIES]

第1のメカニズムは、ユーザーが単一のトランザクションを実行している一方で、異なるウィンドウで複数のトランザクションを同時に実行している可能性のあるシナリオ向けに設計されている。

Cookieはこのケースを実は適切に扱えない。たとえば、ユーザーは、同じサイトを使用して、2つの異なるウィンドウで航空券を購入している可能性がある。サイトが、ユーザーがどの航空券を購入しているかを追跡するためにCookieを使用していた場合、ユーザーが両方のウィンドウでページからページへとクリックして移動するにつれて、現在購入中の航空券が一方のウィンドウから他方のウィンドウへ「リーク」し、その結果、ユーザーが気付かないまま同じ便の航空券を2枚購入してしまう可能性がある。

これに対処するため、この仕様はsessionStorageゲッターを導入する。サイトはセッションストレージにデータを追加でき、そのデータは、そのウィンドウで開かれた同一サイトの任意のページからアクセス可能となる。

たとえば、ページには、ユーザーが保険を希望することを示すためにチェックを入れるチェックボックスを設けることができる:

<label>
 <input type="checkbox" onchange="sessionStorage.insurance = checked ? 'true' : ''">
  I want insurance on this trip.
</label>

後続のページでは、スクリプトから、ユーザーがそのチェックボックスにチェックを入れたかどうかを確認できる:

if (sessionStorage.insurance) { ... }

ユーザーがそのサイト上で複数のウィンドウを開いていた場合、各ウィンドウはセッションストレージオブジェクトの個別のコピーをそれぞれ持つ。

第2のストレージメカニズムは、複数のウィンドウにまたがり、現在のセッションを超えて存続するストレージ向けに設計されている。特に、ウェブアプリケーションは、パフォーマンス上の理由から、ユーザーが作成した文書全体またはユーザーのメールボックスなど、数メガバイトに及ぶユーザーデータをクライアント側に格納することを望む場合がある。

ここでもCookieは、リクエストごとに送信されるため、このケースを適切に扱えない。

localStorageゲッターは、ページのローカルストレージ領域にアクセスするために使用される。

example.comのサイトは、ページの末尾に次のように配置することによって、ユーザーがそのページを読み込んだ回数を表示できる:

<p>
  You have viewed this page
  <span id="count">an untold number of</span>
  time(s).
</p>
<script>
  if (!localStorage.pageLoadCount)
    localStorage.pageLoadCount = 0;
  localStorage.pageLoadCount = parseInt(localStorage.pageLoadCount) + 1;
  document.getElementById('count').textContent = localStorage.pageLoadCount;
</script>

各サイトは、それぞれ独立したストレージ領域を持つ。

localStorageゲッターは共有状態へのアクセスを提供する。この仕様は、マルチプロセスのユーザーエージェントにおける他のエージェントクラスターとの相互作用を定義しておらず、著者にはロック機構が存在しないと仮定することが奨励される。サイトは、たとえば、キーの値を読み取り、その値を増分してから書き戻し、その新しい値をセッションの一意な識別子として使用しようとする場合がある。サイトがこれを2つの異なるブラウザーウィンドウで同時に2回実行する場合、両方のセッションで同じ「一意な」識別子を使用することになり、潜在的に破滅的な影響が生じる可能性がある。

12.2 API

Storage

Support in all current engines.

Firefox3.5+Safari4+Chrome4+
Opera10.5+Edge79+
Edge (Legacy)12+Internet Explorer8+
Firefox Android6+Safari iOS?Chrome Android?WebView Android37+Samsung Internet?Opera Android11+

12.2.1 Storageインターフェイス

[Exposed=Window]
interface Storage {
  readonly attribute unsigned long length;
  DOMString? key(unsigned long index);
  getter DOMString? getItem(DOMString key);
  setter undefined setItem(DOMString key, DOMString value);
  deleter undefined removeItem(DOMString key);
  undefined clear();
};
storage.length

Storage/length

Support in all current engines.

Firefox3.5+Safari4+Chrome4+
Opera10.5+Edge79+
Edge (Legacy)12+Internet Explorer8+
Firefox Android6+Safari iOS?Chrome Android?WebView Android37+Samsung Internet?Opera Android11+

キーと値のペアの数を返す。

storage.key (n)

Storage/key

Support in all current engines.

Firefox3.5+Safari4+Chrome4+
Opera10.5+Edge79+
Edge (Legacy)12+Internet Explorer8+
Firefox Android6+Safari iOS?Chrome Android?WebView Android37+Samsung Internet?Opera Android11+

n番目のキーの名前を返す。nがキーと値のペアの数以上である場合はnullを返す。

value = storage.getItem (key)

Storage/getItem

Support in all current engines.

Firefox3.5+Safari4+Chrome4+
Opera10.5+Edge79+
Edge (Legacy)12+Internet Explorer8+
Firefox Android6+Safari iOS?Chrome Android?WebView Android37+Samsung Internet?Opera Android11+

指定されたkeyに関連付けられた現在の値、または指定されたkeyが存在しない場合はnullを返す。

value = storage[key]

指定されたkeyに関連付けられた現在の値、または指定されたkeyが存在しない場合はundefinedを返す。

storage.setItem (key, value)

Storage/setItem

Support in all current engines.

Firefox3.5+Safari4+Chrome4+
Opera10.5+Edge79+
Edge (Legacy)12+Internet Explorer8+
Firefox Android6+Safari iOS?Chrome Android?WebView Android37+Samsung Internet?Opera Android11+
storage[key] = value

keyによって識別されるペアの値をvalueに設定し、keyについて以前にペアが存在しなかった場合は、新しいキーと値のペアを作成する。

新しい値を設定できなかった場合、QuotaExceededErrorを投げるす。(たとえば、ユーザーがそのサイトのストレージを無効にしている場合、またはクォータを超過している場合には、設定に失敗することがある)。

同等のStorageオブジェクトを保持するWindowオブジェクト上でstorageイベントを送出する。

storage.removeItem (key)

Storage/removeItem

Support in all current engines.

Firefox3.5+Safari4+Chrome4+
Opera10.5+Edge79+
Edge (Legacy)12+Internet Explorer8+
Firefox Android6+Safari iOS?Chrome Android?WebView Android37+Samsung Internet?Opera Android11+
delete storage[key]

指定されたkeyをもつキーと値のペアが存在する場合、その指定したkeyをもつキーと値のペアを削除する。

同等のStorageオブジェクトを保持するWindowオブジェクト上でstorageイベントを送出する。

storage.clear()

Storage/clear

Support in all current engines.

Firefox3.5+Safari4+Chrome4+
Opera10.5+Edge79+
Edge (Legacy)12+Internet Explorer8+
Firefox Android6+Safari iOS?Chrome Android?WebView Android37+Samsung Internet?Opera Android11+

存在する場合、すべてのキーと値のペアを削除する。

同等のStorageオブジェクトを保持するWindowオブジェクト上でstorageイベントを送出する。

A Storage object has an associated:

map
A storage proxy map.
type
"local" or "session".

To reorder a Storage object storage, reorder storage's map's entries in an implementation-defined manner.

Unfortunate as it is, iteration order is not defined and can change upon most mutations.

To broadcast a Storage object storage, given a key, oldValue, and newValue, run these steps:

  1. Let thisDocument be storage's relevant global object's associated Document.

  2. Let url be the serialization of thisDocument's URL.

  3. Let remoteStorages be all Storage objects excluding storage whose:

    and, if type is "session", whose relevant settings object's associated Document's node navigable's traversable navigable is thisDocument's node navigable's traversable navigable.

  4. For each remoteStorage of remoteStorages: queue a global task on the DOM manipulation task source given remoteStorage's relevant global object to fire an event named storage at remoteStorage's relevant global object, using StorageEvent, with key initialized to key, oldValue initialized to oldValue, newValue initialized to newValue, url initialized to url, and storageArea initialized to remoteStorage.

    The Document object associated with the resulting task is not necessarily fully active, but events fired on such objects are ignored by the event loop until the Document becomes fully active again.


The length getter steps are to return this's map's size.

The key(index) method steps are:

  1. If index is greater than or equal to this's map's size, then return null.

  2. Let keys be the result of running get the keys on this's map.

  3. Return keys[index].

The supported property names on a Storage object storage are the result of running get the keys on storage's map.

The getItem(key) method steps are:

  1. If this's map[key] does not exist, then return null.

  2. Return this's map[key].

The setItem(key, value) method steps are:

  1. Let oldValue be null.

  2. Let reorder be true.

  3. If this's map[key] exists:

    1. Set oldValue to this's map[key].

    2. If oldValue is value, then return.

    3. Set reorder to false.

  4. If value cannot be stored, then throw a QuotaExceededError.

  5. Set this's map[key] to value.

  6. If reorder is true, then reorder this.

  7. Broadcast this with key, oldValue, and value.

The removeItem(key) method steps are:

  1. If this's map[key] does not exist, then return.

  2. Set oldValue to this's map[key].

  3. Remove this's map[key].

  4. Reorder this.

  5. Broadcast this with key, oldValue, and null.

The clear() method steps are:

  1. If this's map is empty, then return.

  2. Clear this's map.

  3. Broadcast this with null, null, and null.

12.2.2 sessionStorageゲッター

interface mixin WindowSessionStorage {
  readonly attribute Storage sessionStorage;
};
Window includes WindowSessionStorage;
window.sessionStorage

Window/sessionStorage

Support in all current engines.

Firefox2+Safari4+Chrome4+
Opera10.5+Edge79+
Edge (Legacy)12+Internet Explorer8+
Firefox Android?Safari iOS?Chrome Android?WebView Android37+Samsung Internet?Opera Android11+

Returns the Storage object associated with that window's origin's session storage area.

Documentの生成元が不透明な生成元である場合、またはリクエストがポリシー上の決定に違反する場合(たとえば、ユーザーエージェントがページによるデータの永続化を許可しないように設定されている場合)、"SecurityError" DOMExceptionを投げる。

A Document object has an associated session storage holder, which is null or a Storage object. 最初はnullである。

(This is a tracking vector.) The sessionStorage getter steps are:

  1. If this's associated Document's session storage holder is non-null, then return this's associated Document's session storage holder.

  2. Let map be the result of running obtain a session storage bottle map with this's relevant settings object and "sessionStorage".

  3. If map is failure, then throw a "SecurityError" DOMException.

  4. Let storage be a new Storage object whose map is map.

  5. Set this's associated Document's session storage holder to storage.

  6. Return storage.

After creating a new auxiliary browsing context and document, the session storage is copied over.

12.2.3 localStorageゲッター

interface mixin WindowLocalStorage {
  readonly attribute Storage localStorage;
};
Window includes WindowLocalStorage;
window.localStorage

Window/localStorage

Support in all current engines.

Firefox3.5+Safari4+Chrome4+
Opera10.5+Edge79+
Edge (Legacy)12+Internet Explorer8+
Firefox Android?Safari iOS?Chrome Android?WebView Android37+Samsung Internet?Opera Android11+

windowの生成元のローカルストレージ領域に関連付けられたStorageオブジェクトを返す。

Documentの生成元が不透明な生成元である場合、またはリクエストがポリシー上の決定に違反する場合(たとえば、ユーザーエージェントがページによるデータの永続化を許可しないように設定されている場合)、"SecurityError" DOMExceptionを投げる。

A Document object has an associated local storage holder, which is null or a Storage object. 最初はnullである。

(This is a tracking vector.) The localStorage getter steps are:

  1. If this's associated Document's local storage holder is non-null, then return this's associated Document's local storage holder.

  2. Let map be the result of running obtain a local storage bottle map with this's relevant settings object and "localStorage".

  3. If map is failure, then throw a "SecurityError" DOMException.

  4. Let storage be a new Storage object whose map is map.

  5. Set this's associated Document's local storage holder to storage.

  6. Return storage.

12.2.4 StorageEventインターフェイス

StorageEvent

Support in all current engines.

Firefox13+Safari4+Chrome1+
Opera12.1+Edge79+
Edge (Legacy)12+Internet Explorer9+
Firefox Android?Safari iOS3+Chrome Android?WebView Android37+Samsung Internet?Opera Android12.1+
[Exposed=Window]
interface StorageEvent : Event {
  constructor(DOMString type, optional StorageEventInit eventInitDict = {});

  readonly attribute DOMString? key;
  readonly attribute DOMString? oldValue;
  readonly attribute DOMString? newValue;
  readonly attribute USVString url;
  readonly attribute Storage? storageArea;

  undefined initStorageEvent(DOMString type, optional boolean bubbles = false, optional boolean cancelable = false, optional DOMString? key = null, optional DOMString? oldValue = null, optional DOMString? newValue = null, optional USVString url = "", optional Storage? storageArea = null);
};

dictionary StorageEventInit : EventInit {
  DOMString? key = null;
  DOMString? oldValue = null;
  DOMString? newValue = null;
  USVString url = "";
  Storage? storageArea = null;
};
event.key

変更対象のストレージ項目のキーを返す。

event.oldValue

値が変更されているストレージ項目のキーの古い値を返す。

event.newValue

値が変更されているストレージ項目のキーの新しい値を返す。

event.url

ストレージ項目が変更された文書のURLを返す。

event.storageArea

影響を受けたStorageオブジェクトを返す。

The key, oldValue, newValue, url, and storageArea attributes must return the values they were initialized to.

The initStorageEvent(type, bubbles, cancelable, key, oldValue, newValue, url, storageArea) method must initialize the event in a manner analogous to the similarly-named initEvent() method. [DOM]

12.3 プライバシー

12.3.1 ユーザー追跡

サードパーティの広告主(または複数のサイトにコンテンツを配信させることができるあらゆるエンティティ)は、自身のローカルストレージ領域に格納された一意の識別子を使用して、複数のセッションにわたってユーザーを追跡し、ユーザーの関心事のプロファイルを構築して、高度にターゲティングされた広告を可能にすることがある。ユーザーの実際の身元を認識しているサイト(たとえば、認証情報を必要とする電子商取引サイト)と組み合わせると、これにより、抑圧的な集団が、純粋に匿名のウェブ利用の世界よりも高い精度で、個人を標的にすることが可能になる。

ユーザー追跡のリスクを軽減するために使用できる手法がいくつかある:

サードパーティストレージのブロック

ユーザーエージェントは、トップレベル横断可能なオブジェクトのアクティブ文書のドメインを送信元とするスクリプトに対してlocalStorageオブジェクトへのアクセスを制限してもよく、たとえば、iframe内で実行されている他のドメインからのページのAPIへのアクセスを拒否してもよい。

格納データの期限切れ

ユーザーエージェントは、場合によってはユーザーが設定した方法で、一定期間の経過後に格納データを自動的に削除してもよい。

たとえば、ユーザーエージェントは、サードパーティのローカルストレージ領域をセッション限定ストレージとして扱うように設定でき、そのデータにアクセスできるすべてのナビゲート可能をユーザーが閉じた時点で、そのデータを削除する。

これにより、サイトによるユーザー追跡能力を制限でき、そのサイトは、ユーザーがサイト自体に対して認証を行う場合(たとえば、購入またはサービスへのログインによって)のみ、複数のセッションにわたってユーザーを追跡できることになる。

ただし、これにより、長期的なストレージメカニズムとしてのAPIの有用性も低下する。また、ユーザーがデータの有効期限切れの影響を十分に理解していない場合、ユーザーのデータを危険にさらす可能性もある。

永続ストレージをCookieとして扱う

ユーザーがローカルストレージ領域に格納されたデータを消去せずにCookieを消去することでプライバシーを保護しようとした場合、サイトは、2つの機能を互いの冗長なバックアップとして使用することによって、その試みを無効にできる。ユーザーエージェントは、この可能性をユーザーが理解する助けとなり、すべての永続ストレージ機能内のデータを同時に削除できるような方法で、これらを消去するためのインターフェイスを提示するべきである。[COOKIES]

ローカルストレージ領域へのアクセスのサイト別許可リスト

ユーザーエージェントは、サイトがセッションストレージ領域に無制限にアクセスすることを許可してもよいが、ローカルストレージ領域へのアクセスについてはユーザーに承認を要求してもよい。

格納データの生成元の追跡

ユーザーエージェントは、データの格納を引き起こしたサードパーティ生成元からのコンテンツを含んでいたサイトの生成元を記録してもよい。

この情報を使用して、永続ストレージ内に現在存在するデータの表示を提示する場合、ユーザーは、永続ストレージのどの部分を削除するかについて、情報に基づいた判断を下すことが可能になる。ブロックリスト("このデータを削除し、このドメインが今後データを格納することを一切禁止する")と組み合わせることで、ユーザーは永続ストレージの使用を信頼するサイトに制限できる。

共有ブロックリスト

ユーザーエージェントは、ユーザーが永続ストレージのドメインブロックリストを共有することを許可してもよい。

これにより、コミュニティが協力してプライバシーを保護できるようになる。

これらの提案は、このAPIのユーザー追跡に安易な使用を防ぐものの、そのユーザー追跡を完全に阻止するものではない。単一のドメイン内では、サイトはセッション中もユーザーを追跡し続けることができ、その後、サイトが取得したあらゆる識別情報(氏名、クレジットカード番号、住所)とともに、このすべての情報をサードパーティに渡すことができる。サードパーティが複数のサイトと協力してそのような情報を取得する場合、依然としてプロファイルを作成できる。

しかし、ユーザーエージェントからの協力がまったくなくてもユーザー追跡はある程度可能であり、たとえば、無害な目的で既に一般的に使用されているものの、(遡及的であっても)ユーザー追跡に容易に転用できる手法で、URL内のセッション識別子を使用することによって、それを実現できる。その後、この情報は、訪問者のIPアドレスおよびその他のユーザー固有データ(たとえば、ユーザーエージェントヘッダーおよび設定情報)を使用して、別々のセッションを一貫性のあるユーザープロファイルに統合することで、他のサイトと共有できる。

12.3.2 Sensitivity of data

User agents should treat persistently stored data as potentially sensitive; it's quite possible for emails, calendar appointments, health records, or other confidential documents to be stored in this mechanism.

To this end, user agents should ensure that when deleting data, it is promptly deleted from the underlying storage.

12.4 セキュリティ

12.4.1 DNSスプーフィング攻撃

DNSスプーフィング攻撃の可能性があるため、特定のドメインに属すると主張するホストが実際にそのドメインに属していることを保証することはできない。これを軽減するために、ページはTLSを使用できる。TLSを使用するページは、ユーザー、ユーザーに代わって動作するソフトウェア、および同じドメインに属することを識別する証明書を持つTLS使用中の他のページのみが、自身のストレージ領域にアクセスできることを確認できる。

12.4.2 クロスディレクトリ攻撃

1つのホスト名を共有する異なる著者、たとえば、現在は廃止されたgeocities.comでコンテンツをホスティングしているユーザーは、全員が1つのローカルストレージオブジェクトを共有する。パス名によってアクセスを制限する機能は存在しない。したがって、共有ホスト上の著者は、他の著者がデータを読み取り、上書きすることが容易であるため、これらの機能の使用を避けることが推奨される。

たとえパス制限機能が利用可能になったとしても、通常のDOMスクリプティングのセキュリティモデルでは、この保護を容易に回避して、任意のパスからデータにアクセスできてしまう。

12.4.3 Implementation risks

The two primary risks when implementing these persistent storage features are letting hostile sites read information from other domains, and letting hostile sites write information that is then read from other domains.

Letting third-party sites read data that is not supposed to be read from their domain causes information leakage. For example, a user's shopping wishlist on one domain could be used by another domain for targeted advertising; or a user's work-in-progress confidential documents stored by a word-processing site could be examined by the site of a competing company.

Letting third-party sites write data to the persistent storage of other domains can result in information spoofing, which is equally dangerous. For example, a hostile site could add items to a user's wishlist; or a hostile site could set a user's session identifier to a known ID that the hostile site can then use to track the user's actions on the victim site.

Thus, strictly following the origin model described in this specification is important for user security.