How do I properly escape quotes inside HTML attributes?

前端 未结 6 981
遇见更好的自我
遇见更好的自我 2020-11-22 08:31

I have a drop down on a web page which is breaking when the value string contains a quote.

The value is \"asd, but in the DOM it always appears as an em

6条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-22 09:11

    You really should only allow untrusted data into a whitelist of good attributes like: align, alink, alt, bgcolor, border, cellpadding, cellspacing, class, color, cols, colspan, coords, dir, face, height, hspace, ismap, lang, marginheight, marginwidth, multiple, nohref, noresize, noshade, nowrap, ref, rel, rev, rows, rowspan, scrolling, shape, span, summary, tabindex, title, usemap, valign, value, vlink, vspace, width

    You really want to keep untrusted data out of javascript handlers as well as id or name attributes (they can clobber other elements in the DOM).

    Also, if you are putting untrusted data into a SRC or HREF attribute, then its really a untrusted URL so you should validate the URL, make sure its NOT a javascript: URL, and then HTML entity encode.

    More details on all of there here: https://www.owasp.org/index.php/Abridged_XSS_Prevention_Cheat_Sheet

提交回复
热议问题