1

Introduction

I'm currently working on a project called draw.js. The idea behind the project is to have an online code editor linked to a drawing space so you can draw using code.

I got this idea when I first discovered the two.js library. I wanted to get started drawing right away but couldn't since I had to set up files etc. first. Setting up the files once and hosting it online with the code editor right there next to it made it much easier to get started and on top of that, I could do it from anywhere I wanted (school, home etc.) without having something like a usb stick handy.

Having the code editor right there also made getting the results much quicker. So only big plus points there.

I had to figure out a way to actually run the code I put into the code editor part of my web-app. I did some research online and found out the easiest solution would be to take the code, put the string in a variable and then pass the variable into the eval() function built into java-script.

My problem

This solution works flawlessly and it was super easy to set up. However, research online, along with common sense tells me that this is a really insecure solution.

You can basically put in any valid js and it will run.

Now this is a problem for stuff like $("body").html(""); but this is not the end of the world. Editing the html like that is powerful, but only client side. You can practically do this by opening your dev tools in chrome.

My question

I wasn't able to think of any serious problems with this security flaw myself. However I'm not really a specialist in this field. That's why I'm asking this question.

Are there any real dangers when using eval() in this way? WHAT ARE THOSE DANGERS?

If there are any...

Is there a way to fix this hole in security? Perhaps only allowing access to certain library's?

If there is a way to fix this, please tell me how I would go about fixing it?

2 Answers2

1

Assume I'm a super legit user of your cool web application, authenticated myself, and wrote a cool script you (as developer of the application) want to run. Of course, you as super user can do much more than me as a simple user. And I'm being jealous of your rights.

It shouldn't be too difficult to write a script which, when executed:

  1. Fakes the code input to look like something it is not.
  2. Gets your authentication object and posts it to another server
  3. Get more available information from your system (See https://github.com/Valve/fingerprintjs2)

Next to these privacy issues it can load ads from rogue networks which have the power to inject viruses on your and other user's systems.

Basically just don't trust any users code. If you want to execute user submitted code be careful it only has a limited scope it can access. Such thing (a sandbox) can only alter a specific part of your page. For a specific example of a library you can use to achieve this, see https://github.com/asvd/jailed.

Corstian Boerman
  • 848
  • 14
  • 32
0

My advice would be to setup a whitelist pattern in RegEx. For instance, you may want to not allow javascript commands which contains certain patterns, such as \""|''\ and so on. The list of the whitelist will probably be extremely shorter than a blacklist one.

Alberto Schiabel
  • 1,049
  • 10
  • 23