1. Home
  2. Explore
  3. Computer Science
  4. Web Application Security Basics

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Web Application Security Basics

Web Application Security Fundamentals

Introduction

Web applications are built upon three fundamental building blocks:

  • Hypertext Markup Language (HTML) – defines the structure and content of a web page.

  • Cascading Style Sheets (CSS) – controls the visual rendering of HTML elements (colors, sizes, fonts, layouts).

  • JavaScript – adds dynamic, client‑side behavior, allowing interaction with the page and the user.


User Agent and HTTP Protocol

A user agent is any application that acts on a user’s behalf. The most common user agents are web browsers (e.g., Firefox, Chrome). They communicate using the Hypertext Transfer Protocol (HTTP) – the language of the World Wide Web.

HTTP Request Structure

When a user clicks a link, the browser sends an HTTP request:

  • Verb – e.g., GET

  • URL – the destination address

  • Protocol – e.g., HTTP/1.1

  • Request Headers – additional information sent to the web server, such as:

    • User-Agent – identifies the browser type and version

    • Referer – indicates the previous page from which the link was followed

HTTP Response Structure

The web server replies with an HTTP response:

  • Status Code – e.g., 200 OK (success)

  • Response Headers – include:

    • Server – web server software version

    • Set-Cookie – places a cookie in the browser

    • Content-Type – tells the browser how to interpret the body (e.g., text/html)

  • Body – the actual content (HTML document, JavaScript, image, etc.)

Key Concept: HTTP is a stateless protocol – the server does not remember previous requests. Cookies are the primary mechanism to create sessions and maintain state across requests.


HTML, CSS, and JavaScript in Detail

HTML

HTML documents consist of elements (tags) and attributes that refine an element (e.g., id, name, style). String literals are the text content inside elements.

Example:

<h1>Hello World</h1>

This renders a large heading with the text “Hello World”.

CSS

CSS rules define how elements are rendered. Example:

body { background-color: blue; color: white; }

This makes the body have a blue background and all text white by default.

JavaScript and the DOM

JavaScript is embedded in HTML using <script> tags. It runs on the client side and interacts with the web page through the Document Object Model (DOM) – a programming interface that allows JavaScript to read, modify, or delete elements and data within the page.

  • JavaScript can do anything the user can do with the web application from the browser’s perspective.

  • This capability is both powerful and a security risk if code is injected maliciously.


Same-Origin Policy

The same-origin policy is a fundamental security mechanism in web browsers. It restricts how a document or script loaded from one origin can interact with resources from another origin.

What Is an Origin?

An origin consists of three parts:

  1. Scheme (protocol) – e.g., https

  2. Hostname – e.g., embracethered.com

  3. Port – e.g., 443 (default for HTTPS)

Thus, https://embracethered.com and http://embracethered.com are different origins because the scheme differs.

How the Policy Works

If JavaScript is loaded from https://example.org, it cannot access HTML or JavaScript from https://example.org:8080 or http://example.org – those are different origins.

Example of an attack prevented by same-origin policy:

  • An attacker’s page (attacker.embracethered.com) embeds mail.google.com inside an <iframe>.

  • The attacker tries to access the iframe’s content with JavaScript.

  • The browser throws a DOMException: “Access denied” or “Permission denied to access the body of this cross-origin object.”

  • The same-origin policy blocks this cross-origin access.

Code Illustration

<!-- Attacker's page -->
<iframe id="mail" src="https://mail.google.com"></iframe>
<div id="output"></div>
<script>
  var iframe = document.getElementById('mail');
  var output = document.getElementById('output');
  // Attempting to access iframe content – this will fail
  output.innerHTML = iframe.contentDocument.body.innerHTML;
</script>

The third line attempts the malicious access and triggers the permission denied error.


X-Frame-Options and Clickjacking

X-Frame-Options is an HTTP response header that prevents a page from being displayed in an <iframe> (or similar embedding). It is a defense against clickjacking – an attack where a user is tricked into clicking something invisible (e.g., an overlay) while thinking they are clicking something else.

  • Setting X-Frame-Options: SAMEORIGIN allows framing only from the same origin.

  • Setting X-Frame-Options: DENY blocks framing entirely.

  • Many modern applications (e.g., Gmail) use this header to prevent being iframed by attackers.

Without this protection, an attacker could overlay multiple transparent iframes so that a “Play” button actually triggers a “Buy” action (like Amazon’s one‑click shopping).


