Skip to content

fix: avoid IndexError in is_mostly_bin for short tails - #8196

Merged
mhils merged 2 commits into
mitmproxy:mainfrom
juliosuas:fix/8188-is-mostly-bin-index-out-of-range
Apr 25, 2026
Merged

mhils merged 2 commits into
mitmproxy:mainfrom
juliosuas:fix/8188-is-mostly-bin-index-out-of-range

Conversation

@juliosuas

Copy link
Copy Markdown
Contributor

fixes #8188.

reproducible with any bytes payload of len 101–103 where index 100 is a UTF-8 continuation byte:

>>> from mitmproxy.utils import strutils
>>> strutils.is_mostly_bin(b'a' * 100 + b'\x80')
Traceback (most recent call last):
  ...
IndexError: index out of range

the lookahead loop range(100, 104) reads s[100] through s[103] to find the start of the next UTF-8 char and avoid chopping a multibyte code point in half. when len(s) is between 101 and 103 and the first byte past the 100-byte cutoff is a continuation byte, the loop continues into out-of-range indices.

cap the loop end at len(s). the existing s[:100] fallback still covers the case where every byte in the lookahead window is a continuation byte.

added a small regression assertion for the three affected lengths.

juliosuas and others added 2 commits April 25, 2026 01:01
`is_mostly_bin` looks up to 4 bytes past the 100-byte cutoff to find a
clean UTF-8 character boundary. when the input is just over 100 bytes
and starts with a continuation byte at index 100, the lookahead reads
past the end of the buffer.

cap the loop end at `len(s)`. existing fallback to `s[:100]` still
covers the case where every byte in the lookahead window is a
continuation byte.

fixes #8188

@mhils mhils left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks!

@mhils
mhils merged commit a8fbfc7 into mitmproxy:main Apr 25, 2026
28 checks passed
@juliosuas

Copy link
Copy Markdown
Contributor Author

🫡

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.

Index out of range error when converting to .har file

2 participants