Skip to content

fix: IndexError in detect_media_type_from_base64 for non-base64 data …#5114

Open
Serjbory wants to merge 1 commit intomicrosoft:mainfrom
Serjbory:fix/index-error-in-detect-media-type
Open

fix: IndexError in detect_media_type_from_base64 for non-base64 data …#5114
Serjbory wants to merge 1 commit intomicrosoft:mainfrom
Serjbory:fix/index-error-in-detect-media-type

Conversation

@Serjbory
Copy link
Copy Markdown

@Serjbory Serjbory commented Apr 6, 2026

fix: IndexError in detect_media_type_from_base64 for non-base64 data URIs

Motivation and Context

detect_media_type_from_base64(data_uri=...) raises an unhandled IndexError when the provided URI does not contain the ;base64, separator (e.g. data:text/plain,Hello%20World or a plain HTTP URL). The function blindly calls data_uri.split(";base64,", 1)[1] without first checking that the delimiter exists.

The sister function _get_data_bytes_as_str in the same file already guards against this case, but detect_media_type_from_base64 was missing the same check.

Description

  • Added a guard in detect_media_type_from_base64 that raises a ValueError("Data URI must use base64 encoding.") when data_uri does not contain ;base64,, consistent with the existing pattern in _get_data_bytes_as_str.
  • Added two test assertions to test_data_content_detect_image_format_from_base64 covering a non-base64 data URI (data:text/plain,Hello%20World) and a plain HTTP URL (http://example.com/image.png).

Contribution Checklist

  • The code builds clean without any errors or warnings
  • The PR follows the Contribution Guidelines
  • All unit tests pass, and I have added new tests where possible
  • [no] Is this a breaking change? If yes, add "[BREAKING]" prefix to the title of the PR.

@Serjbory
Copy link
Copy Markdown
Author

Serjbory commented Apr 6, 2026

@microsoft-github-policy-service agree

@Serjbory Serjbory changed the title fix: IndexError in detect_media_type_from_base64 for non-base64 data … Python: fix: IndexError in detect_media_type_from_base64 for non-base64 data … Apr 6, 2026
@Serjbory Serjbory changed the title Python: fix: IndexError in detect_media_type_from_base64 for non-base64 data … fix: IndexError in detect_media_type_from_base64 for non-base64 data … Apr 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant