Beware of Java's String.getBytes
Java's String.getBytes(UTF_8) silently replaces malformed UTF-8 sequences with '?', so two different strings can produce identical byte arrays. In Swiss Post's e-voting system this led to a hash collision, potentially allowing bypass of checks that rely on string equality.
Published on reversemode.com
March 31, 2023
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.
How it worked
Impact
This could lead to hash collisions, potentially allowing bypass of security checks that rely on string equality, though the text only demonstrates the collision in a PoC.
What the attacker needed
Attacker can control a string input; system uses String.getBytes for hashing or comparison
The fix
The bug was fixed in version 1.3.0 of the Swiss Post e-voting system.
How it was found
- Create a string containing a malformed UTF-8 sequence (e.g., a lone surrogate).
- Call stringToByteArray on it, which uses getBytes and replaces the bad sequence with '?'.
- Observe that the resulting byte array is identical to that of a valid string with '?' in that position.
- Compare the original strings with equals() and see they are different, but their hashes collide.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.
When Java converts text to bytes, it sometimes swaps bad characters for a question mark instead of stopping with an error. That means two different pieces of text can turn into the same bytes, which can confuse systems that rely on those bytes being unique. Here it caused a hash collision in an e-voting system.
What is Crypto?
Encryption, signatures, randomness or certificate checks that do not protect what they claim to.