Skip to content

[SUGGESTION] Change string interpolation result from a string to a lazy-evaluated parameterized-string #535

@SecureFromScratch

Description

@SecureFromScratch

**The idea:
For the interpolated string:
"select email from demo.useremails where username = (name)$ and type=(emailType)$"
Instead of interpolated strings generating the following cpp1 code:
"select email from demo.useremails where username = " + cpp2::to_String(name) + " and type=" + cpp2::to_string(emailType)
it will generate this code:
"select email from demo.useremails where username = " + cpp2::embed(name) + " and type=" + cpp2::embed(emailType)

This results in a parameterized_string type, which lazily holds the string literals and embedded arguments for later encoding and effectively implements the recommendation in LangSec revisited: input security flaws of the 2nd kind of delaying input sanitization and encoding to the output phase (where output encoding is known).

Will your feature suggestion eliminate X% of security vulnerabilities of a given kind in current C++ code?

This has the potential to transparently and implicitly reduce or eliminate the following Top 25 CWE categories (by rank):
(2) - CWE-79 - improperly neutralizing input when generating web pages (cross-site scripting)
(3) CWE-89 - improperly neutralizing special elements in SQL commands (SQL injection).
(6) CWE-78 - improperly neutralizing special elements in operating system commands (OS command injection).
(not-in-top25) [CWE-117] Improper Output Neutralization for Logs (log pollution/log forging)
to a lesser degree:
(8) CWE-22 - improperly limiting pathnames to restricted directories (path traversal)
(17) CWE-77 - improperly neutralizing special elements in commands (command injection).
(24) CWE-611 - improperly restricting XML external entity references.
(25) CWE-94 - improper control of code generation (code injection)

Note that these are all security vulnerabilities typically created from string concatenation with poorly sanitized/validated user input.

Describe alternatives you've considered.
Add a special interpolate-string prefix to control whether generated code creates a string or a parameterized-string, but this is not needed by simple adding an operator<< for parameterized strings.

Current methods are relying on programmers to be aware of the above mentioned CWEs and explicitly avoiding them.
By implementing lazy-concatenation and evaluation as interpolated strings default we remove the neccessity for programmer's awareness. Instead, the anus falls on library implementers to stop acceptting strings as input and instead support parameterized strings and perform appropriate validation,sanitization and encoding on them.

A good example is automatically generating a parameterized-sql-query from a parameterized string.

Another example is removing cr/lf characters when logging embedded arguments (but leave them for the string literals, thus supporting multiline log output but preventing unsanitized user input from causing log pollution or forgery)

Same goes for concatenating hard-coded html code with user supplied inputs, passing arguments to an os command by concatenating with user supplied inputs, etc.

**Change to cppfront:
The change to the cppfront is minimal: simply replace output of "cpp2::to_string" with "cpp2::embed" when handling interpolated strings and add an include file for "parameterized.hpp" which will contain the parameterized string implementation.

**POC

Checkout the fork at:
https://github.com/yarivtal/cppfront_parameterized_strings

"Example" folder contains both cpp2 samples and the generated cpp files (so you don't have to generate yourselfs)
The parameterized strings are implemented in a small library I created : pasteur
Its code is included in the fork for easier use, but you can find the lib here: https://github.com/SecureFromScratch/pasteur

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions