Skip to content

Implemented Symfony reverse proxy support for user context hash. #152

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

Conversation

lolautruche
Copy link
Contributor

Ref #26
Relates to ezsystems/ezpublish-kernel#1025

Finally the PR 😄
This adds support for Symfony RP for user context hash feature.

For this to work, you need to:

  • Make your AppCache class extend FOS\HttpCacheBundle\HttpCache instead of Symfony\Bundle\FrameworkBundle\HttpCache\HttpCache.
  • Ensure USER_HASH_ACCEPT_HEADER, USER_HASH_HEADER and USER_HASH_METHOD constants match your configuration (cannot rely on semantic config here as we're way too early for that).
  • Ensure SESSION_NAME_PREFIX matches your environment.

In any case, it is possible to redefine those constants to fit your configuration.

TODO

  • Implement user context hash support for Symfony RP
  • Clear documentation on how to set it up
  • Make user context hash response vary on sessionId(s) only as Cookie may contain undesired info (e.g. analytics)
  • Try to do some tests

@lolautruche
Copy link
Contributor Author

Travis fail seems to be only linked to HHVM. Didn't touch impacted part.

@stof
Copy link
Member

stof commented Oct 20, 2014

yeah, HHVM is currently broken on Travis. It does not allow parsing XML files using an XSD

return strpos($name, $this->getSessionNamePrefix()) === 0;
}

protected function getSessionNamePrefix()
Copy link
Member

Choose a reason for hiding this comment

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

is this method needed ? Other places are relying on overwriting the constant itself

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, I actually left it here for convenience because of the code we have in eZ Publish. However I reckon that this I not very consistent...

@lolautruche
Copy link
Contributor Author

OK, code has been updated and I added a test for new HttpCache class.
Now I need to have a look at the failure with Symfony 2.3 and write some documentation.

@lolautruche lolautruche force-pushed the contextHashSymfonyReverseProxy branch 3 times, most recently from 0a35388 to c1a083e Compare October 22, 2014 10:02
@lolautruche
Copy link
Contributor Author

Tests with Symfony 2.3 fixed, documentation updated, PR rebased.
Ready for review ! 😄

Ping @dbu @ddeboer @stof @andrerom

@andrerom
Copy link
Contributor

+1

