Ajax security?

When I look back at my codes written earlier, I found something horribly bad. Whenever I want to delete a record in the database, I liked this:

$.post("deleteAction.do",{recordId:10});

      

I cannot prevent an attacker from directly accessing my database url directly:

deleteAction.do?recordId=10 

      

What's the solution for this kind of problem?

+2


a source to share


4 answers


It really depends on your data and the checks you do on the server side. For instance. If you are checking to see if the user is allowed to perform a delete action on this record, this is not such a big problem. If you do not do this, it means that the user can delete the data of other users. My suggestion would be:



  • Add additional server side checks to prevent users from deleting other data and then their own.
  • Instead of using integers, you can also use something like guides or some other identifier that is difficult to change (read: unpredictable). This prevents smart heads from trying to crash your application.
+3


a source


It doesn't matter if you are using Ajax or not. If the URI is doing something sensitive on the server or exposing sensitive data, you need to protect it. Usually with some form of authentication + authorization. For this, the usual cookie-based technique. The specifics of its implementation depend on the design of your backend system.



As an aside, you shouldn't allow GET requests for unsafe actions . Since your request is a POST, but you are using the GET problem example, this assumes you just need to add "Is this a POST request?" check server side script. Note that this would not be sufficient protection in and of itself, a malicious user can make arbitrary POST requests almost as easily as arbitrary GET requests. (Which brings us back to Authen / Authz)

+2


a source


Your server side code should check the logged in user and make sure they have permission to delete files. This will prevent malicious behavior of foreign users, but will require adding some authorization / permissions stuff to your web application.

+1


a source


The only way to prevent this is to use authentication on your site and only allow some users to perform these operations. Any public action will be available to any attacker.

+1


a source







All Articles