From 1fccaf60b246026a8965a7909c10fcce7223d4a0 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Mon, 20 Feb 2023 18:20:08 +0100 Subject: [PATCH] Cleanup escaping logic --- lib/mrsk/utils.rb | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/lib/mrsk/utils.rb b/lib/mrsk/utils.rb index 7aa58a4f..73bc5485 100644 --- a/lib/mrsk/utils.rb +++ b/lib/mrsk/utils.rb @@ -1,13 +1,14 @@ module Mrsk::Utils extend self - # Return a list of shell arguments using the same named argument against the passed attributes (hash or array). + # Return a list of escaped shell arguments using the same named argument against the passed attributes (hash or array). def argumentize(argument, attributes, redacted: false) - Array(attributes).flat_map do |k, v| - if v.present? - [ argument, redacted ? redact("#{k}=#{escape_bash_string v.to_s}") : "#{k}=#{escape_bash_string v.to_s}" ] + Array(attributes).flat_map do |key, value| + if value.present? + escaped_pair = [ key, value.to_s.dump.gsub(/`/, '\\\\`') ].join("=") + [ argument, redacted ? redact(escaped_pair) : escaped_pair ] else - [ argument, k ] + [ argument, key ] end end end @@ -26,8 +27,4 @@ module Mrsk::Utils def redact(arg) # Used in execute_command to hide redact() args a user passes in arg.to_s.extend(SSHKit::Redaction) # to_s due to our inability to extend Integer, etc end - - def escape_bash_string(string) - string.dump.gsub(/`/, '\\\\`') - end end