Reputation: 3
Hay Guys,
i have a problem.
Is there any way to prevent client-side manipulation on HTML data-attributes.
My problem is, if someone is clever enougth to look at the source code and changing the attribute, for example im using jQuery/ajax to get the value of my data-attribute and send it with ajax to my controller. My controller now starts to look with switch case is matching with my data i got out of my data-attr.
But if i change the data-attr value in something that doesnt even exist it shows my the default case.
Is there any way i can prevent / avoid this problem.
var saveAttr = jQuery('#example').attr("data-attr");
jQuery.ajax({
url: "forexample",
method: "POST",
data: { action: "someCaseinMyController", saveAttr:saveAttr },
}).done(function (response){
jQuery("#someDiv").html(response);
});
Upvotes: 0
Views: 634
Reputation: 1482
Short answer: no.
Longer answer: Turn your switch case values into a lookup map or an array, and test the value as it comes in so you can throw an error before it reaches your switch case.
That can be done like this:
const arrayOfCases = ['name', 'email', 'city', 'state', 'address']
if(arrayOfCases.includes(userInput)) {
// your switch case here
} else {
// handle invalid data
}
Upvotes: 1