Tainted and secret strings
tainted means the value comes from outside the program, such as a form or a command-line argument.
secret is for passwords, tokens and keys. The compiler checks both words. A type test with is
and a conversion with as keep both words on the result.
tainted and secret
tainted and secret are two words you can write before string or bytes. tainted string is text from outside your program, such as a form field. secret string is a confidential value, such as a password or an API key.
A string built from such a value with . or interpolation is tainted or secret too. Text that a Core method returns is tainted, unless the method's documentation says where the text comes from. The compiler checks both words, so they do not make the program slower.
A tainted string cannot be a regular expression, a format template or a plain string. The error names the method that makes the value safe there, such as Core\IO::within for a path. A secret value does not compile with echo, in an error message, in a debug dump or in Core\Json::encode.
secret can also go before mixed. Core\Secret::reveal($value, "reason") is the only method that gives a plain value from a secret one.
Examples 04 to 07 show each kind of mistake, and none of them compiles.
A mark that travels with the value
<?nvs// Shows which operations on a tainted string give a tainted result, and which give a plain one.
tainted string $typed = '42 apples';
// A string built from a tainted string is tainted too.tainted string $shouted = Core\Str::upper($typed);tainted string $line = 'you asked for: ' . $typed;
// A method that returns a number or a `bool` returns a plain value.uint $length = Core\Str::length($typed);bool $hasApples = Core\Str::contains($typed, 'apples');
// `as int` converts the text to a number, and the number is a plain `int`.tainted string $digits = '42';int $count = $digits as int;
echo $shouted, "\n";echo $line, "\n";echo $length, ' characters, apples: ', $hasApples ? 'yes' : 'no', "\n";echo $count + 1, "\n";42 APPLESyou asked for: 42 apples9 characters, apples: yes43A token the compiler keeps out of the output
<?nvs// Shows what you can do with a secret string, and how to get a plain string from it.
secret string $token = 'hunter2';
// A string built from a secret string is secret too.secret string $header = 'Bearer ' . $token;
// You can compare a secret string. The result is a plain `bool`.bool $matches = $header == 'Bearer hunter2';
// `Core\Secret::reveal` returns a plain string. Its second argument is the reason.string $shown = Core\Secret::reveal($token, 'showing the token in this example');
echo $matches ? 'the header is the one we expected' : 'the header is different', "\n";echo 'the token is ', Core\Str::length($shown), ' characters long', "\n";echo $shown, "\n";the header is the one we expectedthe token is 7 characters longhunter2A search term somebody else typed
<?nvs// Shows a tainted search term in a pattern and in a link, and a secret API key in a header.
class Orders{ public static function search(tainted string $term, secret string $apiKey): void { array<string> $labels = [ 'Order 1042, coffee beans', 'Order 1043, filter papers', 'Order 1044, coffee grinder', ];
// `Core\Regex::quote` returns a plain pattern that matches the exact text. // So the search for "c.ffee" matches no order, because no label has a dot. Core\Regex\Pattern $pattern = Core\Regex::compile(Core\Regex::quote($term)); uint $hits = 0; foreach ($labels as string $label) { if (Core\Regex::matches($label, $pattern)) { $hits = $hits + 1; } }
// `Core\Uri::encodeComponent` returns a plain string that is safe in a link. string $link = 'https://example.test/orders?q=' . Core\Uri::encodeComponent($term);
// Only this line needs the key as a plain string, so only this line calls // `Core\Secret::reveal`. Everywhere else, `$apiKey` is still secret. string $authorization = 'Bearer ' . Core\Secret::reveal($apiKey, 'sending it to the orders service');
echo $hits, ' of ', Core\Arr::count($labels), " orders match\n"; echo $link, "\n"; echo 'the authorization header is ', Core\Str::length($authorization), " characters long\n"; }}
Orders::search('coffee', 'sk-live-abc123');Orders::search('c.ffee', 'sk-live-abc123');2 of 3 orders matchhttps://example.test/orders?q=coffeethe authorization header is 21 characters long0 of 3 orders matchhttps://example.test/orders?q=c.ffeethe authorization header is 21 characters longTainted text in a plain string
<?nvs// Shows that a tainted string cannot be assigned to a plain `string` variable.// Convert it with `as` or check it first. This program does not compile.// proof: compile-error
tainted string $input = 'Ada';string $name = $input;echo $name, "\n";error[E0401]: expected `string`, found `tainted string` --> 04-tainted-text-in-a-plain-string.nvs:7:16 |7 | string $name = $input; | ^^^^^^ this is `tainted string`
error: aborting due to 1 errorTo read what a code means and how to fix it, run `nvs agent show <code>`.Tainted text as a pattern
<?nvs// Shows that a tainted string cannot be a regular expression.// `Core\Regex::quote` gives a pattern that matches the text exactly.// This program does not compile.// proof: compile-error
tainted string $search = 'a+';Core\Regex\Pattern $pattern = Core\Regex::compile($search);echo Core\Regex::matches('aaa', $pattern) ? 'match' : 'no match', "\n";error[E0401]: expected `string`, found `tainted string` --> 05-tainted-text-as-a-pattern.nvs:8:51 |8 | Core\Regex\Pattern $pattern = Core\Regex::compile($search); | ^^^^^^^ this is `tainted string` = help: this parameter cannot take `tainted` text. `Core\Regex::quote` makes the text safe for it = suggestion (review): pass it through `Core\Regex::quote`: `Core\Regex::quote($search)`
error: aborting due to 1 errorTo read what a code means and how to fix it, run `nvs agent show <code>`.A secret in an error message
<?nvs// Shows that a secret string cannot be part of an error message.// An error message can appear in a log or on an error page. This program does not compile.// proof: compile-error
secret string $token = 'hunter2';throw new RuntimeError('bad token ' . $token);error[E0422]: a `secret`-qualified value cannot be passed as a `Throwable` message; it would surface in a stack trace or an error page --> 06-a-secret-in-an-error-message.nvs:7:7 |7 | throw new RuntimeError('bad token ' . $token); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ secret value used as the message here = help: reveal it explicitly first with `Core\Secret::reveal(..., "reason")`
error[E0401]: expected `tainted string`, found `secret string` --> 06-a-secret-in-an-error-message.nvs:7:24 |7 | throw new RuntimeError('bad token ' . $token); | ^^^^^^^^^^^^^^^^^^^^^ this is `secret string`
error: aborting due to 2 errorsTo read what a code means and how to fix it, run `nvs agent show <code>`.A secret in the output
<?nvs// Shows three ways to print a secret string. None of these lines compiles.// A string built from a secret with interpolation is secret too.// proof: compile-error
secret string $token = 'hunter2';echo "Bearer {$token}\n";Core\Debug::dump($token);echo Core\Json::encode($token), "\n";error[E0790]: `echo` cannot print a `secret` value --> 07-a-secret-in-the-output.nvs:7:6 |7 | echo "Bearer {******}\n"; | ^^^^^^^^^^^^^^^^^^^ this value is `secret` = help: Output is read by people and saved in logs. To print the value anyway, call `Core\Secret::reveal($value, "reason")` first. Joining it into a longer string does not help. The result is `secret` too.
error[E0724]: a `secret`-qualified value cannot be passed to `Core\Debug::dump`; a dump is written to be read by a person, so the value would be disclosed rather than used --> 07-a-secret-in-the-output.nvs:8:18 |8 | Core\Debug::dump($token); | ^^^^^^ secret value dumped here = help: reveal it explicitly first with `Core\Secret::reveal(..., "reason")`; a `secret`-typed *property* of a dumped object needs nothing — the record redacts it
error[E0791]: a `secret`-qualified value cannot be passed to `Core\Json::encode`; an encoded document is on its way to a response, a log or a queue, and none of those is the credential being used --> 07-a-secret-in-the-output.nvs:9:24 |9 | echo Core\Json::encode($token), "\n"; | ^^^^^^ secret value encoded here = help: reveal the one field that must travel with `Core\Secret::reveal(..., "reason")`, written at that field rather than at the call, so the rest of the value stays covered
error: aborting due to 3 errorsTo read what a code means and how to fix it, run `nvs agent show <code>`.A secret in a mixed value
<?nvs// Shows how to keep a secret in a `mixed` value: you write `secret mixed`.
class Settings{ // A setting can be a number, some text or a secret. // `secret mixed` can contain every one of them. public static function find(string $name): secret mixed { secret string $apiKey = 'sk-test-4471'; if ($name == 'apiKey') { return $apiKey; } if ($name == 'port') { return 8080; } return 'mail.example.com'; }}
// A value read from `secret mixed` is secret too.// `as string` gives a `secret tainted string`, so `echo` does not compile with it.secret tainted string $key = Settings::find('apiKey') as string;
// `Core\Secret::reveal` gives a plain value. The reason says why you may use it.tainted string $shown = Core\Secret::reveal($key, 'show the length only');echo 'The key has ', Core\Str::length($shown), " characters\n";
// A conversion to a number needs `Core\Secret::reveal` first.// This prints `8080`.secret tainted string $port = Settings::find('port') as string;echo Core\Secret::reveal($port, 'a port number is not confidential') as int, "\n";The key has 12 characters8080Details
You can write tainted, secret or both, in the order secret tainted string. They go before string and bytes, and secret also goes before mixed. tainted int and tainted secret string are syntax errors (E0102). You can use them on a parameter, a local variable, a property and a return type.
A tainted string stays tainted through . and interpolation. It also stays tainted through every Core method that returns text built from it, such as Core\Str::upper, trim and split. A method that returns a count or a bool, such as Core\Str::length or contains, returns a plain value. A parameter that is an instruction does not take a tainted string. That is a regular expression, a Core\Str::format template, a Core\Bytes::pack format and a date pattern. Neither does a Core parameter of type string that is not listed as taking one. Each of these is E0401, and so is string $plain = $input; for a tainted $input.
For a tainted string, as int, as uint, as float, as bool and as an enum give a plain value. For a secret string, each of these is E0894. Write Core\Secret::reveal($value, "reason") as int instead. as string and as bytes keep tainted and secret, so string $s = $input as string; is E0401. A type test keeps them too: after $x is string, a ?tainted string is a tainted string. Two methods give a plain string that is safe for one use: Core\Regex::quote for a pattern and Core\Uri::encodeComponent for a link.
A secret string stays secret through . and interpolation, separately from tainted. These do not compile with a secret value. echo and print are E0790, also for echo "Bearer {$token}". An error message is E0422. Core\Debug::dump and Core\Debug::render are E0724, and Core\Json::encode is E0791. Every Core parameter of type string or bytes is E0401. That includes the parameters that take a tainted string, so Core\Str::length($token) does not compile.
== between two secrets, or between a secret and a plain string, compiles and gives a plain bool. A declared array<secret string> keeps secret, so Core\Json::encode of it is E0791. An array written in the call is the same: Core\Json::encode(["token" => $token]) is E0791 too.
A secret value can be in a mixed variable only when you write secret mixed. mixed $m = $token; is E0401. A value read from secret mixed is secret, and as string gives a secret tainted string. A secret value is never an array key, in any array. $a[$token] = 1; is E0896, because Core\Arr::keys and foreach give the keys back as plain text. Keep the secret in the element instead. A secret or tainted value cannot go to a callable with no parameter types, or to Core\Reflect\ClassInfo::set, call and construct. That is E0895. Write the parameter types, such as callable(secret string): void.
Core\Secret::reveal($value, $reason) gives a plain string, and Core\Secret::revealBytes gives plain bytes. It removes only secret. For a secret tainted string, the result is a tainted string.
Do and don’t
Section titled “Do and don’t”- Do declare request input as
tainted string. - Do convert tainted input with
as intor an enum when you can. - Do declare passwords and API tokens as
secret string. - Do write
secret mixedfor amixedvariable that contains a secret.mixed $m = $secret;does not compile. - Don’t use a secret value as an array key. It does not compile. Use an id as the key.
- Don’t call
Core\Secret::revealonly to print a value. Write a reason that says why it must be shown.
