如何将 Ruby 绑定标记为可信?

发布于 2024-09-16 17:30:51 字数 227 浏览 6 评论 0原文

从这篇文章 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

娇妻 2024-09-23 17:30:51

您应该通过调用 taint 方法来污染绑定。

$SAFE 级别是 Ruby 的一项功能,它根据当前级别以及对象是否被污染来拒绝某些操作。受污染的字符串被假定源自不受信任的来源,例如文件、数据库、HTTP 客户端等。

例如,在 $SAFE 级别 1 下,Ruby 将不允许您 如果参数是受污染的字符串,则需要 文件。

$SAFE 4 级是最极端的。 Ruby 将有效地禁止您修改任何未包含的对象。这个想法是,您可以在应用程序中使用较低的 $SAFE 级别,并使用 $SAFE 级别 4 实例化线程或过程。在此沙箱,您只能修改受污染的对象。

ERB 使用此机制允许您在沙箱中运行模板。如果您尝试从某个绑定获取渲染模板的结果,则会发生以下情况:

class TemplateContext
  def name; "Teflon Ted"; end
end

template_binding = TemplateContext.new.send(:binding)
ERB.new("Hi, <%= name %>!", 4).result(template_binding)

#=> SecurityError: Insecure: can't modify trusted binding

Blam! 这是 Ruby 告诉您不能修改未受污染的模板 > 位于 $SAFE 级别 4 的对象。它不允许您使用给定的绑定调用 eval(这正是 ERB 所尝试的)。

相反,您应该为沙箱提供受污染的绑定。您明确告诉 Ruby,可以在沙箱中使用此绑定,并且在沙箱之外不应信任它。

class TemplateContext
  def name; "Teflon Ted"; end
end

# Binding must be tainted!
template_binding = TemplateContext.new.send(:binding).taint
ERB.new("Hi, <%= name %>!", 4).result(template_binding)

#=> "Hi, Teflon Ted!"

有关 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 to require 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:

class TemplateContext
  def name; "Teflon Ted"; end
end

template_binding = TemplateContext.new.send(:binding)
ERB.new("Hi, <%= name %>!", 4).result(template_binding)

#=> SecurityError: Insecure: can't modify trusted binding

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 call eval 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.

class TemplateContext
  def name; "Teflon Ted"; end
end

# Binding must be tainted!
template_binding = TemplateContext.new.send(:binding).taint
ERB.new("Hi, <%= name %>!", 4).result(template_binding)

#=> "Hi, Teflon Ted!"

For more information about Ruby's $SAFE level, see the excellent description in the Pickaxe book.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文