Why it matters

Most injection and XSS flaws come from untrusted data reaching an interpreter in the wrong form; this chapter sets out where encoding, sanitization and safe deserialization must happen.

How to meet these requirements

  • Decode input once, and encode output as close as possible to the interpreter that will consume it.
  • Use parameterized queries and safe APIs for SQL, operating-system commands and other interpreters.
  • Sanitize rich content with maintained libraries, avoid eval and user-supplied templates, and defend against SSRF.
  • Use memory-safe string handling in unmanaged code, and deserialize untrusted data only into allowed types.

Requirements

Each requirement has a level. Level 1 holds the first-layer defenses (about a fifth of the standard), Level 2 adds what OWASP says most applications should strive for, and Level 3 adds defense-in-depth and harder-to-implement controls; meeting a level means meeting every requirement of the levels below it too. Cite a requirement with the version, for example v5.0.0-1.1.1.

V1.1 Encoding and Sanitization Architecture · 2 requirements

  1. V1.1.1 Level 2

    Verify that input is decoded or unescaped into a canonical form only once, it is only decoded when encoded data in that form is expected, and that this is done before processing the input further, for example it is not performed after input validation or sanitization.

  2. V1.1.2 Level 2

    Verify that the application performs output encoding and escaping either as a final step before being used by the interpreter for which it is intended or by the interpreter itself.

V1.2 Injection Prevention · 10 requirements

  1. V1.2.1 Level 1

    Verify that output encoding for an HTTP response, HTML document, or XML document is relevant for the context required, such as encoding the relevant characters for HTML elements, HTML attributes, HTML comments, CSS, or HTTP header fields, to avoid changing the message or document structure.

  2. V1.2.2 Level 1

    Verify that when dynamically building URLs, untrusted data is encoded according to its context (e.g., URL encoding or base64url encoding for query or path parameters). Ensure that only safe URL protocols are permitted (e.g., disallow javascript: or data:).

  3. V1.2.3 Level 1

    Verify that output encoding or escaping is used when dynamically building JavaScript content (including JSON), to avoid changing the message or document structure (to avoid JavaScript and JSON injection).

  4. V1.2.4 Level 1

    Verify that data selection or database queries (e.g., SQL, HQL, NoSQL, Cypher) use parameterized queries, ORMs, entity frameworks, or are otherwise protected from SQL Injection and other database injection attacks. This is also relevant when writing stored procedures.

  5. V1.2.5 Level 1

    Verify that the application protects against OS command injection and that operating system calls use parameterized OS queries or use contextual command line output encoding.

  6. V1.2.6 Level 2

    Verify that the application protects against LDAP injection vulnerabilities, or that specific security controls to prevent LDAP injection have been implemented.

  7. V1.2.7 Level 2

    Verify that the application is protected against XPath injection attacks by using query parameterization or precompiled queries.

  8. V1.2.8 Level 2

    Verify that LaTeX processors are configured securely (such as not using the "--shell-escape" flag) and an allowlist of commands is used to prevent LaTeX injection attacks.

  9. V1.2.9 Level 2

    Verify that the application escapes special characters in regular expressions (typically using a backslash) to prevent them from being misinterpreted as metacharacters.

  10. V1.2.10 Level 3

    Verify that the application is protected against CSV and Formula Injection. The application must follow the escaping rules defined in RFC 4180 sections 2.6 and 2.7 when exporting CSV content. Additionally, when exporting to CSV or other spreadsheet formats (such as XLS, XLSX, or ODF), special characters (including '=', '+', '-', '@', '\t' (tab), and '\0' (null character)) must be escaped with a single quote if they appear as the first character in a field value.

V1.3 Sanitization · 12 requirements

  1. V1.3.1 Level 1

    Verify that all untrusted HTML input from WYSIWYG editors or similar is sanitized using a well-known and secure HTML sanitization library or framework feature.

  2. V1.3.2 Level 1

    Verify that the application avoids the use of eval() or other dynamic code execution features such as Spring Expression Language (SpEL). Where there is no alternative, any user input being included must be sanitized before being executed.

  3. V1.3.3 Level 2

    Verify that data being passed to a potentially dangerous context is sanitized beforehand to enforce safety measures, such as only allowing characters which are safe for this context and trimming input which is too long.

  4. V1.3.4 Level 2

    Verify that user-supplied Scalable Vector Graphics (SVG) scriptable content is validated or sanitized to contain only tags and attributes (such as draw graphics) that are safe for the application, e.g., do not contain scripts and foreignObject.

  5. V1.3.5 Level 2

    Verify that the application sanitizes or disables user-supplied scriptable or expression template language content, such as Markdown, CSS or XSL stylesheets, BBCode, or similar.

  6. V1.3.6 Level 2

    Verify that the application protects against Server-side Request Forgery (SSRF) attacks, by validating untrusted data against an allowlist of protocols, domains, paths and ports and sanitizing potentially dangerous characters before using the data to call another service.

  7. V1.3.7 Level 2

    Verify that the application protects against template injection attacks by not allowing templates to be built based on untrusted input. Where there is no alternative, any untrusted input being included dynamically during template creation must be sanitized or strictly validated.

  8. V1.3.8 Level 2

    Verify that the application appropriately sanitizes untrusted input before use in Java Naming and Directory Interface (JNDI) queries and that JNDI is configured securely to prevent JNDI injection attacks.

  9. V1.3.9 Level 2

    Verify that the application sanitizes content before it is sent to memcache to prevent injection attacks.

  10. V1.3.10 Level 2

    Verify that format strings which might resolve in an unexpected or malicious way when used are sanitized before being processed.

  11. V1.3.11 Level 2

    Verify that the application sanitizes user input before passing to mail systems to protect against SMTP or IMAP injection.

  12. V1.3.12 Level 3

    Verify that regular expressions are free from elements causing exponential backtracking, and ensure untrusted input is sanitized to mitigate ReDoS or Runaway Regex attacks.

