Skip to content

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

@enigmathix

Description

@enigmathix

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions