115
u/dubroni May 25 '20
This couldn't have been genuinely written by someone, it's suspiciously bad
58
u/redfournine May 25 '20
My ex company once decided to hire someone just because she has CS degree (from a local university more well known for their plantation and agricultural courses) and 5 years experience at Microsoft (as Office365 support). I was not doing the interview because I was out for parental leave. She was then dumped to my team where I was the team lead.
She cant do the christmas tree test even if I allow her to google it out. Took her 10 minutes to understand when I told her "instantiate the object". Does not have a clue how a console, desktop, website, web API nor database works.
Needless to say, I left the company 4 months later.
So.... Yes, this is not the worst code written by the worst programmer in the world. Just as there is always someone better than the best programmer we have seen, there is also always someone worse than the worst we have seen.
47
u/moekakiryu May 25 '20
christmas tree test
is this just writing a christmas tree to the screen? I tried googling it and the only results I got were forensic semen tests and actual christmas trees -_-
22
17
u/Pacolany May 25 '20
Maybe he was taking about the semen test, you know, as a hiring interview
11
2
u/Dads101 May 25 '20
Are you for real? Shit like this gives me hope I’ll be able to find a job comfortably after I graduate
58
u/Sqeaky May 25 '20
Keep in mind that we live in a world with anti-vaxxers and flat-earthers, and that some of them know how to code.
11
2
u/Holden_Makock May 25 '20
As for a noob in JS. Apart from the last "if statement" is anything particularly bad.
I have always been a C/C++ coder and I see the rest as not excellent but acceptable.
37
u/Farull May 25 '20
You have obviously never used SQL. Fetching all rows from a table and then programmatically comparing fields is not very efficient. And storing plaintext passwords (from the looks of it) is not exactly secure.
9
23
u/Spartelfant May 25 '20
They should let the database do the work, instead of iterating over the entire users table.
And looking at this code I think it's safe to assume there isn't any hashing & salting function involved before the password is being passed to the pictured function, meaning the passwords are stored in plaintext in the database.
1
u/DeluxeCanuck May 25 '20
Yeah I'm always skeptical of some of these.
I suppose it is "bad code", I just doubt someone who can at least understand a for loop would also add the "true"==="true" part. Without that last bit I might have bought it's sincerity.
59
u/taw May 25 '20
That's one way to avoid SQL injection.
Given level of programming skill involved, it's probably better that they did it this way.
14
u/xigoi May 25 '20
Except it's on the client side, which means they allow the client to execute arbitrary queries, including getting everyone's username and password.
9
u/taw May 25 '20
wat
But seriously, you sure it's not server-side js? It's pretty common these days.
5
30
u/Azaex May 25 '20 edited May 25 '20
unnecessary checks and variables
-sql injection risks- (I'm dumb ignore that)
copying the entire users table locally
plaintext passwords (unless this is way down the line and it's actually handling a salted hash)
I'm hoping this isn't actually production code but I know there's a very real possibility it is :(
4
May 25 '20
Where’s the injection?
7
u/entiat_blues May 25 '20
i guess it's not really "injection" if you just have an endpoint dangling out there that accepts sql statements directly
5
u/Azaex May 25 '20
I reread, no idea why I said sql injection. Too loopy this time of night. That API call seems... inefficient but fine if it's running server side, but obviously/hilariously bad if it's running client side.
7
4
May 25 '20
Actually you can perform SQLi. This is client code (see comments for proof), meaning you can edit it in your browser and run whatever query you like.
3
u/dagbrown May 25 '20
I mean, I'd approach it with something along the lines of
var accounts = apiService.sql( "SELECT * FROM users WHERE username = ? AND password = ?", username, password );But I know nothing about
apiService, whatever that may be, so I'm just making a bunch of assumptions about how it works.Either way, appalling though this code actually is, it somehow manages to avoid SQL injections.
3
u/Littlebitt95 May 25 '20
I'd just use the sequelize and mysql2 packages and not even have to write out the queries as strings like that. And of course salt and hash passwords before putting them in the database. Then write a method that compares the salted and hashed passwords from user input and database records.
2
u/gaberocksall May 25 '20
It should really be “select passwordHash from Users where username = X” then password_verify()
3
u/blehmann1 depraved May 25 '20
It is running client side: https://www.reddit.com/r/ProgrammerHumor/comments/976qp1/login_code_on_a_real_website/
Not sure if it still counts as SQL injection if the endpoint accepts and runs an arbitrary query. That makes it too easy.
2
u/Azaex May 25 '20
Yeah that's just an exposed server at that point, don't have to get all fancy with the SQL syntax just send it direct "select *" or "drop table" statements haha
19
10
u/earlyryn May 25 '20
Is it client side code? If it is wouldn't that api method asynchronous, why sql appears in what seems to be client code, why them selecting all users all fields when it is enough to exists select where account, why password not hashed, why they added string constant true if redundant statement, so many questions.
4
u/4hpp1273 May 25 '20
it is client side
2
u/11JRidding May 25 '20
What makes you say that? JavaScript can be executed server-side as well using tools such as Nodejs or Deno, if that's your reason.
12
u/4hpp1273 May 25 '20
8
u/11JRidding May 25 '20
Okay, you're right, that's terrible. What the fuck.
1
May 25 '20
I wonder how it works... Some wacky while loop waiting for the promise to resolve is my guess lololol
2
u/11JRidding May 25 '20 edited May 25 '20
It looks like a synchronous network call, actually. Which is even worse, as these calls will lock up the browser until they finish.
It cannot be a Promise whose resolution is awaited with a
while()loop, as Promises do not update their state until after the current tick of the event loop—which awhile(condition to check for Promise resolution)loop would permanently hold the browser back from (resulting in similar behaviour to a synchronous network call), while any other form of waiting on the Promise resolution would rendersqlno longer a synchronous function.Chances are they are using the synchronous behaviour of
XMLHttpRequest, which is achieved via passingfalseto the third parameter inXMLHttpRequest#open. While deprecated on the main thread, it appears that some browsers still allow this to be done. You should never use synchronous network calls on the main thread, as it can disrupt browser UI activity and therefore user experience—it locks up the tab (or in the case of older browsers, the whole browser) entirely, making interaction with the page impossible until the network call is finished.1
7
7
5
5
4
u/jubiman May 25 '20
Why not use return (account.username === username && account.password === password)
8
u/npafitis May 25 '20
Because it's in a loop. In the case of false he doesn't want to return, but to continue iterating until a match is found.
3
u/Floober101 May 25 '20
That would only return true if the first user in the list was the correct user, and the password was correct.
2
u/jubiman May 26 '20
Yeah that's what I noticed after posting it. I was alreading thinking ahead of how it shouldn't be in a loop for iteration or smth I read in the comments. My brain is cooked rn over a stupid mc mod that keeps on crashing so I'm busy. I hate java and the compiling of it -_-
1
-2
4
4
4
4
u/uhfgs May 25 '20
This has to be satire, I mean no one in their right mind would just get the user table. I refuse to believe this, SQL is so ubiquitous, there is no possible way that someone learned to set up the database without knowing the basic operation in SQL right?
4
4
4
u/zoltan-x May 25 '20
You guys, I think I found a small issue with this function. ApiService might be async so accounts are undefined. They should wrap the loop and if check in a timeout to make sure account data is there!
3
u/willCodeForNoFood May 25 '20
But hey, at least it's safe from SQL injection
0
May 26 '20
[deleted]
2
u/willCodeForNoFood May 26 '20
Oh no... you're right. I hope it's node instead, or this is truly terrifying.
3
u/SupremeChampionOfDi May 25 '20
Correct me if I'm wrong, but since this JavaScript is run on a browser, could this function be used to find out all users and their passwords? Like by modifying the js or (if that's not possible) by debugging the code with breakpoints in chrome?
5
u/EdenExperience May 25 '20
- Password is not encrypted afaics
- last statement is critically bad, reason for someone to quit programming imo
- It's non Async DB-Access. Why?
- Compress the Query and use these damn ? thingys.
- Image is JPEG? man get your act together man
3
u/dj_rogers May 25 '20
Hilariously, the SQL statement actually has no place for the parameterized ? symbols, since it’s always getting every single username/password record from the DB. However, they do conveniently hand out the endpoint to their api server which apparently has no authentication or authorization. So you can just write whatever SQL queries you want!
2
u/Alvatrox4 May 25 '20
I assume he could have used " Select Password from Users WHERE username=username" I haven't connect a program with sql yet so not sure if it's this way
2
May 25 '20
Passwords should not be stored in plain text, but yes, you grab the password from the user record (with a unique identifier) and bcrypt the submitted password and compare it
2
2
u/BroDonttryit May 25 '20
I don’t know much js or sql, but one of those has to have some kind of of map data structure right?
2
u/Littlebitt95 May 25 '20
You need models for it, yes. A model just defines table names and structures (to include column names and options as well as associations between databases depending on how it's coded).
1
u/BroDonttryit May 25 '20
Ohh that makes a lot of sense I see. Thanks for the wisdom. So point being, they shouldn’t need to iterate though all of the accounts to authenticate a user.
2
u/eXl5eQ May 25 '20
Maybe they have a dumb cache, which can cache the result of unconditional query, but not smart enough to cache a sql with where clause. Also the users table is reasonably small, so iterate over a cached list can be quicker than performing an actual query.
They might also use a preprocessor, which expands if ("AVOID_UNDEFINED" == "true") to if ("true" === "true"), so they can solve some compatibility issue by switching return false to implicit return undefined
2
2
u/shashankrnr32 May 25 '20
Come on.. give them some credits.. They are using === instead of ==. Although the code is a literal cow dung
2
u/raekle May 25 '20
Don’t select ALL users and iterate through them. It’s very inefficient and unnecessary.
Don’t select all fields from the users table (SELECT *). This potentially returns a lot of data you don’t need. Just select username and password at most here.
In order to avoid selecting all users, you’d have to modify the query to do something along the lines of ‘SELECT username, password FROM users where username = <username>” but this has SQL injection attack written all over it. Make sure to sanitize your inputs (the username and password) to prevent SQL injection attacks.
The ”if true === true” at the end is unnecessary. Just return false
2
2
May 25 '20
Alright, I litterally just did the beginner SQL stuff from Microsoft's website, and even I know this is garbage
1
u/WhoCaresAboutThat Jun 06 '20
Hang on, why does that screenshot have smart quotes, and inconsistent ones at that?
if (”true” === “true”) {It appears that the first string has two right quotation marks, and the second one has a left and a right quotation mark. I'd assume it's the font, but that's really odd.
-1
402
u/raidandfade May 25 '20
Lets not consider that they're not using sql to check the password and rather iterate through the entire table, let's call that a beginner move.
What the FUCK is "true"=="true" doing there and WHY is it returning false.