Skip to content

Core\Taint::assertTrusted

Core\Taint::assertTrusted() returns a tainted string as a plain string, after your own check.

A value that comes from outside your program, such as a visitor's request, has the type tainted string. Functions that run text as code, such as a regular expression or a database query, do not accept it. Usually you use a function made for that one place, such as Core\Html::escape. When none fits and your program has checked the value itself, you call assertTrusted().

Every call needs a second argument: a short reason that says what was checked. The program never reads it. The text does not change at all. Nothing is escaped or removed.

In plain words: a signed note from you. It says "I checked this", and anyone who reads the code can find it by name.

The examples below show a value compared with a list of allowed values, the text staying exactly the same, and a search pattern from the settings used as a regular expression.

Parameters

  • $valuestringThe value your program has checked. A plain string is also allowed, and the result is the same text.
  • $reasonstringWhat was checked, and why the value can be trusted. It is written for the people who read the code. The program never reads it.

Return value

Returns string. The same text as a plain string. Nothing is escaped or removed. A secret value does not compile here, so call Core\Secret::reveal on it first. For tainted bytes, use assertTrustedBytes().

Examples

Trust a value after a check

<?nvs
// Shows how to use a value from a visitor after your program has checked it.
// A value that comes from a visitor is `tainted`. Here it is the column a list is sorted by.
tainted string $sort = 'created_at' as tainted string;
// The program compares the value with the three columns it allows.
// Only after that check does it call `assertTrusted`.
if ($sort == 'name' || $sort == 'price' || $sort == 'created_at') {
string $column = Core\Taint::assertTrusted($sort, 'the value is one of the three allowed columns');
// This prints "ORDER BY created_at".
echo 'ORDER BY ', $column, "\n";
} else {
echo "ORDER BY name\n";
}
Output
ORDER BY created_at

The text does not change

<?nvs
// Shows that `assertTrusted` does not clean or escape the text.
// This `tainted` value has characters that HTML treats in a special way.
tainted string $title = '<b>Tom & Jerry</b>' as tainted string;
// The reason can be a constant, so you can use the same reason in many places.
class Reasons
{
public const string EDITOR = 'the editor typed this title in the admin area';
}
// `assertTrusted` returns exactly the same text. Only the type changes.
// This prints "<b>Tom & Jerry</b>" and then 18.
string $trusted = Core\Taint::assertTrusted($title, Reasons::EDITOR);
echo $trusted, "\n";
echo Core\Str::length($trusted), "\n";
// If you need safe HTML, use an HTML function such as `Core\Html::escape`.
Output
<b>Tom & Jerry</b>
18

Use a pattern from the settings

<?nvs
// Shows how to use a search pattern that the site owner wrote in the settings.
// The pattern is read from outside the program, so it is `tainted`.
// `Core\Regex::compile` does not accept a `tainted` pattern.
tainted string $fromSettings = '^ORD-[0-9]{4}$' as tainted string;
// The site owner wrote this pattern, not a visitor, so the program trusts it.
string $pattern = Core\Taint::assertTrusted($fromSettings, 'only the site owner can edit this setting');
Core\Regex\Pattern $orderId = Core\Regex::compile($pattern);
// This prints "ORD-1042 yes", "ORD-12 no" and "ord-1042 no".
array<string> $ids = ['ORD-1042', 'ORD-12', 'ord-1042'];
foreach ($ids as string $id) {
echo $id, Core\Regex::matches($id, $orderId) ? ' yes' : ' no', "\n";
}
Output
ORD-1042 yes
ORD-12 no
ord-1042 no