Skip to content

Env var processor examples for arrays don't work #50341

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
uncaught opened this issue May 16, 2023 · 10 comments
Closed

Env var processor examples for arrays don't work #50341

uncaught opened this issue May 16, 2023 · 10 comments

Comments

@uncaught
Copy link
Contributor

uncaught commented May 16, 2023

Symfony version(s) affected

symfony/dependency-injection 6.2.10

Description

I'm trying to configure trusted_headers/trusted_hosts via environment variables, but neither the json nor the csv example work.

All I get is:

Invalid type for path "framework.trusted_hosts.0". Expected one of "bool", "int", "float", "string", but got "array".

I've seen #28599 and #27683, but I can't find a solution in them.

Am I missing some component or is the documentation wrong?

How to reproduce

Use one of these in your framework.yaml:

parameters:
    env(TRUSTED_HOSTS): '["10.0.0.1", "10.0.0.2"]'
framework:
    trusted_hosts: '%env(json:TRUSTED_HOSTS)%'
parameters:
    env(TRUSTED_HOSTS): "10.0.0.1,10.0.0.2"
framework:
   trusted_hosts: '%env(csv:TRUSTED_HOSTS)%'

Possible Solution

No response

Additional Context

I've also switch to symfony/flex, so I might miss a component, but the installed EnvVarProcessor seems to include the json/csv handling.

@nicolas-grekas
Copy link
Member

Did you try this?

parameters:
    env(TRUSTED_HOSTS): "10.0.0.1,10.0.0.2"
framework:
   trusted_hosts: '%env(TRUSTED_HOSTS)%'

@uncaught
Copy link
Contributor Author

uncaught commented May 16, 2023

The container build doesn't complain with your example anymore, but the array is not resolved:

var_dump($container->getParameter('kernel.trusted_hosts'));
// array(1) { [0]=> string(17) "10.0.0.1,10.0.0.2" }

And the same for the CLI:

bin/console debug:container --parameter=kernel.trusted_hosts

 ---------------------- --------------------------
  Parameter              Value
 ---------------------- --------------------------
  kernel.trusted_hosts   ["%env(TRUSTED_HOSTS)%"]
 ---------------------- --------------------------

And if I try to configure trusted_headers the same way, I still get the error:

The value "x-forwarded-for,x-forwarded-host,x-forwarded-proto,x-forwarded-port" is not allowed for path "framework.trusted_headers.0". Permissible values: "forwarded", "x-forwarded-for", "x-forwarded-host
  ", "x-forwarded-proto", "x-forwarded-port", "x-forwarded-prefix"

@nicolas-grekas
Copy link
Member

My example doesn't contain any array.

@uncaught
Copy link
Contributor Author

Okay, so is the documentation wrong and this cannot be done with the json or csv processor?

@stof
Copy link
Member

stof commented May 16, 2023

Environment placeholders are not supported for all bundle semantic configuration. It depends whether the bundle processes them or passes them as is to the ContainerBuilder. They are supported only when passed as-is (as their value is not known at compile time so any node processing the value is likely to fail or do weird stuff)

@nicolas-grekas
Copy link
Member

The documentation is completely wrong yes :)
It's wrong because if uses the env var in a way that doesn't work, and it's also wrong because trusted hosts is expected to be a list of regular expressions. This page is correct:
https://symfony.com/doc/current/reference/configuration/framework.html#trusted-hosts

Can you please a PR to the doc to fix this? I suppose we need to find another example.

@uncaught
Copy link
Contributor Author

I went through our whole project, but I can't find anything useful as an example. I'd have to make one up 😄

Maybe some arbitrary list of language codes:

parameters:
  env(ALLOWED_LANGUAGES): '["en","de","es"]'
  app_allowed_languages: '%env(json:ALLOWED_LANGUAGES)%'

@MatTheCat
Copy link
Contributor

Somewhat related to #50323

@uncaught
Copy link
Contributor Author

@nicolas-grekas I've opened a PR with the arbitrary languages example: symfony/symfony-docs#18332

@nicolas-grekas
Copy link
Member

Thanks!

javiereguiluz added a commit to symfony/symfony-docs that referenced this issue May 22, 2023
…v var processors (uncaught)

This PR was merged into the 5.4 branch.

Discussion
----------

[Dotenv] Fix non-working examples of `json` and `csv` env var processors

Changes the examples for `json` and `csv` env var processors. The previous examples were not working.

Closes symfony/symfony#50341

Commits
-------

e73b674 Fix non-working examples of `json` and `csv` env var processors (closes symfony/symfony#50341).
javiereguiluz added a commit to javiereguiluz/symfony-docs that referenced this issue May 22, 2023
* 5.4:
  Fix non-working examples of `json` and `csv` env var processors (closes symfony/symfony#50341).
javiereguiluz added a commit to javiereguiluz/symfony-docs that referenced this issue May 22, 2023
* 6.2:
  Fix non-working examples of `json` and `csv` env var processors (closes symfony/symfony#50341).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants