04.6.4 · Skill in Security and privacy engineering

Secure coding

Writing code that avoids common vulnerabilities.

Secure coding prevents injection, cross-site scripting, insecure deserialization and the rest of the OWASP list. Most of it comes down to validating input, encoding output and using safe defaults. Frameworks help, until you work around them.

Your status · Secure coding

Saved in this browser. Sign in to keep it on every device.It colours this skill's tile on your map.

Why it matters for a product engineer

Most vulnerabilities are ordinary code: a query built from a string, a page that prints input as HTML, a file path taken from a request. A product engineer writes this code every day, often with an agent that writes it faster. Safe defaults, used every time, stop most of the OWASP list before a review has to find it.

How to do it

  1. Treat everything from outside as data: requests, files, webhooks and model output. Validate it against what you expect.
  2. Keep code and data apart: parameterized queries, templates that escape by default, no shell commands built from strings.
  3. Encode output for where it goes: HTML, an attribute, a URL or JavaScript.
  4. Use the framework’s safe defaults. Look carefully at every place that turns one off, such as raw HTML or raw SQL.
  5. Run a static analysis tool such as Semgrep in CI, and keep dependencies up to date.
  6. Apply the same rules to code that an agent writes.

Common mistakes

  • Escaping by hand instead of using parameters.
  • Validation only in the browser.
  • Stack traces and detailed errors in production responses.
  • Trusting an internal service or a model’s output because it is “ours”.

Example

In May 2023 the CL0P ransomware group exploited an unknown SQL injection flaw, CVE-2023-34362, in MOVEit Transfer, a file transfer product used by many organizations. According to CISA and the FBI, the injection let the group install a web shell called LEMURLOOT, run commands on the servers and steal data from the databases behind them. The way in was the flaw that parameterized queries prevent.

Anatomy

A parameterized query, from OWASP's cheat sheet

String query = "SELECT account_balance FROM user_data WHERE user_name = ?"; 1PreparedStatement statement = connection.prepareStatement(query); 2statement.setString(1, customerName); 3ResultSet results = statement.executeQuery();4

  1. 1The SQLComplete, with a placeholder for the value
  2. 2Defined firstThe database knows the SQL before any input
  3. 3The inputBound as a value, so tom' or '1'='1 is only a strange name
  4. 4RunInput cannot change the query

The input can only be a value, never SQL. Binding does not decide access, so the code must still check who may read that account.

ReportedOWASP SQL injection prevention cheat sheet

Sources: OWASP Top 10:2025 ↗ · SQL injection prevention cheat sheet (OWASP) ↗ · CL0P ransomware gang exploits CVE-2023-34362 MOVEit vulnerability (CISA and FBI) ↗

Resources

1
  • docsStart here
    OWASP Top 10 ↗
    OWASP · 30 min

    The most common serious web application risks, the shared starting point for secure coding.

Tools

1
  • ToolFreemium
    Semgrep

    Static analysis for security bugs and code patterns, with an open-source core engine.