V1.4 Memory, String, and Unmanaged Code · 3 requirements

  1. V1.4.1 Level 2

    Verify that the application uses memory-safe string, safer memory copy and pointer arithmetic to detect or prevent stack, buffer, or heap overflows.

  2. V1.4.2 Level 2

    Verify that sign, range, and input validation techniques are used to prevent integer overflows.

  3. V1.4.3 Level 2

    Verify that dynamically allocated memory and resources are released, and that references or pointers to freed memory are removed or set to null to prevent dangling pointers and use-after-free vulnerabilities.

V1.5 Safe Deserialization · 3 requirements

  1. V1.5.1 Level 1

    Verify that the application configures XML parsers to use a restrictive configuration and that unsafe features such as resolving external entities are disabled to prevent XML eXternal Entity (XXE) attacks.

  2. V1.5.2 Level 2

    Verify that deserialization of untrusted data enforces safe input handling, such as using an allowlist of object types or restricting client-defined object types, to prevent deserialization attacks. Deserialization mechanisms that are explicitly defined as insecure must not be used with untrusted input.

  3. V1.5.3 Level 3

    Verify that different parsers used in the application for the same data type (e.g., JSON parsers, XML parsers, URL parsers), perform parsing in a consistent way and use the same character encoding mechanism to avoid issues such as JSON Interoperability vulnerabilities or different URI or file parsing behavior being exploited in Remote File Inclusion (RFI) or Server-side Request Forgery (SSRF) attacks.

Requirement IDs, levels and text are from the OWASP Application Security Verification Standard 5.0.0 (copyright © 2008-2025 The OWASP Foundation), unmodified, under the Creative Commons Attribution-ShareAlike 4.0 International License. Imported from release v5.0.0_release (commit 5cf9b03); license.

Guides on this site

Main subject · Recipe

Fix SQL injection in Python sqlite3

Bind query values separately from SQL syntax, then prove the fix preserves legitimate behavior and blocks an injection probe.

Main subject · Guide

Prevent XSS at the output boundary

Choose text rendering, context-aware encoding, or deliberate HTML sanitization according to how the browser will interpret the value.

Main subject · Guide

Render rich text without granting it application privileges

Choose text rendering or a restricted HTML policy, then test the final DOM after every transformation.

Main subject · Guide

Keep untrusted values out of executable syntax

Choose a safe interface for each interpreter and bound the work performed by parsers and converters.

Main subject · Guide

Parse bounded JSON and XML into an explicit application schema

Separate parser capabilities from schema rules, reject ambiguous and coercible inputs, and implement the boundary in a complete Python project.

Main subject · Guide

Keep generated output out of executable contexts

Choose safe rendering and narrow operations so model text cannot become browser code, database syntax or an unrestricted command.

Also covers this · Guide

Constrain server-side fetches to their intended destinations

Prevent untrusted URLs from turning a preview, import, or webhook feature into access to internal services.

Also covers this · Guide

Fuzz a parser and turn crashes into lasting regressions

Build a narrow harness, observe memory and resource failures, minimize the input, and test the repaired parser.

Also covers this · Guide

ASVS 5.0 verification records: requirement, test and evidence another engineer can repeat

Scope edition-qualified requirements, connect them to implementation and tests, and separate a passing check from an unsupported assurance claim.

Also covers this · Guide

Combine static and dynamic tests around a verified finding

Use analysis tools to find evidence, then retain an application regression that demonstrates the failure and the fix.

Also covers this · Guide

Run external programs without command or argument injection

Run programs from an argument list with no shell, end options before user values, and keep untrusted arguments away from Windows batch files.

Also covers this · Guide

Keep untrusted input out of eval, expression languages and templates

Keep code and templates constant, pass input as data, and treat in-process sandboxes as one layer, not a boundary.

Practice

References

  1. V1 Encoding and Sanitization (ASVS 5.0.0 source)
  2. OWASP LDAP Injection Prevention Cheat Sheet
  3. OWASP Cross Site Scripting Prevention Cheat Sheet
  4. OWASP DOM Based Cross Site Scripting Prevention Cheat Sheet
  5. OWASP XML External Entity Prevention Cheat Sheet

List data from the publisher. A guide is listed as the main subject when the weakness or risk is what the guide is about, and as covering it when a dedicated section or worked example addresses it.