安全地呈现不受信任文本中的超链接
作为项目的一部分,我通过网络表单接受用户的文本并将其显示在网页上。他们提供的文本可能包含 URL,如果是这样,我想将其呈现为超链接以改善体验。例如,用户可能提交包含 http://www.google.com
的文本,我想将其转换为 ...
我想知道在执行此操作时应该注意哪些安全问题。我已经采取措施避免任何简单的 XSS 插入,因为我的 XML 库将转义任何特殊字符,但我想还有更复杂的攻击。
As part of a project, I'm accepting text from a user via web form and displaying it on a web page. The text they provide may contain URLs, if so I'd like to render it as a hyperlink for improved experience. For example the user might submit text containing http://www.google.com
and I want to convert it to <a href="http://www.google.com">...
I'm wondering what security issues I should be aware of while doing this. I've already taken measures to avoid any simple XSS insertions, because my XML library will escape any special characters, but I imagine there are more sophisticated attacks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除了忽略 javascript: 之外,您可能应该只为 http: 协议创建超链接,因为某些应用程序可以通过其他协议启动或控制。我想到的是 Steam、Skype 和 AOL Messenger。
In addition to ignoring javascript:, you should probably only make hyperlinks for the http: protocol, because there are certain applications that can be launched or controlled through other protocols. Steam, Skype, and AOL Messenger come to mind.
如果您仅使用
a
元素包围 URL,则唯一会出现的问题是它们输入恶意 URL(可能会被缩短)并且您最终单击它(前提是所有其他攻击方式都是安全的)在你的软件中(例如不能任意执行JavaScript等)。确保在匹配 URL 时不考虑
javascript:
伪协议。这样不会有什么好的结果。If you are only surrounding URLs with
a
elements, the only problem that should arrise if they enter a malicious URL (it might be shortened) and you end up clicking it, provided all other means of attack are secure in your software (e.g. can not execute arbitrarily JavaScript, etc).Make sure you don't consider the
javascript:
pseudo protocol when you are matching URLs. Nothing nice could come of that.