-3

Let's assume i have created a simple game, which uses several functions, and is not multiplayer. Is it possible to access the function definitions that i wrote, from within the browser?

Also let's say, for instance, there is a function that has a score parameter and sends a "you win" message to the user if his score reaches a certain number. Is it possible to call that function through the console with arguments specified by the user?

If any of the above are possible, then how can i try them?

VlassisFo
  • 650
  • 1
  • 8
  • 26
  • have you tried? – Brian H. Feb 09 '17 at 13:50
  • @Brian I would, but i am not sure how. – VlassisFo Feb 09 '17 at 13:51
  • 2
    yes it is possible – L. Vadim Feb 09 '17 at 13:51
  • have you even written any JS code so far? – Brian H. Feb 09 '17 at 13:52
  • Javascript is client side which means everybody can access the code source – Jesse de gans Feb 09 '17 at 13:54
  • @Brian In what regard?? Yes, i have, but i assume you are referring to the complexity of it, in which case i've only written simple code. – VlassisFo Feb 09 '17 at 13:55
  • write a file with this content: `function saySomething(argument){console.log(argument);}` and type saySomething("something") in the console. that's how simple it is – Brian H. Feb 09 '17 at 13:56
  • @Brian What i meant to say (and i am sorry if i phrased it incorrectly) is if i can call the functions without knowing their name/functionality. If i let a friend play the game on my computer (he has no knowledge of the code) will he be able to find the function names, what they do, and call them so that he can (e.g.) win? – VlassisFo Feb 09 '17 at 14:00
  • Possible duplicate of [Prevent Cheating on Javascript Game](http://stackoverflow.com/questions/7171101/prevent-cheating-on-javascript-game) – shad0w_wa1k3r Feb 09 '17 at 15:34

2 Answers2

2

Is it possible to access the function definitions that i wrote, from within the browser?

Yes.

let's say, for instance, there is a function that has a score parameter and sends a "you win" message to the user if his score reaches a certain number. Is it possible to call that function through the console with arguments specified by the user?

Yes.

If any of the above are possible, then how can i try them?

If the function is a global, just type the name of it.

If it isn't a global, then set a breakpoint somewhere in a scope that has access to that function, do whatever it takes to trigger that breakpoint, then it is as above.

Related: Prevent Cheating on Javascript Game and What good ways are there to prevent cheating in JavaScript multiplayer games?.

Community
  • 1
  • 1
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

Yes it is possible. The question is, how you wrote your code.

If you wrote it like this:

(function(module, window, document, undefined){
    module.public = function(){
        //code here
    }
    function private(){
        //code here
    }
}((window.play = {}), window, document);

You can access the score function with (window.)play.public(); the window. is not necessary. The function privatecould not be accessed outsite the module function.

If your code likes this:

function public(){
    //code here
}
function private(){
    //code here
}

Both functions are availalbe with public(); private();.

How to access your code:

  1. Open the developer tools of the browser.
  2. Open the tab "sources"
  3. Navigate to the script
  4. Look up all the written code
  5. Set breakpoints to modify the data, when a function is called
  6. ...

How to secure your code:

  • You can uglify / minify your code https://jscompress.com/
  • Use a backend for functionality (for a little game it is way overpowered)
  • Google for JavaScript Security Methods
DomeTune
  • 1,401
  • 10
  • 21