让用户指定 RazorEngine 模板有多危险?
我有类似邮件合并的功能,它需要一个模板、一些业务对象,并生成 html,然后将其转换为 PDF。
我正在使用 RazorEngine 将模板+模型转换为 html 位。
如果我让用户指定模板,我会冒什么风险?是否可以降低任何风险?
例如,用户是否可以执行任意代码? (删除文件、更改数据库等?)有什么方法可以检测到这类事情吗? (我知道这通常是不可能的,但是 razor 模板中的代码位应该是模型属性获取,或者可能是基于模型属性值的 if 语句)。
我基本上信任这里的用户(这是一个小型私人项目),但随着模板引擎的发展,这个引擎对于这个应用程序来说似乎过于强大。
I have mail-merge like functionality, which takes a template, some business object, and produces html which is then made into PDF.
I'm using RazorEngine to do the template+model to html bit.
If I let the users specify the templates, what risks am I taking? Is it possible to mitigate any risks?
For example, could the users execute arbitrary code? (delete files, alter database, etc.?) Is there some way I can detect this sort of thing? (I know that would be impossible generally, but the bits of code in the razor template should be model property gets, or possibly if statements based on model property values).
I do basically trust the users here (it's a small private project), but as templating engines go, this one seems excessively powerful for this application.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在版本 3 中,我引入了一个
IsolatedTemplateService
,它支持在另一个AppDomain
中解析/编译模板。您将能够控制将在其中编译模板的应用程序域的创建,这意味着您可以通过将安全策略应用于子应用程序域本身来引入所需的任何安全要求。在未来的推动中,我希望引入一种向管道添加扩展的通用方法,以便您可以执行代码生成检查等操作。我想这将启用在编译之前对生成的代码进行类型检查的方案。
几天前,我将 RazorEngine (v3) 的早期版本推到了 GitHub 上。请随意检查一下。 https://github.com/Antaris/RazorEngine
In version 3 I've introduced an
IsolatedTemplateService
which supports the parsing/compiling of templates in anotherAppDomain
. You'll be able to control the creation of the application domain that templates will be compiled in, which means you can introduce whatever security requirements you want by applying security policies to the child application domain itself.In future pushes, I am hoping to introduce a generic way for adding extensions to the pipeline, so you can do things like code generation inspection. I would imagine this will enable scenarios for type checking of the generated code before it is compiled.
I pushed an early version of RazorEngine (v3) onto GitHub a few days ago. Feel free to check it out. https://github.com/Antaris/RazorEngine
cshtml Razor 文件能够执行任何内容。 NET 代码在网站的上下文中,所以是的,允许用户提供它们是存在安全风险的。
通过接受更通用的 HTML 模板,并使用自定义标记来输入模型数据,您会得到更好的服务。
A cshtml Razor file is able to execute any. NET code in the context of the site so yes, it is a security risk to permit them to be supplied by users.
You would be better served by accepting a more general HTML template, with custom tokens to input Model data.
我相信已删除
using
语句并替换任何@System.[...]
如System.IO.File.Delete(filepath)
使用正则表达式可以减少大量可能的安全漏洞。请记住,模板在上下文中运行,并且只能访问其中可用的内容,但也包括 .NET Framework 程序集。
I believe that having removed
using
statements and replacing any@System.[...]
likeSystem.IO.File.Delete(filepath)
using regex can reduce a fair amount of possible security holes.Keep in mind that the Template runs inside a context and can access only what is available in it but that includes also .NET Framework assemblies.