在 Clojure/Compojure 中转义/清理用户输入
我正在使用 Clojure/Ring/Compojure-0.4/Enlive 堆栈来构建 Web 应用程序。
此堆栈中是否有函数可以剥离 HTML 或 HTML 编码(即 为
<a>
)用户提供的字符串为了防止XSS攻击?
I am using Clojure/Ring/Compojure-0.4/Enlive stack to build a web application.
Are there functions in this stack that would either strip HTML or HTML-encode (i.e. <a>
to <a>
) user-supplied strings in order to prevent XSS attacks?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
hiccup 中的
hiccup.util/escape-html
可以做到这一点。该函数曾经位于 Compojure 本身中(因为 hiccup 中的所有功能都曾经是 Compojure 的一部分)。这是一个足够简单的函数,您可以轻松地自己编写。还有 clojure.contrib.string/escape,它采用 char -> 的映射。字符串转义序列和字符串并为您转义它。
这让我觉得没有那么有用,因为你可能想转义多字符序列,而这不会让你这么做。但它可能适合您的 HTML 转义需求。
当然,还有许多用于此目的的 Java 库。您可以使用 Apache Commons 中的 StringEscapeUtils
:不过,我觉得这个目的有点重量级。
hiccup.util/escape-html
in hiccup does it. That function used to be in Compojure itself (since all of the functionality in hiccup used to be part of Compojure). It's a simple enough function that you could easily write it yourself though.There's also
clojure.contrib.string/escape
, which takes a map of char -> string escape sequences and a string and escapes it for you.This strikes me as not as useful as it could be, because you might want to escape multi-character sequences and this won't let you. But it might work for your HTML-escaping needs.
And then there are many Java libraries for this, of course. You could use StringEscapeUtils from Apache Commons:
This strikes me as a bit heavyweight for this purpose though.
更新:我知道一定不止这些...
来自
ring-core
的ring.util.codec
有一个名为 which work 的函数像这样:这些是 java.net.URLEncoder 和 java.net.URLDecoder 的包装器。相同的命名空间提供了基于 Apache Commons 中的类来处理 Base64 编码的函数。
原始答案如下。
我不确定是否有公共函数可以执行此操作,但
Enlive
有两个名为xml-str
的私有函数和attr-str
执行此操作:(attr-str
也转义"
。)您可以使用
@# 获取该函数'net.cgrand.enlive-html/xml-str
(Clojure 并不倾向于使事情真正私有...)或者只是将其复制到您自己的命名空间。Update: I knew there had to be more than that...
ring.util.codec
fromring-core
has a functions called which work like so:These are wrappers around
java.net.URLEncoder
andjava.net.URLDecoder
. The same namespace provides functions for dealing with Base64 encoding, based on a class from Apache Commons.Original answer follows.
I'm not sure whether there is a public function to do this, but
Enlive
has two private functions calledxml-str
andattr-str
which do this:(
attr-str
also escapes"
.)You could get at that function with
@#'net.cgrand.enlive-html/xml-str
(Clojure doesn't tend to make things really private...) or just copy it to your own namespace.事实证明,如果您使用
net.cgrand.enlive-html/content
将文本放入 HTML 元素,Enlive 默认情况下会转义 HTML。It turns out Enlive does escape HTML by default if you use
net.cgrand.enlive-html/content
to put text into a HTML element.