Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modernize escape functions #1238

Merged
merged 1 commit into from
May 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions lib/puppet/functions/batch_escape.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

# @summary
# Escapes a string so that it can be safely used in a batch shell command line.
#
# >* Note:* that the resulting string should be used unquoted and is not intended for use in double quotes nor in single
# quotes.
Puppet::Functions.create_function(:batch_escape) do
# @param string
# The string to escape
#
# @return
smortex marked this conversation as resolved.
Show resolved Hide resolved
# An escaped string that can be safely used in a batch command line.
dispatch :batch_escape do
param 'Any', :string
end

def batch_escape(string)
result = ''

string.to_s.chars.each do |char|
result += case char
when '"' then '""'
when '$', '\\' then "\\#{char}"
else char
end
end

%("#{result}")
end
end
31 changes: 31 additions & 0 deletions lib/puppet/functions/powershell_escape.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

# @summary
# Escapes a string so that it can be safely used in a PowerShell command line.
#
# >* Note:* that the resulting string should be used unquoted and is not intended for use in double quotes nor in single
# quotes.
Puppet::Functions.create_function(:powershell_escape) do
# @param string
# The string to escape
#
# @return
# An escaped string that can be safely used in a PowerShell command line.
dispatch :powershell_escape do
param 'Any', :string
end

def powershell_escape(string)
result = ''

string.to_s.chars.each do |char|
result += case char
when ' ', "'", '`', '|', "\n", '$' then "`#{char}"
when '"' then '\`"'
else char
end
end

result
end
end
25 changes: 25 additions & 0 deletions lib/puppet/functions/shell_escape.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

# @summary
# Escapes a string so that it can be safely used in a Bourne shell command line.
#
# >* Note:* that the resulting string should be used unquoted and is not intended for use in double quotes nor in single
# quotes.
#
# This function behaves the same as ruby's Shellwords.shellescape() function.
Puppet::Functions.create_function(:shell_escape) do
# @param string
# The string to escape
#
# @return
# An escaped string that can be safely used in a Bourne shell command line.
dispatch :shell_escape do
param 'Any', :string
end

def shell_escape(string)
require 'shellwords'

Shellwords.shellescape(string.to_s)
end
end
36 changes: 0 additions & 36 deletions lib/puppet/parser/functions/batch_escape.rb

This file was deleted.

36 changes: 0 additions & 36 deletions lib/puppet/parser/functions/powershell_escape.rb

This file was deleted.

32 changes: 0 additions & 32 deletions lib/puppet/parser/functions/shell_escape.rb

This file was deleted.

4 changes: 2 additions & 2 deletions spec/functions/batch_escape_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
it { is_expected.not_to eq(nil) }

describe 'signature validation' do
it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) }
it { is_expected.to run.with_params('foo', 'bar').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) }
it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{'batch_escape' expects 1 argument, got none}) }
it { is_expected.to run.with_params('foo', 'bar').and_raise_error(ArgumentError, %r{'batch_escape' expects 1 argument, got 2}) }
end

describe 'stringification' do
Expand Down
4 changes: 2 additions & 2 deletions spec/functions/powershell_escape_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
it { is_expected.not_to eq(nil) }

describe 'signature validation' do
it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) }
it { is_expected.to run.with_params('foo', 'bar').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) }
it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{'powershell_escape' expects 1 argument, got none}) }
it { is_expected.to run.with_params('foo', 'bar').and_raise_error(ArgumentError, %r{'powershell_escape' expects 1 argument, got 2}) }
end

describe 'stringification' do
Expand Down
4 changes: 2 additions & 2 deletions spec/functions/shell_escape_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
it { is_expected.not_to eq(nil) }

describe 'signature validation' do
it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) }
it { is_expected.to run.with_params('foo', 'bar').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) }
it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{'shell_escape' expects 1 argument, got none}) }
it { is_expected.to run.with_params('foo', 'bar').and_raise_error(ArgumentError, %r{'shell_escape' expects 1 argument, got 2}) }
end

describe 'stringification' do
Expand Down