-
Type:
Bug
-
Resolution: Unresolved
-
Priority:
Unknown
-
None
-
Affects Version/s: None
-
Component/s: None
-
None
-
None
-
Python Drivers
-
None
-
None
-
None
-
None
-
None
-
None
Summary
PYTHON-6183 made GridFS.get_version / get_last_version reject a non-string filename, because a dict was treated as query criteria. GridFSBucket.delete_by_name and rename_by_name have the same problem: they put filename straight into the files query, so a dict passed as the filename becomes a query.
bucket = gridfs.GridFSBucket(db)
bucket.delete_by_name(\{"$ne": None}) # filter sent: \{"filename": {"$ne": None}} -> deletes every file
bucket.rename_by_name(\{"$ne": None}, "x") # renames every file to "x"
bucket.open_download_stream_by_name(\{"$ne": None}) # TypeError (already validated)
Expected: TypeError("Wrong type for filename..."), like open_download_stream_by_name and get_version.
Actual: the dict is used as query criteria. If the filename comes from user input (e.g. a JSON request body), one request can delete or rename the whole bucket.
Cause: delete_by_name and rename_by_name in gridfs/asynchronous/grid_file.py (and the generated sync file) don't call validate_string("filename", filename). rename_by_name also doesn't validate new_filename.
Suggested fix: add validate_string for filename (and new_filename), plus tests. I have a PR ready.
Environment
PyMongo: master (f4b9580), 4.20.2.dev0, C extensions enabled
Python: 3.11, Linux
MongoDB: not server-specific, the filter is built client-side. I confirmed the filter passed to find / update_many by replacing those calls in a script. I didn't run it against a live server.
Framework: none