Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent your JavaScript code from being stolen, copied, and viewed? [closed]

I know its impossible for 100% protection, but something high or that works for majority of the users.

For instance, I encountered a site where viewing the current page's source returned nothing.

In another case, accessing or trying to download the .js files itself from browser

http://gget.com/somesecret.js,

would redirect you and stuff.

If you obfuscate your code, will it be very very difficult to decode it? if so that is also another good solution (what software is recommended) ?

like image 726
wghwh Avatar asked Nov 02 '09 08:11

wghwh


People also ask

Can people see your JavaScript code?

As other have said, there is no way to protect JavaScript intended to run in a browser from a determined viewer. If the browser can run it, then any determined person can view/run it also.

Why is JavaScript susceptible to theft?

One of the most sneaky uses of JavaScript is cross-site scripting (XSS). Simply put, XSS is a vulnerability that allows hackers to embed malicious JavaScript code into an legitimate website, which is ultimately executed in the browser of a user who visits the website.


1 Answers

It's simply not possible.

For a visitor's browser to be able to execute the script, they have to be able to download it. Not matter what trickery you try to pull with JS, server permissions etc., at the end of the day they can always just wget http://example.com/yourcoolscript.js. And even if they can't (e.g. you require "secret" headers for that request) that would likely inhibit the behaviour of most browsers, while not stopping a determined person from looking anyway.

Fundamentally, because JS is executed client-side, the client must have access to the "original" JS file.

One minor thing you can do is obfuscation, which can help a little bit. But since JS is interpreted, it's also its own deobfuscator - see one of my earlier answers for an example.

Basically - "if you build it, they will look". :-)

like image 168
Andrzej Doyle Avatar answered Sep 24 '22 05:09

Andrzej Doyle