Like the other comment said, this is the real programming horror.
I'm also a bit confused about what's happening - looks like lowercaseKey is 8 characters of lowercase letters and numbers, and randomBinary is 8 0s followed by a 8 random 1s and 0s. Won't the offset of the regex always be inside the first 8 0 run, meaning it never gets uppercased? Also, why do you need to make some of them uppercase at all? Afaik, you have a 1/256 chance to get 0 when you run this function meaning that you're definitely not uppercasing any letters, in which case why not just skip that whole step that takes up like 3/4ths of the function?
string.padStart in this case will add 0's to the start of the string until the string length is 8, so it will always be an 8 character string of random 1's and 0's, effectively randomizing the casing of the 8 character alphanumeric key. Like you said though, it is entirely possible for this to do nothing, and the output will still be all lowercase, but I'm assuming that is by design.
It's all pointless anyways if the server isn't actually validating any keys.
You're actually reducing your entropy the way you're currently doing it because a letter has a 6/16 chance of being picked and you only conditionally promote it to uppercase based on a 50/50 chance if it's a letter, so you'll only get uppercase letters 3 times out of 16 rather than 6/22 like you'd expect if you did random sampling with the full charset predefined.
It would also be a lot easier to understand if it just randomly sampled from a charset.
This may happen with small apps, but this one isn't small. The service this API key is used on owns around 1.5 million outbound IP addresses and that is used on over 10,000 projects on GitHub alone. Most of these projects contain old code when the API key was not a requirement, and while premium is generally enforced (I believe existing keys associated with free accounts are sometimes blocked), it appears to "fail open" while the company owning that service was still showing developers with countdowns to Product Hunt launches and discounts.
it's a function with no input that returns a string. It only needs to do one thing, as long as you can verify it does that thing I wouldn't care at all how it was written. It could be 2000 lines of code and I wouldn't care much more. Just put in a separate file and forget about anything except it's interface which is nothing in, string out.
167
u/46009361 May 05 '26
I can't find your comment anymore, but this is on the client side. There is no validation on the server side.
32
u/SmallThetaNotation May 05 '26
im a bit uneducated in generating keys but for a simple implementation can we not just return the output of line 1?
Also it seems you are just doing a bunch of random BS, how do you validate this key is real and issued by your server(s)?
13
u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” May 05 '26
Why can't you just do
array[0]?70
u/marky125 May 05 '26
"Ok, they're starting with a UUID, that should be all they- oh wait, they've just thrown most of it away. Ok then."
A key exactly 8 characters long with the charset [0-9A-Fa-f] is probably not a good key, unless this is some kind of internal-only high-trust system.
52
u/t3kner May 05 '26
Always nice to add a little extra pointless computation
7
u/RegisteredJustToSay May 05 '26
You're actually reducing your entropy the way you're currently doing it because a letter has a 6/16 chance of being picked and you only conditionally promote it to uppercase based on a 50/50 chance if it's a letter, so you'll only get uppercase letters 3 times out of 16 rather than 6/22 like you'd expect if you did random sampling with the full charset predefined.
It would also be a lot easier to understand if it just randomly sampled from a charset.
1
u/MMORPGnews May 05 '26
I know many small apps still doing it client side. Just to force premium on user.
It's just, backend is literally free right now with cloudflare. Why don't create it backend.
1
u/RipProfessional3375 May 10 '26
it's a function with no input that returns a string. It only needs to do one thing, as long as you can verify it does that thing I wouldn't care at all how it was written. It could be 2000 lines of code and I wouldn't care much more. Just put in a separate file and forget about anything except it's interface which is nothing in, string out.