8000 [HttpKernel] Disable CSP header on exception pages by ostrolucky · Pull Request #25532 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[HttpKernel] Disable CSP header on exception pages #25532

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 4, 2018

Conversation

ostrolucky
Copy link
Contributor
@ostrolucky ostrolucky commented Dec 17, 2017
Q A
Branch? 2.7
Bug fix? yes
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #24772
License MIT
Doc PR -

This makes exception pages styled normally when using CSP listener. Technically it's new feature, but from user POV it's bugfix, because it's really confusing to get a blank page. It takes a while to realize it is because of enabled CSP. Therefore I'm trying to push it as patch release.

@javiereguiluz
Copy link
Member

@romainneutron as our CSP expert, would you agree with this change? Thanks!

@ostrolucky adding new listeners always impacts performance. Can't this header removal be done in the existing onKernelException ... or better, can this CSP header not added at all for exceptions in the first place? Thanks!

@ostrolucky
Copy link
Contributor Author

There must be CSP header removal, because we don't have control over not adding it, it's done in userspace.
It can't be done in onKernelException, because that event is triggered before onKernelResponse, so header would be re-added after ExceptionListener is long done.

What can be done to avoid unnecessarily adding new listener is to add this listener from inside onKernelException, but that will require access to EventDispatcher in this listener. Let me know if you would like that more, or there is better way.

@stof
Copy link
Member
stof commented Dec 18, 2017

@ostrolucky you can have access to the EventDispatcher. It is passed as third argument to the listener when calling it.

@stof
Copy link
Member
stof commented Dec 18, 2017

However, if the listener is added dynamically, don't forget to remove it dynamically too (to reset state properly for reused kernel).

@nicolas-grekas nicolas-grekas added this to the 2.7 milestone Dec 18, 2017
@ostrolucky
Copy link
Contributor Author
ostrolucky commented Dec 18, 2017

@stof Unfortunately I can't do that. According to BC promise I can't add arguments to public method 🙁

edit: but added it via func_get_arg. Better?

@ostrolucky ostrolucky force-pushed the fix-24772 branch 3 times, most recently from 4b40940 to 187e429 Compare December 18, 2017 23:09
@javiereguiluz
Copy link
Member

Can anybody think of a simpler solution to this issue? The func_get_arg, the listeners, etc. look a bit complicated (but maybe this is the simplest solution possible).

@chalasr
Copy link
Member
chalasr commented Dec 19, 2017

func_get_arg() is the way to go I think. Should it trigger a deprecation notice in 4.1 to really add the dispatcher as optional arg in 5.0?

@nicolas-grekas
Copy link
Member

I would suggest to make the class final in 4.1 (can be done now)

@nicolas-grekas
Copy link
Member

The code looks good to me.
@ostrolucky do you think you could add a test case for it?

@ostrolucky
Copy link
Contributor Author

Sure. What kind of test?

@nicolas-grekas
Copy link
Member

eg one that check that whe appropriate, the header is removed, and the event listener is not left in place?

@@ -41,6 +43,7 @@ public function onKernelException(GetResponseForExceptionEvent $event)
{
$exception = $event->getException();
$request = $event->getRequest();
$eventDispatcher = func_num_args() > 2 ? func_get_arg(2) : null;
Copy link
Member

Choose a reason for hiding this comment

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

just define the proper arguments instead

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can I define them as non-optional?

Copy link
Member

Choose a reason for hiding this comment

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

@stof that'd be a BC break...

Copy link
Member

Choose a reason for hiding this comment

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

hmm, our listener methods are indeed not marked as @internal 😢

@ostrolucky
Copy link
Contributor Author

status: needs review

$cspRemovalListener = function (FilterResponseEvent $event) use (&$cspRemovalListener, $eventDispatcher) {
$event->getResponse()->headers->remove('Content-Security-Policy');
$eventDispatcher->removeListener(KernelEvents::RESPONSE, $cspRemovalListener);
};
Copy link
Member

Choose a reason for hiding this comment

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

This code should be move under the if as it's only used there.

@fabpot
Copy link
Member
fabpot commented Jan 4, 2018

Thank you @ostrolucky.

@fabpot fabpot merged commit f33a383 into symfony:2.7 Jan 4, 2018
fabpot added a commit that referenced this pull request Jan 4, 2018
…ucky)

This PR was merged into the 2.7 branch.

Discussion
----------

[HttpKernel] Disable CSP header on exception pages

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | yes
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #24772
| License       | MIT
| Doc PR        | -

This makes exception pages styled normally when using CSP listener. Technically it's new feature, but from user POV it's bugfix, because it's really confusing to get a blank page. It takes a while to realize it is because of enabled CSP. Therefore I'm trying to push it as patch release.

Commits
-------

f33a383 [HttpKernel] Disable CSP header on exception pages
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.

7 participants
0