Tef*_*Ted 5 ruby multithreading trusted
从这篇文章http://www.stuartellis.eu/articles/erb参考线程安全级别:
"在此级别,必须将指定的绑定标记为受信任,以便ERB使用它."
我搜索过高低,并没有找到一种方法将"绑定"标记为"可信".
有人请赐教吗?
您应该通过调用方法来污染绑定taint.
这些$SAFE级别是Ruby的一个功能,它根据当前级别以及对象是否受到污染而拒绝某些操作.假定污染字符串源自不受信任的来源,例如文件,数据库,HTTP客户端等.
$SAFE例如,在级别1,require如果参数是受污染的字符串,Ruby将不允许您使用文件.
$SAFE4级是最极端的.Ruby将有效地禁止您修改任何未包含的对象.我们的想法是,您可以$SAFE在应用程序中使用较低级别,并使用$SAFE级别4 实例化线程或proc.在此沙箱中,您只能修改受污染的对象.
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
Run Code Online (Sandbox Code Playgroud)
布拉姆!这是Ruby告诉你,在第4级修改一个未着色的对象是不可行的.$SAFE它不允许你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!"
Run Code Online (Sandbox Code Playgroud)
有关Ruby $SAFE级别的更多信息,请参阅Pickaxe书中的优秀描述.
| 归档时间: |
|
| 查看次数: |
856 次 |
| 最近记录: |