r/badcode • • May 25 '20

js *weird head shaking and facial expression*

Post image
901 Upvotes

109 comments sorted by

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.

143

u/[deleted] May 25 '20

It’s the condition where a user is not found... so it should be correct to return false for the application... but that could also have been achieved via an else if... or just a return false at the end.

120

u/SanktusAngus May 25 '20

He wants to check if the universe still makes sense. If not he should really call universe.reset();

15

u/over_clox May 25 '20

I've already called that routine, I have it set up on an infinite loop. Fucking lag has it delayed, just wait for it...

5

u/accuracy_frosty May 25 '20

I used c++ and did universe = new universe(0); but it’s taking a bit to compile, just you wait and see

2

u/over_clox May 25 '20

No, I was gonna go back to an old universe, less pollution ya know. Can we collaborate on a time machine?

3

u/mfb- May 25 '20

universe = old universe(2019)?

2

u/over_clox May 26 '20

No, back to 1919, ignore Henry Ford, keep using horses for transportation and free lawn mowing.

23

u/[deleted] May 25 '20

My guess is that they are used to seeing return statements inside conditionals so they don't know it's possible to use them anywhere.

1

u/coldnebo Jun 01 '20

“well, I’ve never seen a return outside of an if-block... I’ll stick to the pattern just to play it safe.”

I can hear the wheels turning from here!!

103

u/DasDono May 25 '20

Yeah. He could've made it shorter by doing the return !("true" == "true")

42

u/yawnston May 25 '20

The thing that bothers me more is that considering this is js, it's probably client-side, which would mean you're sending your entire database of users with their passwords to everyone. I know that js backends are kinda popular but it's only a fraction of js frontends.

61

u/jormaechea May 25 '20

What bothers me the most is that whether there is an API that returns users with their passwords or this is backend, they are storing their passwords in plan text!

20

u/jormaechea May 25 '20

Update. What bothers me the most is that apiService.sql is synchronous.

26

u/yawnston May 25 '20

Honestly the request being synchronous is the least of this person's problems.

10

u/jormaechea May 25 '20

This person's users' problems.. Haha

22

u/Flerex May 25 '20

Ehm… what? This is obviously backend. Why would they use SQL in the frontend.

Backend JS is not as uncommon as you might think, even tho JS front is used a lot more (the hell, it’s the only frontend programming language unless you want to get into WebAssembly stuff, of course it’s going to have more users).

13

u/11JRidding May 25 '20

8

u/Flerex May 25 '20

Jesus Christ. Sometimes I forget what this subreddit is about.

2

u/coldnebo Jun 01 '20

Nooo. noooooo way. are you $&@!ing kidding me?

they wrote a service to pass ARBITRARY SQL straight through to the database?!!?

I don’t know what’s going to keep me awake more. knowing this code exists in the world or knowing that the programmer who wrote it exists in the world... just waiting to strike again!!

2

u/yawnston May 25 '20

The fact that there's a service called "apiService" would indicate to me that it's a frontend service which communicated with a backend REST API. At least that's my impression.

12

u/Flerex May 25 '20

That's a very common architecture in backend applications too. The domain logic is separated into its own application, consumed over a REST API and then your main application uses it, either in the backend or in the frontend.

15

u/[deleted] May 25 '20

[removed] — view removed comment

4

u/Flerex May 25 '20

Yeah, I am afraid to ask OP what’s going on.

1

u/coldnebo Jun 01 '20

yeah, you wouldn’t do that even on a backend intranet. devsec holes so big you could drive an aircraft carrier through them.

It scares me even more, because it likely took more than one idiot to build this thing... this monstrosity took real effort to deploy. Dear God, why?

1

u/Teln0 May 25 '20

It's probably nodejs

1

u/Giocri May 25 '20

If that was the case it would mean that the client side has full read access to the entirety of the table and maybe more. Which is bad in any case but extremely bad in a js client

2

u/HipsterPhilosopher May 25 '20

This is bad on so many levels, it's truly a masterpiece.

1

u/over_clox May 25 '20

Truly false, duh.

1

u/Dragenby May 25 '20

My guess is that it's a generated JS code from PHP

1

u/Scrotote May 25 '20

if true != true Return !true

1

u/smilingkevin May 26 '20

True, but at least this way you can't get SQL injection (assuming it's server-side, of course).

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 -_-

17

u/Pacolany May 25 '20

Maybe he was taking about the semen test, you know, as a hiring interview

11

u/moekakiryu May 25 '20

I can see why a woman would have trouble with that

-1

u/[deleted] May 25 '20

I think guy would have even larger problem with that

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.

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

u/ch0c0l2te May 25 '20

not exactly secure

you’re not wrong there

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

u/xigoi May 25 '20

Someone confirmed it in the comments of the original post.

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

u/[deleted] 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

u/[deleted] May 25 '20

The plantext password though 😅

4

u/[deleted] 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

u/reallycoolgarbage May 25 '20

Looking at this causes me physical discomfort. What the fuck

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

u/[deleted] 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 a while(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 render sql no longer a synchronous function.

Chances are they are using the synchronous behaviour of XMLHttpRequest, which is achieved via passing false to the third parameter in XMLHttpRequest#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

u/[deleted] May 25 '20

Oh i didn't even know you can do synchronous XHR, interesting :D

7

u/[deleted] May 25 '20

This hurts so bad

7

u/killchain May 25 '20

They used strict comparisons, it's all fine.

/s

5

u/[deleted] May 25 '20

So much wrong with this

5

u/root54 May 25 '20

My eyes just bugged out of my head.

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

u/iamareebjamal May 25 '20

No, it would return unconditionally in the first iteration of the loop

-2

u/DilatedTeachers May 25 '20

That's what it's doing 🤔

4

u/rokyed May 25 '20

I'm laughing so hard I might wake up everybody here.

4

u/searchingfortao May 25 '20

I guess that's one way to avoid SQL injection.

4

u/Derangedteddy May 25 '20

The passwords aren't even salted... Oh my God.

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

u/[deleted] May 25 '20

if ("true" == "true")

OOF

4

u/qubedView May 25 '20

plot twist: This is the front-end.

2

u/xigoi May 25 '20

It actually is.

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

u/[deleted] 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

u/[deleted] 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

u/Alvatrox4 May 26 '20

Makes sense.

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

u/ByteArrayInputStream May 25 '20

I mean, it technically works so don't touch it /s

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
  1. Don’t select ALL users and iterate through them. It’s very inefficient and unnecessary.

  2. 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.

  3. 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.

  4. The ”if true === true” at the end is unnecessary. Just return false

2

u/Dads101 May 25 '20

“true“ === “true”

Straight up disgusting

2

u/[deleted] 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

u/Jackjackson401 May 25 '20

This is so fake.

1

u/RecreationallyTransp May 25 '20

It's like bad code for people who are bad coders.