正在清理 HTML 输入值

Sanitizing HTML input value

除了引号 (") 之外,您是否必须将任何内容转换为 (") inside:

<input type="text" value="$var">

我个人看不出不使用 " on*=... 怎么可能突破它。

这是正确的吗?

编辑:显然有些人认为我的问题太模糊了;

<input type="text" value="<script>alert(0)</script>"> 不执行。因此,如果不使用 ".

就不可能脱离使用

这是正确的吗?

当用户提交数据时,您需要确保他们提供了您期望的内容。

例如,如果您需要一个数字,make sure the submitted data is a number. You can also cast user data into other types。提交的所有内容最初都被视为字符串,因此将已知数字数据强制转换为整数或浮点数可以快速轻松地进行清理。

您需要确保不应包含任何 HTML 内容的字段实际上不包含 HTML。有多种方法可以解决这个问题。

您可以尝试使用 htmlspecialchars. You should not use htmlentities 转义 HTML 输入以中和 HTML,因为它还会对重音字符和它认为也需要编码的其他字符执行编码。

您可以尝试删除任何可能的 HTML。 strip_tags is quick and easy, but also sloppy. HTML Purifier 在去除所有 HTML 和允许通过标签和属性的选择性白名单方面做了更彻底的工作。

可以使用OWASP PHP Filters。它们非常易于使用且有效。

您可以使用 filter extension,它提供了一种全面清理用户输入的方法。

例子

下面的代码将从字符串中删除所有 HTML 标签:

$string = "<h1>Hello, World!</h1>";
$new_string = filter_var($string, FILTER_SANITIZE_STRING);
// $new_string is now "Hello, World!"

下面的代码将确保变量的值是一个有效的 IP 地址:

$ip = "127.0.0.1";
$valid_ip = filter_var($ip, FILTER_VALIDATE_IP);
// $valid_ip is TRUE
 
$ip = "127.0.1.1.1.1";
$valid_ip = filter_var($ip, FILTER_VALIDATE_IP);
// $valid_ip is FALSE

正在清理和验证电子邮件地址:

<?php
$a = 'joe@example.org';
$b = 'bogus - at - example dot org';
$c = '(bogus@example.org)';

$sanitized_a = filter_var($a, FILTER_SANITIZE_EMAIL);
if (filter_var($sanitized_a, FILTER_VALIDATE_EMAIL)) {
    echo "This (a) sanitized email address is considered valid.\n";
}

$sanitized_b = filter_var($b, FILTER_SANITIZE_EMAIL);
if (filter_var($sanitized_b, FILTER_VALIDATE_EMAIL)) {
    echo "This sanitized email address is considered valid.";
} else {
    echo "This (b) sanitized email address is considered invalid.\n";
}

$sanitized_c = filter_var($c, FILTER_SANITIZE_EMAIL);
if (filter_var($sanitized_c, FILTER_VALIDATE_EMAIL)) {
    echo "This (c) sanitized email address is considered valid.\n";
    echo "Before: $c\n";
    echo "After:  $sanitized_c\n";    
}
?>

参考:

What are the best PHP input sanitizing functions?

http://code.tutsplus.com/tutorials/sanitize-and-validate-data-with-php-filters--net-2595

https://security.stackexchange.com/q/42498/71827

http://php.net/manual/en/filter.examples.sanitization.php

$var = "><script>alert(0);</script> 会起作用...如果您可以关闭引号,则可以关闭标签并打开另一个...但我认为您是对的,不关闭引号就没有注入有可能...

您确实在问两个问题(或者至少可以解释):

  1. 如果不允许使用引号,是否可以注入 input[type="text"] 的引用 value 属性?

  2. 如果不允许使用引号,是否可以注入元素的任意引用属性。

第二个由以下简单地证明:

<a href="javascript:alert(1234);">Foo</a>

<div onmousemove="alert(123);">...

第一个有点复杂。

HTML5

根据 HTML5 spec:

Attribute values are a mixture of text and character references, except with the additional restriction that the text cannot contain an ambiguous ampersand.

在引用的属性中进一步细化为:

The attribute name, followed by zero or more space characters, followed by a single U+003D EQUALS SIGN character, followed by zero or more space characters, followed by a single """ (U+0022) character, followed by the attribute value, which, in addition to the requirements given above for attribute values, must not contain any literal U+0022 QUOTATION MARK characters ("), and finally followed by a second single """ (U+0022) character.

