使 Web 表单输入在各种情况下安全的正确方法是什么?
你们都认为什么是正确的(阅读:最灵活、松散耦合、最健壮等)方法来使来自 Web 的用户输入安全地用于 Web 应用程序的各个部分? 显然,我们可以为每个上下文(数据库、屏幕显示、保存在磁盘上等)使用各自的清理功能,但是是否有一些通用的“模式”来处理不安全的数据并使其安全? 是否有一种既定的方法来强制将其视为不安全,除非它得到适当的安全?
What do you all think is the correct (read: most flexible, loosely coupled, most robust, etc.) way to make user input from the web safe for use in various parts of a web application? Obviously we can just use the respective sanitization functions for each context (database, display on screen, save on disk, etc.), but is there some general "pattern" for handling unsafe data and making it safe? Is there an established way to enforce treating it as unsafe unless it is properly made safe?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您无法使用单一方法来清理所有用途的数据,但一个好的开始是:
过滤器 Var 接受许多不同类型的数据并剔除坏字符(例如您期望数字的非数字),并确保其格式有效(IP 地址)。
注意:电子邮件地址比 Filter_Var 的实现复杂得多,因此请 Google 寻找正确的函数。
在将内容输入到 Mysql 数据库之前,我不建议使用它一个数据库,无论如何,最好只使用准备好的 mysqli 语句。
You cannot use a single method to sanitize data for all uses, but a good start is:
Filter Var takes a number of different types of data and strips out bad characters (like non-digits for things you expect to be numbers), and makes sure it is of valid format (IP Addresses).
Note: Email Addresses are far more complicated than the Filter_Var's implementation, so Google around for the proper function.
I wouldn't suggest using this until you are about to input stuff into a database, and it is probably better to just use prepared mysqli statements anyway.
正如已经说过的,当您担心网络安全时,需要考虑几件事。 以下是需要考虑的一些基本原则:
所以这意味着没有像
$variable = $_POST['user_input']
这样的东西。 对于任何这样的情况,您都会将太多的控制权交给用户。 如果输入影响某些数据库查询,请始终使用白名单来验证用户输入。 如果查询针对用户名,请根据良好用户名列表进行验证。 不要简单地直接将用户输入放入查询中。一个(可能的)例外是搜索字符串。 在这种情况下,您需要消毒,就这么简单。
如果用户正在为其他用户创建个人资料或上传信息,您必须有一个可接受的数据类型的白名单,或者删除任何可能是恶意的数据。 这不仅是为了您的系统的安全,也是为了您的其他用户(请参阅下一点)。
这可能是安全顾问向我强调的最重要的事情。 您不能简单地依赖于在用户收到输入时对其进行清理。 如果您没有自己编写输出,请始终通过对任何 HTML 字符进行编码或将其包装在
</code> 标记中来确保输出无害。 如果用户 A 上传了一些 javascript,损害了查看该页面的任何其他用户,那么这只是开发人员的疏忽。 您晚上会睡得更好,因为您知道任何和所有用户输出都只能在所有浏览器上显示为文本。</plaintext></code></p> <ul> <li> 绝不允许除用户之外的任何人控制表单。</li> </ul> <p> XSS 比它应该的更容易,并且在一个段落中描述起来确实很痛苦。 简而言之,每当您创建表单时,您都会向用户授予对处理表单数据的脚本的访问权限。 如果我窃取某人的会话或某人的 cookie,我现在可以与脚本对话,就像我在表单页面上一样。 我知道它期望的数据类型以及它将查找的变量名称。 我可以简单地将这些变量传递给它,就好像我是用户一样,脚本无法区分。</p> <p> 以上不是卫生问题,而是用户验证问题。 我的最后一点与这个想法直接相关。</p> <ul> <li> 避免使用 cookie 进行用户验证或角色验证。</li> </ul> <p> 如果我可以窃取用户的 cookie,那么我所做的可能不仅仅是让该用户度过糟糕的一天。 如果我注意到 cookie 有一个名为“member”的值,我可以很容易地将该值更改为“admin”。 也许它不起作用,但对于许多脚本,我可以立即访问任何管理级信息。</p> <p> 简而言之,没有一种简单的方法来保护 Web 表单,但有一些基本原则可以简化您应该做的事情,从而减轻保护脚本的压力。</p> <p> 再次采取良好措施:</p> <ul> <li>清理所有输入</li> <li>对所有输出进行编码</li> <li>根据严格的白名单验证用于执行的任何输入</li> <li>确保输入来自实际用户</li> <li>切勿使任何基于用户或角色的验证浏览器端/用户可修改</li> </ul> <p>且绝不假设任何人的清单都是详尽或完美的。</p>
Like it's already been said, there are several things to take into account when you are concerned about web security. Here are some basic principals to take into account:
So this means don't have something like
$variable = $_POST['user_input']
. For any situation like this, you are handing over too much control to the user. If the input affects some database query, always have whitelists to validate user input against. If the query is for a user name, validate against a list of good user names. Do NOT simply make a query with the user input dropped right in.One (possible) exception is for a search string. In this case, you need to sanitize, simple as that.
If the user is creating a profile or uploading info for other users, you have to either have a white-list of what kind of data is acceptable, or strip out anything that could be malicious. This not only for your system's security, but for your other users (See next point.)
This is probably the most important thing that security consultants have emphasized to me. You can not simply rely on sanitizing input when it is received by the user. If you did not write the output yourself, always ensure that the output is innocuous by encoding any HTML characters or wrapping it in a
<plaintext>
tag. It is simple negligence on the part of the developer if user A uploads a bit of javascript that harms any other users that view that page. You will sleep better at night knowing that any and all user output can do nothing but appear as text on all browsers.XSS is easier than it should be and a real pain to cover in one paragraph. Simply put, whenever you create a form, you are giving users access to a script that will handle form data. If I steal someone's session or someone's cookie, I can now talk to the script as though I was on the form page. I know the type of data it expects and the variables names it will look for. I can simply pass it those variables as though I were the user and the script can't tell the difference.
The above is not a matter of sanitation but of user validation. My last point is directly related to this idea.
If I can steal a user's cookie, I may be able to do more than make that one user have a bad day. If I notice the cookie has a value called "member", I can very easily change that value to "admin". Maybe it won't work, but for many scripts, I would have instant access to any admin-level info.
Simply put, there is not one easy way to secure a web form, but there are basic principals that simplify what you should be doing, and thus eases the stress of securing your scripts.
Once more for good measure:
And never assume that any one person's list is exhaustive or perfect.
我非常怀疑这样的通用框架是否存在并且比编程语言复杂。
不同层之间“安全”的定义如此不同
还要考虑发现违规时的操作。
也许我错过了你的愿景? 您是否看到过任何与您的想法接近的东西?
I'm more than a little sceptical that such a general purpose framework could both exist and be less complex than a programming language.
The definition of "safe" is so different between different layers
Also consider the actions when violations are discovered.
Perhaps I'm missing your vision? Have you seen anything that gets close to what you have in mind?