Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent HTML form action from being changed

I have a form on my page where users enter their credit card data. Is it possible in HTML to mark the form's action being constant to prevent malicious JavaScript from changing the form's action property? I can imagine an XSS attack which changes the form URL to make users posting their secret data to the attacker's site.

Is it possible? Or, is there a different feature in web browsers which prevents these kinds of attacks from happening?

like image 982
Michał Fronczyk Avatar asked Sep 19 '11 19:09

Michał Fronczyk


People also ask

How do you stop form from resetting on submit?

You can use preventDefault method of the event object. Show activity on this post. Show activity on this post. stopPropagation should have no effect.

How do I stop form action in HTML?

The simplest solution to prevent the form submission is to return false on submit event handler defined using the onsubmit property in the HTML <form> element.

How do I stop default form action?

The preventDefault() method cancels the event if it is cancelable, meaning that the default action that belongs to the event will not occur. For example, this can be useful when: Clicking on a "Submit" button, prevent it from submitting a form. Clicking on a link, prevent the link from following the URL.

How do I stop Enter key from submitting the form?

Disallow enter key anywhere on("keydown", "form", function(event) { return event. key != "Enter"; }); This will cause that every key press inside the form will be checked on the key .


2 Answers

This kind of attack is possible, but this is the wrong way to prevent against it. If a hacker can change the details of the form, they can just as easily send the secret data via an AJAX GET without submitting the form at all. The correct way to prevent an XSS attack is to be sure to encode all untrusted content on the page such that a hacker doesn't have the ability to execute their own JavaScript in the first place.


More on encoding...

Sample code on StackOverflow is a great example of encoding. Imagine what a mess it would be if every time someone posted some example JavaScript, it actually got executed in the browser. E.g.,

<script type="text/javascript">alert('foo');</script>

Were it not for the fact that SO encoded the above snippet, you would have just seen an alert box. This is of course a rather innocuous script - I could have coded some JavaScript that hijacked your session cookie and sent it to evil.com/hacked-sessions. Fortunately, however, SO doesn't assume that everyone is well intentioned, and actually encodes the content. If you were to view source, for example, you would see that SO has encoded my perfectly valid HTML and JavaScript into this:

&lt;script type="text/javascript"&gt;alert('foo');&lt;/script&gt;

So, rather than embedding actual < and > characters where I used them, they have been replaced with their HTML-encoded equivalents (&lt; and &gt;), which means that my code no longer represents a script tag.

Anyway, that's the general idea behind encoding. For more info on how you should be encoding, that depends on what you're using server-side, but most all web frameworks include some sort of "out-of-the-box" HTML Encoding utility. Your responsibility is to ensure that user-provided (or otherwise untrusted) content is ALWAYS encoded before being rendered.

like image 170
jmar777 Avatar answered Oct 04 '22 21:10

jmar777


Is there a different feature in web browsers which prevents these kinds of attacks from happening?

Your concern has since been addressed by newer browser releases through the new Content-Security-Policy header.

By configuring script-src, you can disallow inline javascript outright. Note that this protection will not necessarily extend to users on older browsers (see CanIUse ).

Allowing only white-labeled scripts will defeat most javascript XSS attacks, but may require significant modifications to your content. Also, blocking inline javascript may be impractical if you are using a web frameworks that relies heavily on inline javascript.

like image 32
Brian Avatar answered Oct 04 '22 21:10

Brian