Skip to content
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

Handle DB session callback custom fields before session closed #17188

Merged
merged 19 commits into from
Mar 9, 2019

Conversation

lubosdz
Copy link
Contributor

@lubosdz lubosdz commented Mar 5, 2019

Attempt to fix #9438, #13740, #15037.
This fixes error Unkown: Failed to write session data (user).
Explanation of cause here.

Please review - perhaps somebody might get better idea how to populate fields before closing the session without corrupting the data integrity .. (e.g. if a session variable stored into session field changed between being populated and closing the session). Personally I am not happy with this commit, but so far haven't found better solution :-|

@lubosdz lubosdz changed the title #9438 Handle session callback custom fields before session closed Handle DB session callback custom fields before session closed Mar 5, 2019
@samdark
Copy link
Member

samdark commented Mar 5, 2019

Doesn't seem to pass tests: https://travis-ci.org/yiisoft/yii2/jobs/501995713

@lubosdz
Copy link
Contributor Author

lubosdz commented Mar 5, 2019

This is really hard to fix without BC break.
The implementation is faulty since the very beginning.

Method MultiFieldSession::composeFields() attempts to do two things at the same time - populate custom fields in custom session handler after session is closed (inactive). This results into permanent errors when somebody tries to read some variable from session into custom field, typically user ID.

I tried 3 attempts to move preparation of custom fields, but there is always some unit test failing. I could update unit tests but it might break backward compatibility if somebody used this feature to set custom session content.

Any idea how to fix ? - either introduce minor BC break or leave faulty implementation ? :-)

Also - it there guide how to run locally all framework tests? Before pushing the change I would like to run specific tests locally.

EDIT: Found guide here

@samdark
Copy link
Member

samdark commented Mar 5, 2019

The tests could be executed with ./vendor/bin/phpunit. As the argument you may pass the name of the test class.

@samdark
Copy link
Member

samdark commented Mar 5, 2019

if somebody used this feature to set custom session content

That is possible.

@lubosdz
Copy link
Contributor Author

lubosdz commented Mar 6, 2019

Fail on codeclimate writeSession accesses the super-global variable $_SESSION - it's used in many methods.

Final fix works, but IMO modifying session data directly via callback should not be allowed, just like it's not possible for FileSession. Users mainly use custom writeCallback fields for tracking who's online or IDs-related stuff etc. It's kinda undocumented dirty feature specific only to DB session :-)

@samdark
Copy link
Member

samdark commented Mar 6, 2019

We may consider disallowing it for 3.0 (sessions will be handled differently there anyway).

Copy link
Member

@samdark samdark left a comment

Choose a reason for hiding this comment

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

Looks good except minor things.

framework/web/DbSession.php Show resolved Hide resolved
framework/web/DbSession.php Show resolved Hide resolved
framework/web/DbSession.php Outdated Show resolved Hide resolved
@samdark samdark added this to the 2.0.17 milestone Mar 6, 2019
@samdark samdark added type:bug Bug status:under development Someone is working on a pull request. labels Mar 6, 2019
Copy link
Member

@samdark samdark left a comment

Choose a reason for hiding this comment

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

Is it possible to add unit tests for it?

framework/web/MultiFieldSession.php Outdated Show resolved Hide resolved
@lubosdz
Copy link
Contributor Author

lubosdz commented Mar 6, 2019

Unit tests for sessions are already there:
https://github.com/yiisoft/yii2/blob/master/tests/framework/web/session/AbstractDbSessionTest.php#L136
There is no new feature to test.
Or what should be added?

@samdark
Copy link
Member

samdark commented Mar 6, 2019

A test that failed before these changes and isn't failing with these changes.

@lubosdz
Copy link
Contributor Author

lubosdz commented Mar 6, 2019

There was no failing test before this fix.
Travis was failing, because my fix was not compatible with original implementation.
Now the fix is backwards compatible so no failing tests now - just like before the fix.

@lubosdz
Copy link
Contributor Author

lubosdz commented Mar 6, 2019

OK, I think it would make sense to add test for new method close() with user ID pulled out from session - to ensure custom fields do not interfere with inactive session anymore.

@lubosdz
Copy link
Contributor Author

lubosdz commented Mar 6, 2019

Whooops .. I did not intent to merge anything,
I just clicked Updated branch button .. and it merged.
Please check if it's OK or squash commits, whatever needed ..
sorry, I am not really familiar with github administration, I use gitlab or bitbucket.

@samdark
Copy link
Member

samdark commented Mar 6, 2019

It's fine to merge master into pull request branch. No issues with it.

@samdark samdark added status:code review The pull request needs review. and removed status:under development Someone is working on a pull request. labels Mar 7, 2019
Copy link
Member

@sergeymakinen sergeymakinen left a comment

Choose a reason for hiding this comment

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

LGTM. Hacky but should work.

@samdark samdark removed the status:code review The pull request needs review. label Mar 9, 2019
@samdark samdark merged commit 8bb334b into yiisoft:master Mar 9, 2019
@samdark
Copy link
Member

samdark commented Mar 9, 2019

Merged. Thank you!

rhertogh added a commit to rhertogh/yii2 that referenced this pull request Mar 26, 2019
yiisoft#17188 might brake existing projects when the yii\web\DbSession class is extended.
samdark pushed a commit that referenced this pull request Mar 26, 2019
…kip ci]

#17188 might brake existing projects when the yii\web\DbSession class is extended.
yii-bot pushed a commit to yiisoft/yii2-framework that referenced this pull request Mar 26, 2019
…kip ci]

yiisoft/yii2#17188 might brake existing projects when the yii\web\DbSession class is extended.
samdark pushed a commit to yiisoft/yii2-mongodb that referenced this pull request Oct 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

error saving sessions
3 participants