Closed (fixed)
Project:
Drupal core
Version:
8.0.x-dev
Component:
base system
Priority:
Critical
Category:
Task
Assigned:
Unassigned
Issue tags:
Reporter:
Created:
6 Jul 2013 at 23:09 UTC
Updated:
29 Jul 2014 at 22:38 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #0.0
xanoExplained acronym
Comment #1
xanodrupal_get_token() uses session_id(), which will be refactored in #1858196: [meta] Leverage Symfony Session components.
Comment #2
xanoComment #3
xanoThe patch adds a csrf_token service plus parity PHPUnit test (untested). The procedural functions remain, and pass on the call to the service. My local testing install fails on this, and I need to do a few other things now, so the patch is incomplete.
Comment #4
dawehnerLet's inject the request into the service.
Comment #5
xano#2032553: The _account attribute on the Request is not available during web tests needs to be fixed for that, or tons of other tests will break.
Comment #6
xanoWe should probably also provide a testing version of this service that does not use session_id() and drupal_get_hash_salt() for use in web tests. This allows us to get rid of \Drupal\simpletest\WebTestBase::drupalGetToken(). However, the client session ID is stored in WebTestBase::session_id. We need to find a way to work around that.
Comment #7
damiankloip commentedThis is the issue I was looking for (based on #2042487: Token checking for Views enable/disable is missing)! This should fix the unit tests. At some point we need to do something with drupal_get_hash_salt() maybe?
Comment #8
damiankloip commentedSorry, that was two interdiffs.
Comment #9
ParisLiakos commentedas a first step #2036259: Move $drupal_hash_salt to settings()
Comment #10
damiankloip commentedYes, something pretty much like that :)
Comment #12
dawehnerAfaik this depcreated should be more like "@ deprecated as of 8.0" or something like that.
Missing "\"
I guess the private key is always a string?
$key equals $key equals $key
I like the @return self, as it has a slightly better semantic.
OT: It feels wrong to hash the database connection, wouldn't the hash change in different enviroments?
needs typehints.
global $user can be replaced by the request object now.
Nice name! I agree but you need a description as well.
Needs docs.
This should also ensure that the new set private key can be retrieved.
Let's better add a @todo
Comment #13
damiankloip commentedThanks for the review! I have made those changes, I added a helper method and mocked the state methods in each actual test, seems to be more thorough, rather than just using any() for all of them.
Comment #14
dawehnerThere is no need to inject the request object into that class, but we could use the approach of LanguageManager which is some magic provided by symfony 2.3 http://symfony.com/blog/new-in-symfony-2-3-what-else
Missing empty line.
Comment #15
damiankloip commentedHow about this?
Comment #17
damiankloip commented#15: 2036351-15.patch queued for re-testing.
Comment #19
xjmRelated: #1798296: Integrate CSRF link token directly into routing system
Comment #20
damiankloip commentedOhh, we need to set the request in a subscriber?!
Comment #21
damiankloip commentedComment #23
dawehnerPatch based on #15 which works at least on the comments.
Comment #24
damiankloip commentedI didn't know about that. That's much better!
Comment #25
fubhy commented@return \Drupal\Core\Access\CsrfTokenManager
"The CSRF token manager."
Missing empty line.
Since when do we do @see for tests? I quite like that though.
Missing "(Optional)"
80 characters.
Not sure (not a native speaker) but isn't "provided by" better than "provided from"?
Missing "(Optional)"
Redundant (()).
Whoops.
Interface mock with "disableOriginalConstructor()"?
Comment #26
damiankloip commentedGreat, thank fubhy! Here are those changes.
Comment #28
damiankloip commented#26: 2036351-26.patch queued for re-testing.
Comment #29
fubhy commentedStill *whoops* :) (2 periods, leading forward-slash one white-space off).
Why not just "getMock()" directly then? ;)
Looking good otherwise.
Comment #30
damiankloip commentedTotally right :)
Comment #31
fubhy commentedStill, the whitespace :/
Should be
and not
Comment #32
damiankloip commentedBAHHHH.
Comment #33
fubhy commentedThanks
Comment #34
chx commentedSo this will be only fired if the request service exists?
Just validate should be enough, no? The class/getter/servicename is already token...
This is wrong to be public. This should not be called from the outside. I am not even sure it's worth a method.
Comment #35
damiankloip commentedOk, I've changed the methods to get() and validate(). If we're changing from validateToken, we should also change from getToken. I have made createPrivateKey to private.
Yes, the '@?service'syntax is for optional dependencies.
Comment #36
dawehnerYou missed to rename it in the docs as well.
Hasn't mark posted his idea to not name it manager but CrsfTokenGenerator instead?
Comment #37
damiankloip commentedThat change sounds reasonable to me.
Comment #38
dawehnerGreat thank you!
Comment #39
catchThe private key is used for other things than CRSF protection - shouldn't it go into its own service? i.e. update module uses it as well.
Isn't this a syntax error? How did the patch pass?
Comment #40
webchickThat screwed me up too... the missing opening brace is up at:
Apparently this is a PHPUnit thing.
Comment #41
damiankloip commentedIts just a php thing :-) otherwise you can't declare anything outside of that namespace.
Comment #42
damiankloip commentedSo...do you both agree with a private key service?
Comment #43
damiankloip commentedJust tracking some work, added a private key service etc... but haven't converted/split up the tests yet.
Comment #44
damiankloip commentedWith the tests split up too. This actually makes alot more sense to me. Good call.
@catch, what do you think?
Comment #45
catchPatch looks much better like this, thanks!
The conditionally defined function makes me wince still, we'll need to bump the other issue to major/critical once this is in.
Comment #46
dawehnerI really like this additional service.
Let's call it 'private_key' here as well, so people know the exact name of the service.
I am wondering whether it is really worth to add this to \Drupal, as this potentially is not used a lot of places.
This function needs a visibility added.
We could just call it with two different random names to make sure what the expected behavior is.
We have done this workaround in a lot places already, so let's not freak out everytime we do that.
It is possible to typehint both the mock object as well as the actual interface, feel free to do that if you reroll.
Needs visibility.
Maybe we could/should use $this->once() ?
Comment #47
damiankloip commentedThanks Daniel, you're a good man.
I think I've addressed all your points; removed the Drupal::privateKey() method, as you're right I think, this is not all that common, and Drupal::service('private_key') is not too bad for that :)
Comment #48
dawehnerPerfect!
Comment #49
catchThanks!
Committed/pushed to 8.x.
Could use a change notice.
Comment #50
andypostSuppose this one should be
_accountComment #54
dawehnerThere we go. So we actually found a missing test coverage in drupal and solved it.
Comment #55
chx commentedAfter commit please reset to "Change notice: Convert CSRF tokens to a service" thanks
Comment #56
andypostmakes sense, previously it was not broken because $skip_anonymous always was FALSE so $user->id() is not executed
Comment #57
chx commentedAfter commit please reset title to "Change notice: Convert CSRF tokens to a service" thanks
Comment #58
catchanoymous.
Comment #59
damiankloip commentedJust rerolled with that typo change, I think I'm good to RTBC this again. Great work Daniel.
Let's get this in, so it doesn't hold back anything that wants to use it :)
Comment #60
catchCommitted/pushed to 8.x, thanks!
Comment #61
catchComment #62
dawehnerhttps://drupal.org/node/2068221
Comment #63
andypostSuppose notice should mention about setRequest() that needed for _account
Comment #64
catchLet's not mention _account - there's a critical issue to sort that out.
Comment #65.0
(not verified) commentedAdded issue link.