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

_set_cache_value doesn't check for None type and crashes if no_cache is set to False #2379

Closed
enigmathix opened this issue Apr 5, 2022 · 0 comments
Assignees
Milestone

Comments

@enigmathix
Copy link

enigmathix commented Apr 5, 2022

Setting response.cache_control.no_cache = False used to work up until version 2.0.x thanks to this code in datastructures.py, where the type value received is None:

    def _set_cache_value(self, key, value, type):
        """Used internally by the accessor properties."""
        if type is bool:
(...)
        else:
            if value is None:
                self.pop(key, None)
            elif value is True:
                self[key] = None
            else:
                self[key] = value

Now it crashes because type is not checked for None in 2.1.x:

    def _set_cache_value(self, key, value, type):
        """Used internally by the accessor properties."""
        if type is bool:
(...)
       else:
            if value is None:
                self.pop(key, None)
            elif value is True:
                self[key] = None
            else:
                self[key] = type(value) # <-- crashes if type is None

It type is None, this will crash:
TypeError: 'NoneType' object is not callable

I think the correct code should be:

    def _set_cache_value(self, key, value, type):
        """Used internally by the accessor properties."""
        if type is bool:
(...)
       else:
            if value is None:
                self.pop(key, None)
            elif value is True:
                self[key] = None
            elif type is None:
                # do something, e.g. self[key] = value
            else:
                self[key] = type(value) 

To reproduce:

def send_static(path):
    response = send_from_directory('assets', path)
    response.cache_control.no_cache = False

Expected behavior:
At least check for type being None before trying to call it and throw an appropriate exception if needed (not sure why it should be an error, the code worked fine in 2.0.3)

Environment:

  • Python version: 3.10.x
  • Werkzeug version: 2.1.1
@davidism davidism added this to the 2.1.2 milestone Apr 5, 2022
@davidism davidism self-assigned this Apr 24, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 10, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants