简体   繁体   English

mysql_real_escape_string是否足以防止HTML注入?

[英]Is mysql_real_escape_string enough for preventing HTML injection?

I want to know how to prevent HTML injection. 我想知道如何防止HTML注入。 I have created a site where users are allowed to paste articles in a HTML form. 我创建了一个网站,允许用户以HTML形式粘贴文章。 I have used mysql_real_escape_sting but I want to know whether this is enough for preventing HTML injections. 我使用了mysql_real_escape_sting但是我想知道这是否足以防止HTML注入。 I tried htmlspecialchars but it's showing error with mysql_real_escape_string . 我尝试了htmlspecialchars但它显示mysql_real_escape_string错误。

You should use prepared statements to be absolutely sure to prevent sql injection. 您应该使用准备好的语句来绝对确保防止sql注入。

Taken from documentation (read the part in bold) 摘自文档(以粗体显示部分)

Many of the more mature databases support the concept of prepared statements. 许多更成熟的数据库支持预准备语句的概念。 What are they? 这些是什么? They can be thought of as a kind of compiled template for the SQL that an application wants to run, that can be customized using variable parameters. 可以将它们视为应用程序要运行的SQL的一种已编译模板,可以使用可变参数对其进行自定义。 Prepared statements offer two major benefits: 准备好的报表有两个主要好处:

  • The query only needs to be parsed (or prepared) once, but can be executed multiple times with the same or different parameters. 该查询只需要解析(或准备)一次,但是可以使用相同或不同的参数多次执行。 When the query is prepared, the database will analyze, compile and optimize it's plan for executing the query. 准备查询后,数据库将分析,编译和优化其执行查询的计划。 For complex queries this process can take up enough time that it will noticeably slow down an application if there is a need to repeat the same query many times with different parameters. 对于复杂的查询,此过程可能会占用足够的时间,如果需要使用不同的参数多次重复同一查询,则会明显降低应用程序的速度。 By using a prepared statement the application avoids repeating the analyze/compile/optimize cycle. 通过使用准备好的语句,应用程序可以避免重复分析/编译/优化周期。 This means that prepared statements use fewer resources and thus run faster. 这意味着准备好的语句使用较少的资源,因此运行速度更快。
  • The parameters to prepared statements don't need to be quoted; 准备好的语句的参数不需要用引号引起来。 the driver automatically handles this. 驱动程序会自动处理。 If an application exclusively uses prepared statements, the developer can be sure that no SQL injection will occur (however, if other portions of the query are being built up with unescaped input, SQL injection is still possible). 如果应用程序仅使用准备好的语句,则开发人员可以确保不会发生SQL注入 (但是,如果使用未转义的输入来构建查询的其他部分,则仍然可以进行SQL注入)。

Prepared statements are so useful that they are the only feature that PDO will emulate for drivers that don't support them. 准备好的语句非常有用,它们是PDO将为不支持它们的驱动程序模拟的唯一功能。 This ensures that an application will be able to use the same data access paradigm regardless of the capabilities of the database. 这确保了应用程序将能够使用相同的数据访问范例,而不管数据库的功能如何。

If you meant to prevent XSS (Cross site scripting) you should use the function htmlspecialchars() whenever you want to output something to the browser that came from user input or from any non secure source. 如果要防止XSS (跨站点脚本编制),则每当您想要将来自用户输入或任何非安全来源的内容输出到浏览器时,都应使用htmlspecialchars()函数。 Always treat any unknown source as unsecure 始终将任何未知来源视为不安全

 echo htmlspecialchars($string, ENT_QUOTES, 'UTF-8');

No, mysql_real_escape_sting does only prepare data to be safely inserted into MySQL string declarations to prevent SQL injections in that specific context. 不, mysql_real_escape_sting只是准备将数据安全地插入到MySQL字符串声明中,以防止在该特定上下文中进行SQL注入。 It does not prevent other injections like HTML injection or Cross-Site Scripting (XSS). 它不会阻止其他注入,例如HTML注入或跨站点脚本(XSS)。

Both HTML injection and XSS happen in different contexts where there are different contextual special characters that need to be taken care of. HTML注入和XSS都发生在不同的上下文中,在这些上下文中需要注意不同的上下文特殊字符。 In HTML it's especially < , > , & , " , and ' that delimit the different HTML contexts. With XSS in mind you also need to be aware of the different JavaScript contexts and their special characters. 在HTML中,尤其是<>&"'界定了不同的HTML上下文。考虑到XSS,您还需要注意不同的JavaScript上下文及其特殊字符。

htmlspecialchars should suffice the handle the former attack while json_encode can be used for a safe subset of JavaScript. htmlspecialchars应该足以应付以前的攻击,而json_encode可以用作JavaScript的安全子集。 See also the XSS (Cross Site Scripting) Prevention Cheat Sheet as well as my answer to Are these two functions overkill for sanitization? 另请参阅XSS(跨站点脚本)预防速查表以及我对以下两个问题的回答: 这两个功能是否在杀毒方面过头了? and related questions for further information on this topic. 以及有关此主题的更多信息的相关问题。

No. In fact, I believe that for advanced coders, you shouldn't be using mysql_real_escape_string() as a crutch. 否。事实上,我相信对于高级编码器,您不应该将mysql_real_escape_string()用作拐杖。
For each value you need to use in a DB query, seriously consider the possible characters that could appear. 对于需要在数据库查询中使用的每个值,请认真考虑可能出现的字符。 If it is a dollar amount, the only characters you should accept are numbers, a period, and possible preceding dollar sign. 如果是美元金额,则您应该接受的唯一字符是数字,句点以及可能的美元符号。 If it is a name, you should only allow letters, a hyphen, and possibly a period (for fulls names like Joseph A. Bank). 如果是名称,则应只允许字母,连字符和句点(如约瑟夫·A·班克斯这样的全名)。
Once you determine a strict character range that's acceptable for a value, write a Regex to match that value against. 确定值可接受的严格字符范围后,编写正则表达式以匹配该值。 For any values that don't match, display a bogus error and log the value in a textfile (read: not a db ) along with the user's IP. 对于任何不匹配的值,将显示假错误,并将该值与用户的IP一起记录在文本文件(读取: 不是db )中。 Frequently check this file so you can see if values users have tried that didn't work were hacking attempts. 经常检查此文件,以便您可以查看用户尝试的不起作用的值是否是黑客尝试。 Not only will this uncover valid inputs for which you need to adjust your Regex, but it will also reveal the IP's of hackers who try to find SQL vulnerabilities on your site. 这不仅会发现您需要调整正则表达式的有效输入,而且还将揭示试图在您的站点上发现SQL漏洞的黑客的IP。 This approach ensures that new and old SQL vulnerabilities that might not immediately be addressed by mysql_real_escape_string() , will be blocked. 这种方法确保了mysql_real_escape_string()可能不会立即解决的新旧SQL漏洞将被阻止。

No, it's not. 不,这不对。 Refer to the docs 参考文档

It doesn't escape < or >. 它不会转义<或>。

Simple answer: No 简单答案:否

mysql_real_escape_string only helps you get rid of SQL Injections and not XSS and html injection. mysql_real_escape_string仅帮助您摆脱SQL注入,不是XSS和html注入。 To avoid these you need more sophisticated input validation. 为了避免这些,您需要更复杂的输入验证。 Start by looking at strip_tags and htmlentities . 首先查看strip_tagshtmlentities

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM