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?
a source to share
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.
a source to share
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)
a source to share