Core\Env::secret
Returns the value of one environment variable as a secret string, or null if nothing set it.
Use it for a password, an API key or a token. A secret can go into an outbound HTTP header, a form field, a JSON body and a database password. echo, print, Core\Log::write and a response body do not accept it. A program that tries does not compile, so the value cannot reach a page or a log by mistake.
In plain words: a secret is a value your program can use but never show. Core\Env::get reads the same variable as a tainted string. Use secret when the value is a credential.
The value is not tainted. The name must be text your program wrote itself. A name that came from a request does not compile, because a visitor could then choose which secret the program reads.
Good to know: if the value is not valid UTF-8 text, secret throws a RuntimeError. The message has the name of the variable, and never the value.
Parameters
$namestringThe variable's name. It may not betainted, so a request cannot choose which secret the program reads.
Return value
Returns ?secret string. The value as a secret string, or null if the variable is not set.
Errors
RuntimeErrorThe variable contains bytes that are not valid UTF-8, so its value is not astring. The message names the variable and not its value.
Examples
Send an api key in a header
<?nvs// Shows how to read an API key from the environment and send it in a request header.
// The server's reply is fixed here, so the example runs without a network.Core\Test::answerHttp('https://api.example.com/orders', 200, { body: '3 open orders' });
// `Core\Env::secret` returns a `secret string`. `SHOP_API_KEY` is not set on// this computer, so the `??` operator gives an empty key here.secret string $key = Core\Env::secret('SHOP_API_KEY') ?? '';
// A request header accepts a secret. This prints `200 3 open orders`.var $response = Core\Http\Client::get('https://api.example.com/orders', { headers: ['authorization' => 'Bearer ' . $key] });echo $response->status(), ' ', $response->text(), "\n";200 3 open ordersA database password from the environment
<?nvs// Shows how to pass a database password from the environment to `Core\Db::open`.
// `SHOP_DB_PASSWORD` is not set on this computer, so the password is empty here.secret string $password = Core\Env::secret('SHOP_DB_PASSWORD') ?? '';
// The `password` setting accepts a secret. This example does not allow the host// `db.example.com`, so `open` throws an error before it connects.try { var $db = Core\Db::open({ driver: Core\Db\Driver::Postgres, host: 'db.example.com', database: 'shop', user: 'shop', password: $password, }); echo "connected\n";} catch (RuntimeError $error) { echo $error->message, "\n";}Core\Db::open needs the capability `db.open` for db.example.com, which is not grantedhelp: add `open = ["db.example.com"]` under `[capabilities.db]` in nvs.tomlStop when the key is missing
<?nvs// Shows how a program stops with a clear message when a secret it needs is not set.
class Secrets{ // Returns the secret `$name`, or throws an error when the variable is not set. public static function required(string $name): secret string { var $value = Core\Env::secret($name); if ($value == null) { throw new RuntimeError("{$name} is not set"); } return $value; }}
// `SHOP_API_KEY` is not set on this computer, so this prints the error message.// The message has the name of the variable. It never has the value.try { secret string $key = Secrets::required('SHOP_API_KEY'); echo "the shop has its API key\n";} catch (RuntimeError $error) { echo 'The shop cannot start: ', $error->message, "\n";}The shop cannot start: SHOP_API_KEY is not set