Dealing with DoS when using a HashMap

I’ve noticed that, by default, Zig hash map doesn’t depend on random number generation, and instantiates a particular hash function with a fixed seed. It seems to me that this is potentially vulnurable to a DoS attack, when the attacker can craft hash map entries which force collisions.

In particular, it seems that recently added http server has this problem when dealing with headers:

Am I correct that the code as is is vulnerable to DoS? That is, that a malicious client can send a sequence of headers which would require O(N^2) CPU time to process?

What would be the correct and idiomatic way to fix that?

1 Like

The way to defend against this class of attack is to use a cryptographic hash function (for example std.crypto.auth.siphash.SipHash128(1,2) with a secret key (not 0).

1 Like

Cryptographic hash functions are secure when you use enough bits if your hashmap is not EiBs big you will only use very little bits, it is easy to find collisions on the few bits your hashmap will use, you will run out of memory way before a cryptographic hash function is secure.

Would you initialize the secret key at runtime with random bytes? Or hard-code the secret key into the source for determinism?

This needs to be done at runtime. Using a key in source would mean the adversary can just use this information and forge an attack based on this key.

2 Likes

This needs to be done at runtime. Using a key in source would mean the adversary can just use this information and forge an attack based on this key.

+1 - especially when insider attacks are 20% of data breaches these days.