Same-Site Concept

Same-site is a newer concept that includes subdomains under the same top‑level domain (TLD) plus one label (the eTLD+1).

  • Example: www.embracethered.com and blog.embracethered.com are same‑site.

  • The public suffix list (e.g., github.io, co.uk) defines exceptions where the “site” extends further.

Same‑site is used by the SameSite cookie attribute and several new HTTP request headers that help browsers communicate the request context to the server.


HTTP Headers for Request Context (Optional)

Modern browsers are introducing headers that tell the server where the request came from and how it was initiated. Three important headers are:

  • Referer (older) – the previous page’s URL (sometimes misspelled in the video as “referrer”).

  • Origin – the scheme, host, and port of the requesting source.

  • Sec-Fetch-* – a set of headers that describe the request’s destination, mode, site, and user‑initiated state.

These headers can simplify server‑side mitigation of several attacks. They are not yet fully standardized across all browsers but are gaining support.


Cookies

Cookies are small pieces of data stored by the browser at the server’s request. They are the primary method for maintaining state in HTTP.

Cookie Lifecycle

  1. Setting a cookie: The web server sends a Set-Cookie header in the HTTP response.

  2. Automatic inclusion: For every subsequent request to the same domain (and matching path and site), the browser automatically sends the cookie back to the server.

  3. Purpose: Because HTTP is stateless, cookies enable session tracking (e.g., remembering that a user is logged in).

Security warning: If an attacker steals a user’s session cookie, they can impersonate that user without knowing their credentials.

Cookie Security Flags

To protect cookies, the server can set these attributes in the Set-Cookie header:

  • HttpOnly – The cookie is inaccessible to JavaScript (e.g., document.cookie). It is sent only in HTTP requests, reducing the risk of theft via cross‑site scripting.

  • Secure – The cookie will only be transmitted over HTTPS (encrypted) connections, preventing interception on unencrypted networks.

  • SameSite – Controls whether the cookie is sent in cross‑site requests. Values:

    • Strict – never sent on cross‑site requests.

    • Lax – sent on top‑level navigations (e.g., clicking a link) but not on sub‑requests (e.g., images, iframes).

    • None – sent on all cross‑site requests (requires Secure flag).

SameSite is a powerful mitigation for certain attacks like cross‑site request forgery (CSRF).


Transport Security

Always Use HTTPS

  • All web traffic should be encrypted. There is no longer any justification for unencrypted HTTP, even for internal applications.

  • HTTPS protects against man‑in‑the‑middle attacks, eavesdropping, and data tampering.

HTTP Strict Transport Security (HSTS)

The Strict-Transport-Security response header instructs the browser to always use HTTPS for future requests to the domain, even if the user types http:// or clicks an http:// link.

  • Example: Strict-Transport-Security: max-age=31536000; includeSubDomains

  • This prevents downgrade attacks and accidental plaintext connections.

Free Certificates

Let’s Encrypt provides free, automated TLS certificates. They are widely trusted and easy to configure.


Useful Tools for Inspecting Web Traffic

Browser Developer Tools

All modern browsers include built‑in developer tools:

  • Inspect Element – view the HTML, CSS, and JavaScript.

  • Network Tab – inspect all HTTP requests and responses, including headers and payload.

Dedicated Proxies

  • Fiddler – free, Windows‑based, intercepts and displays HTTP/HTTPS traffic.

  • Burp Suite – popular for web application security testing.

  • OWASP ZAP – open‑source alternative with similar capabilities.

These tools allow you to examine every request and response, modify them, and replay them – essential for understanding and testing web security.


Summary

This session introduced the foundational concepts needed to understand web application vulnerabilities:

  • User agents (browsers) communicate via HTTP, a stateless protocol.

  • HTML, CSS, and JavaScript form the core of modern web pages.

  • The same‑origin policy is the cornerstone of web security, preventing scripts from one origin from accessing data of another.

  • X‑Frame‑Options protects against clickjacking.

  • Same‑site and related headers help servers understand request context.

  • Cookies enable session state but must be secured with HttpOnly, Secure, and SameSite flags.

  • HTTPS with HSTS is mandatory for protecting data in transit.

  • Developer tools and proxies are invaluable for testing and debugging.

Future sessions will dive into specific attacks such as cross‑site scripting, tab‑nabbing, and cross‑site request forgery.

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent