Sub topic 15
Hash flooding and untrusted input
This chapter assumes you know how a hash table works: a hash function picks a bucket, and collisions are resolved inside the bucket. It also assumes you know the average and worst-case costs from the earlier chapters. Here the question is what happens when someone chooses the keys on purpose.
Why the worst case matters
Hash tables are O(1) on average because a good hash function spreads keys evenly across buckets. That promise holds for keys that arrive without any bias. If an attacker can pick the keys, they can pick keys that all land in the same bucket.
Every operation on that bucket then scans a chain of colliding entries. Inserting n such keys costs 1 + 2 + ... + n comparisons, which is O(n²) in total. A request body with 100,000 colliding keys can keep a CPU busy for many seconds, while the request itself stays small. This is a denial-of-service attack called hash flooding.
Where untrusted keys come from
Any place where outside data becomes a key is a possible entry point:
- JSON object keys, query-string parameters and form fields parsed into a dictionary
- HTTP headers and cookies
- Words, usernames or file names counted with a map, as in the counting example earlier in this tutorial
If the hash function is public and does not change between runs, an attacker can compute colliding keys offline and send them to any server running that code.
Defenses
- Randomized hashing. The runtime mixes a secret random seed into the hash function when the process starts. Colliding keys for one process are then unlikely to collide in another, and the attacker cannot precompute them. Python does this for
strandbyteshashes, and many other languages do something similar. - Keyed hash functions. Languages such as Rust use SipHash by default, which takes a secret key and is designed to resist this attack. Faster non-keyed hashes are an opt-in choice, so only use them for trusted keys.
- Resistant collision handling. Some implementations convert a long chain into a balanced tree. Java's
HashMapdoes this, which limits the worst case to O(log n) per operation when keys are comparable. - Input limits. Cap the number of keys, the body size and the nesting depth before you build the map. Most web frameworks and JSON parsers have such settings.
You can see the randomization at work in Python:
import subprocess
import sys
code = "print(hash('hello'))"
first = subprocess.run([sys.executable, "-c", code], capture_output=True, text=True).stdout
second = subprocess.run([sys.executable, "-c", code], capture_output=True, text=True).stdout
print(first.strip() == second.strip()) # almost always FalseEach child process picks its own seed, so the two hashes differ.
Practical checklist
- Use the standard library map for untrusted keys. Do not write your own hash function for them.
- Do not expose raw hash values or hash-based ordering to clients, because they leak information about the seed.
- Bound input size before parsing it into a map.
- If you need guaranteed worst-case behavior, use an ordered structure such as a balanced tree, which costs O(log n) regardless of the keys.
Exercise
A service parses JSON request bodies into dictionaries and uses a custom hash function with a fixed, published algorithm for the keys. Name two changes that reduce the risk of hash flooding.
Show answer
Any two of: switch to the runtime's default hashing, which is randomized or keyed (such as SipHash or Python's seeded string hash); limit the body size and number of keys before parsing; use a map implementation that falls back to trees for long chains, or an ordered structure with O(log n) worst case.