eval()

Why eval js inbuilt function is not recommended to use ??
65 Replies
ἔρως
ἔρως7d ago
eval allows you to execute a string as code, in the same context as the code outside the eval() this can be extremely bad, specially if what you pass to eval() comes from an url for example, a fetch
Vandana
Vandana7d ago
ya in the case of fetch we cant use.
ἔρως
ἔρως7d ago
and it's not just that this can lead to executing malicious code without you knowing and that code can have access to information that's supposed to be private (aka - not accessible to the "public")
Vandana
Vandana7d ago
OMG!
ἔρως
ἔρως7d ago
eval is a huge opportunity for security issues
Vandana
Vandana7d ago
But then its states in mdn it returns a completion value,never they mentioned its bad idea.So how will a person know which are the inbuilt functions not to be used??
Jochem
Jochem7d ago
No description
Jochem
Jochem7d ago
what are you talking about, there's a giant red warning box at the top of the MDN page on eval?
ἔρως
ἔρως7d ago
if you REALLY need to use eval, STOP you're doing something wrong eval is a danger can you show an example of where you used eval?
Vandana
Vandana7d ago
onclicking equal to
ἔρως
ἔρως7d ago
you don't need eval to do that
Vandana
Vandana7d ago
there might be diff approaches. but i went with a very basic one. we cant use eval for this simple thing as well?
ἔρως
ἔρως7d ago
it seems """"safe-ish"""" but i wouldn't risk it
Jochem
Jochem7d ago
Calculators are also terrible beginner projects They're very hard to do properly and teach you very bad habits if you do them the easy way (which is to use eval)
ἔρως
ἔρως7d ago
it's also extremelly hard to make eval reasonably ok to use without worrying
Vandana
Vandana7d ago
Got it . Thanks for the support.
ἔρως
ἔρως7d ago
im trying to find an example, i saw a while ago
ἔρως
ἔρως7d ago
https://www.youtube.com/watch?v=PZ-H099IaWo <-- here's an example of a real-life exploit because of a bug on cleaning up data to be passed to eval in another language (perl)
Daniel Boctor
YouTube
MAJOR EXPLOIT: GitLab was Hacked with an IMAGE??
Try SquareX for free today! 👉 https://sqrx.io/db_yt In this video, we take a deep dive into the GitLab / ExifTool metadata parsing vulnerability, which enables attackers to gain access to GitLab servers via an RCE (remote code execution). Whether you're a pen tester, security researcher, or cyber security expert, having a solid foundation in es...
Vandana
Vandana7d ago
OMG.
ἔρως
ἔρως7d ago
"omg" indeed this is why people should avoid eval (and it's concepts) like the plague
Vandana
Vandana7d ago
great .ill keep this in mind.
ἔρως
ἔρως7d ago
just so you can understand how bad this is, the result is that anyone can execute commands in the server this means, steal data, access to things they shouldn't access, infect the machine (and maybe others) to do anything, including distributing viruses and malware all because of eval (or the equivalent) that server is compromised and should be burned in a fire now you see how bad it can get?
Vandana
Vandana7d ago
yes.
ἔρως
ἔρως7d ago
in short: eval is evil
Aoi
Aoi6d ago
There are libraries of evaluating math equations. Though as already mentioned. A calculator as a learning project is a really bad idea. It is a project which looks really simple which causes new people to get to use many bad code patterns.
Jochem
Jochem6d ago
and almost all of the skills you learn (other than building the layout maybe) are entirely useless in most other programming projects
Vandana
Vandana6d ago
come again.
Jochem
Jochem6d ago
the hard parts of building a calculator properly in javascript require skills that are not really used getting the buttons to work properly without eval requires logic that is only really useful to make buttons on a calculator work properly without using eval. There's much better projects to learn DOM manipulation
Aoi
Aoi6d ago
If you are making a calculator on a very serious level, almost all of the skills will be related to backend / algorithms more than the frontend.
Vandana
Vandana6d ago
yea got it.I just tried eval with a simple one. Is it?
Aoi
Aoi6d ago
At least if I make a calculator app, the most important things will be parsing the math equations and managing the history. Most of this will include algorithms which are not often used in frontend
Vandana
Vandana6d ago
I guess both parsing the equation and keeping the previous result can be done in frontend. Correct me if i am wrong.
ἔρως
ἔρως6d ago
it's something you almost never do
Vandana
Vandana6d ago
simple basic calculator can be done in frontend right?
ἔρως
ἔρως6d ago
yes but the input parsing is something you almost never do and i mean "almost never"
Vandana
Vandana6d ago
ya thats what using eval() parsing can be done but its dangerous.
ἔρως
ἔρως6d ago
yes i did made the beginnings of a calculator without eval but, again, it's something you will only use for a calculator
Vandana
Vandana6d ago
?
ἔρως
ἔρως6d ago
the skills you get from making a calculator usually only apply to making a calculator and don't advance you in any other aspect
Vandana
Vandana6d ago
but we will atleast learn something .00001%
ἔρως
ἔρως6d ago
eh
Vandana
Vandana6d ago
And managing the history i mean storing previous results can be done in f.E
ἔρως
ἔρως6d ago
that's still something you apply only for a calculator
Vandana
Vandana6d ago
then what is not used in F.E here?
ἔρως
ἔρως6d ago
you're mixing "can be done in front end" with "will be used in front end" just because if can be done doesn't mean you will use it outside the calculator
Vandana
Vandana6d ago
algo which are not often used in f.e if something similiar comes out of the box then we can use right .eg:storing previous results
ἔρως
ἔρως6d ago
again, just because you can use doesn't mean you will in a calculator, EVERYTHING you do IS USED IN FRONT END, but almost everything won't be used anywhere else
Vandana
Vandana6d ago
okay cool got it
ἔρως
ἔρως6d ago
i still like it as a mental exercise
Vandana
Vandana6d ago
?
ἔρως
ἔρως6d ago
like, how to implement receiving data and not using much parsing
Vandana
Vandana6d ago
ya ya . Just a yes or no question is for ....in loop just used for objects??
ἔρως
ἔρως6d ago
it's not really used anymore
Vandana
Vandana6d ago
ok thank you
ἔρως
ἔρως6d ago
now-a-days, for ... of ... is used
Vandana
Vandana6d ago
ONLY for objects?
ἔρως
ἔρως6d ago
and arrays too which are objects EVERYTHING is an object in javascript 5 <-- object true <-- object "hi" <-- object null <-- "object" if you need to go through an object, use this:
for (const [key, value] of Object.entries(object1)) {
console.log(`${key}: ${value}`);
}
for (const [key, value] of Object.entries(object1)) {
console.log(`${key}: ${value}`);
}
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries but this is getting off-topic way back when, you used to need to use eval to access properties by string
Vandana
Vandana6d ago
but we cant use that eval
ἔρως
ἔρως6d ago
yes, but it was used way back then good thing we learned to do not use it and have the syntax to do not use it
Aoi
Aoi6d ago
If you are doing a project for learning, it's not just about "it is possible to do it in frontend" it's more of "will i ever do it in frontend in an actual product.". In my entire journey I never had to use any algorithms used in a calculator for any real project. That's why so many professional developers say it's a bad project as a starter. Just read the whole chat, @ἔρως already covered everything....
Vandana
Vandana5d ago
yes got it. So suggest projects that covers algo/steps used in real project as well as for starters
ἔρως
ἔρως5d ago
depends a lot on what you want to do
Vandana
Vandana5d ago
Suggession/idea please
Jochem
Jochem5d ago
there's some project ideas in #discussions if you want to go looking for them, or you can make a post there asking for projects. You will have to include some more info that "suggestion/idea please" though, maybe a couple you've already made, what level of complexity you want, and what your eventual goal is going to be (become a frontend dev, or just as a hobby, learn backend eventually...)