无法理解命令行选项“-T”在实践中是如何使用的

时间:2013-02-13 22:21:49

标签: ruby ruby-1.9.3

无法理解如何在实践中使用下面的命令行选项。

-T[level=1]

我试过这段代码:

#commandoptionstest.rb
puts "hello world"

有各种SAFE级别:

输出正常

@ubuntu:~/script$ ruby -x commandoptionstest.rb
# => hello world

为什么会出错?在commandoptionstest.rb中我需要做什么才能-x -T {/ 1}}?

@ubuntu:~/script$ ruby -x -T commandoptionstest.rb
# => ruby: no -x allowed in tainted mode (SecurityError)

输出即将到来

@ubuntu:~/script$ ruby -T commandoptionstest.rb
# => hello world

输出即将到来

@ubuntu:~/script$ ruby -T1 commandoptionstest.rb
# => hello world

输出即将到来

@ubuntu:~/script$ ruby -T2 commandoptionstest.rb
# => hello world

输出即将到来

@ubuntu:~/script$ ruby -T3 commandoptionstest.rb
# => hello world

再次出现错误原因?

@ubuntu:~/script$ ruby -T4 commandoptionstest.rb
# => commandoptionstest.rb:15:in `write': Insecure operation `write' at level 4 (SecurityError)
#   from commandoptionstest.rb:15:in `puts'
#   from commandoptionstest.rb:15:in `puts'
#   from commandoptionstest.rb:15:in `<main>'

在上述代码的帮助下,您能解释为什么SAFE级别123正在打印"hello world",而{ {1}}等级SAFE不是吗?要允许4SAFE的写操作,应该在这做什么?

2 个答案:

答案 0 :(得分:2)

设置$SAFE级别。

这决定了输入的处理方式,以及有关环境变量,I / O,线程,异常,解释器命令行参数等的大量其他内容。

http://www.ruby-doc.org/docs/ProgrammingRuby/html/taint.html

IMO文档是一个很好的起点。如果您对特定的行为有疑问,请询问。


要解决您的评论和修改内容:

是的,我可以,但文档可以,可能更好。

为什么-x不起作用?

因为文档说它不会:

  

$ SAFE&gt; = 1
  *不允许使用命令行选项-e,-i,-I,-r,-s,-S和-x。

[〜] $ ruby​​ --help 用法:ruby [开关] [ - ] [programfile] [参数]   #elided   -T [level = 1]打开污点检查

因此,如果指定的-T没有数字,则默认级别为1,这意味着$SAFE >= 1,这意味着文档的确切含义:-x是不允许的。

为什么puts无效?

很难说因为你实际上没有提供你正在执行的代码,但很有可能,再次,正如文档所说:

  

$ SAFE&gt; = 4
  *无法写入文件或管道。

答案 1 :(得分:0)

The perl CGI FAQ在解释这方面做得比我做得好得多。基本上,这是确保您的参数得到验证的一种方法。

相关问题