Ruby 的 Object#taint 和 Object#trust 方法是什么? [英] What are the Ruby's Object#taint and Object#trust methods?

查看:33
本文介绍了Ruby 的 Object#taint 和 Object#trust 方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 docs 中阅读了有关 Ruby 字符串方法的内容并遇到了方法

I was reading about Ruby string methods in the docs and came accross the methods

  • 污点
  • 信任
  • 无污染
  • 不信任

我不知道它们是做什么的,我们在什么情况下使用它们?有没有人使用过其中的任何一个?例子会很好.

I don't know what they do, which situation do we use them? Has anyone used any of them? Examples would be nice.

推荐答案

tainttrust 是 Ruby 安全模型的一部分.在 Ruby 中,每个对象都有一些随身携带的标志,其中两个是 Trusted 标志和 Tainted 标志.这些标志的作用取决于所谓的安全级别.安全级别存储在 $SAFE 中.

taint and trust are part of Ruby's security model. In Ruby, each object has a few flags that it carries around with it, two of which are the Trusted flag and the Tainted flag. How these flags are acted on depends on something called the safe level. The safe level is stored in $SAFE.

程序中的每个线程和纤程都可以有自己的安全级别.安全级别的范围从 0 到 4,0 表示没有安全性,4 表示非常安全,只有在您evaling 代码时才应该使用它.您不能$SAFE 分配比现有值更低的值.此外,在 Ruby 脚本作为 setuid 运行的 UNIX 系统上,Ruby 会自动将安全级别设置为 1.

Each thread and fiber in a program can have its own safe level. Safe levels range from 0 through 4, with 0 enforcing no security and 4 enforcing so much it should only be used when you're evaling code. You can't assign a lower value to $SAFE than it already has. Also, on UNIX systems where a Ruby script runs as setuid, Ruby automatically sets the safe level to 1.

当一个对象设置了它的污染标志时,这大致意味着该对象来自不可靠的来源,因此不能用于敏感操作.当安全级别为 0 时,将忽略 taint 标志(但仍然设置,如果需要,您可以关注它).有一些与 tainting 相关的方法:

When a object has it's tainted flag set, that means, roughly, that the object came from an unreliable source and therefore can't be used in sensitive operations. When the safe level is 0, the taint flag is ignored (but still set, you can pay attention to it if you want). There are a few methods related to tainting:

  • taint -- 使对象受到污染.除了安全级别 4,您可以在所有级别上污染对象.
  • tainted? -- 检查对象是否被污染.
  • untaint -- 从对象中移除污点.这可以用于安全级别 0、1 和 2.
  • taint -- Make an object tainted. You can taint an object on all levels with the exception of safe level 4.
  • tainted? -- Check if an object is tainted.
  • untaint -- Remove tainting from an object. This can only be used in safe levels 0, 1, and 2.

以下是 pragprog 镐(来源)的一个示例,显示了污染:

Here's an example from the pragprog pickaxe (source) that shows tainting:

# internal data
# =============
x1 = "a string"
x1.tainted?     → false
x2 = x1[2, 4]
x2.tainted?     → false
x1 =~ /([a-z])/ → 0
$1.tainted?     → false
# external data
# =============
y1 = ENV["HOME"]
y1.tainted?      → true
y2 = y1[2, 4]
y2.tainted?      → true
y1 =~ /([a-z])/  → 1
$1.tainted?      → true

总而言之,您不能对受污染的数据使用危险的方法.因此,如果您在安全级别 3 中执行此操作,则会出现错误:

To summarize, you can't use dangerous methods on tainted data. So if you do this in safe level 3, you'd get an error:

eval(gets)

信任

信任要简单得多.信任与对象是否来自受信任或不受信任的来源有关——基本上,它是否来自低于安全级别 4 或安全级别 4 的任何内容.我不确定 Ruby 的信任究竟有什么影响,但请看一下看这里:http://www.ruby-forum.com/topic/1887006.

这里有更多资源:http://phrogz.net/ProgrammingRuby/taint.html -- 一些安全级别的好东西,但我认为它是从 1.8 开始的——1.9 有一个更新版本,只是在本书的印刷版中.

Here are some more resources: http://phrogz.net/ProgrammingRuby/taint.html -- Some great stuff on safe levels, but I think it's from 1.8 -- there is an updated version for 1.9, just only in the printed version of the book.

http://www.ruby-forum.com/topic/79295 -- 关于安全是否足够安全.

http://www.ruby-forum.com/topic/79295 -- On whether safe is safe enough.

这篇关于Ruby 的 Object#taint 和 Object#trust 方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