Just meta comment to add from my side; @dbu as this and also some earlier code is based on code from eZ Publish, would it be ok to add eZ Systems AS to Resources/meta/LICENSE ? (maybe I'm wrong, but seems to be the convention with both liip and driebit mentioned atm)

@stof
Copy link
Member

stof commented Oct 22, 2014

@andrerom The license mentions them because they are the original authors of the bundle (FOSHttpCache has been created by merging 2 bundles together). We don't update the license holder each time a new contributor appears.
the place where it could appear is with an @author annotation on the class being contributed (alongside @lolautruche's name)

this looks good to me, but I will wait for @dbu to review as well

@andrerom
Copy link
Contributor

Ok, clear.

*/
protected function isInternalRequest(Request $request)
{
return $request->attributes->get('internalRequest', false) === true;
Copy link
Contributor

Choose a reason for hiding this comment

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

is there nothing more explicit for this? if there isn't, this solution seems ok to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not that I could think about, there is no req.restart like in Varnish 😉

@dbu
Copy link
Contributor

dbu commented Oct 23, 2014

thanks a lot! very cool, and seems sane to me. i commented on a bunch of details. i really want to be paranoid and in doubt trade default performance for default security in case people misconfigured things.

for the doc:

i think we should have the HttpCache specific doc in its own chapter - i hope the kernel will provide at least purge/refresh at some point, even if ban is not very realistic. lets add a new file to the Features section named "Symfony2 HttpCache" that explains that instead of varnish or nginx you can also use this kernel. the doc would explain to extend our kernel and for the rest of the topic link to the relevant symfony core doc.

then we should

protected function generateForwardRequest(Request $request)
{
$forwardReq = Request::create(static::USER_HASH_URI, static::USER_HASH_METHOD, array(), $request->cookies->all(), array(), $request->server->all());
$forwardReq->attributes->set('internalRequest', true);
Copy link
Contributor

Choose a reason for hiding this comment

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

is this also done by symfony in other cases of internal requests? or is this your way of transporting the information that this is a valid request for the hash through the call to handle()? if its the later, can't we do this simpler, e.g. have a dedicated method that calls parent::handle at the end and have our handle() unconditionally refuse hash requests?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's not used anywhere in Symfony, this is just a convenient flag used in handle() method only. I don't understand your proposal. Example?

Copy link
Contributor

Choose a reason for hiding this comment

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

remove this line, but in getUserHash do $this->handleInternalRequest which just calls parent::handleInternalRequest.

okay, i see the problem, if the user extends handle() it could be surprising. might not be the best idea then.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So, can I leave it as is ?

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah i think its better than my proposal.

(just trying to challenge your solutions to see if it could be simpler)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

:octocat:

{
$sessionIds = array();
foreach ($originalRequest->cookies as $name => $value)
{
Copy link
Member

Choose a reason for hiding this comment

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

Syntax is not according to PSR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

@ddeboer
Copy link
Member

ddeboer commented Oct 29, 2014

@lolautruche lolautruche Can you fix the last few CS issues?

Otherwise, I think we can merge this.

@dbu
Copy link
Contributor

dbu commented Oct 29, 2014

@lolautruche can you please add a line to the CHANGELOG.md file? i would say add a title 1.1.x-dev and then something like * **2014-10-29** Adding support for the user context lookup with the Symfony built-in reverse proxy HttpCache

@lolautruche lolautruche force-pushed the contextHashSymfonyReverseProxy branch from 7f31ece to 4f3c874 Compare October 29, 2014 09:56
@lolautruche
Copy link
Contributor Author

CS fixed and last request from @dbu taken into account (cookie string for user hash request).

One request from me: Once merged, would it be possible to tag 1.1.0? We're indeed about to tag eZ 5.4 beta and we'd need to rely on a stable tag if possible :-). Thanks

$forwardReq->cookies->set($name, $value);
}
}
$forwardReq->headers->set('Cookie', http_build_query($sessionIds, '', '; '));
Copy link
Contributor

Choose a reason for hiding this comment

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

thanks, i think this is more robust now.

@ddeboer
Copy link
Member

ddeboer commented Oct 29, 2014

One thing I’m noticing now: @lolautruche, you created protected methods in HttpCache, while we generally prefer private ones in order to improve backwards compatibility. Do you have a reason for using protected here?

@lolautruche
Copy link
Contributor Author

@ddeboer in one word: extensibility 😉
This class is abstract and will be extended and might need to be tweaked for some reason.

@lolautruche
Copy link
Contributor Author

@ddeboer However, isInternalRequest() and getUserHash() might better be private...

@ddeboer
Copy link
Member

ddeboer commented Oct 29, 2014

I think so, yes. But perhaps the other methods (except handle()), too. Having private methods will allow us to change implementation details without breaking BC. While I agree that the class might need to be tweaked, users of the bundle can always open issues or PRs if they need some extra functionality.

@dbu What do you think?

@lolautruche lolautruche force-pushed the contextHashSymfonyReverseProxy branch from 4f3c874 to 106c4ed Compare October 29, 2014 21:56
@lolautruche
Copy link
Contributor Author

Changed. But I kept cleanupForwardRequest() protected though.

*
* @return bool
*/
private function isSessionName($name)
Copy link
Contributor

Choose a reason for hiding this comment

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

hm, i think we should keep this protected. i could have a different logic or support a legacy cookie or something like that.

dbu added a commit that referenced this pull request Oct 30, 2014
Implemented Symfony reverse proxy support for user context hash.
@dbu dbu merged commit d495ec8 into FriendsOfSymfony:master Oct 30, 2014
@lolautruche lolautruche deleted the contextHashSymfonyReverseProxy branch October 30, 2014 15:28
@lolautruche
Copy link
Contributor Author

Thanks for merging !
But you didn't leave me time to make some private methods to protected :-)

I guess I'll open a new PR

@dbu
Copy link
Contributor

dbu commented Oct 30, 2014

i don't want to block you any longer, so i merged and tagged 1.1.0 !

i created #155 to track the question of basic auth.

@lolautruche
Copy link
Contributor Author

Thank you so much @dbu @ddeboer 😃

@ddeboer
Copy link
Member

ddeboer commented Oct 31, 2014

Thank you, @lolautruche!

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

Successfully merging this pull request may close these issues.

6 participants