Symfony 8.2 New Published on

An expression that doesn't read your secrets

constant() and enum() could read any constant of the application. ConstantFunctionProvider limits them to a list: a rule written in the back office can no longer guess an API key.

Topics covered: ExpressionLanguageSecurity

New in Symfony 8.2 (in development). Symfony's expression language (the one behind #[IsGranted], access_control, workflow guards) offers two functions, constant() and enum(), that read any constant or enum case of the application. As long as expressions are written by developers, it doesn't matter. But when they come from users, a promotion rule typed in the back office for example, they can read an API key stored in a constant.

And a mere true or false is enough: constant("…") matches "/^sk_live_5/", then /^sk_live_51/… one answer per character, and the key comes out.

ConstantFunctionProvider

This function provider replaces constant() and enum() with versions limited to a list:

$language = new ExpressionLanguage(null, [
    new ConstantFunctionProvider([
        'PHP_EOL',
        'App\Security\Roles::ROLE_*',   // the constants of Roles starting with ROLE_
        'App\Enum\*::*',                // every constant and every case of that namespace
    ]),
]);

$language->evaluate('constant("DB_PASSWORD")'); // RuntimeException: Constant "DB_PASSWORD" is not allowed.

* matches any sequence of characters except ::. The name is checked before the constant is read, so a class outside the list isn't even loaded.

constant() and enum() without a list are deprecated: every ExpressionLanguage you create yourself must now register a ConstantFunctionProvider (['*', '*::*'] if you deliberately allow everything). Symfony's own already have one.

Your task

The marketing team writes its promotion rules in the back office, and tries them on a basket: the “Try ‘free shipping’” request in the “Requests” tab is one. The “Guess the Stripe key” request shows the problem: the response says true.

  1. Limit the rules to the constants of Seuils and the cases of Niveau.

Where the feature comes from: the original pull request.