What is the correct way to detect whether string inputs contain HTML or not?

前端 未结 13 747
旧时难觅i
旧时难觅i 2020-12-23 15:08

When receiving user input on forms I want to detect whether fields like \"username\" or \"address\" does not contain markup that has a special meaning in XML (RSS feeds) or

13条回答
  •  情书的邮戳
    2020-12-23 15:30

    I am certainly not a security expert, but from what I gather something like your suggested

    if (htmlspecialchars($data, ENT_NOQUOTES, 'UTF-8') === $data)
    

    should work to prevent you from passing on contaminated strings, given you got your encoding right there.

    XSS attacks that don't require '<' or '>' rely on the string being handled in a JavaScript block right there and then, which, from how I read your question, is not what you are concerned with in this situation.

提交回复
热议问题