Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Ruby中,插入字符串的数据会导致字符串终止吗?_Ruby_String_String Interpolation - Fatal编程技术网

在Ruby中,插入字符串的数据会导致字符串终止吗?

在Ruby中,插入字符串的数据会导致字符串终止吗?,ruby,string,string-interpolation,Ruby,String,String Interpolation,在Ruby中,通过插值添加到字符串中的数据是否可以终止字符串?例如: "This remains#{\somekindofmagic} and this does not" # => "This remains" 我想不会,但我想确定 something.send("#{untrusted_input}=", more_untrusted_input) 实际上并没有留下插入字符串可以终止并用于发送eval的方式。对于输入字符串数据AFAIK是不可能的。RubyStrings可以包含任意

在Ruby中,通过插值添加到字符串中的数据是否可以终止字符串?例如:

"This remains#{\somekindofmagic} and this does not" # => "This remains"
我想不会,但我想确定

something.send("#{untrusted_input}=", more_untrusted_input)

实际上并没有留下插入字符串可以终止并用于发送eval的方式。

对于输入字符串数据AFAIK是不可能的。Ruby
String
s可以包含任意二进制数据,不应该有任何字节的神奇组合提前终止
String

如果您担心对Ruby字符串的“注入”式攻击,那么如果输入是以已转换为字符串的外部数据的形式进行的,那么这通常不容易实现(并且您对触发eval的特定担忧不会发生)。这种类型的攻击依赖于将输入字符串传递到其他解释器(例如SQL或JavaScript)而不正确转义语言结构的代码

但是,如果在同一过程中,
String
参数以Ruby对象的形式来自不受信任的Ruby代码,则可能会给它们添加副作用:

class BadString
  def to_s
    puts "Payload"
    "I am innocent"
  end
end

b = BadString.new

c = "Hello #{b}"
Payload
 => "Hello I am innocent"
编辑:你的例子

something.send("#{untrusted_input}=", more_untrusted_input)

我还是有点担心,如果
untrusted\u input
真的不可信,那么您很大程度上依赖于这样一个事实,即没有任何方法以
=
结尾,您会不高兴调用它。有时,由于使用框架或gem,可以在核心类上定义新方法,您可能不知道它们,或者它们可能出现在gem的更高版本中。就我个人而言,出于这个原因,我会将允许的方法名列入白名单,或者对传入数据使用其他验证方案,而不管您对开放式eval有多安全。

ruby中的字符串在内部被处理为堆上的字节数组和保存字符串长度的整数。因此,在C中,NUL字节(
\0
)终止字符串,但在ruby中不能发生这种情况

这里有更多关于ruby字符串内部的信息(还包括ruby 1.9中长度超过23字节的ruby字符串速度较慢的原因)