Skip to content
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

Can't seem to enable/disable CSP with environment variable #362

Open
pauljura opened this issue Nov 13, 2024 · 0 comments
Open

Can't seem to enable/disable CSP with environment variable #362

pauljura opened this issue Nov 13, 2024 · 0 comments

Comments

@pauljura
Copy link

I'd like to be able to easily toggle CSP on/off, and also have my templates aware of the setting, so I can conditionally include content when needed. I tried making an environment variable for this, but for some reason CSP is always enabled.

What I've got so far:

.env

APP_CSP_ENABLED=1

.env.local

APP_CSP_ENABLED=0

services.yaml

parameters:
    app.csp_enabled: '%env(bool:APP_CSP_ENABLED)%'

nelmio_security.yaml

nelmio_security:
    csp:
        enabled: '%app.csp_enabled%'

twig.yaml

twig:
    globals:
        csp_enabled: '%app.csp_enabled%'

With the configuration above, I can write the following logic in my templates and it works as expected:

{% if csp_enabled %}
    {% cspscript %}
    {% set nonce = csp_nonce('script') %}
    {# etc. #}
{% endif %}

If I toggle between APP_CSP_ENABLED=0 and APP_CSP_ENABLED=1 then the csp_enabled global Twig variable updates as expected, and the correct logic is executed in templates. I can verify the correct value is set with dump(csp_enabled) and it is always true or false as expected.

However, the bundle always outputs CSP headers as though the enabled setting was true.

If I set enabled: false then it will be disabled as expected, so there is nothing else overriding this setting elsewhere. But setting enabled: '%app.csp_enabled%' makes it always enabled.

I've deleted my cache, but that doesn't change anything.

I tried changing the config to refer to the environment variable directly with enabled: '%env(bool:APP_CSP_ENABLED)%' but that doesn't change anything.

What am I missing here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant