In computing, the challenge of serializing human-readable text into structured protocols is known as the in-band signaling problem. When control delimiters (such as quotation marks, angle brackets, slashes, or ampersands) share the same transmission stream as raw user data, parsers can become confused between syntax instructions and literal content.
String escaping establishes unambiguous boundaries by substituting ambiguous literal characters with designated escape sequences (frequently prefixed with a backslash \ or an ampersand &).
| Standard / Format | Prefix / Delimiter | Critical Metacharacters | Primary Vulnerability Prevented |
|---|---|---|---|
| JSON (RFC 8259) | Backslash \ |
", \, \n, \r, \t, \b, \f |
JSON syntax breakage, invalid payload parse errors |
| HTML5 Entities | Ampersand &...; |
&, <, >, ", ' |
Cross-Site Scripting (XSS), DOM injection |
| URL Percent-Encoding (RFC 3986) | Percent %HEX |
Spaces, ?, #, &, =, / |
HTTP Request Parameter Tampering, URI parsing errors |
| Regular Expressions (PCRE / JS) | Backslash \ |
\, ^, $, ., *, +, ?, (, ), [, ], {, }, | |
ReDoS (Regular Expression Denial of Service), wildcard attacks |
| SQL Literals (ANSI / MySQL) | Quote doubling '' or \' |
', ", \0, \n, \r, \Z |
SQL Injection (SQLi) query escape breakout |
The JSON specification strictly mandates that all code points from U+0000 through U+001F (control characters) MUST be escaped. Specifically:
\"\\\u0000 through \u001F./) may optionally be escaped as \/ to prevent </script> breakout when embedding JSON directly inside inline HTML script tags.While escaping literal strings is critical for text representation and logging, it is essential to remember that string escaping is not a substitute for parameterized queries:
? or $1). Escaping is only a fallback when building dynamic table names or identifiers.<p>...</p>) requires HTML entity encoding. Escaping user data for inside an HTML attribute (<input value="...">) or inside an inline JavaScript script block requires JavaScript unicode escaping.\/ is allowed specifically to allow JSON payloads to be safely embedded within HTML <script> tags without prematurely terminating the script block via </script>.'O''Reilly') to escape string literals. MySQL and SQLite also support C-style backslashes ('O\'Reilly'), but quote doubling is universal and portable across PostgreSQL, Oracle, SQL Server, and SQLite.