Skip to content

fix: validate module, class, and filename during MediaUpload deserialization - #2796

Merged
parthea merged 8 commits into
mainfrom
fix-media-upload
Aug 31, 2026
Merged

parthea merged 8 commits into
mainfrom
fix-media-upload

Conversation

@parthea

@parthea parthea commented Aug 31, 2026

Copy link
Copy Markdown
Contributor

Fixes unsafe reflection in MediaUpload.new_from_json() where _module and _class were passed directly to __import__() and getattr().

Key changes:

  • Replaced dynamic imports in MediaUpload.new_from_json() with an explicit lookup mapping limited to googleapiclient.http.MediaFileUpload.
  • Added a dict type check on parsed JSON in both MediaUpload.new_from_json() and MediaFileUpload.from_json() to avoid AttributeError on primitive/array JSON inputs.
  • Added field validation and fallbacks in MediaFileUpload.from_json():
    • _filename must be a non-empty string without null bytes.
    • _chunksize must be an integer, defaulting to DEFAULT_CHUNK_SIZE if None.
    • _resumable must be a boolean, defaulting to False if None.
    • _mimetype must be a string or None.

Fixes b/531771000

@product-auto-label product-auto-label Bot added the size: m Pull request size is medium. label Aug 31, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request mitigates a deserialization vulnerability (CWE-502) in googleapiclient/http.py by replacing dynamic imports and reflection in MediaUpload.new_from_json with an explicit allowlist of supported subclasses. It also adds input validation to MediaFileUpload.from_json to prevent null-byte injection and malformed filenames. Feedback on the changes highlights two issues: first, the new test class uses pytest features and does not inherit from unittest.TestCase, which will cause them to be skipped or fail under the existing unittest runner; second, using .get() without fallback values for _chunksize and _resumable can pass None to the constructor, resulting in a TypeError.

Comment thread tests/test_http.py Outdated
Comment thread googleapiclient/http.py Outdated
@parthea
parthea force-pushed the fix-media-upload branch 2 times, most recently from d6c4c26 to 0a4ce39 Compare August 31, 2026 16:26
@parthea

parthea commented Aug 31, 2026

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request introduces security mitigations for CWE-502 (unsafe dynamic deserialization) in MediaUpload.new_from_json by restricting deserialization to an explicit allowlist of classes and validating that the module is googleapiclient.http. It also hardens MediaFileUpload.from_json by validating the filename and providing safe fallbacks for chunk size and resumability, accompanied by comprehensive unit tests. The review feedback suggests further hardening the deserialization process by ensuring that the parsed JSON is indeed a dictionary before calling .get() on it, and validating the types of _chunksize, _resumable, and _mimetype to prevent unexpected runtime errors.

Comment thread googleapiclient/http.py
Comment thread googleapiclient/http.py Outdated
Comment thread googleapiclient/http.py
@product-auto-label product-auto-label Bot added size: l Pull request size is large. and removed size: m Pull request size is medium. labels Aug 31, 2026
@parthea

parthea commented Aug 31, 2026

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request introduces safe deserialization mitigations for CWE-502 in googleapiclient/http.py by replacing dynamic imports and reflection with an explicit class allowlist. It also adds strict type and structure validation for deserialized JSON payloads in MediaUpload.new_from_json and MediaFileUpload.from_json, along with comprehensive unit tests. The review feedback suggests defining the class allowlist dictionary after the MediaFileUpload class definition to avoid forward-reference lambdas, and replacing an f-string with standard % formatting to maintain consistency with the rest of the codebase.

Comment thread googleapiclient/http.py Outdated
Comment thread googleapiclient/http.py
Comment thread googleapiclient/http.py Outdated
Comment thread googleapiclient/http.py Outdated
@parthea parthea added the owlbot:run Add this label to trigger the Owlbot post processor. label Aug 31, 2026
@gcf-owl-bot gcf-owl-bot Bot removed the owlbot:run Add this label to trigger the Owlbot post processor. label Aug 31, 2026
@parthea
parthea marked this pull request as ready for review August 31, 2026 19:07
@parthea
parthea requested a review from a team as a code owner August 31, 2026 19:07
@parthea parthea added the owlbot:run Add this label to trigger the Owlbot post processor. label Aug 31, 2026
@gcf-owl-bot gcf-owl-bot Bot removed the owlbot:run Add this label to trigger the Owlbot post processor. label Aug 31, 2026
Comment thread googleapiclient/http.py
Comment thread googleapiclient/http.py Outdated
Comment thread googleapiclient/http.py
Comment thread googleapiclient/http.py
@parthea parthea added the owlbot:run Add this label to trigger the Owlbot post processor. label Aug 31, 2026
@gcf-owl-bot gcf-owl-bot Bot removed the owlbot:run Add this label to trigger the Owlbot post processor. label Aug 31, 2026
@parthea parthea added kokoro:force-run Add this label to force Kokoro to re-run the tests. kokoro:run Add this label to force Kokoro to re-run the tests. labels Aug 31, 2026
@parthea parthea added the owlbot:run Add this label to trigger the Owlbot post processor. label Aug 31, 2026
@gcf-owl-bot gcf-owl-bot Bot removed the owlbot:run Add this label to trigger the Owlbot post processor. label Aug 31, 2026
@yoshi-kokoro yoshi-kokoro removed kokoro:run Add this label to force Kokoro to re-run the tests. kokoro:force-run Add this label to force Kokoro to re-run the tests. labels Aug 31, 2026
@parthea
parthea enabled auto-merge (squash) August 31, 2026 21:46

@daniel-sanche daniel-sanche left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@parthea
parthea merged commit 00e95fd into main Aug 31, 2026
17 checks passed
@parthea
parthea deleted the fix-media-upload branch August 31, 2026 22:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size: l Pull request size is large.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants