Skip to content

[3.4, 4.0] [DependencyInjection] Env Vars that resolve to an array throw a RunTimeException #27239

@jamesthomasonjr

Description

@jamesthomasonjr

Symfony version(s) affected: 3.4, 4.0

Description
Compiling a container while resolving environment variables fails if an environment variable is typecast to an array and isn't the last parameter defined.

When a value is resolved to an array in ContainerBuilder::resolveEnvPlaceholders and the foreach loop continues, stripos() throws a warning and returns null. Since the check is a strict comparison against false, null is considered a match, and since $placeholder !== $value, the logic falls through to the is_string() and is_numeric() checks, which fail, resulting in a RunTimeException.

How to reproduce

  • Create two environment variables, at least one that will resolve to an array (json or const, csv in 4.1)
  • Reference the environment variables as parameters and typehint appropriately (json, const)
    • Reference at least one environment variable after the array environment variable
  • Compile the parameters, resolving the environment variables

Example code
https://github.com/jamesthomasonjr/symfony-di-array-bug

git clone git@github.com:jamesthomasonjr/symfony-di-array-bug.git
cd symfony-di-array-bug
composer install
bin/console.php

Possible Solution(s)

  • Use break 2; to break out of the two for each loops in the cases where $placeholder === $value (would increase performance a little, too)
  • Check that the value is a string at the beginning of the second for each loop, before calling stripos()

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions