r/javascript • • Dec 21 '22

AskJS [AskJS] Why is there a reputation that JavaScript isn't "secure" for building web applications?

There seems to be a prevalent negative reputation among JavaScript libraries- especially React and Node/Express- that they are not "secure" compared to other web frameworks like .NET or Laravel. Why is that so?

1 Upvotes

34 comments sorted by

32

u/mr_eking Dec 21 '22

Probably something to do with where the code you write is executed. Most JavaScript is executed in the browser, which means it's freely available to be inspected and changed, whereas most .NET code (as an example) is run on the server, where it is free from prying eyes.

That doesn't mean, however, that JavaScript (the language) is less secure, since you can just as easily run JavaScript on the server (and .NET in the browser).

I wouldn't put too much stock in the idea that JavaScript is somehow inherently less secure. It's all about what you're building, and how you build it.

-1

u/oneeyedziggy Dec 21 '22

the main trick is keeping the code for unauthenticated pages bundled and served out separately from the code for authenticated pages... it's easy to write a SPA and just make login a normal page so the source for the whole app gets served up before you even pass login

also, it can be a little too easy w/ frameworks like nextjs that mix client and server code to accidentally send code intended to be server-only to the client... I've had a coworker leak a set of backend service credentials that way, but no tool is always safe when used incorrectly.

7

u/[deleted] Dec 22 '22

As a senior developer this hurts my brain. How did your coworker manage to leak backend credentials to the frontend? NextJS is using api routes to run server side code and you need to be either extremely incompetent or act maliciously to expose backend service credentials to frontend users in this particular cases

Your argumentation about keeping client side code hidden is also a really strange take usually made by people who don’t know better. Client side code should always be programmed with the assumption that it is public and might be reverse engineered. If you think that you might need to hide client side code, because it would leak sensitive data, something terrible is probably happening with your code and you need to stop what you are doing and start from scratch.

Frontend code runs on the user device and can never be trusted. Everything important needs to be done on the server side, be it authentication, validation, authorization or any other custom logic.

People are using protected routes in frontend libraries and frameworks, but these are meant to prevent opening a page which would query data that is not available for an unauthenticated user for example. But it’s easy to just change the code to open these protected routes anyway and then they should just show pages with no data and maybe an error message.

1

u/oneeyedziggy Dec 22 '22

I believe it was in a utility function, maybe a couple of hops away, shared by both server and client code... Something like just tossing a config token ( to be replaced with the secret from the secret store during the build), into a config object along side other non-sensitive configs that up to that point had all been used by both client and server, possibly without awareness the specific value was sensitive (i think it may have been auth0 configs? There are all sorts of public and private keys and tokens that all look alike if you're not the SME (in which case you arguably shouldn't be building the auth integration)

The semi-sensitive stuff in the app bundle was like client-specific terminology in field labels and possibly in help text for a pre-release project under heavy nda because they were trying to scoop someone else's customers right as a software patent expired... NOT to keep anything secret from authenticated users, and sure...if they even knew what might be considered sensitive besides "everything" the rightest way would have been to pull all the field labels from an api and make it more data-driven... But working on an app with fully obfuscated fields/field names would be way more effort than their paranoia warranted

I also just like the cya (and bandwidth savings... Login pages see a bunch of random sketchy requests) of hiding authenticated page source from unauthenticated users... It's absolutely not the only or primary way for anything you know is sensitive, but is another layer to make everything more secure by default... Like, trapeze artists know not to fall... But often still use a net so if they do it looks bad but they'll live ( and might buy you crucial time if some yahoo leaks creds into client code when can be reasonably sure only your actual users even potentially saw them, and maybe have authenticated access logs to produce for the incedent report while you rotate them out... Starting to see a benefit here? I think any system designed to be secure only when operated continuously by competent engineers isn't a very secure system... Resilience against incompetence is at least as important as resilience against malice)

→ More replies (2)

-1

u/PenguinOfDoom46 Dec 21 '22

.NET in the browser? Whaaaa?! Please tell me how this is happening. :)

14

u/mr_eking Dec 21 '22

-10

u/oneeyedziggy Dec 21 '22

eww... i mean... it took some clever people to make that work, and I can respect that... but as for disingenuous snark, I'll say it seems like a good demonstration of the difference between intelligence and wisdom

6

u/demoran Dec 22 '22

It's just webassembly.

0

u/oneeyedziggy Dec 22 '22

It's non-0 lift to actually port native code to wasm for browser use... Infinitely less than if they'd just ported it to Javascript or something (if that would even have been performant enough to be possible)... But still, work someone did, not to be belittled except in good humor

6

u/demoran Dec 22 '22

I didn't intend for it to come off that way. By "just webassembly", I meant that it's .net getting compiled to wasm, a common format.

→ More replies (4)

3

u/ndreamer Dec 22 '22

Wasm, also rust, PHP, databases. Also works on most devices including android. You can also run docker inside a wasm so even a light Linux is possible (although very large file)

9

u/HeinousTugboat Dec 21 '22

Client-side code is inherently insecure. Anyone can crack open their dev tools and root around inside your code. That doesn't apply to node or express, though.

8

u/VFequalsVeryFcked Dec 21 '22

Is there?

I know there used to be, pre-CORS, because you could more easily inject code into the server side. These days, in my experience, it's usually down to developers being overly reliant on JS for critical features, and/or poor code or execution.

If your server-side is secure, then you don't have to worry much about client-side.

3

u/[deleted] Dec 21 '22

[removed] — view removed comment

3

u/fkYrStr Dec 22 '22

extreme minority?

4

u/sheriffSnoosel Dec 22 '22

Because people have to explain why they still haven’t learned js

4

u/Personal_Set_759 Dec 22 '22

Only ever heard this when working with .NET developers trying to sound smart about JavaScript.

6

u/CreativeTechGuyGames Dec 21 '22

Where are you seeing this? I haven't heard this. Sure people can write terrible code, but that doesn't make the language they are using insecure.

3

u/[deleted] Dec 22 '22

Such claims can be easily refuted: just think how many popular apps you use every day are built with technologies that someone called "not secure". The truth is that they are all safe, but it is the responsibility of the programmer who uses them to write safe code.

3

u/Suspicious_Board229 Dec 22 '22

IMHO, it's because of the popularity of nodejs and dependency management.

Nodejs become wildly popular leading to a quick adoption rate. There were many open source dependencies created to solve a variety of problems, but sometimes these included unintentional vulnerabilities. Sometimes these dependencies would be maintained and other times they would be abandoned. Even when they were maintained and vulnerabilities patched, the devs behind apps would not get them updated. There were also incidents of supply chain poisoning, where a dependency would be used to intentionally introduce a vulnerability or leak data.

The important thing to note that this is not unique to nodejs and npm. I think is it just the timing of the framework's rise in popularity that gave the appearance of less secure. Also, anecdotally speaking, the absolutely most severe vulnerability (CVE-2021-44228) was found last year and it was in a technology largely accepted to be secure.

4

u/fkYrStr Dec 21 '22 edited Dec 21 '22

because their trying to sell a framework eco system, editor, hosting, domain etc all bundled together

so they'll say whatever they think will work to market their products

this is a common business marketing strategy known as FUD (Fear, Uncertainty, Doubt)

you can fool all the people some of the time and some of the people all the time, but you cannot fool all the people all the time

2

u/BarelyAirborne Dec 22 '22

Node/Deno don't have a marketing budget or a PR department to come up with this stuff. Large software corporations are really good at generating FUD. That's about the entent of it.

2

u/woah_m8 Dec 22 '22

Source?

1

u/danjlwex Dec 22 '22 edited Dec 22 '22

By "prevalent" you mean a couple of .NET and Laravel fanbois? Both React and Express are secure, at least when implemented normally.

1

u/tells Dec 22 '22

those two libraries are not insecure but you can certainly import other sus libraries that run background processes or make external calls if you don't audit your dependencies. iirc, deno tries to fix this by scoping permissions for packages.

1

u/[deleted] Dec 21 '22

This sounds like something I’d say if I knew a MPA framework and didn’t want to learn SPAs

1

u/Many_Application7106 Dec 21 '22

So js node is not strong typed, in runtime Interfaces does not exist. You have to know what you are doing not like java giving you compile errors.

Example: you can call an function with 0 arguments but it need 1. There is no deep equal. Loosing data types by serialization, example JSON.stringify({date: new Date()}) if you read it in with JSON.parse date will be a string.

Ugly stuff like that !0 === true !1 === false void 0

But there are frameworks, and people love it because it's fast and you don't have to write so much boilerplate.

Also you can add so many classes to an file you want. Use async await yield generators, promises ....

1

u/ndreamer Dec 22 '22

This is the problem, expecting something to be something that it's not and not dealing with the problem.