Take the 2-minute tour ×

With all the recent hype about JavaScript and HTML5 replacing Flash, I wanted to know - How would it be possible to protect client-side js code? Of course, it is possible to obfuscate it, but that would only make it a little harder. Also, for games which submit high scores to the server, wouldn't it be incredibly easy to modify those scores before they are sent to the server? I know even Flash files can be decompiled, but they can be obfuscated and flash decompilation is not as easy as modifying data in JS - could be done easily using a plugin such as Firebug. I'd like to know everyone's views on this.

share|improve this question
add comment

2 Answers

up vote 8 down vote accepted

Javascript, being parsed on the client, is never 100% safe. There will always be ways to find out what it does. A few days ago I've even seen a tool which unpacks packed javascript so the only thing you can really do is using "ugly" variable names (or actually, make a javascript packer transform your "good" variable names into short/ugly/nonsense ones)

To protect game results, you have to move some of the game logic to the server so the client cannot send arbitrary results.

Summarizing it: Don't put secrets in javascript code and don't trust anything coming from the client - no matter if it's from a form or generated/submitted via javascript.

share|improve this answer
Thanks. Even if I do move some of the game logic, the algorithm to generate data to be sent to the server is still very open. Also, wouldn't this be a big factor in the adoption of HTML5 especially with regard to games? Why/How are people predicting that flash is on it's way out? –  BlueSilver May 10 '10 at 7:49
Flash is almost as insecure as javascript. There are good actionscript decompilers out there and even if that wouldn't be possible, you could still sniff the request sent to the server. Besides that, lots of flash games are just for fun, not for rewards etc. so it doesn't matter at all if people cheat there or not. –  ThiefMaster May 10 '10 at 7:54
Sorry for asking this repeatedly, but wouldn't this be a huge factor for HTML5 adoption? Also, sniffing out the request isn't as easy as viewing the source. Plus, There are flash game portals where getting high scores could fetch you money, so cheating would matter there. –  BlueSilver May 10 '10 at 8:05
Those sites will most likely stay with flash or add sanity check to the server-side. For example (depending on the game of course) you could ensure there's enough time between the game start and end. And obviously only sensible scores should be accepted - i.e. rejecting unnaturally high scores. Besides that: someone who wants to cheat on such a site to get more money/rewards WILL sniff. It might be even easier if there's no sanity check currently because of a developer relying on data from flash being trustworthy than with more readable code whose response isn't simply accepted as valid. –  ThiefMaster May 10 '10 at 8:15
add comment

You say that for game that sends high scores to the server it would be too easy to modify javascript and forge request?

Except for case, when you use some cryptography on the client, it is the easiest way to forge such request not even analysing the script but sending false request itself. Everything you send between server and browser can be easily viewed on computer, analysed and changed.

share|improve this answer
add comment

Your Answer


