如何克服对用户输入的恐惧(网络开发)
我正在编写一个供公众使用的网络应用程序...您如何克服/处理对用户输入的恐惧?作为一名 Web 开发人员,您知道存在的技巧和漏洞,尤其是在 Web 上可以利用的技巧和漏洞,通过 Firebug 等附加组件变得更加容易。
有时,它是如此令人难以承受,您只想忘记整个交易(确实使不过,您很欣赏 Intranet 开发!)
抱歉,如果这不是一个可以简单回答的问题,但也许想法或策略会有所帮助......谢谢!
I'm writing a web application for public consumption...How do you get over/ deal with the fear of User Input? As a web developer, you know the tricks and holes that exist that can be exploited particularly on the web which are made all the more easier with add-ons like Firebug etc
Sometimes it's so overwhelming you just want to forget the whole deal (does make you appreciate Intranet Development though!)
Sorry if this isn't a question that can be answered simply, but perhaps ideas or strategies that are helpful...Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
一个词:服务器端验证(好吧,这可能是三个词)。
One word: server-side validation (ok, that may have been three words).
其他答案中有很多合理的建议,但我将添加一个不那么“编程”的答案:
制定处理计划。
为恶意用户确实设法偷偷过去的意外情况做好准备你。制定计划来减轻损害、恢复干净和完整的数据,并与用户(以及潜在的其他相关方,例如您持有的任何信用卡详细信息的发卡行)进行沟通,告诉他们发生了什么。了解如何检测漏洞并关闭它。要知道关键的运营和开发人员是可以联系到的,这样,在公共假期前的周五下午 5:01 发动攻击的坏人在您下线之前不会有 72 个以上的空闲时间,更不用说开始修复问题了。
制定适当的计划并不能帮助您阻止不良的用户输入,但它应该有助于克服您的恐惧。
There's lots of sound advice in other answers, but I'll add a less "programming" answer:
Have a plan for dealing with it.
Be ready for the contingency that malicious users do manage to sneak something past you. Have plans in place to mitigate damage, restore clean and complete data, and communicate with users (and potentially other interested parties such as the issuers of any credit card details you hold) to tell them what's going on. Know how you will detect the breach and close it. Know that key operational and development personnel are reachable, so that a bad guy striking at 5:01pm on the Friday before a public holiday won't get 72+ clear hours before you can go offline let alone start fixing things.
Having plans in place won't help you stop bad user input, but it should help a bit with overcoming your fears.
如果您需要解决与“安全”相关的问题,那么安全和漏洞利用是软件中不可避免的事实,并且需要在开发过程中正面解决它们。
以下是一些建议:
If its "security" related concerns you need to just push through it, security and exploits are a fact of life in software, and they need to be addressed head-on as part of the development process.
Here are some suggestions:
用粗体字写起来很容易,但在实践中却有点困难。
That's very easy to write in bold letter and a little harder to do in practice.
我经常做的事情是将所有用户字符串包装在一个对象中,例如
StringWrapper
,它强制您调用编码方法来获取字符串。换句话说,只需提供对s.htmlEncode()
s.urlEncode().htmlEncode()
等的访问即可。当然,您需要获取原始字符串,这样您就可以有一个 s.rawString() 方法,但现在您可以使用 grep 来查看原始字符串的所有使用。因此,当您遇到“echo userString”时,您将收到类型错误,然后系统会提醒您通过公共方法对字符串进行编码/转义。
其他一些一般性的事情:
Something I always did was wrap all user strings in an object, something like
StringWrapper
which forces you to call an encoding method to get the string. In other words, just provide access tos.htmlEncode()
s.urlEncode().htmlEncode()
etc. Of course you need to get the raw string so you can have as.rawString()
method, but now you have something you can grep for to review all uses of raw strings.So when you come to 'echo userString' you will get a type error, and you are then reminded to encode/escape the string through the public methods.
Some other general things:
首先,我会试图安慰你一点,指出偏执是件好事。正如开车时有点害怕是好事一样,害怕用户输入也是好事。尽可能做最坏的打算,你不会失望的。
其次,防御性编程。假设您与外界的任何通信都完全受到损害。仅接受用户应该能够控制的参数。仅公开用户应该能够看到的数据。
净化输入。消毒消毒消毒。如果它的输入将显示在网站上(排行榜的昵称、论坛上的消息等),请对其进行适当的清理。如果输入可能会发送到 SQL,也请对其进行清理。事实上,甚至不要直接编写 SQL,而是使用某种中介。
如果您使用 HTTP,实际上只有一件事是您无法防御的。如果您使用 cookie 来识别某人的身份,则您无法阻止咖啡馆中的其他人嗅探该咖啡馆中其他人的 cookie(如果他们都使用相同的无线连接)。只要他们不使用安全连接,就没有什么可以帮助您避免这种情况。即使 Gmail 也无法免受这种攻击。您唯一能做的就是确保授权 cookie 不会永远持续下去,并考虑让他们在做一些大事(例如更改密码或购买东西)之前重新登录。
但别担心。许多安全细节已经由您在其上构建的任何系统处理(您正在构建在某些东西之上,不是吗?Spring MVC?Rails?Struts?)。这真的没那么难。如果涉及大笔资金,您可以付费聘请安全审计公司来尝试破解它。如果没有,就试着想出一切合理的办法,并在发现漏洞时修复它们。
但不要停止偏执。他们总是出去找你。这只是受欢迎的一部分。
PS 还有一个提示。如果你有这样的 JavaScript:
那么你肯定在后端有这样的代码:
First, I'll try to comfort you a bit by pointing out that it's good to be paranoid. Just as it's good to be a little scared while driving, it's good to be afraid of user input. Assume the worst as much as you can, and you won't be disappointed.
Second, program defensively. Assume any communication you have with the outside world is entirely compromised. Take in only parameters that the user should be able to control. Expose only that data that the user should be able to see.
Sanitize input. Sanitize sanitize sanitize. If it's input that will be displayed on the site (nicknames for a leaderboard, messages on a forum, anything), sanitize it appropriately. If it's input that might be sent to SQL, sanitize that too. In fact, don't even write SQL directly, use an intermediary of some sort.
There's really only one thing you can't defend from if you're using HTTP. If you use a cookie to identify somebody's identity, there's nothing you can do from preventing somebody else in a coffeehouse from sniffing the cookie of somebody else in that coffee house if they're both using the same wireless connection. As long as they're not using a secure connection, nothing can save you from that. Even Gmail isn't safe from that attack. The only thing you can do is make sure an authorization cookie can't last forever, and consider making them re-login before they do something big like change password or buy something.
But don't sweat it. A lot of the security details have been taken care of by whatever system you're building on top of (you ARE building on top of SOMETHING, aren't you? Spring MVC? Rails? Struts? ). It's really not that tough. If there's big money at stake, you can pay a security auditing company to try and break it. If there's not, just try to think of everything reasonable and fix holes when they're found.
But don't stop being paranoid. They're always out to get you. That's just part of being popular.
P.S. One more hint. If you have javascript like this:
Then you'd sure as hell have code in the backend that goes:
“引用”所有内容,使其在“目标”语言中没有任何含义:SQL、HTML、JavaScript 等。
这当然会造成妨碍,因此您必须小心确定何时需要特殊处理,就像通过管理权限来处理一些如果数据一样。
"Quote" everything so that it can not have any meaning in the 'target' language: SQL, HTML, JavaScript, etc.
This will get in the way of course, so you have to be careful to identify when this needs special handling, like through administrative privileges to deal with some if the data.
有多种类型的注入和跨站点脚本(请参阅这个早期的答案),但对所有这些都有防御措施。首先,您显然需要查看存储过程、白名单(例如,用于 HTML 输入)和验证。
除此之外,很难给出一般性建议。其他人给出了一些很好的建议,例如始终进行服务器端验证和研究过去的攻击。
保持警惕,但不要害怕。
There are multiple types of injection and cross-site scripting (see this earlier answer), but there are defenses against all of them. You'll clearly want to look at stored procedures, white-listing (e.g. for HTML input), and validation, to start.
Beyond that, it's hard to give general advice. Other people have given some good tips, such as always doing server-side validation and researching past attacks.
Be vigilant, but not afraid.
http://imar.spaanjaars.com/QuickDocId.aspx?quickdoc=477
http://imar.spaanjaars.com/QuickDocId.aspx?quickdoc=477
应该有一些关于您正在使用的语言/系统的已知漏洞的文档。我知道 Zend PHP 认证稍微涵盖了这个问题,您可以阅读学习指南。
为什么不聘请专家时常审核您的申请呢?考虑到您的关注程度,这是一项值得的投资。
There should be some documentation of known exploits for the language/system you're using. I know the Zend PHP Certification covers that issue a bit and you can read the study guide.
Why not hire an expert to audit your applications from time to time? It's a worthwhile investment considering your level of concern.
我们的客户总是说:“与我的用户打交道,因为他们不区分日期和文本字段!!”
我用 Java 编写代码,我的代码充满了断言,我认为客户端的一切都是错误的,我在服务器上检查了一切。
Our client always say: "Deal with my users as they dont differentiate between the date and text fields!!"
I code in Java, and my code is full of
asserts
i assume everything is wrong from the client and i check it all at server.对我来说,第一件事就是始终构建静态 SQL 查询并将数据作为参数传递。这极大地限制了您必须处理的报价问题。另请参阅 http://xkcd.com/327/
这也具有性能优势,因为您可以重新使用准备好的查询。
#1 thing for me is to always construct static SQL queries and pass your data as parameters. This limits the quoting issues you have to deal with enormously. See also http://xkcd.com/327/
This also has performance benefits, as you can re-use the prepared queries.
实际上,您只需要注意两件事:
避免SQL 注入。使用参数化查询将用户控制的输入保存在数据库中。用 Java 术语来说:使用
PreparedStatement
< /a>.用 PHP 术语来说:使用mysql_real_escape_string()
或 PDO。避免XSS。在显示期间转义用户控制的输入。在 Java/JSP 术语中:使用 JSTL
。用 PHP 术语来说:使用htmlspecialchars()
。就这样。您无需担心数据的格式。只是关于你如何处理它的方式。
There are actually only 2 things you need to take care with:
Avoid SQL injection. Use parameterized queries to save user-controlled input in database. In Java terms: use
PreparedStatement
. In PHP terms: usemysql_real_escape_string()
or PDO.Avoid XSS. Escape user-controlled input during display. In Java/JSP terms: use JSTL
<c:out>
. In PHP terms: usehtmlspecialchars()
.That's all. You don't need to worry about the format of the data. Just about the way how you handle it.