Fix array bounds vulnerability in Fragment iteration methods #91
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "fragment_bounds"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Fragment.nodesBetween() and Fragment.cut() both contain unsafe loop conditions that can cause out-of-bounds array access when the 'to' parameter exceeds the fragment's actual size. This is a defensive programming failure that violates the principle that interfaces should be safe against invalid input.
The fix adds proper bounds checking to prevent accessing this.content[i] when i >= this.content.length. This ensures the methods remain robust even when called with parameters that exceed the fragment bounds.
Added tests to verify the edge case behavior.
Defensive programming, as the practice of silently checking and correcting invalid inputs, is not a principle this codebase subscribes to. I often consider breaking a better behavior than papering over issues in the code that's passing in the bogus parameters, since it'll help the programmer writing and testing that code realize that there is a problem. As such, I don't want to add additional loop termination tests to these methods (and the hundreds of other methods in the project that could be equipped with such checks).
Also, since this isn't a memory-unsafe language, 'vulnerability' is not the appropriate word here.
Pull request closed