Web Application Security Basics


Web Application Security Basics

Michael
Alright, Hope, let's dive into web application security fundamentals. So the first thing to understand is that every web app is built on three core technologies: HTML for structure, CSS for styling, and JavaScript for dynamic behavior.

Hope
Exactly. HTML is like the skeleton, CSS makes it look good, and JavaScript brings it to life. But the interaction between the user and that app happens through something called the user agent—typically a browser like Chrome or Firefox.

Michael
Right, and the browser speaks HTTP, the Hypertext Transfer Protocol. When you click a link, the browser sends an HTTP request. For example, a GET request includes the request line (GET, URL, protocol version) and headers like User-Agent and Referer.

Hope
And the server responds with a status code—like 200 OK—and response headers. One crucial header is Content-Type, which tells the browser how to interpret the body. Often it's text/html, but it could be JavaScript or an image.

Michael
The response body contains the HTML document. Inside that, you have elements with attributes, CSS to style them, and script tags with JavaScript. JavaScript runs in the browser and can interact with the page through the Document Object Model, or DOM.

Hope
And here's where security gets interesting. JavaScript from one website could theoretically access data from another website if there were no restrictions. That's why the same-origin policy exists.

Michael
The same-origin policy says that JavaScript from one origin can only interact with resources from that same origin. An origin is defined by the scheme (like HTTPS), the host (like example.com), and the port. So https://example.com and http://example.com are different origins.

Hope
To see it in action, imagine an attacker's page that embeds Gmail in an iframe. The attacker can't use JavaScript to read the iframe's content because of the same-origin policy. The browser throws a permission denied error.

Michael
And there's an extra layer of protection: the X-Frame-Options response header. Sites like Gmail set it to SAMEORIGIN or DENY, so the browser refuses to embed them in iframes at all. That's a defense against clickjacking attacks where invisible iframes trick you into clicking something else.

Hope
Another concept is same-site, which is a bit broader than same-origin. It considers the top-level domain plus one, so embracethered.com and blog.embracethered.com are same-site. This is important for newer cookie security features.

Michael
Speaking of cookies—they're central to web security. HTTP is stateless, meaning each request is independent. Cookies are a way to create sessions. When you log in, the server sets a cookie in your browser. The browser then automatically sends that cookie with every subsequent request to that domain.

Hope
That's why cookie theft is so dangerous—it lets an attacker impersonate your session. To protect cookies, you can set flags like HttpOnly (makes it inaccessible to JavaScript), Secure (only sent over HTTPS), and SameSite (controls when cookies are sent across sites).

Michael
And encryption is a must nowadays. We should always use HTTPS. The Strict-Transport-Security header tells browsers to always upgrade HTTP to HTTPS, even if the user types http. You can get free certificates from Let's Encrypt.

Hope
Great. Finally, to see all this in practice, you can use tools like Fiddler or the built-in browser developer tools. They let you inspect HTTP requests and responses, see HTML structure, and debug security issues.

Michael
So to wrap up: we covered the building blocks—HTML, CSS, JavaScript, user agents, HTTP, the same-origin policy, same-site, cookies with their security flags, encryption, and some tools. This foundation will help us understand the specific vulnerabilities we'll tackle next, like cross-site scripting and cross-site request forgery.

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