Clean the session files upon unpackling Error in FileSession clean up#2012
Clean the session files upon unpackling Error in FileSession clean up#2012jiangwen365 wants to merge 2 commits into
Conversation
I've seen clean failures in the error log files due to UnpicklingError: invalid load key, '\x00'.
for more information, see https://pre-commit.ci
| # Session expired: deleting it | ||
| os.unlink(path) | ||
|
|
||
| except pickle.UnpicklingError: |
There was a problem hiding this comment.
Could you add a test for this?
There was a problem hiding this comment.
I wish I am able to write a test for this - but with limited skill, I couldn't figure out how to write one - if possible could you please provide some guidance and thoughts?
There was a problem hiding this comment.
I'm not sure either. It would probably be an integration test that sets up a server with sessions, makes a query (which results in a session file appearing on disk), then corrupts that file by writing arbitrary data into it (a random byte might be enough), followed by tearing down the server and that should raise the exception. Maybe, it'd be easier (or more stable) with calling clean_up() and checking that the file disappeared from disk. Additionally, it would be wise to use pytest-mocker's mocker.spy() on the _load() method to verify that the exception was raised, indeed.
I've seen clean failures in my production error log files due to
UnpicklingError: invalid load key, '\x00'. I suspect the session file was created by attackers. This would 1) break the cleansing thread, leaving many expired sessions not cleaned up, also 2) I tend to think it's not good/safe to ignore and keep these "bad" session files. Below is the original traceback info in the error log.What kind of change does this PR introduce?
What is the related issue number (starting with
#)What is the current behavior? (You can also link to an open issue here)
UnpicklingError: invalid load key, '\x00'would break the cleansing thread, leaving many expired sessions not cleaned up, also I tend to think it's not good/safe to ignore and keep these "bad" session files.What is the new behavior (if this is a feature change)?
If pickling a session file is throwing pickling error, then just delete that file.
Other information:
Checklist:
and description in grammatically correct, complete sentences