所以简而言之,除了 "ambiguous ampersand"(&[a-zA-Z0-9]+; 当结果不是有效字符引用时)和引号字符之外的任何字符在属性内部都是有效的。

HTML 4.01

HTML 4.01 在语法方面的描述不如 HTML5(HTML5 最初创建的原因之一)。但是,它确实 say this:

When script or style data is the value of an attribute (either style or the intrinsic event attributes), authors should escape occurrences of the delimiting single or double quotation mark within the value according to the script or style language convention. Authors should also escape occurrences of "&" if the "&" is not meant to be the beginning of a character reference.

注意,这是说作者应该做什么,而不是解析器应该做什么。因此解析器可以在技术上接受或拒绝无效输入(或将其修改为有效输入)。

XML 1.0

XML 1.0 Spec 将属性定义为:

Attribute ::= Name Eq AttValue

其中 AttValue 定义为:

AttValue ::= '"' ([^<&"] | Reference)* '"' | "'" ([^<&'] | Reference)* "'"

& 类似于 HTML5 中的 "ambiguous ampersand" 的概念,但它基本上是说 "any unencoded ampersand"。

请注意,它明确拒绝 < 来自属性值。

因此,虽然 HTML5 允许它,但 XML1.0 明确拒绝它。

这是什么意思

这意味着对于一个兼容且没有错误的解析器,HTML5将忽略属性中的<个字符,XML将错误。

这也意味着对于兼容且无错误的解析器,HTML 4.01 将以未指定且可能奇怪的方式运行(因为规范未详细说明行为)。

这就是问题的症结所在。在过去,HTML 是一个非常松散的规范,以至于每个浏览器对于如何处理格式错误的 html 都有略微不同的规则。每个人都会尝试 "fix" 它,或者 "interpret" 你的意思。所以这意味着虽然 HTML5 兼容的浏览器不会在 <input type="text" value="<script>alert(0)</script>"> 中执行 JS,但没有什么可以说 HTML 4.01 兼容的浏览器不会。并且没有什么可以说 XML 或 HTML5 解析器中可能不存在导致它被执行的错误(尽管那将是一个非常重要的问题)。

THAT 是 OWASP(和大多数安全专家)建议您对属性值内的所有非字母数字字符或 &<" 进行编码的原因。这样做没有任何成本,只是增加了安全性,因为 知道 浏览器的解析器将如何解释该值。

吗?不。但纵深防御表明,由于这样做没有成本,潜在的好处是值得的。

如果你的问题是 "what types of xss-attacks are possible" 那么你最好 google 它。我会留下一些例子来说明为什么你应该清理你的输入

  • 如果输入是由 echo '<input type="text" value="$var">' 生成的,那么简单的 ' 会破坏它。

  • 如果 PHP 页面中的输入是纯 HTML 那么 value=<?php deadly_php_script ?> 会破坏它

  • 如果这是 HTML 文件中的纯 HTML 输入 - 那么转换双引号就足够了。

不过,转换其他特殊符号(如 <> 等)是一种很好的做法。输入是为了将存储在 server\transferred 上的信息输入到另一个 page\script 上,因此您需要检查可能破坏这些文件的内容。假设我们有这个设置:

index.html:

<form method=post action=getinput.php> <input type="text" name="xss"> <input type="submit"></form>

getinput.php:

echo $_POST['xss'];

输入值 ;your_deadly_php_script 完全破坏了它(在这种情况下你也可以清理服务器端)

如果这还不够 - 请提供有关您的问题的更多信息,添加更多代码示例。

我相信此人指的是跨站点脚本攻击。他们将此标记为 php、安全和 xss

为例
<input type="text" value=""><script>alert(0)</script><"">

以上代码将执行警告框代码;

<?php $var= "\"><script>alert(0)</script><\""; ?>
<input type="text" value="<?php echo $var ?>">

这也将执行警告框。 要解决这个问题,您需要转义 "、< > 以及其他一些以确保安全。PHP 有几个值得研究的函数,每个函数都有其优缺点!

htmlentities() - Convert all applicable characters to HTML entities
htmlspecialchars() - Convert special characters to HTML entities
get_html_translation_table() - Returns the translation table used by  htmlspecialchars and htmlentities
urldecode() - Decodes URL-encoded string

你必须要小心的是,你正在传递一个变量,并且有一些方法会产生错误,从而导致它爆发。最好的办法是确保数据不会以可执行的方式格式化,以防出现错误。但是,如果它们不是引号,那么您是对的,但是您或我目前不了解的某些方式将允许这种情况发生。