Lesson 1 of 4
Same origin policy
Listen to an AI summary of Same origin policy
The parts of a URL
The picture cuts one URL from the sports shop website into pieces. Three of the pieces are marked.
https://shop.example:443/orders
- httpsthe scheme
- shop.examplethe host name
- 443the port
- /ordersthe path
The scheme is the first part of a URL and says how the browser talks to the server. Web pages use two schemes, http and https. The host name is the name of the computer a website runs on, such as shop.example.
A port is a number that picks one program on a server. A server can run several programs, and each program answers on its own port. A URL can leave the port out. The browser then uses port 443 for https and port 80 for http.
What is an origin
An origin is the scheme, the host name and the port of a URL taken together. Two URLs have the same origin when all three parts match. The path, the rest of the URL, is not part of the origin.
The table compares four URLs with https://shop.example/orders, a URL that uses port 443.
https://shop.example/basket- The same origin. Only the path is different.
http://shop.example/orders- A different origin. The scheme is
http. https://help.shop.example/orders- A different origin. The host name is
help.shop.example. https://shop.example:8443/orders- A different origin. The port is
8443.
What is the same origin policy
A page gets data from a server by sending a request. The server answers with a response. The code in a page is JavaScript.
The same origin policy is a browser rule that stops a page from one origin reading data from another origin.
The same origin policy works like a block of flats with a letterbox on each door. Each flat is one origin. Anyone can push a letter through any letterbox, which is sending a request. Nobody can read the post inside another flat, which is reading a response.
A page on another origin
Sam, a customer, is signed in to the sports shop, so Sam's browser holds a cookie from shop.example. The shop reads the cookie to know that a request is Sam's.
Sam then opens a page on https://evil.example, which an attacker wrote. JavaScript in the attacker's page sends a request to https://shop.example/orders.
1.Page on evil.example
the attacker's JavaScript asks for Sam's orders
- asks
2.Sam's browser
sends the request, and can add Sam's cookie
- request
3.Server of shop.example
sends a response with Sam's orders
- response
4.Sam's browser
does not give the response to the page
The page on evil.example has a different origin from shop.example. The browser does not let the page read the response, or the cookie from shop.example.
What the same origin policy allows
The same origin policy stops reading and does not stop sending. A page can send a form, the boxes a person fills in, to any origin. A page can also show an image or run a file of JavaScript from any origin.
A request the page cannot read can still place an order in Sam's name. The attack is called cross-site request forgery.
Origin and site
A site is the scheme and the main part of the host name. The main part is the name an organisation registers, such as shop.example. The sports shop keeps help pages at https://help.shop.example. The help pages and the shop are two origins and one site.
Of the four URLs in the table, https://help.shop.example/orders and https://shop.example:8443/orders are the same site as the shop. The http URL is a different site, because the scheme is different.
The cookie setting SameSite (Sessions) compares sites, so a page on help.shop.example can still send Sam's cookie to shop.example.
What is CORS
A server sometimes wants a page on another origin to read a response. A sports club's website at https://club.example shows each member's ticket orders, and reads the orders from the sports shop.
CORS is a set of headers that a server sends to let pages from named origins read the server's responses. CORS is short for cross-origin resource sharing. In the block of flats, CORS is a person choosing to show a letter to a named neighbour.
The browser adds the header Origin to a request that a page sends to another origin. Origin names the origin of the page. The server answers with Access-Control-Allow-Origin, which names one origin. The browser lets the page read the response when the two origins match.
Access-Control-Allow-Credentials: true covers a request that carried a cookie. With that header, the page can read a response made for the signed-in customer. The value * in Access-Control-Allow-Origin allows every origin. A browser does not let a page read the response to a request that carried a cookie where the value is *, so the star is fine for data anyone may read. The star is a weakness on a server that asks for no sign-in because only an internal network can reach the server.
A CORS setting that trusts every origin
The Python code runs on the server of the sports shop and adds the two CORS headers to a response. The variable request holds the request and the variable response holds the response. Look at where the value of origin comes from.
# Weakness: every origin is trusted, with the customer's cookie origin = request.headers.get("Origin") response.headers["Access-Control-Allow-Origin"] = origin response.headers["Access-Control-Allow-Credentials"] = "true"
Line 2 reads the Origin header from the request. Line 3 copies the same origin into the response, so the server allows every origin that asks. A request from the attacker's page arrives with Origin: https://evil.example, and the server allows https://evil.example. With line 4, the attacker's page can read Sam's orders.
The safer code keeps a fixed list of allowed origins and checks the list first.
# Safer: a fixed list of origins ALLOWED = ["https://club.example"] origin = request.headers.get("Origin") if origin in ALLOWED: response.headers["Access-Control-Allow-Origin"] = origin response.headers["Access-Control-Allow-Credentials"] = "true"
Line 2 lists the one origin the sports shop trusts. Line 4 lets the two CORS headers through only when the origin of the request is in the list. A request from https://evil.example gets no CORS headers, so the attacker's page cannot read the response.
A change to a CORS line changes which origins can read data. A reviewer reads the change as a change to authorisation.
Two more mistakes
A check on part of the origin can let the wrong origin through. The Python origin.endswith("club.example") checks the end of the origin only. The check is true for https://club.example and for https://evilclub.example, a host name an attacker can register.
The word null in the list of allowed origins is the second mistake. A browser sends Origin: null from some pages that have no origin, such as a page opened from a file. An attacker can make a page that sends Origin: null.
Examples of what to look for
- A line that copies the
Originheader of a request intoAccess-Control-Allow-Origin, withAccess-Control-Allow-Credentialsset totrue. cors({ origin: true, credentials: true })in JavaScript, which copies theOriginheader in the same way.- An origin checked by the end of the text only, with
endswithin Python orendsWithin JavaScript. - The word
nullin a list of allowed origins. - The value
*inAccess-Control-Allow-Originon a response that holds data not meant for everyone. - Code written by hand to find the host name in a URL, which can pick the wrong part of the URL.
Check your understanding
Name the three parts of a URL that make an origin.
Show the answer
The scheme, the host name and the port.
A page on
https://evil.examplesends a request tohttps://shop.example/orderswhile Sam is signed in. State what the browser does with the response.Show the answer
The browser does not let the page read the response, because the two origins are different. The request is still sent.
Give one reason why a server must not copy the
Originheader of a request intoAccess-Control-Allow-Origin.Show the answer
The server then allows every origin that asks, so an attacker's page can read the response.