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

Clean the session files upon unpackling Error in FileSession clean up #2012

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

jiangwen365
Copy link

@jiangwen365 jiangwen365 commented Dec 24, 2023

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.

Traceback (most recent call last):
  File "C:\Program Files (x86)\Python311-32\Lib\site-packages\cherrypy\process\plugins.py", line 518, in run
    self.function(*self.args, **self.kwargs)
  File "C:\Program Files (x86)\Python311-32\Lib\site-packages\cherrypy\lib\sessions.py", line 586, in clean_up
    contents = self._load(path)
               ^^^^^^^^^^^^^^^^
  File "C:\Program Files (x86)\Python311-32\Lib\site-packages\cherrypy\lib\sessions.py", line 520, in _load
    return pickle.load(f)
           ^^^^^^^^^^^^^^
_pickle.UnpicklingError: invalid load key, '\x00'.

What kind of change does this PR introduce?

  • bug fix
  • feature
  • docs update
  • tests/coverage improvement
  • refactoring
  • other

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:

  • I think the code is well written
  • I wrote good commit messages
  • I have squashed related commits together after the changes have been approved
  • Unit tests for the changes exist
  • Integration tests for the changes exist (if applicable)
  • I used the same coding conventions as the rest of the project
  • The new code doesn't generate linter offenses
  • Documentation reflects the changes
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences

jiangwen365 and others added 2 commits December 24, 2023 16:31
I've seen clean failures in the error log files due to UnpicklingError: invalid load key, '\x00'.
@jiangwen365 jiangwen365 changed the title Clean the session files upon unpacking Error Clean the session files upon unpackling Error Dec 24, 2023
@jiangwen365 jiangwen365 changed the title Clean the session files upon unpackling Error Clean the session files upon unpackling Error in FileSession clean up Dec 24, 2023
@@ -603,6 +603,10 @@ def clean_up(self):
if expiration_time < now:
# Session expired: deleting it
os.unlink(path)

except pickle.UnpicklingError:
Copy link
Member

Choose a reason for hiding this comment

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

Could you add a test for this?

Copy link
Author

Choose a reason for hiding this comment

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

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?

Copy link
Member

Choose a reason for hiding this comment

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

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.

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.

None yet

2 participants