BLACK FRIDAY deals live across the site – up to 20% off all courses! – use code: BF26WEEK3SAT

18 November 2026

Web application security fundamentals: XSS, CSRF and SQL injection explained

Web application security is about protecting web apps from attacks that exploit flaws in their code. Three common vulnerabilities are cross site scripting (XSS), cross site request forgery (CSRF) and...

StayAhead Team

Web application security is about protecting web apps from attacks that exploit flaws in their code. Three common vulnerabilities are cross site scripting (XSS), cross site request forgery (CSRF) and SQL injection. The primary defences are output encoding for XSS, anti CSRF tokens for CSRF and parameterised queries for SQL injection.

What is web application security?

Web application security covers the practices developers use to prevent attackers exploiting weaknesses in websites and web based applications. Developers need to understand how attacks work even when modern frameworks provide built in protections.

Our recent webinar, Ethical Hacking and Secure Coding, demonstrates why: relatively small coding choices determine whether user input is treated as harmless data or executable code. The OWASP Top 10 tracks major web application security risks, while OWASP’s guidance on SQL injection provides further detail on how this particular attack works.

Cross site scripting (XSS)

How an XSS attack works

Cross site scripting occurs when untrusted input reaches a web page in a form the browser interprets as executable script.

In our webinar example, a comments page stores whatever a user submits and displays it to later visitors. The attacker enters HTML containing JavaScript instead of an ordinary comment. Because the application renders that input directly, the script executes for every user who visits the page.

Vulnerable:

{{ comment | safe }}

The demonstration uses a harmless alert, but the same weakness could expose information or alter page content.

How to prevent XSS

The most important defence against XSS is context appropriate output encoding, so untrusted values are displayed as data rather than executed as code.

Fixed:

{{ comment }}

Input validation adds another layer by rejecting values that do not match what a field should accept. A Content Security Policy (CSP) provides useful defence in depth but should not replace correct encoding or sanitisation.

Cross site request forgery (CSRF)

How a CSRF attack works

CSRF exploits a user's authenticated session. An attacker tricks someone who is already logged into a vulnerable application into sending a request they did not intend.

The webinar demonstrates this with a money transfer page. The logged in user follows a deceptive link from another page. The request contains enough information to transfer £500 to another account, and the vulnerable application accepts it because the user's authenticated session is still active.

Vulnerable:

<a href="/transfer?to=Carol&amount=500">Claim reward</a>

How to prevent CSRF

The best way to protect a web app from CSRF is to use anti CSRF tokens, supported by appropriately configured SameSite cookies.

A token adds session specific information that an attacker cannot simply place in a malicious link. The server checks it before accepting a state changing request.

Fixed:

<input type="hidden" name="csrf_token" value="{{ csrf_token }}">

SameSite cookie settings provide additional protection by restricting when authentication cookies travel with cross site requests.

SQL injection

How a SQL injection attack works

SQL injection occurs when untrusted input is incorporated into a database query as SQL syntax. In the webinar login demo, the attacker adds SQL characters to a known username. The vulnerable query then ignores the password check, allowing access without the correct password.

Vulnerable:

query = f"SELECT id, username FROM users WHERE username='{username}' AND password='{password}'"

The example shows why even a short piece of unsafe query construction can undermine authentication.

How to prevent SQL injection

The most important defence against SQL injection is parameterised queries, also called prepared statements, as explained in PortSwigger’s guide to SQL injection. They keep SQL structure separate from user supplied values, so the database treats those values as data.

Fixed:

cursor.execute(

"SELECT id, username FROM users WHERE username=? AND password=?",

(username, password)

)

Database accounts should also have only the permissions the application requires. Least privilege does not stop SQL injection, but it limits the damage if another control fails. Developers who work regularly with databases may also benefit from SQL training.

H2: XSS, CSRF and SQL injection compared

Although all three attacks exploit weaknesses in a web application, they work in different ways and require different primary defences.

Vulnerability What the attacker does Main fix
XSS Injects content that the browser executes as script Context appropriate output encoding
CSRF Tricks an authenticated user into sending an unwanted request Anti CSRF tokens
SQL injection Makes malicious input part of a database query Parameterised queries

Building secure coding habits

Frameworks and libraries increasingly include protections against these attacks, but developers still need to understand what those protections are doing. Our webinar exposed this necessity by running the same application first in vulnerable mode and then in secure mode. Developers who understand the underlying attacks are better placed to test their software without inadvertently weakening existing safeguards.

Hands on practice against a deliberately vulnerable application is one of the fastest ways to learn web application security. StayAhead's web development courses provide routes for developers who want to strengthen the underlying coding skills.

Explore secure coding and web development training.

FAQs

What is the difference between XSS and CSRF?

XSS injects malicious content that executes in a user's browser. CSRF instead abuses an authenticated user's session to make the application accept an unwanted request.

Do modern frameworks stop XSS automatically?

Modern frameworks often provide automatic escaping and safer templating, which reduces XSS risk. They do not remove it completely: developers still need to understand unsafe contexts and how framework protections may be bypassed or disabled.

Can a web application firewall stop SQL injection on its own?

No. A web application firewall may block some malicious requests, but it does not fix unsafe query construction. Parameterised queries remain the primary defence against SQL injection.