Tokens are unusable, because DrupalWebTestCase::drupalGetToken() does not use a hash salt, while drupal_get_token() does. This bug makes it impossible to test code that uses tokens.
Core does not use this method. However, I have a contrib patching waiting that does: #1550940: Extend and test payment permissions
| Comment | File | Size | Author |
|---|---|---|---|
| #41 | 1555862-41-drupalGetToken_hash_salt-D7.patch | 595 bytes | daffie |
Comments
Comment #2
xanoComment #3
xanoThis patch adds a test to make sure DrupalWebTestCase::drupalGetToken() and drupal_get_token() return identical tokens. The assertion lives in a test that can be reused for testing other simpletest methods that are 'aliases' of core functionality.
Comment #5
sunWhy do we have this method in the first place?
Comment #6
xanoThe session ID differs between the global one the tests can access and the one available when accessing the site through Simpletest's browser. Because of this, we need a method that matched drupal_get_token(), but uses Simpletest's browser's session_id.
Comment #7
sunThe problem is: drupalGetToken() does not do what testAliasCode() is doing.
Thus, it'll never work, except if you replicate all of the testAliasCode() code. But if you do that, then you no longer need drupalGetToken(), because you can directly call drupal_get_token().
Comment #8
xanoWell, there are two ways of generating tokens for use in Simpletest's browser:
1) We fake the session IDs every time and use drupal_get_token()
2) We use an 'alias' to prevent people from having to fake the session ID every time
Currently, core tries to do 2 and it's actually able to do that after applying my patch. You are suggesting approach 1?
Comment #9
sunNope, this patch is correct. Can you re-roll (also, without the EOF newline error)?
Comment #10
sunWhile this patch fixes drupalGetToken(), there's actually another problem in that $this->session_id gets reset to NULL on subsequently performed requests after drupalLogin().
Curious, what breaks?
Comment #11
sunAnd once more without irrelevant changes.
Comment #12
andypostNot sure that $this->session_id is set by simpletest... let's see
PS: masquerade module tests depends on this issue #1835954: Cleanup code and allow pass tests
Comment #13
sun$this->session_id is definitely set by WebTestBase's curl methods. That's also why this patch removes an
unset()there. ;)Comment #15
andypost@sun Not sure about 8.x but in 7.x $this->session_id is empty - try debug($this->session_id)
EDIT: Probably $this->session_name is wrong too
Comment #16
andypostAlso better to have a check that session_id() !== $this->session_id
This really needs more testing? but actually help to pass tests
Comment #17
andypost#11: drupal8.test-drupalgettoken.11.patch queued for re-testing.
Comment #19
andypost#11: drupal8.test-drupalgettoken.11.patch queued for re-testing.
Comment #21
xano#11: drupal8.test-drupalgettoken.11.patch queued for re-testing.
Comment #23
andypostFixes tests, still can't find a reason why session tests are broken
Related issue commited #1739986: Fix fallback in drupal_get_hash_salt(), move it to bootstrap.inc, use instead of $GLOBALS['drupal_hash_salt']
Comment #25
xanoA possible problem is that drupalGetToken() still calls drupal_hash_salt(), which defaults to the host site's database to create the salt. Maybe we solve this the same way as we set a custom session ID for the client site, but I'm not sure how that is done.
Moving to major, as this is important for testing some security issues.
Comment #25.0
xanoOhai
Comment #26
martin107 commentedPatch no longer applies.
Comment #27
Anonymous (not verified) commentedReroll, fixing merge conflicts in WebTestBase.php and SimpleTestTest.php.
This should also fix the failed test regarding the summary text. I'm not sure how to fix the one with the session cookie though.
Comment #28
martin107 commentedTriggering testbot.
Comment #30
Anonymous (not verified) commented27: simpletest-add-hash-salt-1555862-27.patch queued for re-testing.
Comment #32
Anonymous (not verified) commentedThe failure is probably caused by removing the session id reset in the curlExec() method. What is the reason that line got removed?
- $this->session_id = NULL;Comment #33
andypost27: simpletest-add-hash-salt-1555862-27.patch queued for re-testing.
Comment #35
andypostComment #36
stefank commentedRe-roll of last patch.
Comment #37
stefank commentedComment #39
pfrenssenDrupal 7 is also affected by this. Patch contains D7 backport of the fix. I have not backported the test.
Comment #40
andypostComment #41
daffie commentedThis is never going to be fixed in Drupal 8, because in #2328995: Remove unused and defective WebTestBase::drupalGetToken() the whole function drupalGetToken() has been removed.
Re-uploaded the patch from comment #39 and removed the do-not-test naming part.
Comment #42
daffie commentedThe function drupal_get_token() builds the token is the same way. So for me it is RTBC.
Comment #43
andypost+1 here, that's used in test for masquerade module
PS: Currently in D8 there's no way to build csrf token properly, within test, so added tag
Comment #44
xanoSeeing as this doesn't work in Drupal 8 and we need this to fix #144538: User logout is vulnerable to CSRF, shouldn't we fix this for D8 first and backport to D7?
Comment #45
dave reid@Xano: Considering this helper was removed from D8, I would consider them two separate issues at this point. Not having this in D7 core is a major blocker for testing in contrib modules.
Comment #46
pfrenssenWe will need a test for D7 now.