safe_shell 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +22 -0
- data/LICENSE +20 -0
- data/README.rdoc +54 -0
- data/Rakefile +45 -0
- data/VERSION +1 -0
- data/lib/safe_shell.rb +24 -0
- data/spec/safe_shell_spec.rb +39 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +9 -0
- metadata +96 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Envato, Ian Leitch, & Pete Yandell.
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
= SafeShell
|
2
|
+
|
3
|
+
SafeShell lets you execute shell commands and get the resulting output, but without the security problems of Ruby's backtick operator.
|
4
|
+
|
5
|
+
== Usage
|
6
|
+
|
7
|
+
gem install safe_shell
|
8
|
+
|
9
|
+
require 'safe_shell'
|
10
|
+
SafeShell.execute("echo", "Hello, world!")
|
11
|
+
|
12
|
+
SafeShell sets the $? operator to the process status, in the same manner as the backtick operator.
|
13
|
+
|
14
|
+
# Send stdout and stderr to files:
|
15
|
+
SafeShell.execute("echo", "Hello, world!", :stdout => "output.txt", :stderr => "error.txt")
|
16
|
+
|
17
|
+
# Return true if the command exits with a zero status:
|
18
|
+
SafeShell.execute?("echo", "Hello, world!")
|
19
|
+
|
20
|
+
== Why?
|
21
|
+
|
22
|
+
If you use backticks to process a file supplied by a user, a carefully crafted filename could allow execution of an arbitrary command:
|
23
|
+
|
24
|
+
file = ";blah"
|
25
|
+
`echo #{file}`
|
26
|
+
sh: blah: command not found
|
27
|
+
=> "\n"
|
28
|
+
|
29
|
+
SafeShell solves this.
|
30
|
+
|
31
|
+
SafeShell.execute("echo", file)
|
32
|
+
=> ";blah\n"
|
33
|
+
|
34
|
+
== Compatibility
|
35
|
+
|
36
|
+
Tested with Ruby 1.8.7, but it should be happy on pretty much any Ruby version. Maybe not so much on Windows.
|
37
|
+
|
38
|
+
== Developing
|
39
|
+
|
40
|
+
* Fork the project.
|
41
|
+
* Make your feature addition or bug fix.
|
42
|
+
* Add tests for it. This is important so I don't break it in a
|
43
|
+
future version unintentionally.
|
44
|
+
* Commit, do not mess with rakefile, version, or history.
|
45
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
46
|
+
* Send me a pull request. Bonus points for topic branches.
|
47
|
+
|
48
|
+
== Status
|
49
|
+
|
50
|
+
In use on at least one big site, so should be pretty solid. There's not much to it, so I'm not expecting there'll be many releases.
|
51
|
+
|
52
|
+
== Copyright
|
53
|
+
|
54
|
+
Copyright (c) 2010 Envato, Ian Leitch, & Pete Yandell. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "safe_shell"
|
8
|
+
gem.summary = %Q{Safely execute shell commands and get their output.}
|
9
|
+
gem.description = %Q{Execute shell commands and get the resulting output, but without the security problems of Ruby’s backtick operator.}
|
10
|
+
gem.email = "pete@notahat.com"
|
11
|
+
gem.homepage = "http://github.com/envato/safe_shell"
|
12
|
+
gem.authors = ["Envato", "Ian Leitch", "Pete Yandell"]
|
13
|
+
gem.add_development_dependency "rspec", ">= 1.2.9"
|
14
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
|
+
end
|
16
|
+
Jeweler::GemcutterTasks.new
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'spec/rake/spectask'
|
22
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
23
|
+
spec.libs << 'lib' << 'spec'
|
24
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
25
|
+
end
|
26
|
+
|
27
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
28
|
+
spec.libs << 'lib' << 'spec'
|
29
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
30
|
+
spec.rcov = true
|
31
|
+
end
|
32
|
+
|
33
|
+
task :spec => :check_dependencies
|
34
|
+
|
35
|
+
task :default => :spec
|
36
|
+
|
37
|
+
require 'rake/rdoctask'
|
38
|
+
Rake::RDocTask.new do |rdoc|
|
39
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
40
|
+
|
41
|
+
rdoc.rdoc_dir = 'rdoc'
|
42
|
+
rdoc.title = "safe_shell #{version}"
|
43
|
+
rdoc.rdoc_files.include('README*')
|
44
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
45
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.0.0
|
data/lib/safe_shell.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
module SafeShell
|
2
|
+
def self.execute(command, *args)
|
3
|
+
opts = args.last.kind_of?(Hash) ? args.pop : {}
|
4
|
+
read_end, write_end = IO.pipe
|
5
|
+
new_stdout = opts[:stdout] ? File.open(opts[:stdout], "w+") : write_end
|
6
|
+
new_stderr = opts[:stderr] ? File.open(opts[:stderr], "w+") : write_end
|
7
|
+
pid = fork do
|
8
|
+
read_end.close
|
9
|
+
STDOUT.reopen(new_stdout)
|
10
|
+
STDERR.reopen(new_stderr)
|
11
|
+
exec(command, *args)
|
12
|
+
end
|
13
|
+
write_end.close
|
14
|
+
output = read_end.read
|
15
|
+
Process.waitpid(pid)
|
16
|
+
read_end.close
|
17
|
+
output
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.execute?(*args)
|
21
|
+
execute(*args)
|
22
|
+
$?.success?
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe "SafeShell" do
|
4
|
+
|
5
|
+
it "should return the output of the command" do
|
6
|
+
SafeShell.execute("echo", "Hello, world!").should == "Hello, world!\n"
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should safely handle dangerous characters in command arguments" do
|
10
|
+
SafeShell.execute("echo", ";date").should == ";date\n"
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should set $? to the exit status of the command" do
|
14
|
+
SafeShell.execute("test", "a", "=", "a")
|
15
|
+
$?.exitstatus.should == 0
|
16
|
+
|
17
|
+
SafeShell.execute("test", "a", "=", "b")
|
18
|
+
$?.exitstatus.should == 1
|
19
|
+
end
|
20
|
+
|
21
|
+
context "output redirection" do
|
22
|
+
before do
|
23
|
+
File.delete("tmp/output.txt") if File.exists?("tmp/output.txt")
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should let you redirect stdout to a file" do
|
27
|
+
SafeShell.execute("echo", "Hello, world!", :stdout => "tmp/output.txt")
|
28
|
+
File.exists?("tmp/output.txt").should be_true
|
29
|
+
File.read("tmp/output.txt").should == "Hello, world!\n"
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should let you redirect stderr to a file" do
|
33
|
+
SafeShell.execute("cat", "tmp/nonexistent-file", :stderr => "tmp/output.txt")
|
34
|
+
File.exists?("tmp/output.txt").should be_true
|
35
|
+
File.read("tmp/output.txt").should == "cat: tmp/nonexistent-file: No such file or directory\n"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
data/spec/spec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: safe_shell
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 23
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
- 0
|
10
|
+
version: 1.0.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Envato
|
14
|
+
- Ian Leitch
|
15
|
+
- Pete Yandell
|
16
|
+
autorequire:
|
17
|
+
bindir: bin
|
18
|
+
cert_chain: []
|
19
|
+
|
20
|
+
date: 2010-11-08 00:00:00 +11:00
|
21
|
+
default_executable:
|
22
|
+
dependencies:
|
23
|
+
- !ruby/object:Gem::Dependency
|
24
|
+
name: rspec
|
25
|
+
prerelease: false
|
26
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
27
|
+
none: false
|
28
|
+
requirements:
|
29
|
+
- - ">="
|
30
|
+
- !ruby/object:Gem::Version
|
31
|
+
hash: 13
|
32
|
+
segments:
|
33
|
+
- 1
|
34
|
+
- 2
|
35
|
+
- 9
|
36
|
+
version: 1.2.9
|
37
|
+
type: :development
|
38
|
+
version_requirements: *id001
|
39
|
+
description: "Execute shell commands and get the resulting output, but without the security problems of Ruby\xE2\x80\x99s backtick operator."
|
40
|
+
email: pete@notahat.com
|
41
|
+
executables: []
|
42
|
+
|
43
|
+
extensions: []
|
44
|
+
|
45
|
+
extra_rdoc_files:
|
46
|
+
- LICENSE
|
47
|
+
- README.rdoc
|
48
|
+
files:
|
49
|
+
- .document
|
50
|
+
- .gitignore
|
51
|
+
- LICENSE
|
52
|
+
- README.rdoc
|
53
|
+
- Rakefile
|
54
|
+
- VERSION
|
55
|
+
- lib/safe_shell.rb
|
56
|
+
- spec/safe_shell_spec.rb
|
57
|
+
- spec/spec.opts
|
58
|
+
- spec/spec_helper.rb
|
59
|
+
- tmp/.gitkeep
|
60
|
+
has_rdoc: true
|
61
|
+
homepage: http://github.com/envato/safe_shell
|
62
|
+
licenses: []
|
63
|
+
|
64
|
+
post_install_message:
|
65
|
+
rdoc_options:
|
66
|
+
- --charset=UTF-8
|
67
|
+
require_paths:
|
68
|
+
- lib
|
69
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
70
|
+
none: false
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
hash: 3
|
75
|
+
segments:
|
76
|
+
- 0
|
77
|
+
version: "0"
|
78
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
79
|
+
none: false
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
hash: 3
|
84
|
+
segments:
|
85
|
+
- 0
|
86
|
+
version: "0"
|
87
|
+
requirements: []
|
88
|
+
|
89
|
+
rubyforge_project:
|
90
|
+
rubygems_version: 1.3.7
|
91
|
+
signing_key:
|
92
|
+
specification_version: 3
|
93
|
+
summary: Safely execute shell commands and get their output.
|
94
|
+
test_files:
|
95
|
+
- spec/safe_shell_spec.rb
|
96
|
+
- spec/spec_helper.rb
|