Core\SignedCookie::open
Core\SignedCookie::open reads a cookie that Core\SignedCookie::seal made, and returns the value stored in it. You give it the same cookie name and the same list of keys that you gave seal. A cookie made with any key in the list is accepted.
open throws a RuntimeError when the cookie was changed, when it is empty, when it is not a cookie at all, when it was made for another cookie name, or when it was made with a key that is no longer in the list. All of these give the same message, so an attacker cannot tell which one happened.
The value open returns is a tainted string (marked as input from outside). Your program may have stored text that a visitor sent, so the value is treated like any other input. Escape it or check it before you use it in HTML, SQL or a file path.
The examples below open a cookie, treat a changed cookie as a guest, and change to a new key while old cookies still work.
Parameters
$namestringThe name you gaveseal. This is usually the name the cookie arrived with.$keysarray<secret bytes>The keys you gaveseal, newest first. A cookie made with any key in this list is accepted. A cookie made with a key you removed from the list is not.
Return value
Returns tainted string. The value that was given to seal, exactly as it was, as a tainted string.
Errors
LogicError$keysis empty, or a key thatopentries is not 32 bytes long.opentries the keys in order and stops at the first one that opens the cookie.RuntimeError$cookiewas not made for$namewith any key in$keys. This includes a changed cookie, an empty cookie, text that is not a cookie, a cookie made for another name, and a cookie made with a key you removed. All of these give the same message, so an attacker learns nothing from it.
Examples
Read the stored value
<?nvs// Shows how to read the value that a cookie contains.
array<secret bytes> $keys = [Core\Crypto::generateKey()];string $cookie = Core\SignedCookie::seal('prefs', 'lang=de; theme=dark', $keys);
// `open` returns the value exactly as it was sealed, including `;` and `=`.// The value is `tainted`, like any input from outside. This prints lang=de; theme=dark.tainted string $value = Core\SignedCookie::open('prefs', $cookie, $keys);echo $value, "\n";lang=de; theme=darkA changed cookie is a guest
<?nvs// Shows how a server treats a changed cookie as a visitor who is not logged in.
class Login{ // Returns the user id in the cookie, or "guest" when the cookie is not valid. public static function userOf(string $cookie, array<secret bytes> $keys): tainted string { try { return Core\SignedCookie::open('user', $cookie, $keys); } catch (RuntimeError $e) { return 'guest'; } }}
array<secret bytes> $keys = [Core\Crypto::generateKey()];string $cookie = Core\SignedCookie::seal('user', '42', $keys);
// The real cookie gives the user id. This prints 42.echo Login::userOf($cookie, $keys), "\n";
// Somebody adds characters to the cookie. `open` throws an error, so this prints guest.echo Login::userOf($cookie . 'AAAA', $keys), "\n";42guestChange to a new key
<?nvs// Shows how to start using a new key while old cookies still work.
secret bytes $oldKey = Core\Crypto::generateKey();string $oldCookie = Core\SignedCookie::seal('user', 'user=42', [$oldKey]);
// The new key goes first, so `seal` uses it. The old key stays second.secret bytes $newKey = Core\Crypto::generateKey();array<secret bytes> $keys = [$newKey, $oldKey];string $newCookie = Core\SignedCookie::seal('user', 'user=7', $keys);
// Both cookies are accepted. This prints user=42 and then user=7.echo Core\SignedCookie::open('user', $oldCookie, $keys), "\n";echo Core\SignedCookie::open('user', $newCookie, $keys), "\n";
// Later, the old key is removed. Cookies made with it now throw an error.array<secret bytes> $newOnly = [$newKey];try { Core\SignedCookie::open('user', $oldCookie, $newOnly); echo "accepted\n";} catch (RuntimeError $e) { echo "The old cookie is no longer accepted.\n";}user=42user=7The old cookie is no longer accepted.