Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can the client modify react component state?

Tags:

reactjs

I'm building an admin page for an application and have a state value 'authenticated' that flips from 'false' to 'true' after a successful login (which is authenticated on the server) which then shows the actual admin panel.

Are component state values safe from tampering by the client? Basically, if the client can modify my 'authenticated' state value to 'true', they can skip the login and go straight to the admin panel (which I obviously don't want).

I read that React Dev Tools allows the client to modify values yet everyone says "validate on the server" but I am validating on the server and updating my state accordingly, if the user is approved. If it is not wise to have a state value manage this, what is the right way to conditionally show the admin page after a successful, server-side authenticated login?

I think this is an important question since tampering with state values in a React app can have huge negative consequences on data integrity within an app/database.

like image 413
HaulinOats Avatar asked Feb 21 '18 17:02

HaulinOats


People also ask

Can user change React state?

Unfortunately, unless you're already using Web Sockets or depending on a session, it will likely require a lot of changes.

What method is used to modify States in React component?

To update state , React developers use a special method called setState that is inherited from the base Component class. The setState method can take either an object or a function as the first argument.

Can React state be changed in browser?

If you want to update your state in the browser— you can! Modify it by clicking and editing state attributes in the React tab. This will re-render the DOM, passing the state down through the props.


Video Answer


2 Answers

TL;DR: Either require an authentication token with every request or require authentication through a session.


Never trust users always. One potentially big issue is if you "hide" admin actions behind the admins page without requiring authentication.

For example, assume the backend server uses a REST API to accept commands. In the admin panel you get links to administrative actions like a button 'Delete Everything' that sends a DELETE request to server.net:8080/api/admin/everything without requiring any authentication. If you're a user, you can find that in the code potentially and then send a DELETE request to that address from anywhere without any repercussions.

We'd never give administrative privileges to anyone who would want to delete everything... Because we'll never untrust someone. Right?

Worse, someone might find the server and fuzz some inputs to it, and oops! They manage to delete everything (or even worse, GET everything stored in the database). This wouldn't be hard to do, especially if the server you use to authenticate is the same server you use to issue commands. History has proven "security through obscurity" to be a very bad paradigm. Every action should be authenticated, even if it seems like the actions will be hard to find.

Generally, providing a JSON web token or some other form of authentication token and having the user send that with every request is a good start at least, especially if it has an expiration date. The token would be provided through a separate request with valid credentials.

Sending a token with every single request obviously isn't ideal. There are a couple of other things to try. For servers using PHP, you can probably trust sessions (though very many people who know more than me would probably disagree). In more modern cases, you could try to use Web Sockets, requiring the token after connection. Then only after authentication with the token do you allow the user to make administrative requests.

That way, even if a user knows the exact command they can send to perform any action, the server won't let them without a current session or token. Unfortunately, unless you're already using Web Sockets or depending on a session, it will likely require a lot of changes. I'd consider this to be critical though.

like image 178
Kevin Hoerr Avatar answered Nov 15 '22 04:11

Kevin Hoerr


It is always possible to tamper values in the front-end, there is no way you can rely solely on the front end to ensure security.

Your best approach is to implement some form of authentication and authorization on your backend. In this way, even is some users pretend to be admin, they will be blocked when you do the next request to the server.

Perhaps if you can send more information regarding your problem, we can think of a more specific solution.

like image 44
Anderson Luiz Ferrari Avatar answered Nov 15 '22 03:11

Anderson Luiz Ferrari