0

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);
});
1
  • 1
    Is there any way to prevent client-side manipulation on HTML data-attributes? => no. Commented Oct 9, 2019 at 13:29

1 Answer 1

1

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
}
Sign up to request clarification or add additional context in comments.

1 Comment

Oh, thats realy nice idea !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.