Skip to content

Core\Taint::assertTrustedBytes

Core\Taint::assertTrustedBytes(bytes $value, string $reason): bytesCore\Taint::assertTrustedBytes(
    bytes $value,
    string $reason,
): bytes

Core\Taint::assertTrustedBytes() returns tainted bytes as plain bytes, after your own check.

Bytes that may contain data from outside your program have the type tainted bytes. Core\Serialize::decode does not accept them. Core\Serialize::encode also returns tainted bytes when the value can contain outside text. That is the case for a mixed property, for example. When your program wrote the bytes itself, you call assertTrustedBytes() once, and then decode() reads them.

Every call needs a second argument: a short reason that says what was checked. The program never reads it. The bytes do not change at all.

It works like Core\Taint::assertTrusted, which does the same for a tainted string.

In plain words: a signed note from you. It says "my program wrote these bytes", and anyone who reads the code can find it by name.

The examples below show an object saved and read back, the bytes staying exactly the same, and a list of jobs kept in memory and read later.

Parameters

  • $valuebytesThe bytes your program has checked. Plain bytes are also allowed, and the result is the same bytes.
  • $reasonstringWhat was checked, and why the bytes can be trusted. It is written for the people who read the code. The program never reads it.

Return value

Returns bytes. The same bytes as plain bytes. Nothing is changed. A secret value does not compile here, so call Core\Secret::revealBytes on it first.

Examples

Read back an object you saved

<?nvs
// Shows how a program reads back an object that it turned into bytes itself.
class Draft
{
public string $title;
public mixed $extra;
public function constructor(string $title, mixed $extra)
{
$this->title = $title;
$this->extra = $extra;
}
}
// `$extra` is `mixed`, so it can contain outside text.
// For this reason, `encode()` returns `tainted bytes`.
tainted bytes $saved = Core\Serialize::encode(new Draft('Spring sale', 3));
// This program wrote the bytes, so it trusts them. Then `decode()` accepts them.
bytes $mine = Core\Taint::assertTrustedBytes($saved, 'this program encoded the draft above');
Draft $draft = Core\Serialize::decode($mine) as Draft;
// This prints "Spring sale".
echo $draft->title, "\n";
Output
Spring sale

The bytes do not change

<?nvs
// Shows that `assertTrustedBytes` does not change the bytes. Only the type changes.
// These four bytes are not valid text. `assertTrustedBytes` accepts them too.
tainted bytes $data = Core\Bytes::pack('C4', 0, 255, 60, 195) as tainted bytes;
bytes $trusted = Core\Taint::assertTrustedBytes($data, 'the test wrote these four bytes');
// This prints 4, and then 0, because the two values have the same bytes.
echo Core\Bytes::length($trusted), "\n";
echo Core\Bytes::compare($trusted, $data as bytes), "\n";
Output
4
0

Keep a list of jobs and read it later

<?nvs
// Shows a list of jobs that is saved as bytes and read back later in the same program.
// The list has the type `array<mixed>`, so `encode()` returns `tainted bytes`.
array<mixed> $jobs = [];
$jobs[] = 'send the newsletter';
$jobs[] = 42;
tainted bytes $saved = Core\Serialize::encode($jobs);
// The program trusts the bytes in one place. Every later step uses `$ready`.
bytes $ready = Core\Taint::assertTrustedBytes($saved, 'the job list was encoded a few lines above');
// This prints 2.
array<mixed> $back = Core\Serialize::decode($ready) as array<mixed>;
echo Core\Arr::count($back), "\n";
Output
2