如何将 Ruby 绑定标记为可信?
从这篇文章 http://www.stuartellis.eu/articles/erb 引用线程安全级别:
“在此级别,指定的绑定必须标记为受信任,ERB 才能使用它。”
我到处搜索,但没有找到一种将绑定“标记”为“可信”的方法。
有人请启发我吗?
From this article http://www.stuartellis.eu/articles/erb referring to thread safety levels:
"At this level, the specified binding must be marked as trusted for ERB to use it."
I've searched high and low and haven't found a way to "mark" a Binding as "trusted".
Will somebody please enlighten me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该通过调用
taint
方法来污染绑定。$SAFE
级别是 Ruby 的一项功能,它根据当前级别以及对象是否被污染来拒绝某些操作。受污染的字符串被假定源自不受信任的来源,例如文件、数据库、HTTP 客户端等。例如,在
$SAFE
级别 1 下,Ruby 将不允许您如果参数是受污染的字符串,则需要
文件。$SAFE
4 级是最极端的。 Ruby 将有效地禁止您修改任何未包含的对象。这个想法是,您可以在应用程序中使用较低的$SAFE
级别,并使用$SAFE
级别 4 实例化线程或过程。在此沙箱,您只能修改受污染的对象。ERB 使用此机制允许您在沙箱中运行模板。如果您尝试从某个绑定获取渲染模板的结果,则会发生以下情况:
Blam! 这是 Ruby 告诉您不能修改未受污染的模板 > 位于
$SAFE
级别 4 的对象。它不允许您使用给定的绑定调用eval
(这正是 ERB 所尝试的)。相反,您应该为沙箱提供受污染的绑定。您明确告诉 Ruby,可以在沙箱中使用此绑定,并且在沙箱之外不应信任它。
有关 Ruby 的
$SAFE
级别的更多信息,请参阅Pickaxe 书中的精彩描述< /a>.You should taint the binding by calling the
taint
method.The
$SAFE
levels are a feature of Ruby that denies certain actions depending on the current level and whether an object is tainted. Tainted strings are assumed to originate from an untrusted source, such as a file, a database, a HTTP client, etc.At
$SAFE
level 1, for example, Ruby will not allow you torequire
files if the argument is a tainted string.$SAFE
level 4 is the most extreme. Ruby will effectively disallow you to modify any nontained object. The idea is that you can use a lower$SAFE
level in your application, and instantiate a thread or proc with$SAFE
level 4. Within this sandbox, you can modify tainted objects only.ERB uses this mechanism to allow you to run a template within a sandbox. If you try to get the result of a rendered template from a certain binding, this is what happens:
Blam! This is Ruby telling you that it is not okay to modify a nontainted object at
$SAFE
level 4. It will not allow you to calleval
with the given binding (which is exactly what ERB attempts).Instead, you should provide the sandbox with a tainted binding. You are explicitly telling Ruby that it is okay to use this binding in a sandbox, and that it should not be trusted outside the sandbox.
For more information about Ruby's
$SAFE
level, see the excellent description in the Pickaxe book.