3

is it possible to hide codes written in java script (j query)? i have written a program and i have used two much load() function . every one can see my pages address is it a risk?

something Like this:

   load('account/module/message/index.php');
load('account/module/ads/index.php');
load('account/module/stat/index.html');
Saeid
  • 448
  • 1
  • 7
  • 19
  • 5
    javascript is ran on the client side - so the users browser has to be able to see it... which means the user will be able to see it as well. it shouldn't be a risk as long as your php is secure anyway. – Smern Aug 08 '13 at 12:17
  • hackers cant attack this codes and variables in it? – Saeid Aug 08 '13 at 12:18
  • 3
    Hackers do not need your javascript for anything, they just directly go for the server. The server **must** protect itself. – Esailija Aug 08 '13 at 12:18

8 Answers8

8

No.

JavaScript is client side therefore all code written is, in some fashion, directly visible to the client (end user). You can obfuscate it and make it more difficult to decipher, but in the end is still accessible.

If security is of concern you can keep "business logic" within php and access it using JavaScript (e.g. Ajax calls) but the end points would still be visible.

Brad Christie
  • 100,477
  • 16
  • 156
  • 200
5

On every site that uses Javascript, that javascript code is visible to the end user. Not only that, but the end user is able to debug it, and change the either the variable contents or even the code itself at any moment.

Despite this, millions of sites use Javascript, and many of those sites are considered secure. The point is that while JS code may be visible to the end user, it doesn't necessarily mean your system is insecure. You just have to write your system with the understanding of how it works.

Here are some pointers:

  • If you put secrets (eg passwords or business logic that must be kept private) into your JS code, then those secrets are not secure. Don't do this; keep those details on the server.

  • If your JS code does any kind of validation, then that validation could be bypassed, so your server-side code must also do the same validation.

  • If your JS code makes calls that run code on the server (eg your load(...) calls, then the server must verify that the user has permission to do so; don't rely on the JS code to do that check.

Spudley
  • 166,037
  • 39
  • 233
  • 307
3

You can't "hide" the client-side code, the most you could hope to do is obfuscate it, which to me is largely pointless in the context of the web - code that is delivered to the client should be exposable without being dangerous - and you can hardly obfsucate URLs, anyway.

For parts that shouldn't be exposed, don't expose them. Do server-side generation and output only what is needed, what is "safe"; some trouble can come when mixing the two (say, wanting to hide away logic by doing it on the server, but still deliver it dynamically using AJAX), because your logic is indirectly exposed (that is, although it can't be seen, the results can be gathered, perhaps from a different domain to use your content, etc.)

Grant Thomas
  • 44,454
  • 10
  • 85
  • 129
2

You can try using an Obfuscation Tool like YUI Compressor http://yui.github.io/yuicompressor/

So your code will not be readable for end user... but hidding it it's impossible

Hidding values and stuff

If you want to keep your values private, so user can't read them obfuscation won't be really your choice, but of course your source will be minified, it will be a mess if you want to read it, but it's still there...

So your choice here is use some kind of encryption which will be decrypted when page loads, but it is a hard work, you can use base64, sha1 or whatever you want only the strings or values you want. But anyone can decrypt it if they really want to.

Robert W. Hunter
  • 2,895
  • 7
  • 35
  • 73
2

Definately not, because javascript executed client side so either if possible you do all the operation on server side scripting ( jsp/php/asp) or minify/compress your javascript code after moving it to a sepatate file.

Maneesh Kumar
  • 1,367
  • 2
  • 9
  • 13
1

Unfortunately not.

Javascript runs on the client machine in the web browser and cannot be hidden from someone looking at the source code.

However this does not pose a security risk for your application provided nothing internal is visible should you visit those pages in your browser.

Mike Hancock
  • 230
  • 2
  • 8
1

process all your "secret" code on the server, where the user doesn't have access to the code. Send only "non secret" things to the client, like for the UI. If you can't avoid sending secret code to the client, obfuscate it to make it more difficult to read.

chris.nesbit1
  • 333
  • 2
  • 9
1

Put your Javascript code in external file. And then minified your javscript code, may this helps you.

To Convert Normal Javascript into Minified Javascript Refer this http://jscompress.com/

Manish Chauhan
  • 595
  • 2
  • 7
  • 14