Todo 列出防止 xss 攻击的方法,哪些是最有效的?

发布于 2024-12-01 14:08:20 字数 468 浏览 1 评论 0原文

我很想知道我们必须使用哪些最佳方法/技巧来防止 xss 攻击并使其变得困难?

我知道有:

需要完成数据库的用户怎么办? 新开发人员通常会犯哪些错误?

谢谢

I would love to know which best methods/tips do we have to use to prevent and make difficult a xss attack ?

I know there are :

What's about users who need to complete a database?
What kind of mistakes usually can do a new developer?

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甲如呢乙后呢 2024-12-08 14:08:20

规则第一:永远不要相信用户输入。

你的建议很好,我会添加这两个:

  • addslashes()
  • mysql_real_escape_string()(假设你使用MySQL,也有其他供应商的函数)

我总是建议在使用它之前过滤用户输入。以防万一你以后忘记了。

Rule number one: never trust user input.

Your suggestions are great, I would add these two:

  • addslashes()
  • mysql_real_escape_string() (assuming you are using MySQL, there are functions for other vendors too)

I always suggest to filter user input before you do any work with it. Just in case you forget it later.

小巷里的女流氓 2024-12-08 14:08:20

哪种方法最有效取决于您的要求。如果您需要允许用户发送某些 HTML,您可能需要 HTML 净化器。

但是,如果您永远不会接受用户的 HTML,则需要应用上下文编码。您可以拥有 XSS,而无需用户注入新标签。攻击字符串可以更改现有标签。典型示例:

<input type="text" name="email" value="<?php echo $email; ?>">

在此示例中,如果 $email 为

" autofocus onfocus="alert(1)

onfocus 事件处理程序中的 javascript,则会触发。

您需要使用的转义取决于上下文。我们是在 javascript 中,还是在 HTML 中,或者在 HTML 属性中,还是在 CSS 中?
请参阅 https://www.owasp.org/index.php/XSS_(Cross_Site_Scripting) )_Prevention_Cheat_Sheet 了解有关如何编码的更多信息。

Which method is most efficient depends on your requirements. If you need to allow some HTML from users, you probably want HTML purifier.

If however you will never accept HTML from users, you need to apply contextual encoding. You can have XSS, without the user injecting a new tag. The attack string can alter an existing tag. Typical example:

<input type="text" name="email" value="<?php echo $email; ?>">

In this example, if $email is

" autofocus onfocus="alert(1)

The javascript in the onfocus eventhandler will fire.

The escaping you need to use, depends on the context. Are we in javascript, or in HTML or in an HTML attribute or in CSS?
See the https://www.owasp.org/index.php/XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet for more info on how to encode.

弃爱 2024-12-08 14:08:20

我会使用以下内容:

$variable = trim(strip_tags(stripslashes($_POST['variablename'])));

或者

// Clean up the input values 
foreach($_POST as $key => $value) {  
    $_POST[$key] = stripslashes($_POST[$key]); 

    $_POST[$key] = htmlspecialchars(strip_tags($_POST[$key])); 
}

两者都适用于 HTML/PHP。

I would use the following:

$variable = trim(strip_tags(stripslashes($_POST['variablename'])));

OR

// Clean up the input values 
foreach($_POST as $key => $value) {  
    $_POST[$key] = stripslashes($_POST[$key]); 

    $_POST[$key] = htmlspecialchars(strip_tags($_POST[$key])); 
}

Both works great in respective to HTML/PHP.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文