diff --git a/Readme.md b/Readme.md index 8e0593b..1d3fc6a 100644 --- a/Readme.md +++ b/Readme.md @@ -18,6 +18,12 @@ Read the user's default netrc file (`$HOME/.netrc` on Unix; n = Netrc.read +Configure netrc to allow permissive files (with permissions other than 0600): + + Netrc.configure do |config| + config[:allow_permissive_netrc_file] = true + end + Look up a username and password: user, pass = n["example.com"] diff --git a/lib/netrc.rb b/lib/netrc.rb index 9b37520..b3fa321 100644 --- a/lib/netrc.rb +++ b/lib/netrc.rb @@ -15,9 +15,18 @@ def self.default_path end end + def self.config + @config ||= {} + end + + def self.configure + yield(self.config) if block_given? + self.config + end + def self.check_permissions(path) perm = File.stat(path).mode & 0777 - if perm != 0600 && !(WINDOWS) + if perm != 0600 && !(WINDOWS) && !(Netrc.config[:allow_permissive_netrc_file]) raise Error, "Permission bits for '#{path}' should be 0600, but are "+perm.to_s(8) end end diff --git a/test/test_netrc.rb b/test/test_netrc.rb index 3bb459d..177985b 100644 --- a/test/test_netrc.rb +++ b/test/test_netrc.rb @@ -62,12 +62,30 @@ def test_permission_error original_windows = Netrc::WINDOWS Netrc.const_set(:WINDOWS, false) Netrc.read("data/permissive.netrc") + assert false, "Should raise an error if permissions are wrong on a non-windows system." rescue Netrc::Error - assert true, "Should raise an error if permissions are wrong on a non-windows system." + assert true, "" ensure Netrc.const_set(:WINDOWS, original_windows) end + def test_allow_permissive_netrc_file_option + Netrc.configure do |config| + config[:allow_permissive_netrc_file] = true + end + original_windows = Netrc::WINDOWS + Netrc.const_set(:WINDOWS, false) + Netrc.read("data/permissive.netrc") + assert true, "" + rescue Netrc::Error + assert false, "Should not raise an error if allow_permissive_netrc_file option is set to true" + ensure + Netrc.const_set(:WINDOWS, original_windows) + Netrc.configure do |config| + config[:allow_permissive_netrc_file] = false + end + end + def test_permission_error_windows original_windows = Netrc::WINDOWS Netrc.const_set(:WINDOWS, true)