按照 Joel 的建议,使用类型系统防止 XSS 攻击
在 Podcast 58(大约 20 分钟)中,Jeff 抱怨了 HTML.Encode 的问题()
和 Joel 谈论使用类型系统来拥有普通字符串和 HTMLString:
关于无法处理 HTML 的视图引擎的邪恶的简短政治咆哮 默认编码。 问题在于 这个设计选择是它不是 “默认安全”,这始终是 框架或 API 选择错误。 忘记编码一些 用户输入的数据在一个单一的 你的网络应用程序中的臭地方,以及 你将完全被XSS所掌控。 相信它。 我知道因为它是 发生在我们身上。 多次!
Joel 认为,使用强类型语言和正确的 框架,这是可能的(理论上) 彻底消除 XSS——这 需要使用特定的数据 type,这是你唯一的方法 发送数据到浏览器。 那个数据 类型将在编译时验证 时间。
博客文章中的评论提到使用静态分析来查找潜在的弱点。 转录 Wiki 尚未完成。
是否可以在不使用新的 ASP.NET 框架的情况下实现 Joel 的建议?
是否可以简单地通过子类化每个控件并基于 HTMLString 强制执行新接口来实现它? 如果大多数人已经子类化控件以便更好地注入特定于站点的功能,那么这不是很容易实现吗?
这样做是否值得而不是投资于静态分析?
In Podcast 58 (about 20 minutes in), Jeff complains about the problems of HTML.Encode()
and Joel talks about using the type system to have ordinary strings and HTMLStrings:
A brief political rant about the evil of view engines that fail to HTML
encode by default. The problem with
this design choice is that it is not
“safe by default”, which is always the
wrong choice for a framework or API.
Forget to encode some bit of
user-entered data in one single
stinking place in your web app, and
you will be totally owned with XSS.
Believe it. I know because it’s
happened to us. Multiple times!Joel maintains that, with a strongly-typed language and the right
framework, it’s possible (in theory)
to completely eliminate XSS — this
would require using a specific data
type, a type that is your only way to
send data to the browser. That data
type would be validated at compile
time.
The comments at the blog post mention using static analysis to find potential weaknesses. The transcript Wiki isn't done yet.
Is it possible to implement Joel's suggestion without having a new ASP.NET framework?
Might it be possible to implement it simply by subclassing every control and enforcing new interfaces based on HTMLString? If most people already subclass controls in order to better able to inject site-specific functionality, wouldn't this be fairly easy to implement?
Would it be worth doing this instead of investing in static analysis?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要在任何地方使用 HtmlString,您实际上必须重写每个 Web 控件的每个属性和方法。 System.String 是密封的,因此您无法对其进行子类化。
一种更简单(但仍然非常耗时)的方法是使用控件适配器来用安全的替代方案替换 Web 控件。 在这种情况下,您可以对每个 Web 控件进行子类化并重写 Render 方法以对动态内容进行 HTML 编码。
To use HtmlString everywhere, you would essentially have to rewrite every property and method of every web control. System.String is sealed, so you can't subclass it.
An easier (but still very time consuming) approach would be to use control adapters to replace web controls with safe alternatives. In this case, you would subclass each web control and override the Render methods to HTML-encode dynamic content.