Web Security
�Week 4 - Exceptions to the Same Origin Policy
Old Dominion University
Department of Computer Science
CS 433/533 Fall 2023
Michael L. Nelson <mln@cs.odu.edu>
2024-09-16
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
Same origin policy exceptions
2
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
<img>, <script>, and CSS are
same origin policy exceptions!
3
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
Same origin policy and
ambient authority
<h1>Welcome to your account!</h1>
<img src='https://target.com/avatar.png' />
4
In the above example, the URL target.com/avatar.png is used for all accounts, with the cookie used by the server to distinguish your avatar from my avatar. This is instead of having the server include different URLs (target.com/avatar-001.png, avatar-002.png, etc.) which would make the HTML page less cacheable.
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
Solution: SameSite cookies
From target.com:
GET /avatar.png HTTP/1.1
Cookie: sessionId=1234
Referer: https://target.com/
From attacker.com:
GET /avatar.png HTTP/1.1
Referer: https://attacker.com/
5
if attacker.com is a phishing site, it is now a lot harder to build a convincing look-alike.
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
Solution: Referer header
6
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
7
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
8
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
9
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
10
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
11
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
12
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
13
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
Referer
14
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
Remember that forms can POST to another origin!
15
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
Cookies do not obey
same origin policy
16
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
We can harden or relax the
same origin policy
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
Revisiting what the same origin policy allows
18
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
Hardening the
same origin policy
19
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
Recall the Referer request header
20
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
Referer can leak info to other sites
21
Here's a link I only share with my friends – don't tell anyone else!!!
https://www.cs.odu.edu/~mln/teaching/cs595-s21/sooper-sekrit-product-ideas.html
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
Now Google knows my secret URL
22
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
Referrer-Policy response header
23
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
docs.google.com prevents your browser from leaking Referer info to non-Google Docs sites
24
$ curl -I https://docs.google.com/presentation/d/1NFyC1huil5uOic4ITtEUlogUqAfP8PQIpNGChtUJE7Y/edit#slide=id.gbc694ac400_0_109
HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
X-Robots-Tag: noindex, nofollow, nosnippet
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: Mon, 01 Jan 1990 00:00:00 GMT
Date: Wed, 10 Feb 2021 23:09:33 GMT
Content-Length: 447525
P3P: CP="This is not a P3P policy! See g.co/p3phelp for more info."
Strict-Transport-Security: max-age=31536000; includeSubDomains
Content-Security-Policy: base-uri 'self';object-src 'self' blob:;report-uri https://docs.google.com/presentation/cspreport;script-src 'nonce-gLqphbwqOVspb3On6UUgRg' 'unsafe-inline' 'strict-dynamic' https: http: 'unsafe-eval'
Referrer-Policy: origin
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Server: GSE
Set-Cookie: NID=209=I91MVBS_rjRS743-o56cJmCDW265Pc_heOTp1mkqZoWNSD607unmBwGbJtPAqW_yuWuLB1JcnelNlusFvFPv5vbceFJ2pQyaPkF6fZEUk-M6bJv8N_ccE6xDZAId6PHIU4dvTtcbZ9S0u_5waaDSs1ExL8DZ03ddkoy3Ytag5_w; expires=Thu, 12-Aug-2021 23:09:33 GMT; path=/; domain=.google.com; HttpOnly
Set-Cookie: S=apps-presentations=JGHgefN3DDdAbjDbv_fnwWTzOMb8ogaxLXJwVGrigXg; Domain=.docs.google.com; Expires=Thu, 11-Feb-2021 00:09:33 GMT; Path=/presentation/d/1NFyC1huil5uOic4ITtEUlogUqAfP8PQIpNGChtUJE7Y; Secure; HttpOnly; SameSite=none
Set-Cookie: GFE_RTT=462; Domain=.docs.google.com; Expires=Wed, 10-Feb-2021 23:14:33 GMT; Path=/; Secure; Priority=LOW; SameSite=strict
Alt-Svc: h3-29=":443"; ma=2592000,h3-T051=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
Policies
&
Examples
25
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
Change the response depending on how the user got to your site?
26
GET https://badguys.com/ HTTP/1.1
Referer: https://goodguys.com/somepage.html
…
Should badguys.com:
Cf. Cloaking
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
In the bad old days...
27
GET http://photobucket.com/winneri/someImage.jpeg
Referer: http://www.stampboards.com/viewtopic.php?t=1234
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
The server can’t really prevent being linked to, but it can make it frustrating for normal users
28
a tutorial to implement limiting 3rd party image hosting, the demos of which, ironically, no longer work:
https://alistapart.com/article/hotlinking/
many browser extensions to defeat these kinds of restrictions, here’s one:
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
Sites can control
who embeds them
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
Clickjacking!
30
attack site is
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
Frame busting!
31
<script>
if (window.top.location != window.location) {
window.top.location = window.location
}
</script>
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
Turns out that doesn’t work...
32
Takeaway: Don’t rely on .js to bust out of frames because the framing (attacker) site can counter your .js. Instead, use HTTP headers to specify framing preferences and let the browser enforce those policies.
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
X-Frame-Options response header
33
$ curl -IL www.google.com
HTTP/1.1 200 OK
Content-Type: text/html; charset=ISO-8859-1
P3P: CP="This is not a P3P policy! See g.co/p3phelp for more info."
Date: Thu, 11 Feb 2021 00:03:43 GMT
Server: gws
X-XSS-Protection: 0
X-Frame-Options: SAMEORIGIN
Transfer-Encoding: chunked
Expires: Thu, 11 Feb 2021 00:03:43 GMT
Cache-Control: private
Set-Cookie: 1P_JAR=2021-02-11-00; expires=Sat, 13-Mar-2021 00:03:43 GMT; path=/; domain=.google.com; Secure
Set-Cookie: NID=209=LV-khsyfMtVPfMrFmpjNu373gMzk4jg4TsyvZ346nnZAu9zLyz1vVSFOcHfTWOeVCUrEXI-XM_9XaeZ-1O8yWZKxCwxQQEBrdIgrV4ah1XPhJwYS9nQjn3B1utiTPCpGYuF3b8pFvQq57Y2LtHxVrzsFSz2IvGgO1e5w6Tt7Tdw; expires=Fri, 13-Aug-2021 00:03:43 GMT; path=/; domain=.google.com; HttpOnly
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
Unclear specs are bad...
34
not present = “anyone can frame me”
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
35
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
36
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
37
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
38
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
39
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
40
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
41
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
42
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
43
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
44
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
45
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
46
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
47
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
It’s turtles all the way down...
48
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
49
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
50
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
51
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
52
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
53
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
54
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
55
yikes!
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
56
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
57
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
58
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
59
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
60
better
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
Replacement for X-Frame-Options:
Content-Security-Policy: frame-ancestors
61
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/frame-ancestors
More on the Content-Security-Policy: response header in later weeks.
Spoiler: Good news: CSP is rich and powerful! Bad news: CSP is rich and powerful!
same as X-Frame-Options: deny
allow frames from two origins:
(self, aka sameorigin)
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
Can we prevent a site from submitting a form to our site?
62
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
Can we prevent a site from embedding images from our site?
63
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
Can we prevent a site from embedding scripts from our site?
64
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
Typical cross-site script embed
65
<script src='https://ajax.googleapis.com/ajax/libs/d3js/5.12.0/d3.min.js'></script>
<script>
d3.select('svg').selectAll('rect').data(data).enter()
</script>
Cross-site use of standard libraries (like D3) is common.
We want to encourage this kind of reuse.
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
Revisiting the same origin policy
66
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
Is site A allowed to read data
from site B?
67
const res = await fetch('https://leoonline.odu.edu/transcript.pdf')
const data = await res.body.arrayBuffer()
console.log(data)
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
What if site A and site B cooperate?
{ "date": 1570552348157 }
68
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
Use case: Date API Server
app.get('/api/date', (req, res) => {
res.send({ date: Date.now() })
})
{ "date": 1570552348157 }
69
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
How to read the response from the client?
const res = await fetch('https://site-b.com/api/date') const data = await res.body.json()
console.log(data)
70
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
Using <script> for cross-site communication
71
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
Naive idea
<script src='https://site-b.com/api/date'></script>
{ "date": 1570552348157 }
72
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
JSONP: JSON with Padding
<script>
function handleTime (data) {
console.log('got the date', data.date)
}
</script>
<script src='https://site-b.com/api/date?callback=handleTime'></script>
handleTime({ "date": 1570552348157 })
73
site-a.com: I would like to access your response in a function I'm choosing to call handleTime(). Please name it that.
site-b.com: I hear you loud and clear, and 've named my little Javascript response accordingly.
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
Downsides of JSONP
74
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
Cross-Origin Resource Sharing (CORS)
Access-Control-Allow-Origin: https://site-a.com
Access-Control-Allow-Origin: *
75
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
76
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
77
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
78
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
79
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
80
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
81
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
82
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
83
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
84
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
CORS version of the Date API
app.get('/api/date', (req, res) => {
res.set('Access-Control-Allow-Origin', '*')
res.send({ date: Date.now() })
})
{ "date": 1570552348157 }
85
this is the server (site-b.com) telling all browsers: “it’s cool if anyone reads this data -- there’s nothing private or protected in the response”
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
Ensuring private data returned by an authenticated API route isn't read by other sites?
86
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
Real world CORS example
87
% curl -isL memgator.cs.odu.edu/timemap/json/http://www.odu.edu/ | head -20
HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Access-Control-Expose-Headers: Link, Location, X-Memento-Count, Server
Content-Type: application/json
Date: Thu, 11 Feb 2021 17:42:30 GMT
Server: MemGator/1.0-rc8
X-Memento-Count: 5116
Transfer-Encoding: chunked
{
"original_uri": "http://www.odu.edu/",
"self": "https://memgator.cs.odu.edu/timemap/json/http://www.odu.edu/",
"mementos": {
"list": [
{
"datetime": "1996-12-21T05:13:52Z",
"uri": "https://web.archive.org/web/19961221051352/http://odu.edu:80/"
},
{
"datetime": "1997-12-11T00:24:13Z",
[much more to the response, this is only the first 20 lines]
These two headers are readable by scripts running at any origin
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh
The other two headers
88
% curl -I https://memgator.cs.odu.edu/timegate/http://www.odu.edu/
HTTP/1.1 302 Found
Server: nginx/1.18.0 (Ubuntu)
Date: Wed, 20 Sep 2023 18:03:13 GMT
Content-Type: text/html; charset=utf-8
Connection: keep-alive
Access-Control-Allow-Origin: *
Access-Control-Expose-Headers: Link, Location, X-Memento-Count, Server
Link: <http://www.odu.edu/>; rel="original", <http://archive.md/20120805114200/http://www.odu.edu/>; rel="first memento"; datetime="Sun, 05 Aug 2012 11:42:00 GMT", <https://wayback.archive-it.org/all/20230826045613/http://odu.edu/>; rel="prev memento"; datetime="Sat, 26 Aug 2023 04:56:13 GMT", <https://web.archive.org/web/20230918165416/https://www.odu.edu/>; rel="last memento"; datetime="Mon, 18 Sep 2023 16:54:16 GMT", <https://memgator.cs.odu.edu/timemap/link/http://www.odu.edu/>; rel="timemap"; type="application/link-format", <https://memgator.cs.odu.edu/timemap/json/http://www.odu.edu/>; rel="timemap"; type="application/json", <https://memgator.cs.odu.edu/timemap/cdxj/http://www.odu.edu/>; rel="timemap"; type="application/cdxj+ors", <https://memgator.cs.odu.edu/timegate/http://www.odu.edu/>; rel="timegate"
Location: https://web.archive.org/web/20230918165416/https://www.odu.edu/
Vary: accept-datetime
These two headers are readable by scripts running at any origin
ODU CS 433/533 Web Security Fall 2024 mln@cs.odu.edu
Based on Stanford CS 253 by Feross Aboukhadijeh