Tainted input
Many attacks on web servers work the same way. Text from a request reaches a place that runs it as code. SQL injection, cross-site scripting and shell injection are all this one mistake.
In Novis, this mistake is a compile error. A program that makes it does not build, so it never runs.
A type for text from outside
Section titled “A type for text from outside”In Novis, a value from a request is tainted. This includes a form field, a header, a cookie, the
address and a JSON body. Its type is tainted string, and the compiler treats it as a different
type from string. A JSON body has the type mixed, and text converted from a mixed value is
also a tainted string.
A database query, an HTML page and a shell command need a plain string. So a tainted value does
not compile there. The error points at the value, before the program ever runs.
In the code examples on this site, a tainted variable has a dotted underline. Move the mouse over it to see a short note.
The safe ways to use it
Section titled “The safe ways to use it”You remove the taint by checking or escaping the value:
as ?intconverts the text to a whole number. The result is a plainint, ornull.- A value you put into an
htmltemplate with{$name}is escaped. - A value in a database query is passed as a parameter.
Core\Validatechecks a value against a rule, such as an email address.
Each of these is safe for one kind of use. HTML escaping does not make a value safe for SQL, so the compiler needs the right check for each place.
Secret values
Section titled “Secret values”A second word, secret, works the same way for passwords, keys and tokens. A secret string
cannot be printed, written to a log, put in JSON or used in an error message. Each of these does
not compile. A debug dump shows a placeholder for the value.
When one line really must use the text, it calls Core\Secret::reveal() with a short reason. The
reason is for the people who read the code, so a reviewer can search for every place.
Why this is different
Section titled “Why this is different”- The compiler adds the taint itself. You do not have to say which values come from outside. Every way into a program gives a tainted value, including a value read back from a database.
- A new function is safe by default. A
Corefunction that does not say what it accepts does not accept a tainted value. Text that aCorefunction returns is tainted, unless the function says where the text comes from. So a function that nobody checked fails at compile time. - There is no general
sanitize(). Each kind of use has its own escape function, named for that use. Some uses have none. - Nothing can turn it off. The check is part of
nvs checkand of every run. There is no setting for it, and no library you must annotate first.
Other languages
Section titled “Other languages”Most languages leave this check to a library, a framework or a separate scanner:
| Language | Text from outside | Secrets | What enforces it |
|---|---|---|---|
| PHP | Nothing in the language. The taint extension is not part of PHP. |
Nothing | You, each time |
| JavaScript, TypeScript | Nothing in the language. Trusted Types protects the browser page only. | Nothing | A browser setting |
| Python | Nothing in the language. Template engines such as Django’s escape HTML. | Nothing | Your framework |
| Java | @Tainted and @Untainted from the Checker Framework, a separate tool |
Nothing | A tool you can skip |
| C# | Code analyzers such as CodeQL. They give warnings, not errors. | Nothing | A scan you can ignore |
| Go | html/template escapes HTML. text/template has the same functions and does not escape. |
Nothing | The package you import |
| Ruby | taint and $SAFE existed. Ruby 3.0 removed the checks. |
Nothing | Nothing |
| Rust | Nothing in the language. HTML crates escape. | The secrecy crate |
The crate you choose |
| Novis | tainted, added to every value from outside |
secret |
The compiler |
Where to go next
Section titled “Where to go next”- Guides: tainted input shows each safe way in a program.
- Syntax: tainted and secret lists the rules.
