Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Editing and Saving user HTML with Javascript - how safe is it?

For example I have a Javascript-powered form creation tool. You use links to add html blocks of elements (like input fields) and TinyMCE to edit the text. These are saved via an autosave function that does an AJAX call in the background on specific events.

The save function being called does the database protection, but I'm wondering if a user can manipulate the DOM to add anything he wants(like custom HTML, or an unwanted script).

How safe is this, if at all?

First thing that comes to mind is that I should probably search for, and remove any inline javascript from the received html code.

Using PHP, JQuery, Ajax.

like image 300
Nick Zinger Avatar asked Apr 26 '12 08:04

Nick Zinger


2 Answers

Not safe at all. You can never trust the client. It's easy even for a novice to modify DOM on the client side (just install Firebug for Firefox, for example).

While it's fine to accept HTML from the client, make sure you validate and sanitize it properly with PHP on the server side.

like image 78
Nadh Avatar answered Oct 04 '22 14:10

Nadh


Are you saving the full inline-html in your database? If so, try to remake everything and only save the nessesary data to your backend. ALL fields should also be controlled if they are recieved in the expected way.

All inline-js is easily removed.

You can never trust the user!

like image 42
Gustav Westling Avatar answered Oct 04 '22 13:10

Gustav Westling