You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Set and Map are vulnerable to hash collisions #205
I exploit a weakness of map_hash_key() by inserting keys with ever longer zero prefixes - "k", "\0k", "\0\0k", etc. - that all hash to the same value and end up in the same hash bucket.
Doubles-as-keys are exploitable too; maybe ints too because they're cast to doubles before hashing. The hash algorithm is this:
u.d=d;
h= (u.u32[0] ^ u.u32[1]) *3163;
But there are literally millions of normal and subnormal doubles with high and low words that are bitwise identical and cancel each other out when xor'ed.
Tangential: other JS engines (and python, ruby, etc.) added random seeds to their hash tables about 10 years ago to mitigate DoS attacks. We probably want to do likewise.
Reacted by Saúl Ibarra Corretgé, GalaxySnail, liulun, 叶芝秋 and Dmitry Volyntsev
As the comment indicates, /* XXX: better hash ? */, the hash methods are simplistic and easy to defeat.
Combining the string length into the hash value is a simple way to fix hide the problem.
Implementing a fast reliable hash is of course a better approach.
Note by the way that the hash performed on BigInt values might pose problems too as I am not convinced the representation in the array is necessarily normalized. I discussed this with Fabrice and we agree a fresh biginteger package is necessary and would also vastly improve the bigint performance.
At a quick glance I don't think that commit tries to address prefix hash collisions. It seems to make the spread of non-adversarial input more equitable.
Activity
bnoordhuis commentedon Dec 15, 2023
Tangential: other JS engines (and python, ruby, etc.) added random seeds to their hash tables about 10 years ago to mitigate DoS attacks. We probably want to do likewise.
chqrlie commentedon Feb 26, 2024
As the comment indicates,
/* XXX: better hash ? */, the hash methods are simplistic and easy to defeat.Combining the string length into the hash value is a simple way to
fixhide the problem.Implementing a fast reliable hash is of course a better approach.
Note by the way that the hash performed on BigInt values might pose problems too as I am not convinced the representation in the array is necessarily normalized. I discussed this with Fabrice and we agree a fresh biginteger package is necessary and would also vastly improve the bigint performance.
bnoordhuis commentedon Feb 28, 2024
FWIW, I thought along similar lines and even made a start but ran out of time/steam.
chqrlie commentedon Mar 1, 2024
Fabrice has a few in his treasure chest, let's wait a bit to get that when he is ready.
saghul commentedon Jan 31, 2026
@bnoordhuis Do you reckon it's worth it to port bellard/quickjs@a151ce1 in this context?
bnoordhuis commentedon Feb 1, 2026
At a quick glance I don't think that commit tries to address prefix hash collisions. It seems to make the spread of non-adversarial input more equitable.
Harden against hash collisions