关于将用户提交的内容转义为 html、javascript 和 PHP 的概述、流程图
任何人都可以向我指出有关将用户提交的内容转义为 HTML、JavaScript 和 PHP 的 StackOverflow 良好答案和其他资源(包括书籍)吗?
例如,假设用户在文本框中键入信息,然后单击提交按钮。然后文本由 JavaScript 写入页面上的 div 中,并通过 GET 发送给 PHP,并由 PHP 放入 MySQL 数据库中。
我正在寻找一个良好的、广泛的、但也详细的概述所涉及的所有不同类型的转义。流程图也会有帮助!
谢谢!
谢谢!我正在找人制作一份备忘单,其中包含以下部分:1)转义为 html 显示,2)转义为放入 URL 3),将 URL 发送到 PHP,4)将数据从 URL 插入数据库。每个部分都应包含 1) 有关潜在问题情况和应转义的字符的示例,2) 有关如何转义字符的示例,以及 3) 如何在稍后需要时对字符进行解码。
这样做的好处是提供一站式资源,其中包含许多关于转义的示例和解决方案,以便其他用户不必浏览大量不同的网站、答案和资源,而这些网站和答案以及资源很少有示例和解决方案。我认为那会很棒。
到目前为止,这张图表看起来不错 http://www.the-art-of-web.com/javascript/逃逸/
can anyone point me to good StackOverflow answers and other resources, including books, on escaping user submitted content for HTML, JavaScript, and PHP?
For example, say a user types information in a text box, and clicks a submit button. Then the text is written by JavaScript into a div on the page, and is also sent via GET to PHP, and by PHP is put into a MySQL database.
I am looking for a good, broad, but also detailed overview all the different types of escaping involved. A flowchart would help too!
Thanks!
Thanks! I'm looking for someone to make like a cheatsheet, with sections on 1) escaping for html display, 2) escaping for putting in a URL 3), sending the URL to PHP, 4) inserting data from the URL into a database. Each section should have 1) examples on potential problematic situations and characters that should be escaped, 2) examples on how to escape the characters, and 3) how to decode the characters if necessary later.
The benefit would be a one-stop source with many examples and solutions on escaping so that other users don't have to go through tons of different sites and answers and resources which have few examples and solutions. I think it would be great.
This chart looks pretty good so far
http://www.the-art-of-web.com/javascript/escape/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我自己总是对用户数据使用“POST”,而不是“GET”,下面的讨论反映了这一点,但无论哪种方式,您仍然可以使用我下面所说的大约 90%。所以这里是......
一般规则:在转义数据时不要“提前思考”。只进行立即必要的转变。规范示例:在执行数据库插入时不要转义为 HTML,因为您最终会得到“&”等内容变成“&”经过几次往返之后。
一般规则:始终使用 UTF-8。当有人第一次从包含 unicode 省略号的电子邮件中进行复制粘贴时,您会对此表示感谢。 (您会惊讶于这种情况发生的频率。)所需的典型设置;这可能因 PHP/MySQL/HTML 版本而异:
一般规则:未经清理(尽管正确转义)的用户数据只能进入五个位置:(
如果您想将用户数据放在任何其他位置,则必须对其进行清理。这超出了问题的范围,但在典型情况下,您可以使用正则表达式将任何不是 ASCII 字母或数字的内容替换为带下划线的内容。在唯一性很重要的情况下,例如文件名或 HTML 'id' 属性,必须完成额外的工作来确保清理后的名称是唯一的(例如,确保 'a^b' 和 'a& ;b' 都被清理为 'a_b' 已解析)。典型的解决方案如下所示:
最后,我的回答的要点是:用于将数据移入和移出未修改的用户数据可以移动的五个特殊位置的特定转义函数:
PHP 变量 ->数据库字段:PDO 准备语句:
数据库字段 -> PHP 变量:无需转义,但使用 PDO 准备语句转义查询值:
PHP 变量 -> JavaScript 变量:json_encode:
PHP 变量 -> HTML textNode 或表单值:htmlspecialchars:
Javascript <-> HTML textNode 或表单值:浏览器内置的textNode 和.value 属性/函数:
使用这样的字符串进行重复的往返测试,以确保它始终遍历整个 HTML->PHP->DB-> ;PHP->[Javascript->]HTML 每次循环都完全相同:
这是我的脚本,用于测试每种方式的转义;显然,它需要一个数据库、一个名为“roundtrip”、列为“id”和“name”的表,以及在运行之前创建 id=1 的单行:
I'd always use "POST" for user data myself, not "GET", and the following discussion reflects that, but you'll still be able to use about 90% of what I say below either way. So here goes...
General rule: Don't "think ahead" when escaping data. Only make the immediately necessary transformation. Canonical example: Don't escape for HTML when doing a database insertion, since you'll end up with, for example, '&' turning into '&' after a couple of round trips.
General rule: Use UTF-8 throughout. You'll be thankful for this the first time someone does a copy-paste from an email that has a unicode ellipsis in it. (You'd be surprised how often that happens.) Typical settings needed; this may vary with PHP/MySQL/HTML version:
General rule: There are only five places that unsanitized (though properly escaped) user data can go:
If you want to put user data in any other place, it must be sanitized. This is beyond the scope of the question, but in a typical case you might use a regular expression to replace anything that isn't an ASCII letter or number with an underscore. In cases where uniqueness matters, like a filename or an HTML 'id' attribute, additional work has to be done to make sure that the sanitized names are unique (e.g. make sure that the clash that happens when'a^b' and 'a&b' are both sanitized to 'a_b' is resolved). A typical solution looks something like:
And, finally, the meat of my response: The specific escape functions to use to move data to and from those Five Special Places where unmodified user data can go:
PHP variable -> database field: PDO prepared statement:
Database field -> PHP variable: No escaping necessary, but use PDO prepared statement to escape query values:
PHP variable -> Javascript variable: json_encode:
PHP variable -> HTML textNode or form value: htmlspecialchars:
Javascript <-> HTML textNode or form value: Browser's built-in textNode and .value attributes/functions:
Do a repeated round-trip test with a string like this to make sure it always goes through the whole HTML->PHP->DB->PHP->[Javascript->]HTML cycle exactly the same every time:
Here's my script that tests escaping every which way; it obviously needs a database, a table with name 'roundtrip' and columns 'id' and 'name', and single row with id=1 to be created before running it:
要使用 PHP(和其他编程语言)在 MySQL 数据库中插入转义的内容,可以使用 PreparedStatements。
如果您想直接在 div 框中显示用户输入并转义输入(我猜这样 HTML 标签不会被解释),您可以检查 本文中的第 4 点 或使用 Google。
To insert escaped things in a MySQL-Database using PHP (and other programming languages), there are the PreparedStatements.
If you want to directly display the users input in a div box and escape the input (so that HTML-Tags aren't interpreted i guess), you can check Point #4 in this article or use Google.
OWASP 有一个 XSS 预防备忘单,涵盖了大部分(全部? )您似乎正在寻找的内容。不直接处理 PHP。
OWASP has an XSS Prevention Cheat Sheet which covers most (all?) of what you seem to be looking for. Does not address PHP directly.