SQL injection and cross-site scripting (XSS) explained: why they still happen and how to stop them

A plain explanation of SQL injection and cross-site scripting, why they are still in the OWASP Top 10 2025, how to prevent them, and how AI-written code can bring them back.

· 8 min read · By RiposteSecurity Research Team

Hong KongSingaporeMalaysia

Key points

  • SQL injection lets an attacker change the questions your app asks its database. Cross-site scripting (XSS) lets an attacker run their code in your users' browsers.
  • Both come from the same mistake: treating text from a user as instructions instead of plain data.
  • Injection is A05 in the OWASP Top 10 2025, the current edition. It covers more weakness types than any other category.
  • The fixes are well known: parameterised queries, output encoding, a content security policy and modern frameworks. AI coding tools do not always use them.

SQL injection and cross-site scripting are among the oldest web security flaws, and they still turn up in penetration tests every month. This guide explains both in plain words, with harmless examples, and sets out how to prevent them, how AI-written code can reintroduce them, and how testing finds them.

What is SQL injection?

Most apps store data in a database and talk to it in a language called SQL. When you search for your order, the app builds a question like "find the order with number 12345 for this customer" and sends it to the database.

The problem starts when the app builds that question by pasting in whatever the user typed. If the user types an order number, all is fine. If the user types text that the database reads as part of the question, such as an extra condition, the meaning changes. The question might become "find the order with number 12345, or any order at all". The database does what it is told.

Think of a bank teller who reads out a customer's form word for word to the vault staff. If a customer writes "my account, and also open every other box" in the account field, and nobody checks, the vault staff follow the whole instruction.

Depending on the app, SQL injection can let an attacker read whole customer tables, change records, skip a login check, or in bad cases take over the server. Singapore's PDPC and CSA named SQL injection in their 12 November 2026 joint advisory on insecure APIs: APIs that do not check user input "may also be vulnerable to injection attacks, such as SQL injection".

What is cross-site scripting (XSS)?

Cross-site scripting is the same mistake, but in the browser instead of the database. Web pages are made of text plus instructions (HTML and JavaScript). If a site takes something a user typed and puts it into a page without marking it as plain text, the browser may treat it as instructions and run it.

Harmless example: a product review box. A normal user writes "Great shoes". A malicious user writes a review that contains hidden page code. If the site shows that review to every visitor without making it safe, the code runs in each visitor's browser, as if your site sent it.

What that code can do depends on the site. It can steal a logged-in session, change what a page shows, redirect people to a fake login page, or make requests as the user. There are three common forms:

  • Stored: the bad input is saved, like the review above, and shown to many users.
  • Reflected: the bad input is in a link. The site echoes it back to whoever clicks the link.
  • DOM-based: the page's own JavaScript takes something from the address bar or page and inserts it unsafely.

Why SQL injection and XSS still appear: OWASP Top 10 2025

The OWASP Top 10 is a widely used list of the most serious web application risks. The current edition is the 2025 list, which replaces the 2021 list. Injection is A05:2025, down from A03 in 2021. It still maps to 37 weakness types, more than any other category. OWASP describes XSS as high frequency and lower impact, with more than 30,000 published vulnerabilities, and SQL injection as lower frequency but high impact, with more than 14,000.

The US cyber agency CISA and the FBI issued "Secure by Design" alerts on SQL injection (25 March 2024) and on cross-site scripting (17 September 2024). Both said these flaws are preventable and called on software makers to remove them as a whole class.

Why they persist:

  • Old code: systems written before modern frameworks, still running and still connected to customer data.
  • Shortcuts: a developer builds one query by gluing text together "just for this report" or an admin tool.
  • Escape hatches: frameworks are safe by default but let developers switch protection off for one field, and that field is often the one with user content.
  • Third-party and supplier code: plug-ins, vendor portals and apps built by contractors that never had a security review.

Regulators in the region point to the same list. Hong Kong's Privacy Commissioner, in its guidance note on data security for information and communications technology, refers organisations to the OWASP Top Ten. MAS's Technology Risk Management Guidelines name "code injection attack" and "cross-site scripting" among the common attacks that online financial services should be protected against.

How to prevent SQL injection and XSS

For SQL injection

  • Parameterised queries (prepared statements). The app sends the question and the user's text separately, so the database never treats the text as part of the question. OWASP and CISA both name this as the main fix. The PDPC and CSA advisory also names parameterised queries.
  • Use the framework's database layer properly. Most modern frameworks parameterise for you. Watch for "raw query" functions.
  • Allow-lists for names you cannot parameterise, such as which column to sort by. Pick from a fixed list; never pass the user's text through.
  • Least privilege for the database account. The web app's account should not be able to drop tables or read data it does not need. This limits the damage if a flaw is found.

