A browser runs code and displays content from many sources on a single page, and some of those sources are hostile. The same-origin policy keeps content from different sites apart, and cookies maintain login state across requests. Most web attacks abuse one of these two mechanisms, exploit a server that trusts its input, or deceive the user.
The Browser as an Application Platform
Early browsers displayed static pages, so attacks targeted servers. JavaScript lets a page run code inside the browser. Scripts can modify the page through the Document Object Model (DOM), exchange data with servers without reloading the page, and use device features such as the camera and location. Each capability adds to the browser’s attack surface.
HTTPS and Its Limits
HTTPS is HTTP sent over a TLS connection. It provides confidentiality, integrity, and server authentication for data in transit. HTTPS does not protect against the following:
-
A compromised server, which sees data after it is decrypted.
-
Malware on the client, which reads data before encryption or after decryption.
-
A malicious site, which can hold a valid certificate for its own lookalike domain.
-
Observation of which sites a user visits, since destination IP addresses remain visible.
Web attacks work over HTTPS because they target the browser, server, or user rather than the network.
The Same-Origin Policy
A page assembles content from many sources: scripts, images, fonts, and frames. An iframe embeds one page inside another.
An origin is the combination of a URL’s scheme, hostname, and port. Two URLs have the same origin only when all three match. The path isn’t part of the origin, and a subdomain is a different hostname.
The same-origin policy allows a page’s scripts to read and modify only data that came from the page’s own origin. Each origin has its own JavaScript namespace, DOM, and local storage. A script on one page cannot read another origin’s page content, stored data, or network responses.
A script runs with the authority of the frame that loads it, not the server it was downloaded from. A third-party script that a page includes has full access to that page, so including a script is a trust decision. An embedded frame keeps the origin of its own content and is isolated from the page around it. Passive content, such as images and stylesheets, has no authority.
The policy restricts reading, not sending. A page can display images, follow links, and submit forms to other origins, and those requests reach the server. Cross-Origin Resource Sharing (CORS) lets a server permit specified origins to read its responses. CORS doesn’t authenticate users and doesn’t prevent forged requests.
Cookies
HTTP is stateless. A cookie is a name-value pair that a server sets and the browser returns with every later request to that server. Cookies have three uses:
-
Session management: a cookie holds a session ID that identifies a logged-in user.
-
Personalization: a cookie records the user’s preferences.
-
Tracking: a cookie with a unique ID lets a server record a browser’s visits.
All three use the same mechanism. A session ID must be unpredictable, since anyone who holds it can use the session.
Three cookie attributes protect cookies:
-
HttpOnly prevents scripts on the page from reading the cookie.
-
Secure sends the cookie only over HTTPS.
-
SameSite controls whether the cookie accompanies requests that originate from another site, which helps prevent CSRF.
A first-party cookie belongs to the site shown in the address bar. A third-party cookie belongs to another site whose content the page loads, such as an ad or a social media button. The browser sends a third-party server’s cookie each time it loads that server’s content. A tracker whose content appears on many sites receives the same cookie from all of them and can link a browser’s visits into one profile. Browsers increasingly block or isolate third-party cookies.
Cross-Site Scripting
Cross-site scripting (XSS) runs an attacker’s JavaScript inside a page from a legitimate site. The script runs with that page’s origin, so the same-origin policy doesn’t protect against it. XSS is an injection attack: the site places user input into a page where the browser treats it as code.
The common forms of XSS are:
-
Reflected XSS: the script is part of a request, typically a link the victim clicks, and the server echoes it into its response.
-
Stored XSS: the script is saved on the server, as in a forum post, and runs for every later visitor to the page.
An injected script can do anything the page’s own scripts can do. It can read the site’s cookies, make authenticated requests to the site through the victim’s browser, modify the page, capture keystrokes, and display a fake login form on the trusted site. HttpOnly keeps the script from reading the session cookie, but the script can still make requests to the site, and the browser sends the cookie with them.
The main defense is to keep untrusted data from becoming executable page content. Defenses include encoding output for the place where it is inserted, using templates that escape text automatically, and sanitizing user-supplied HTML with a library.
Cross-Site Request Forgery
Cross-site request forgery (CSRF) makes a victim’s browser send an unwanted request to a site where the victim is logged in. The browser attaches a site’s cookies to every request to that site, including a request triggered by another site’s page. The server receives an authenticated request and performs the action. The attacker never obtains the cookie.
Defenses include the following:
-
An anti-CSRF token is an unpredictable value that the server includes in its forms and requires with state-changing requests. The server rejects requests with a missing or incorrect token. An attacker’s page can’t read the token, so it can’t supply the required value.
-
The server sets SameSite on its authentication cookies to restrict them on cross-site requests.
-
The server uses GET only to retrieve information, never to change application state. State-changing actions use POST and require an anti-CSRF token. POST doesn’t prevent CSRF by itself.
Broken Access Control
Authentication establishes who a user is. Authorization decides what the user is allowed to do. Broken access control lets someone access data or perform an action without the required permission because the server doesn’t enforce authorization. A common case is changing a record ID in a URL to retrieve another user’s record.
Browser-side controls don’t secure the server. An attacker can modify requests, change hidden form fields, and bypass a page’s JavaScript checks. The server must validate input and enforce authorization on every request.
Clickjacking
Clickjacking places an invisible frame over visible content, so a user’s click lands on a hidden control from another site. The click is real and uses the victim’s logged-in session. The defense is for a server to tell browsers which sites may display its pages in a frame.
Deception Attacks
A homograph attack uses a domain name whose characters look like those of a legitimate name. The lookalikes can be similar Latin characters or identical-looking letters from other alphabets, which internationalized domain names allow. Browsers display suspicious international names in an encoded form, but only after the user has followed the link. A link in an email or text message still shows the lookalike name.
A tracking pixel is an invisible one-pixel image. Loading it sends a request that gives the image’s server the user’s IP address, browser type, and any cookies for that server. In an email message, it tells the sender that the message’s images were fetched.
A page can display borrowed logos to appear credible. A script can also change a link’s destination when the user clicks it, so the URL shown before the click isn’t where the browser goes.
The Attacks Compared
The four attacks on web applications differ in what the attacker causes to happen:
| Attack | What the attacker causes |
|---|---|
| Cross-site scripting | The trusted page executes the attacker’s code. |
| Cross-site request forgery | The browser sends an unwanted authenticated request. |
| Clickjacking | The user clicks a concealed control. |
| Broken access control | The server permits an unauthorized action or disclosure. |
What You Don’t Need to Study
These exclusions apply to the web security material. Topics developed in earlier lectures, including SQL injection, drive-by downloads, typosquatting, and combosquatting, are covered by those lectures’ study guides.
The following are not required for the exam:
-
WebAssembly.
-
JavaScript code, including postMessage, textContent, innerHTML, and the code shown in the notes. Know that a script runs with the authority of the page that loads it.
-
The names of HTTP headers, such as Set-Cookie, Access-Control-Allow-Origin, X-Content-Type-Options, X-Frame-Options, Origin, and Referer. Know what cookies and CORS do, and that a server can limit which sites may frame its pages.
-
The Content Security Policy.
-
Cookie scope: the domain and path rules, and the difference between session cookies and persistent cookies. Know the three attributes that protect cookies.
-
The SameSite settings (Strict, Lax, and None) and the exact definition of a site. Know that SameSite controls whether cookies accompany cross-site requests, and know the definition of an origin.
-
DOM-based XSS. Know reflected XSS and stored XSS.
-
MIME sniffing and mixed content.
-
Dates, the history of JavaScript, and the incidents used as examples, such as the Netflix and ING Direct attacks.
-
Unicode code points and the name Punycode. Know that letters from different alphabets can look identical.
-
Which browsers block third-party cookies, the names of advertising products, and the list of ranked web risks.