Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[FrameworkBundle] Auto-enable CSRF if the component *+ session* are loaded #25508

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

Merged
merged 1 commit into from
Jan 16, 2018

Conversation

nicolas-grekas
Copy link
Member

@nicolas-grekas nicolas-grekas commented Dec 15, 2017

Q A
Branch? 3.4
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets symfony/recipes#262
License MIT
Doc PR -

By binding CSRF and session default state, we provide better DX, but we also provide a way for bundles to enable session on its own: they just need to require "symfony/security-csrf".
Yes, that's a side effect, but I think that's a nice one for 3.4/4.0.
Of course, we might do better in 4.1, but for bug fix only releases, LGTM.

@sroze
Copy link
Contributor

sroze commented Dec 15, 2017

Sorry for not picking the session before. If it's the behaviour we want (and I believe yes), I can help with changing the tests to reflect that @nicolas-grekas.

@stof
Copy link
Member

stof commented Dec 15, 2017

@nicolas-grekas can you check why deps=high and deps=low are failing here ?

@sroze sroze force-pushed the auto-surf branch 2 times, most recently from b271567 to 54eef22 Compare December 15, 2017 11:12
@sroze
Copy link
Contributor

sroze commented Dec 15, 2017

Travis is 💚 and AppVeyor's issues are not related.

@weaverryan
Copy link
Member

I really want to help fix the session issue... but this strikes me as weird:

  1. It's weird that the session config key has a special case specifically for CSRF.

  2. If I require symfony/security, won't I have CSRF? And so won't my sessions suddenly be enabled?

  3. It IS convenient that libraries could require security-csrf as a way to auto-enable sessions, but that seems like a hack.

Sorry to not be on board... I'm just not convinced yet...

@javiereguiluz
Copy link
Member

javiereguiluz commented Dec 18, 2017

Without knowing the internals of this, I agree with Ryan that requiring security-csrf just to auto-enable sessions look like a hack.

@nicolas-grekas
Copy link
Member Author

This is not the main purpose of this PR. It's main purpose is enabling CSRF protection when security-csrf is installed.

@weaverryan
Copy link
Member

This is not the main purpose of this PR. It's main purpose is enabling CSRF protection when security-csrf is installed.

@nicolas-grekas Ok - fair enough then - it makes more sense looking at it through that lense. But, what about this:

If I require symfony/security, won't I have CSRF? And so won't my sessions suddenly be enabled?

Is that not a problem?

@nicolas-grekas
Copy link
Member Author

There is a discussion somewhere about discouraging to use this meta repo. Should remove the issue.

@weaverryan
Copy link
Member

There is a discussion somewhere about discouraging to use this meta repo. Should remove the issue.

@nicolas-grekas Hmm, ok... I'm going to be really annoying 😇. Should we remove the security alias then? What package should we tell users to install when they're starting with security (and we don't know yet if they are building a pure API or a form login)?

Another issue is that simply enabling the session means that it will store sessions into the var/cache/dev/sessions directory (see

->scalarNode('storage_id')->defaultValue('session.storage.native')->end()
->scalarNode('handler_id')->defaultValue('session.handler.native_file')->end()
->scalarNode('name')->end()
->scalarNode('cookie_lifetime')->end()
->scalarNode('cookie_path')->end()
->scalarNode('cookie_domain')->end()
->booleanNode('cookie_secure')->end()
->booleanNode('cookie_httponly')->defaultTrue()->end()
->booleanNode('use_cookies')->end()
->scalarNode('gc_divisor')->end()
->scalarNode('gc_probability')->defaultValue(1)->end()
->scalarNode('gc_maxlifetime')->end()
->scalarNode('save_path')->defaultValue('%kernel.cache_dir%/sessions')->end()
). But that's not what we want - we want them to use the default Flex config, which sets handler_id to null and thus allows PHP to handle storage).

I don't see the smooth solution yet :/.

@weaverryan
Copy link
Member

I'm here to complicate things more :). The SecurityBundle has a dependency on all of symfony/security :). So the idea of only installing the piece you need (e.g. security-guard) is a non-starter (as well as my issue above about session configuration).

@nicolas-grekas
Copy link
Member Author

OK, if this is not obvious, this PR should not be merged :)

@nicolas-grekas
Copy link
Member Author

Reopening as I think this should be merged when #25699 will be.

