3 ms·
One way to help protect this kind of issue is to be explicit about validation steps. This is the buggy code: !!str.match(/^[0-9a-f]{24}$/i) That regex is
by jph 11y ago
One way to help protect this kind of issue is to be explicit about validation steps.
This is the buggy code:
!!str.match(/^[0-9a-f]{24}$/i)
That regex is trying to do three different things: validate the length is 24, validate the string contains alphanums, and ensure the matching is pinned from start to finish.
I prefer code that makes the validation steps explicit and simpler:
str.length==24 && str!~/[^0-9a-z]/i
- deleted 11y ago[deleted]
- simcop2387 11y agothe [^ part negates the characters inside the []. So it'll match anything that is NOT 0-9a-z, case insensitive /i. The !~ then says that str should not match the regex. so you end up with it saying that str should not match anything other than 0-9a-z case insensitive.
- deleted 11y ago[deleted]
- arielby 11y agoIts a whitelist. It is "verify the string is a 24-character alphanumeric string".