Oh I’ve seen some scary stuff. Was pentesting a Java desktop app a few years ago and the client passed the username to the server during login, which then returned the password hash to the client to verify if the credentials were valid client side… you just use a debugger to modify the value and then send requests for any user. This was sending serialised Java objects so crafting the requests manually wasn’t trivial, leveraging the auth bypass did the trick!
Well not really, simply knowing the salt doesn’t really help there because salts are unique per hash, the fact the hash is salted already provides provides protection against precomputed rainbow tables. This is exact property why salts are used.
The salt should be random, but say it’s a predictable salt (for example the email address of the user and this is known - say it’s an open source app), it doesn’t make it any easier because rainbow tables are already out of the question and you then need to leverage a dictionary attack.
Speaking in general there, in the case I mentioned the hash is largely irrelevant. You don’t need to crack it at all because you just modify the client to always return true from the local check.
To be honest, I’m more concerned with the bad variable names and with the purpose of the login() function. Please tell me that you aren’t building your own authentication.
It’s a bad habit to have as a professional, for example I would not approve a PR like this. If it’s simply for a one-off script, then it doesn’t matter.
Imagine if you saw "psw" without context. It's not a way people normally abbreviate "password". For all I know it's something like "process wait" or some acronym or "pounds per square whatever". And how many keystrokes did you save?
I've got two more variables "wbt" and "lgip". They're related to "psw". Maybe you can figure out what they stand for, but every second you spend thinking about it is a second of time that I'm wasting from every developer who comes after me.
I'm going to go against the grain here, although personally I would have also written user and password
If the scope of these variables is very small, like only used within this function, it's really not a big readability cost to shorten them like this. I would know what this is doing as easily as if they weren't, and I would roll my eyes before begrudgingly expanding the names in code review if called out.
What I would not do is consistently refer to a "password" as "psw" all over the application.
Shortening variable names was important back in the days of limited storage and such, but its better practice these days to use long, descriptive names because they take up insignificantly more space and are much more readable to someone new to your code.
Unless your dev environment/prod pipeline is super shabby longer variable names will be shortened by the compiler or minified, so usually a longer var name has exactly 0 effect on performance and requirements
Even as an industry dev I'll still nearly write this when I'm on auto pilot. The only thing that's changed is I've gotten faster at noticing when I do it. Nowadays I'll notice it just as I start to write the value True!
This is what I was going to say. I start with some debug output or other code in one of the conditions, then at some point realize I've removed it and I have an Embarrassing Pattern in my code.
I feel like it’s a very natural pattern to end up with: I wrote down the if statement because I was sure something needs to be checked, and I haven’t yet figured out that just passing the checks value is what I want to do after the check.
Like others have said, the function's return being checked here is ambiguous. It might not be returning a bool, so this is probably okay. But in a lot of cases, the function is more clearly named, so the pattern of just writing return is_foo(bar) is both neat and clear
You should cast it either way for readability. Even if login always returns a bool, as a reader you wouldn't know unless you dig deeper (into the login function).
Hungarian notation is an identifier naming convention in computer programming, in which the name of a variable or function indicates its intention or kind, and in some dialects its type. The original Hungarian notation uses intention or kind in its naming convention and is sometimes called Apps Hungarian as it became popular in the Microsoft Apps division in the development of Word, Excel and other apps. As the Microsoft Windows division adopted the naming convention, they used the actual data type for naming, and this convention became widely spread through the Windows API; this is sometimes called Systems Hungarian notation.
I still stuff like this. Usually the result if iterations of changing the code and having tunnel vision around the actual boolean logic itself rather than the stuff around it. It's get caught in a code review where you'd catch a quick rub, but we all do it from time to time.
tbh, I don't think it's that bad, especially in a dynamic language. Yes, it's verbose, but on the other hand, it is more explicit about what it returns.
This is fine code. Shorter is not always better, and the compiler will optimize this anyway. The return values of login() are not necessarily obvious, but it's clear what happens here. If someone on my project wrote code like this, I would approve.
A function called "login" shouldn't do anything else than that. It should not return a value, except maybe login details, and throw an error if it didn't work.
False says nothing about what happened. This is bad code in more than one way.
I don't see anything wrong with this. In fact It makes debugging easier as you can see what was returned from the login method before return value is sent back.
Doing it in one line is cool and all but you are making life unnecessarily difficult.
65
u/-True_- Jan 23 '23 edited Jan 23 '23
(I fixed it by adding "# TODO: function call" before returning true.)
50
u/denideniz Jan 23 '23
login doesnt returning any error, auth token or at least status code is more frightening to me.
138
u/hoomei Jan 23 '23
Does "login" always return a boolean? If not, maybe you're just coercing the result into a boolean, which isn't necessarily bad code.
15
u/[deleted] Jan 23 '23
To be honest, I’m more concerned with the bad variable names and with the purpose of the login() function. Please tell me that you aren’t building your own authentication.
31
u/cdwpmaster Jan 23 '23
return login(driver, usr, psw);
356
u/PooSham Jan 23 '23
This is a very common pattern by beginners.
73
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Jan 23 '23
Image Transcription: Code
I'm a human volunteer content transcriber and you could be too! If you'd like more information on what we do and why we do it, click here!
22
u/SOSFILMZ Jan 23 '23
props to posting your own code, that takes mad confidence.
7
u/bisoldi Jan 23 '23
Am I the only one annoyed that a login method returns a Boolean?
It should return something useful (or nothing at all) and throw an exception in the event of failure.
But then yes…stop checking the Boolean and returning a Boolean!
1
u/AdearienRDDT shameless Jan 27 '23
aight, now please somebody tell me what's wrong with that? /srs
6
u/cdp1337 Jan 23 '23
(If we're playing Jeopardy): "What is, what happens when you code without adequate amounts of caffeine?"
3
u/valenbel123 Jan 23 '23
Feel like 99% of the posts here are about this exact pattern
3
u/Codepressed Jan 24 '23
I wanna see ur Git repo
2
u/timlmul Jan 23 '23
tbh I wouldn't be able to tell that login returns a bool... so a score for readability?
1
u/cheezballs Jan 23 '23
I still stuff like this. Usually the result if iterations of changing the code and having tunnel vision around the actual boolean logic itself rather than the stuff around it. It's get caught in a code review where you'd catch a quick rub, but we all do it from time to time.
1
u/ZielAnima Jan 23 '23
This is a good way of leaving code as a placeholder so you can work on it later :)
1
u/flankstek Jan 23 '23
What's so bad with this?
1
u/mateo8421 Jan 23 '23
Yeah, seen this too many times…
1
u/cgmystery Jan 23 '23
I want to see the function name for this code.
1
u/Rogoreg Jan 24 '23
What's wrong with that? Not everyone's code is perfect. There's always better practices, but this isn't so bad.
1
u/thomasz Jan 24 '23
tbh, I don't think it's that bad, especially in a dynamic language. Yes, it's verbose, but on the other hand, it is more explicit about what it returns.
1
u/KlingonButtMasseuse Jan 24 '23 edited Jan 24 '23
1
u/[deleted] Jan 24 '23
It's backwards, if you assume a failure returns a host of different errors, and zero when sucessful.
If login(): return false else: return true
1
u/lardgsus Jan 24 '23
It’s understandable, doesn’t have performance impact, and testable. It’s ultimately fine.
1
u/GabeAV1122 Jan 24 '23
i mean, it’s not “horrible”
1
u/velcro44 Jan 24 '23
Big brain
1
u/default-username-2 Jan 25 '23
Never hurts to be suer a boolean is actually a boolean.
This code is now resilient to cosmic ray corruptions!
1
u/jlianoglou Jan 25 '23
I’ve seen worse from an off-shore outsourced developer team I was working with at around 2008 or so:
And, yes, the casing of those strings is indeed representative of the brain-breaking code that was part of the code review 😬😉
1
u/Bisquizzle Feb 02 '23
Yeah this happens a lot, it's funny but also a common mistake.
1
u/rafpad Jan 24 '23
return login(driver, usr, psw)
0
u/[deleted] Jan 24 '23
This is fine code. Shorter is not always better, and the compiler will optimize this anyway. The return values of login() are not necessarily obvious, but it's clear what happens here. If someone on my project wrote code like this, I would approve.
-3
u/jacobbeasley Jan 23 '23
0
u/JasonLokiSmith Jan 24 '23
I don't see anything wrong with this. In fact It makes debugging easier as you can see what was returned from the login method before return value is sent back.
Doing it in one line is cool and all but you are making life unnecessarily difficult.
0
u/[deleted] Jan 24 '23
Average Python user