summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeddy Wing2018-01-29 00:07:56 +0100
committerTeddy Wing2018-01-31 21:08:08 +0100
commite820b8a2c7901e25231b68274fa3c853573e4fa0 (patch)
tree9ad2b0fc7465ff182d13c4d8b4ff19cffacee1fe
parent327fde066844d6e35d7b723a052f6f8157536e9c (diff)
downloadhcl-e820b8a2c7901e25231b68274fa3c853573e4fa0.tar.bz2
App#save_password: Shell escape password
Escape special shell characters so that passwords with single quotes can be saved to the keychain. Otherwise, the single quotes surrounding the "-w '%s'" cause an error on passwords with single quotes. I know testing private methods is not recommended, but this seemed like something that would be good to test. Maybe the method should be made public if that's a concern?
-rw-r--r--lib/hcl/app.rb5
-rw-r--r--test/app_test.rb13
2 files changed, 16 insertions, 2 deletions
diff --git a/lib/hcl/app.rb b/lib/hcl/app.rb
index f15c071..b4c9fd3 100644
--- a/lib/hcl/app.rb
+++ b/lib/hcl/app.rb
@@ -1,5 +1,6 @@
require 'yaml'
require 'fileutils'
+require 'shellwords'
require 'trollop'
require 'highline/import'
@@ -226,10 +227,10 @@ EOM
end
def save_password config
- if system("security add-internet-password -U -l hcl -a '%s' -s '%s.harvestapp.com' -w '%s'" % [
+ if system("security add-internet-password -U -l hcl -a '%s' -s '%s.harvestapp.com' -w %s" % [
config['login'],
config['subdomain'],
- config['password'],
+ Shellwords.escape(config['password']),
]) then config.delete('password') end
end
end
diff --git a/test/app_test.rb b/test/app_test.rb
index 4c997d7..499909d 100644
--- a/test/app_test.rb
+++ b/test/app_test.rb
@@ -65,4 +65,17 @@ class AppTest < HCl::TestCase
assert_match /API failure/i, error_output
end
+ def test_save_password_allows_passwords_with_quotes
+ app = HCl::App.new
+ app.expects(:system).with("security add-internet-password -U -l hcl -a 'taco@example.com' -s 'acme.harvestapp.com' -w pass\\ with\\ \\'\\ quote")
+
+ config = {
+ 'login' => 'taco@example.com',
+ 'subdomain' => 'acme',
+ 'password' => "pass with ' quote",
+ }
+
+ app.send :save_password, config
+ end
+
end