Lesson 2 of 3 Acquired Software and Secure Coding

Source Code Weaknesses and Vulnerabilities

Many vulnerabilities come from a small number of recurring coding mistakes. This lesson covers the main source-code weaknesses and the OWASP Top 10.

Learning outcomes

After completing this lesson, you will be able to:

  • Identify common source-code weaknesses such as injection, XSS, CSRF and race conditions.
  • Describe the categories of the OWASP Top 10:2025.

Common weaknesses

Most source-code weaknesses share one root cause: the program trusts something it should have checked. Injection is the clearest example. Suppose a login function builds a database query by joining text: the query looks up the user whose name equals whatever was typed into the form. If an attacker types ' OR '1'='1 as the user name, the quote closes the intended text, and the rest becomes part of the query's logic, a condition that is always true, so the query may return every user. The database cannot tell which parts of the query the developer wrote and which the attacker supplied. The same pattern produces OS command injection when input is placed into a shell command, and LDAP injection when it is placed into a directory query.

Cross-site scripting (XSS) is injection into a web page. If a forum displays posts exactly as they were submitted, an attacker can post text containing a script, and every visitor's browser runs it as though it came from the forum, allowing the attacker to act within the victim's session. In stored XSS the script is saved on the server, in reflected XSS it is bounced back from a crafted link, and in DOM-based XSS the page's own scripts insert it. Cross-site request forgery (CSRF) works differently: no script is injected, but a malicious site causes the victim's browser to send a request, such as "change email address", to a site where the victim is logged in, and the browser attaches the victim's session cookie automatically. XSS abuses the user's trust in the site; CSRF abuses the site's trust in the user's browser.

Memory and timing weaknesses arise in different ways. A buffer overflow occurs when a program copies input into a fixed-size area of memory without checking its length, so excess data overwrites adjacent memory, which may include the address the program will jump to next; it is mainly a risk in languages such as C and C++. A race condition, particularly time of check to time of use (TOCTOU), occurs when a program checks a condition and then acts on it later, and the state changes in between. Insecure deserialisation occurs when an application rebuilds objects from data supplied by the user, allowing an attacker to craft data that creates unexpected objects or runs code.

Some weaknesses are deliberate or careless rather than subtle. A backdoor is a hidden way to bypass authentication, such as a maintenance account with a fixed password left in the code. Improper error handling reveals internal details or leaves the system in an inconsistent state. The following key terms summarise the weaknesses most often tested.

Buffer overflow
Writing more data into a buffer than it can hold, overwriting adjacent memory and potentially executing attacker code.
Injection
Untrusted data is interpreted as part of a command or query, such as SQL, OS command or LDAP injection.
Cross-site scripting (XSS)
Untrusted data is placed in a web page without encoding, so the victim's browser runs the attacker's script (stored, reflected or DOM-based).
Cross-site request forgery (CSRF)
A malicious site causes the victim's browser to send an authenticated request the victim did not intend.
Race condition (TOCTOU)
A time-of-check to time-of-use gap lets the state change between checking a condition and acting on it.
Insecure deserialisation
Deserialising untrusted data allows attackers to manipulate objects or execute code.
Backdoor
Hidden code or credentials that bypass normal authentication, sometimes left by developers for maintenance.
Improper error handling
Errors that reveal stack traces, queries or configuration, or that leave the system in an insecure state.

Other weaknesses include integer overflows, hardcoded credentials, path traversal, server-side request forgery, and missing authorisation checks. The Common Weakness Enumeration (CWE) catalogues weakness types, and CVE identifies specific vulnerabilities in products.

The following diagram shows a time-of-check to time-of-use race and its fix.

A time-of-check to time-of-use race condition. The application checks that the user may act on one resource, then acts on a resource identified again in a later request, which the attacker changes in between. Performing the check and the action atomically on the same server-side data fixes it.
TOCTOU is a classic race condition.
Sort into groups

Name the weakness

Exam skills 8.5.1

Identify the source-code weakness in each case.

Log in to try this activity Interactive activities are free with an account.

The OWASP Top 10

The OWASP Top 10 is the best-known awareness list of web application risks. It is compiled from data contributed by testing organisations and a community survey, and each category groups several related CWE weaknesses. It is an awareness document, not a complete standard; organisations that need a full set of testable requirements use the OWASP Application Security Verification Standard instead.

Several categories reflect themes covered elsewhere in this guide. Broken access control, at the top of the list, covers failures to enforce what authenticated users may do, such as viewing another user's record by changing an identifier. Software supply chain failures covers vulnerable or malicious components and compromised build processes. Insecure design covers flaws that no amount of correct coding can fix, because the design itself lacks a necessary control, which is why threat modelling appears in the SDLC. Mishandling of exceptional conditions covers errors and unusual states that leave an application failing open or leaking information. The 2025 edition lists the following categories.

RankCategory
A01Broken Access Control
A02Security Misconfiguration
A03Software Supply Chain Failures
A04Cryptographic Failures
A05Injection
A06Insecure Design
A07Authentication Failures
A08Software or Data Integrity Failures
A09Security Logging and Alerting Failures
A10Mishandling of Exceptional Conditions
Match the pairs

OWASP Top 10:2025 categories

Exam skills 8.5.1

Match each OWASP Top 10:2025 category with an example of it.

Log in to try this activity Interactive activities are free with an account.
Worked example

Identifying the flaw

A banking application checks that a user owns account A, then a few milliseconds later transfers money from the account ID supplied again in a second request. An attacker changes the account ID between the two requests. Which weakness is this?

This is a time-of-check to time-of-use (TOCTOU) race condition combined with broken access control: the authorisation check and the action use different inputs. The fix is to perform the check and the transfer atomically on the same server-side data, re-validating ownership at the moment of the transfer.

Check yourself

Which category is A01 in the OWASP Top 10:2025?

Show answer

Broken Access Control.

Exam Tips

Remember for the exam

  • Injection happens when data is treated as code.
  • XSS runs attacker script in the victim's browser; CSRF abuses the victim's session.
  • Broken access control is A01 in the OWASP Top 10:2025.