@nicolas-grekas nicolas-grekas reopened this Jan 7, 2018
@@ -449,7 +450,7 @@ private function addSessionSection(ArrayNodeDefinition $rootNode)
->children()
->arrayNode('session')
->info('session configuration')
->canBeEnabled()
->{!class_exists(FullStack::class) && interface_exists(CsrfTokenManagerInterface::class) ? 'canBeDisabled' : 'canBeEnabled'}()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how yet but I believe we could find a better way than relying on a Symfony\Component\Security class to enable the session automatically. Maybe a meta-package containing a Symfony\Meta\RequiresSession class? 🤷‍♂️

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should not go in that direction. If enabling the session has no downside (with #25699), we should just enable them by default. But I wouldn't change the default in 3.4. Thus this PR, which is back in the game as is for me.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you're right that it costs nothing. The interface_exists check is what is bothering me. Can we check on the SessionInterface interface directly instead of the CsrfTokenManagerInterface?

@weaverryan
Copy link
Member

+1 to reopen! But I think the logic should be changed: we should never automatically (in the Configuration class) enable the session... because it will default to trying to write to a var/cache directory that may not exist (so the user will get an error).

Could we auto-enable csrf if the code exists AND if the session was enabled by the user?

@nicolas-grekas
Copy link
Member Author

Could we auto-enable csrf if the code exists AND if the session was enabled by the user?

here we are :)

@nicolas-grekas nicolas-grekas force-pushed the auto-surf branch 2 times, most recently from ec93d0f to 1ab06c0 Compare January 8, 2018 19:28
Copy link
Member

@weaverryan weaverryan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definite +1: csrf_protection enables itself if the session is enabled & CSRF code exists.

@nicolas-grekas nicolas-grekas changed the title [FrameworkBundle] Automatically enable the CSRF protection *and session* if CSRF manager exists pick 8a8ef52 [FrameworkBundle] Automatically enable the CSRF if component *+ session* are loaded Jan 9, 2018
@nicolas-grekas nicolas-grekas changed the title pick 8a8ef52 [FrameworkBundle] Automatically enable the CSRF if component *+ session* are loaded [FrameworkBundle] Automatically enable the CSRF if component *+ session* are loaded Jan 9, 2018
@nicolas-grekas nicolas-grekas force-pushed the auto-surf branch 2 times, most recently from 53ba38a to 9e8231f Compare January 10, 2018 17:16
@nicolas-grekas
Copy link
Member Author

Just removed 2nd commit, which belongs to another PR.

@nicolas-grekas nicolas-grekas changed the title [FrameworkBundle] Automatically enable the CSRF if component *+ session* are loaded [FrameworkBundle] Auto-enable CSRF if the component *+ session* are loaded Jan 10, 2018
@@ -229,6 +231,11 @@ public function load(array $configs, ContainerBuilder $container)
$this->registerRequestConfiguration($config['request'], $container, $loader);
}

if (null === $config['csrf_protection']['enabled']) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this ever be reached? I would have expected the treatNullLike() call above to result in the value here being true.

Copy link
Member Author

@nicolas-grekas nicolas-grekas Jan 11, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

verified: yes! "defaultNull" applies after "treatNullLike" (which applies only to explicit user values)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we should add a test for this :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's a test for the Config component :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant for different configs here, but it will become quite hard as the FullStack class is always available.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

treatNullLike is on the csrf_protection node, not on the enabled node.

@nicolas-grekas nicolas-grekas merged commit 9e8231f into symfony:3.4 Jan 16, 2018
nicolas-grekas added a commit that referenced this pull request Jan 16, 2018
…sion* are loaded (nicolas-grekas)

This PR was merged into the 3.4 branch.

Discussion
----------

[FrameworkBundle] Auto-enable CSRF if the component *+ session* are loaded

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | symfony/recipes#262
| License       | MIT
| Doc PR        | -

By binding CSRF and session default state, we provide better DX, but we also provide a way for bundles to enable session on its own: they just need to require "symfony/security-csrf".
Yes, that's a side effect, but I think that's a nice one for 3.4/4.0.
Of course, we might do better in 4.1, but for bug fix only releases, LGTM.

Commits
-------

9e8231f [FrameworkBundle] Automatically enable the CSRF if component *+ session* are loaded
@nicolas-grekas nicolas-grekas deleted the auto-surf branch January 16, 2018 14:44
@stof
Copy link
Member

stof commented Jan 16, 2018

then we again have the issue that installing the CSRF component is not enough to be protected (as it requires enabling session manually first and nothing will warn you). People using forms might not even see that they don't have csrf protection enabled on them.

@nicolas-grekas
Copy link
Member Author

in Flex, the session is enabled by default, so that this is less an issue
I think the real best solution is to not need the session at all for CSRF protection - aka double submit strategy

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

Successfully merging this pull request may close these issues.

8 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.