安全捕获 URL 编码字符串以用于搜索输入?
Safely capture URL Encoded String for Search Input?
我基本上有一个带有输入的搜索表单:
<input type="text" name="search" />
这最终会将用户发送到:
/search/[URL_ENCODED_STRING]
所以如果他们搜索
http://www.whosebug.com/
url 将是:
/search/http%253A%252F%252Fwww.whosebug.com%252F
我的问题是知道我稍后阅读的输入是否安全。然后我会在搜索页面上使用 Drupal 的固有方式来读取值 (arg(1))。但即使没有 drupal,结果也基本相同。我最终会得到:
$variable = urldecode($input);
如果我随后打印出 $variable,它将显示:
http://www.whosebug.com/
我的问题是,在 SQL 中使用该字符串之前,我必须对其应用什么样的清理?只是 "addslashes" 吗?或者我应该删除所有非字母数字和数字值吗?
注意
我还没有讲到那部分,但我相当确定如果我将这个变量传递给内置搜索功能,Drupal 会应用它自己的清理,但我仍然想知道什么是正确的方法是清理此输入以避免恶意用户在网站上做奇怪的事情。
更新
我到了这部分,Drupal 确实负责准备好的语句部分。但我仍然不知道在此处打印时如何清理字符串:
<div id="searchedFor">
<span class="preLabel">You searched for</span>
<h2><?php print $_REQUEST['search']; ?></h2>
</div>
最正确的打印方法是什么?
要清理页面,请使用 htmlentities()
或 strip_tags()
或 htmlspecialchars()
:
<div id="searchedFor">
<span class="preLabel">You searched for</span>
<h2><?php echo htmlentities($_REQUEST['search'], ENT_QUOTES); ?></h2>
</div>
示例:
<?php echo htmlentities("<script>NastyJS('code');</script>", ENT_QUOTES); ?>
<!-- Shows in browser this way -->
<script>NastyJS('code');</script>
<!-- but shows in source this way -->
<script>NastyJS('code');</script>
我基本上有一个带有输入的搜索表单:
<input type="text" name="search" />
这最终会将用户发送到:
/search/[URL_ENCODED_STRING]
所以如果他们搜索
http://www.whosebug.com/
url 将是:
/search/http%253A%252F%252Fwww.whosebug.com%252F
我的问题是知道我稍后阅读的输入是否安全。然后我会在搜索页面上使用 Drupal 的固有方式来读取值 (arg(1))。但即使没有 drupal,结果也基本相同。我最终会得到:
$variable = urldecode($input);
如果我随后打印出 $variable,它将显示:
http://www.whosebug.com/
我的问题是,在 SQL 中使用该字符串之前,我必须对其应用什么样的清理?只是 "addslashes" 吗?或者我应该删除所有非字母数字和数字值吗?
注意
我还没有讲到那部分,但我相当确定如果我将这个变量传递给内置搜索功能,Drupal 会应用它自己的清理,但我仍然想知道什么是正确的方法是清理此输入以避免恶意用户在网站上做奇怪的事情。
更新
我到了这部分,Drupal 确实负责准备好的语句部分。但我仍然不知道在此处打印时如何清理字符串:
<div id="searchedFor">
<span class="preLabel">You searched for</span>
<h2><?php print $_REQUEST['search']; ?></h2>
</div>
最正确的打印方法是什么?
要清理页面,请使用 htmlentities()
或 strip_tags()
或 htmlspecialchars()
:
<div id="searchedFor">
<span class="preLabel">You searched for</span>
<h2><?php echo htmlentities($_REQUEST['search'], ENT_QUOTES); ?></h2>
</div>
示例:
<?php echo htmlentities("<script>NastyJS('code');</script>", ENT_QUOTES); ?>
<!-- Shows in browser this way -->
<script>NastyJS('code');</script>
<!-- but shows in source this way -->
<script>NastyJS('code');</script>