惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

I
Intezer
V
Vulnerabilities – Threatpost
Google Online Security Blog
Google Online Security Blog
T
The Exploit Database - CXSecurity.com
C
CXSECURITY Database RSS Feed - CXSecurity.com
AWS News Blog
AWS News Blog
G
GRAHAM CLULEY
P
Privacy & Cybersecurity Law Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
C
Cybersecurity and Infrastructure Security Agency CISA
N
News | PayPal Newsroom
T
Tenable Blog
Spread Privacy
Spread Privacy
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
S
Secure Thoughts
P
Privacy International News Feed
IT之家
IT之家
Project Zero
Project Zero
T
The Blog of Author Tim Ferriss
Engineering at Meta
Engineering at Meta
大猫的无限游戏
大猫的无限游戏
博客园_首页
GbyAI
GbyAI
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
量子位
雷峰网
雷峰网
Apple Machine Learning Research
Apple Machine Learning Research
Hacker News: Ask HN
Hacker News: Ask HN
Google DeepMind News
Google DeepMind News
MongoDB | Blog
MongoDB | Blog
N
Netflix TechBlog - Medium
Martin Fowler
Martin Fowler
NISL@THU
NISL@THU
I
InfoQ
D
DataBreaches.Net
有赞技术团队
有赞技术团队
K
Kaspersky official blog
Security Latest
Security Latest
The Register - Security
The Register - Security
Hugging Face - Blog
Hugging Face - Blog
S
Security @ Cisco Blogs
P
Proofpoint News Feed
M
MIT News - Artificial intelligence
H
Hackread – Cybersecurity News, Data Breaches, AI and More
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
AI
AI
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Proofpoint News Feed
Security Archives - TechRepublic
Security Archives - TechRepublic
N
News and Events Feed by Topic

博客园 - Tachikoma

用Ruby实现 Web Service Server ,并用Ruby发送 HTTP请求 Web Service Android 使用 Ksoap2 出现的低级错误... - Tachikoma Cygwin中通过RJB在Ruby下调用ICTCLAS(JAVA) ICTCLAS4J 的编译脚本 Ruby在使用MongoDB时,对Cursor的重新包装 ruby 下使用 ICTCLAS(JAVA) RJB 在windows下的一些安装事项 Ruby手工测试正确,rcov测试失败的解决 在dell dimension 5150 上安装 leopard 手记 Rails测试中清空数据表/载入空fixtures RDoc 解决同名module 与 Class的问题 C 实用的 e-editor 的bundle Best of Ruby Quiz - Animal Quiz Best of Ruby Quiz - GEDCOM Parser Best of Ruby Quiz - MadLib - Tachikoma 使用selenium-on-rails的一些讨论 3]assertXpathCount的使用 使用selenium-on-rails的一些讨论 2]清理缓存 - Tachikoma - 博客园 使用selenium-on-rails的一些讨论 [0,1] 关于 rails ActiveRecord 属性 以及 foreign_key 不直接用数据库项目 时的一些讨论
Ruby Sandbox 实现运行客户代码 - Tachikoma
Tachikoma · 2010-03-24 · via 博客园 - Tachikoma

碰到的问题是需要 运行客户自己编程的代码,同时要保证安全性,在碰到 system("rm")时,保证不抓狂

Ruby 自身是带有安全机制的,详细参看 Programming Ruby 2nd , 第25章 Ruby 安全

简略的说Ruby有四个安全级(详细参看Programming Ruby),等级4正是客户代码运行的理想的环境,不会污染到其他代码,也不会破坏系统,相当严格

示例代码如下:

def safely(code)
	sandbox = lambda do
		input = "something"
		$SAFE = 4
		eval code
	end
	sandbox.call
end

code = File.new("file.rb").read
result = safely(code)
puts result

代码很简单

file.rb读入的code 是 "input * 2"

sandbox提供了code的运行环境 : input作为输入变量 , 提升了$SAFE等级

得到的输出是 "somethingsomething"

当我们在code中加入 system("rm") / system("del *") 时,得到以下错误

try.rb:5:in `safely': (eval):1:in `system': Insecure operation - system (Securit

yError)

Insecure operation - system (SecurityError)

$SAFE 成功保护了我们的系统 , 当试图侵入源程序,也会得到错误

问题讨论:

2. 考虑以下代码

def safely(code)
	temp_box = lambda do
		input = "something"
		eval code
	end
	sandbox = lambda do		
		$SAFE = 4
		temp_box.call
	end
	sandbox.call
end

code = File.new("file.rb").read
result = safely(code)
puts result

当code中的代码是 system("shutdown")的时候,你很有可能看不到下面的内容(是很有可能,可以来测试你是不是有管理员的权限,当然也可以用 system("format")测试,未知结果)

temp_box 的运行环境 $SAFE 是默认值0,不受之前$SAFE的影响,一定要注意

3. 剩下待解决的问题就是:code中不能声明函数,Programming Ruby 2nd中这样描述:不能在未污染的类或模块内定义、重定义、删除或取消定义方法。需要寻找方法让code中能声明函数.

补充:关于问题3的讨论

1. 考虑以下代码

class Test
	def safely
		sandbox = lambda do
			self.class.taint
			$SAFE = 4
			#code 
			def something
				"success !!"
			end
			something
			#code end
		end
		sandbox.call
	end
end

result = Test.new.safely
puts result

我们将在code域内成功定义方法,并得到输出 "success!!"

但是注意到 self.class.taint , 我们就看到整个Test类被污染了,something方法会渗透添加到 所有Test对象中,这违背了我们期望的沙盒原则

需要注意的是: def 中的def 是在类中添加实例方法

2. 对于以上问题的解决:

class Test
	def safely
		sandbox = lambda do
			self.taint
			$SAFE = 4
			#code 
			def self.something
				"success !!"
			end
			something
			#code end
		end
		sandbox.call
	end
end

result = Test.new.safely
puts result

以上代码只将 self这一个实例 "污染" ,定义了单例方法 something,得到了成功的结果

作为代价,方法定义的时候必须要 self. , 这个很不人性化,需要考虑其他的解决方案