Filtering out "bad characters" is not enough on its own. CISA's alert calls that approach fragile.

For cross-site scripting

  • Output encoding. Before showing user text on a page, convert it so the browser displays it as text and never runs it. The right encoding depends on where the text goes: page body, attribute, JavaScript or web address.
  • Use a modern framework and keep its default protection on. Frameworks such as React, Angular and Vue encode output by default. The risk is in the functions that bypass this.
  • Content Security Policy (CSP). A header that tells the browser which scripts your site is allowed to run. A strict policy blocks many XSS attacks even if a flaw slips through.
  • Clean rich text with a trusted library if users can submit formatted content.
  • Protect session cookies with the HttpOnly and Secure flags, so a script cannot read them.

A web application firewall can block many obvious attempts, but it is a backup, not the fix.

How AI-written code can bring them back

AI coding assistants learn from huge amounts of public code, including old and insecure code. They often produce something that works on the first try. Working is not the same as safe.

In its 2026 report on AI-generated code, published 28 July 2026, the security testing vendor Veracode said code from the models it tested passed its cross-site scripting checks only 15% of the time on average, against 83% for SQL injection. Overall, about 44% of tasks introduced a security flaw, barely changed from its first report.

Singapore's PDPC said in an advisory on 30 September 2026 that AI-assisted code "may contain errors or security vulnerabilities that are not immediately apparent", and that AI outputs must be reviewed by technically competent people before deployment.

Common patterns we look for in AI-written code:

  • queries built by joining strings, especially in quick scripts, reports and admin pages;
  • use of a framework's "unsafe" HTML function to make formatting work;
  • no content security policy, or one loosened to make an example run;
  • code copied from one place to many, so one flaw appears in dozens of files.

The fix is the same as for human-written code: clear rules, review and testing. Our secure coding and DevSecOps guide covers rules for AI-written code.

How testing finds SQL injection and XSS

  • Code review and static testing: a person or tool reads the code looking for user input that reaches a query or a page without protection. Good for finding the root cause and every copy of it.
  • Dynamic scanning: a tool sends many test inputs to the running app and watches the responses. Fast, but it misses pages behind logins or multi-step flows unless configured well.
  • Penetration testing: a tester works through the app as an attacker would, including logged-in areas, APIs and admin tools, and confirms what can actually be reached. See our guide to vulnerability assessment vs penetration testing.

OWASP recommends combining code review with automated testing in the build pipeline. Test after every major change, not only once a year.

Checklist for engineering and IT leads

  • Search your code for queries built by joining strings. Replace them with parameterised queries.
  • List every place your framework's output protection is switched off. Justify or remove each one.
  • Add a content security policy, start in report-only mode, then enforce it.
  • Check database accounts used by web apps have only the rights they need.
  • Set HttpOnly and Secure on session cookies.
  • Add static and dynamic security testing to your build pipeline.
  • Review AI-written code with the same rules, especially quick scripts and internal tools.
  • Include supplier-built apps and APIs in your penetration tests.

Sources

  1. OWASP, Top 10:2025: owasp.org
  2. OWASP, A05:2025 Injection: owasp.org
  3. CISA and FBI, Secure by Design Alert: Eliminating SQL Injection Vulnerabilities in Software, 25 Mar 2024: cisa.gov
  4. CISA and FBI, Secure by Design Alert: Eliminating Cross-Site Scripting Vulnerabilities, 17 Sep 2024: cisa.gov
  5. PDPC and CSA, Joint Advisory on Data Protection Risks Arising from Insecure APIs (AD-2026-016), 12 Nov 2026: csa.gov.sg
  6. PDPC, Advisory for Data Protection Risks When Using Unauthorised AI Tools for the Development of Software or Scripts, 30 Sep 2026: pdpc.gov.sg
  7. Veracode, 2026 GenAI Code Security Report, 28 Jul 2026: veracode.com
  8. PCPD, Guidance Note on Data Security Measures for Information and Communications Technology, Aug 2022: pcpd.org.hk
  9. Monetary Authority of Singapore, Technology Risk Management Guidelines, 18 Jan 2021: mas.gov.sg

This guide is based on public documents as of 26 November 2026. It contains no working attack code by design.