javascript - Encoding user input to be stored in MongoDB -
i'm trying determine best practices storing , displaying user input in mongodb. obviously, in sql databases, user input needs encoded prevent injection attacks. however, understanding mongodb need more worried xss attacks, user input need encoded on server before being stored in mongo? or, enough encode string before displayed on client side using template library handlebars?
here's flow i'm talking about:
- on client side, user updates name "<script>alert('hi');</script>".
- does need escaped "<script>alert('hi');</script>" before sending server?
- the updated string passed server in json document via ajax request.
- the server stores string in mongodb under "user.name".
- does server need escape string in same way safe? have first un-escape string before escaping not double on '&'?
- later, user info requested client, , name string sent in json ajax response.
- immediately before display, user name encoded using _.escape(name).
would flow display correct information , safe xss attacks? about unicode characters chinese characters?
this change how text search need done, search term may need encoded before starting search if user text encoded.
thanks lot!
does need escaped
"<script>alert('hi');</script>"before sending server?
no, has escaped before ends in html page - step (5) above.
the right type of escaping has applied when text injected new surrounding context. means html-encode data @ moment include in html page. ideally using modern templating system escaping automatically.
(similarly if include data in javascript string literal in <script> block, have js-encode it; if include data in in stylesheet rule have css-encode it, , on. if using sql queries data injected strings need sql-escaping, luckily mongo queries typically done javascript objects rather string language, there no escaping worry about.)
the database not html context html-encoding input data on way database not right thing do.
(there other sources of xss injections, commonly unsafe url schemes.)
Comments
Post a Comment