multinet-app / multinet-server

Multinet server application
https://multinet-app.readthedocs.io
Apache License 2.0
4 stars 2 forks source link

Return copy of data if returning value from cached function call #432

Closed jjnesbitt closed 2 years ago

jjnesbitt commented 4 years ago

The primary example I found was here: https://github.com/multinet-app/multinet-server/blob/ba7f385e7e5226be28b782fabf612adae579ca49/multinet/db.py#L218-L229

Since workspace_mapping is cached, if the caller of get_workspace_metadata decides to directly modify the return of that function, it will inadvertently be modifying the cache of that call to workspace_mapping.

For this reason, if a function directly returns the response from a cached function, or includes the response from a cached function as part of the return (e.g. a value in a dictionary), a copy of that data should be returned, to prevent mutating the cached response.

waxlamp commented 4 years ago

I can't think of any harm to making a copy of these values before returning them. When you fix this, if you could include a comment explaining why we're bothering to make the copy, that would be great.