safe_shell 1.1.0 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/.github/workflows/test.yml +34 -0
- data/README.md +9 -7
- data/Rakefile +0 -11
- data/lib/safe_shell/version.rb +1 -1
- data/lib/safe_shell.rb +14 -10
- data/safe_shell.gemspec +1 -0
- data/spec/safe_shell_spec.rb +27 -5
- metadata +67 -53
- data/.document +0 -5
- data/.travis.yml +0 -17
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
|
-
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 72c02c96f3fad6fa465c03602fdabd9a6200352439f3f1adc59b791341418a54
|
|
4
|
+
data.tar.gz: 5511b235e447e222d62c84500257f82b3805e0524d064c85a9a8635ddcb95ecc
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 738b2014ff22af4a1aacbe44a71bc126c5717b1576a67b6c393ad5edc18cf17306db7cadd8e1524d897875eb897dfee6b5a74af99c9a879955a00a482661f9d3
|
|
7
|
+
data.tar.gz: cd578a5cdc0c0cc6e4815fe53235a18208dc3f9f6fe0fedcf8141dba12bab2166f5ff8ba4ed8d1d0c16c856b8d2c220356e3b515455dde78c2d8053d19550368
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Tests
|
|
3
|
+
on: [ push, pull_request ]
|
|
4
|
+
jobs:
|
|
5
|
+
test:
|
|
6
|
+
name: Test (Ruby ${{ matrix.ruby }})
|
|
7
|
+
runs-on: ubuntu-24.04
|
|
8
|
+
strategy:
|
|
9
|
+
fail-fast: false
|
|
10
|
+
matrix:
|
|
11
|
+
ruby:
|
|
12
|
+
- '2.0'
|
|
13
|
+
- '2.1'
|
|
14
|
+
- '2.2'
|
|
15
|
+
- '2.3'
|
|
16
|
+
- '2.4'
|
|
17
|
+
- '2.5'
|
|
18
|
+
- '2.6'
|
|
19
|
+
- '2.7'
|
|
20
|
+
- '3.0'
|
|
21
|
+
- '3.1'
|
|
22
|
+
- '3.2'
|
|
23
|
+
- '3.3'
|
|
24
|
+
- '3.4'
|
|
25
|
+
- '4.0'
|
|
26
|
+
- 'jruby'
|
|
27
|
+
- 'truffleruby'
|
|
28
|
+
steps:
|
|
29
|
+
- uses: actions/checkout@v3
|
|
30
|
+
- uses: ruby/setup-ruby@v1
|
|
31
|
+
with:
|
|
32
|
+
ruby-version: ${{ matrix.ruby }}
|
|
33
|
+
bundler-cache: true
|
|
34
|
+
- run: bundle exec rake
|
data/README.md
CHANGED
|
@@ -6,20 +6,20 @@ SafeShell lets you execute shell commands and get the resulting output, but with
|
|
|
6
6
|
|
|
7
7
|
Install gem:
|
|
8
8
|
|
|
9
|
-
```
|
|
9
|
+
```sh
|
|
10
10
|
gem install safe_shell
|
|
11
11
|
```
|
|
12
12
|
|
|
13
13
|
Use gem:
|
|
14
14
|
|
|
15
|
-
```
|
|
15
|
+
```ruby
|
|
16
16
|
require 'safe_shell'
|
|
17
17
|
SafeShell.execute("echo", "Hello, world!")
|
|
18
18
|
```
|
|
19
19
|
|
|
20
20
|
SafeShell sets the $? operator to the process status, in the same manner as the backtick operator.
|
|
21
21
|
|
|
22
|
-
```
|
|
22
|
+
```ruby
|
|
23
23
|
# Send stdout and stderr to files:
|
|
24
24
|
SafeShell.execute("echo", "Hello, world!", :stdout => "output.txt", :stderr => "error.txt")
|
|
25
25
|
|
|
@@ -37,7 +37,7 @@ SafeShell.execute!("echo", "Hello, world!")
|
|
|
37
37
|
|
|
38
38
|
If you use backticks to process a file supplied by a user, a carefully crafted filename could allow execution of an arbitrary command:
|
|
39
39
|
|
|
40
|
-
```
|
|
40
|
+
```ruby
|
|
41
41
|
file = ";blah"
|
|
42
42
|
`echo #{file}`
|
|
43
43
|
sh: blah: command not found
|
|
@@ -46,18 +46,20 @@ sh: blah: command not found
|
|
|
46
46
|
|
|
47
47
|
SafeShell solves this.
|
|
48
48
|
|
|
49
|
-
```
|
|
49
|
+
```ruby
|
|
50
50
|
SafeShell.execute("echo", file)
|
|
51
51
|
=> ";blah\n"
|
|
52
52
|
```
|
|
53
53
|
|
|
54
|
+
The first argument is always the program to run and is never passed to a shell, even when it is the only argument. Each remaining argument reaches the program as a single, literal argument. A command string such as `"echo #{file}"` is therefore looked up as a program named `echo #{file}` and fails with `Errno::ENOENT`; pass the program and its arguments separately instead.
|
|
55
|
+
|
|
54
56
|
## Compatibility
|
|
55
57
|
|
|
56
|
-
Tested
|
|
58
|
+
Tested on every push against Ruby 2.0 through 4.0, JRuby, and TruffleRuby, on Linux. The gem requires Ruby 2.0 or newer. It is not tested on Windows.
|
|
57
59
|
|
|
58
60
|
## Test
|
|
59
61
|
|
|
60
|
-
```
|
|
62
|
+
```sh
|
|
61
63
|
bundle exec rake
|
|
62
64
|
````
|
|
63
65
|
|
data/Rakefile
CHANGED
|
@@ -6,14 +6,3 @@ require 'rspec/core/rake_task'
|
|
|
6
6
|
RSpec::Core::RakeTask.new(:spec)
|
|
7
7
|
|
|
8
8
|
task :default => :spec
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
require 'rdoc/task'
|
|
12
|
-
Rake::RDocTask.new do |rdoc|
|
|
13
|
-
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
|
14
|
-
|
|
15
|
-
rdoc.rdoc_dir = 'rdoc'
|
|
16
|
-
rdoc.title = "safe_shell #{version}"
|
|
17
|
-
rdoc.rdoc_files.include('README*')
|
|
18
|
-
rdoc.rdoc_files.include('lib/**/*.rb')
|
|
19
|
-
end
|
data/lib/safe_shell/version.rb
CHANGED
data/lib/safe_shell.rb
CHANGED
|
@@ -9,10 +9,16 @@ module SafeShell
|
|
|
9
9
|
env = opts[:env]
|
|
10
10
|
opts = {:in => read_end, :out => new_stdout, :err => new_stderr}
|
|
11
11
|
|
|
12
|
+
# Passing the program as a two-element array forces Kernel#spawn to exec
|
|
13
|
+
# it directly. A bare String with no further arguments would go through
|
|
14
|
+
# /bin/sh -c whenever it contains shell metacharacters.
|
|
15
|
+
program = [command.to_s, command.to_s]
|
|
16
|
+
argv = args.map { |a| a.to_s }
|
|
17
|
+
|
|
12
18
|
pid = if env
|
|
13
|
-
spawn(env,
|
|
19
|
+
spawn(env, program, *argv, opts)
|
|
14
20
|
else
|
|
15
|
-
spawn(
|
|
21
|
+
spawn(program, *argv, opts)
|
|
16
22
|
end
|
|
17
23
|
|
|
18
24
|
write_end.close
|
|
@@ -29,14 +35,12 @@ module SafeShell
|
|
|
29
35
|
|
|
30
36
|
def self.execute!(*args)
|
|
31
37
|
execute(*args).tap do
|
|
32
|
-
|
|
38
|
+
unless $?.success?
|
|
39
|
+
# Leave the options hash out of the message: :env is where callers
|
|
40
|
+
# put credentials for the child process.
|
|
41
|
+
argv = args.last.kind_of?(Hash) ? args[0...-1] : args
|
|
42
|
+
raise CommandFailedException.new("Shell command #{argv.inspect} failed with status #{$?}")
|
|
43
|
+
end
|
|
33
44
|
end
|
|
34
45
|
end
|
|
35
|
-
|
|
36
|
-
private
|
|
37
|
-
|
|
38
|
-
def self.raise_command_failed_exception(*args)
|
|
39
|
-
raise CommandFailedException.new("Shell command #{args.inspect} failed with status #{$?}")
|
|
40
|
-
end
|
|
41
|
-
|
|
42
46
|
end
|
data/safe_shell.gemspec
CHANGED
|
@@ -12,6 +12,7 @@ Gem::Specification.new do |s|
|
|
|
12
12
|
s.description = %q{Execute shell commands and get the resulting output, but without the security problems of Ruby’s backtick operator.}
|
|
13
13
|
s.license = "MIT"
|
|
14
14
|
|
|
15
|
+
s.add_development_dependency "rake"
|
|
15
16
|
s.add_development_dependency "rspec"
|
|
16
17
|
|
|
17
18
|
s.files = `git ls-files`.split("\n")
|
data/spec/safe_shell_spec.rb
CHANGED
|
@@ -11,6 +11,18 @@ describe "SafeShell" do
|
|
|
11
11
|
expect(SafeShell.execute("echo", ";date")).to eql(";date\n")
|
|
12
12
|
end
|
|
13
13
|
|
|
14
|
+
it "never passes a command with no arguments to a shell" do
|
|
15
|
+
expect {
|
|
16
|
+
SafeShell.execute("echo ;echo INJECTED")
|
|
17
|
+
}.to raise_error(Errno::ENOENT)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
it "never passes a command with only options to a shell" do
|
|
21
|
+
expect {
|
|
22
|
+
SafeShell.execute("echo $HOME", :env => { "HOME" => "/nowhere" })
|
|
23
|
+
}.to raise_error(Errno::ENOENT)
|
|
24
|
+
end
|
|
25
|
+
|
|
14
26
|
it "allows to add new env vars" do
|
|
15
27
|
result = SafeShell.execute('env')
|
|
16
28
|
expect(result).to_not include("HELLO=world")
|
|
@@ -35,18 +47,18 @@ describe "SafeShell" do
|
|
|
35
47
|
|
|
36
48
|
context "output redirection" do
|
|
37
49
|
before do
|
|
38
|
-
File.delete("tmp/output.txt") if File.
|
|
50
|
+
File.delete("tmp/output.txt") if File.exist?("tmp/output.txt")
|
|
39
51
|
end
|
|
40
52
|
|
|
41
53
|
it "should let you redirect stdout to a file" do
|
|
42
54
|
SafeShell.execute("echo", "Hello, world!", :stdout => "tmp/output.txt")
|
|
43
|
-
expect(File.
|
|
55
|
+
expect(File.exist?("tmp/output.txt")).to eql(true)
|
|
44
56
|
expect(File.read("tmp/output.txt")).to eql("Hello, world!\n")
|
|
45
57
|
end
|
|
46
58
|
|
|
47
59
|
it "should let you redirect stderr to a file" do
|
|
48
60
|
SafeShell.execute("cat", "tmp/nonexistent-file", :stderr => "tmp/output.txt")
|
|
49
|
-
expect(File.
|
|
61
|
+
expect(File.exist?("tmp/output.txt")).to eql(true)
|
|
50
62
|
expect(File.read("tmp/output.txt")).to eql("cat: tmp/nonexistent-file: No such file or directory\n")
|
|
51
63
|
end
|
|
52
64
|
end
|
|
@@ -56,10 +68,20 @@ describe "SafeShell" do
|
|
|
56
68
|
expect(SafeShell.execute!("echo", "Hello, world!")).to eql("Hello, world!\n")
|
|
57
69
|
end
|
|
58
70
|
|
|
59
|
-
it "raises an exception
|
|
71
|
+
it "raises an exception if the command fails" do
|
|
60
72
|
expect {
|
|
61
73
|
SafeShell.execute!("test", "a", "=", "b")
|
|
62
|
-
}.to raise_error(SafeShell::CommandFailedException)
|
|
74
|
+
}.to raise_error(SafeShell::CommandFailedException, /\["test", "a", "=", "b"\] failed with status/)
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
it "keeps environment variables out of the exception message" do
|
|
78
|
+
expect {
|
|
79
|
+
SafeShell.execute!("false", :env => { "SECRET" => "hunter2" })
|
|
80
|
+
}.to raise_error(SafeShell::CommandFailedException) { |error|
|
|
81
|
+
expect(error.message).to include('["false"]')
|
|
82
|
+
expect(error.message).not_to include("hunter2")
|
|
83
|
+
expect(error.message).not_to include("SECRET")
|
|
84
|
+
}
|
|
63
85
|
end
|
|
64
86
|
end
|
|
65
87
|
|
metadata
CHANGED
|
@@ -1,78 +1,92 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: safe_shell
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.1.
|
|
4
|
+
version: 1.1.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
|
-
- Envato
|
|
8
|
-
- Ian Leitch
|
|
9
|
-
- Pete Yandell
|
|
10
|
-
autorequire:
|
|
7
|
+
- Envato
|
|
8
|
+
- Ian Leitch
|
|
9
|
+
- Pete Yandell
|
|
11
10
|
bindir: bin
|
|
12
11
|
cert_chain: []
|
|
13
|
-
date:
|
|
12
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
14
13
|
dependencies:
|
|
15
|
-
- !ruby/object:Gem::Dependency
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
14
|
+
- !ruby/object:Gem::Dependency
|
|
15
|
+
name: rake
|
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
|
17
|
+
requirements:
|
|
18
|
+
-
|
|
19
|
+
- ">="
|
|
20
|
+
- !ruby/object:Gem::Version
|
|
21
|
+
version: "0"
|
|
22
|
+
type: :development
|
|
23
|
+
prerelease: false
|
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
25
|
+
requirements:
|
|
26
|
+
-
|
|
27
|
+
- ">="
|
|
28
|
+
- !ruby/object:Gem::Version
|
|
29
|
+
version: "0"
|
|
30
|
+
- !ruby/object:Gem::Dependency
|
|
31
|
+
name: rspec
|
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
|
33
|
+
requirements:
|
|
34
|
+
-
|
|
35
|
+
- ">="
|
|
36
|
+
- !ruby/object:Gem::Version
|
|
37
|
+
version: "0"
|
|
38
|
+
type: :development
|
|
39
|
+
prerelease: false
|
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
41
|
+
requirements:
|
|
42
|
+
-
|
|
43
|
+
- ">="
|
|
44
|
+
- !ruby/object:Gem::Version
|
|
45
|
+
version: "0"
|
|
46
|
+
description: "Execute shell commands and get the resulting output, but without the security problems of Ruby’s backtick operator."
|
|
31
47
|
email:
|
|
32
|
-
- pete@notahat.com
|
|
48
|
+
- pete@notahat.com
|
|
33
49
|
executables: []
|
|
34
50
|
extensions: []
|
|
35
51
|
extra_rdoc_files:
|
|
36
|
-
- LICENSE
|
|
37
|
-
- README.md
|
|
52
|
+
- LICENSE
|
|
53
|
+
- README.md
|
|
38
54
|
files:
|
|
39
|
-
-
|
|
40
|
-
-
|
|
41
|
-
-
|
|
42
|
-
-
|
|
43
|
-
-
|
|
44
|
-
-
|
|
45
|
-
-
|
|
46
|
-
- lib/safe_shell.rb
|
|
47
|
-
-
|
|
48
|
-
-
|
|
49
|
-
- spec/
|
|
50
|
-
-
|
|
51
|
-
|
|
52
|
-
homepage: http://github.com/envato/safe_shell
|
|
55
|
+
- .github/workflows/test.yml
|
|
56
|
+
- .gitignore
|
|
57
|
+
- Gemfile
|
|
58
|
+
- LICENSE
|
|
59
|
+
- README.md
|
|
60
|
+
- Rakefile
|
|
61
|
+
- lib/safe_shell.rb
|
|
62
|
+
- lib/safe_shell/version.rb
|
|
63
|
+
- safe_shell.gemspec
|
|
64
|
+
- spec/safe_shell_spec.rb
|
|
65
|
+
- spec/spec.opts
|
|
66
|
+
- tmp/.gitkeep
|
|
67
|
+
homepage: "http://github.com/envato/safe_shell"
|
|
53
68
|
licenses:
|
|
54
|
-
- MIT
|
|
69
|
+
- MIT
|
|
55
70
|
metadata: {}
|
|
56
|
-
post_install_message:
|
|
57
71
|
rdoc_options: []
|
|
58
72
|
require_paths:
|
|
59
|
-
- lib
|
|
73
|
+
- lib
|
|
60
74
|
required_ruby_version: !ruby/object:Gem::Requirement
|
|
61
75
|
requirements:
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
76
|
+
-
|
|
77
|
+
- ">="
|
|
78
|
+
- !ruby/object:Gem::Version
|
|
79
|
+
version: 2.0.0
|
|
65
80
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
66
81
|
requirements:
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
82
|
+
-
|
|
83
|
+
- ">="
|
|
84
|
+
- !ruby/object:Gem::Version
|
|
85
|
+
version: "0"
|
|
70
86
|
requirements: []
|
|
71
|
-
|
|
72
|
-
rubygems_version: 2.5.1
|
|
73
|
-
signing_key:
|
|
87
|
+
rubygems_version: 4.1.0
|
|
74
88
|
specification_version: 4
|
|
75
89
|
summary: Safely execute shell commands and get their output.
|
|
76
90
|
test_files:
|
|
77
|
-
- spec/safe_shell_spec.rb
|
|
78
|
-
- spec/spec.opts
|
|
91
|
+
- spec/safe_shell_spec.rb
|
|
92
|
+
- spec/spec.opts
|
data/.document
DELETED