|
|
cbe7eb |
From c0b225b90d1056e29d681258a2008ae8aff2b508 Mon Sep 17 00:00:00 2001
|
|
|
cbe7eb |
From: Marian Koncek <mkoncek@redhat.com>
|
|
|
cbe7eb |
Date: Tue, 5 Apr 2022 13:56:20 +0200
|
|
|
cbe7eb |
Subject: [PATCH] Unconditionally single quote executable and arguments
|
|
|
cbe7eb |
|
|
|
cbe7eb |
Upstream: https://github.com/apache/maven-shared-utils/pull/40/commits
|
|
|
cbe7eb |
---
|
|
|
cbe7eb |
.../shared/utils/cli/shell/BourneShell.java | 48 ++++++++-----------
|
|
|
cbe7eb |
.../maven/shared/utils/cli/shell/Shell.java | 39 ++++++++++-----
|
|
|
cbe7eb |
.../utils/cli/shell/BourneShellTest.java | 35 ++++++++++----
|
|
|
cbe7eb |
3 files changed, 72 insertions(+), 50 deletions(-)
|
|
|
cbe7eb |
|
|
|
cbe7eb |
diff --git a/src/main/java/org/apache/maven/shared/utils/cli/shell/BourneShell.java b/src/main/java/org/apache/maven/shared/utils/cli/shell/BourneShell.java
|
|
|
cbe7eb |
index 11a447a..f0de631 100644
|
|
|
cbe7eb |
--- a/src/main/java/org/apache/maven/shared/utils/cli/shell/BourneShell.java
|
|
|
cbe7eb |
+++ b/src/main/java/org/apache/maven/shared/utils/cli/shell/BourneShell.java
|
|
|
cbe7eb |
@@ -23,7 +23,6 @@ package org.apache.maven.shared.utils.cli.shell;
|
|
|
cbe7eb |
import java.util.ArrayList;
|
|
|
cbe7eb |
import java.util.List;
|
|
|
cbe7eb |
import org.apache.maven.shared.utils.Os;
|
|
|
cbe7eb |
-import org.apache.maven.shared.utils.StringUtils;
|
|
|
cbe7eb |
|
|
|
cbe7eb |
/**
|
|
|
cbe7eb |
* @author Jason van Zyl
|
|
|
cbe7eb |
@@ -31,17 +30,15 @@ import org.apache.maven.shared.utils.StringUtils;
|
|
|
cbe7eb |
public class BourneShell
|
|
|
cbe7eb |
extends Shell
|
|
|
cbe7eb |
{
|
|
|
cbe7eb |
- private static final char[] BASH_QUOTING_TRIGGER_CHARS =
|
|
|
cbe7eb |
- { ' ', '$', ';', '&', '|', '<', '>', '*', '?', '(', ')', '[', ']', '{', '}', '`' };
|
|
|
cbe7eb |
-
|
|
|
cbe7eb |
/**
|
|
|
cbe7eb |
- * Create instance of BournShell.
|
|
|
cbe7eb |
+ * Create instance of BourneShell.
|
|
|
cbe7eb |
*/
|
|
|
cbe7eb |
public BourneShell()
|
|
|
cbe7eb |
{
|
|
|
cbe7eb |
+ setUnconditionalQuoting( true );
|
|
|
cbe7eb |
setShellCommand( "/bin/sh" );
|
|
|
cbe7eb |
setArgumentQuoteDelimiter( '\'' );
|
|
|
cbe7eb |
- setExecutableQuoteDelimiter( '\"' );
|
|
|
cbe7eb |
+ setExecutableQuoteDelimiter( '\'' );
|
|
|
cbe7eb |
setSingleQuotedArgumentEscaped( true );
|
|
|
cbe7eb |
setSingleQuotedExecutableEscaped( false );
|
|
|
cbe7eb |
setQuotedExecutableEnabled( true );
|
|
|
cbe7eb |
@@ -57,7 +54,7 @@ public class BourneShell
|
|
|
cbe7eb |
return super.getExecutable();
|
|
|
cbe7eb |
}
|
|
|
cbe7eb |
|
|
|
cbe7eb |
- return unifyQuotes( super.getExecutable() );
|
|
|
cbe7eb |
+ return quoteOneItem( super.getExecutable(), true );
|
|
|
cbe7eb |
}
|
|
|
cbe7eb |
|
|
|
cbe7eb |
/** {@inheritDoc} */
|
|
|
cbe7eb |
@@ -110,47 +107,40 @@ public class BourneShell
|
|
|
cbe7eb |
StringBuilder sb = new StringBuilder();
|
|
|
cbe7eb |
sb.append( "cd " );
|
|
|
cbe7eb |
|
|
|
cbe7eb |
- sb.append( unifyQuotes( dir ) );
|
|
|
cbe7eb |
+ sb.append( quoteOneItem( dir, false ) );
|
|
|
cbe7eb |
sb.append( " && " );
|
|
|
cbe7eb |
|
|
|
cbe7eb |
return sb.toString();
|
|
|
cbe7eb |
}
|
|
|
cbe7eb |
|
|
|
cbe7eb |
- /** {@inheritDoc} */
|
|
|
cbe7eb |
- protected char[] getQuotingTriggerChars()
|
|
|
cbe7eb |
- {
|
|
|
cbe7eb |
- return BASH_QUOTING_TRIGGER_CHARS;
|
|
|
cbe7eb |
- }
|
|
|
cbe7eb |
-
|
|
|
cbe7eb |
/**
|
|
|
cbe7eb |
* Unify quotes in a path for the Bourne Shell.
|
|
|
cbe7eb |
*
|
|
|
cbe7eb |
*
|
|
|
cbe7eb |
- * BourneShell.unifyQuotes(null) = null
|
|
|
cbe7eb |
- * BourneShell.unifyQuotes("") = (empty)
|
|
|
cbe7eb |
- * BourneShell.unifyQuotes("/test/quotedpath'abc") = /test/quotedpath\'abc
|
|
|
cbe7eb |
- * BourneShell.unifyQuotes("/test/quoted path'abc") = "/test/quoted path'abc"
|
|
|
cbe7eb |
- * BourneShell.unifyQuotes("/test/quotedpath\"abc") = "/test/quotedpath\"abc"
|
|
|
cbe7eb |
- * BourneShell.unifyQuotes("/test/quoted path\"abc") = "/test/quoted path\"abc"
|
|
|
cbe7eb |
- * BourneShell.unifyQuotes("/test/quotedpath\"'abc") = "/test/quotedpath\"'abc"
|
|
|
cbe7eb |
- * BourneShell.unifyQuotes("/test/quoted path\"'abc") = "/test/quoted path\"'abc"
|
|
|
cbe7eb |
+ * BourneShell.quoteOneItem(null) = null
|
|
|
cbe7eb |
+ * BourneShell.quoteOneItem("") = ''
|
|
|
cbe7eb |
+ * BourneShell.quoteOneItem("/test/quotedpath'abc") = '/test/quotedpath'"'"'abc'
|
|
|
cbe7eb |
+ * BourneShell.quoteOneItem("/test/quoted path'abc") = '/test/quoted pat'"'"'habc'
|
|
|
cbe7eb |
+ * BourneShell.quoteOneItem("/test/quotedpath\"abc") = '/test/quotedpath"abc'
|
|
|
cbe7eb |
+ * BourneShell.quoteOneItem("/test/quoted path\"abc") = '/test/quoted path"abc'
|
|
|
cbe7eb |
+ * BourneShell.quoteOneItem("/test/quotedpath\"'abc") = '/test/quotedpath"'"'"'abc'
|
|
|
cbe7eb |
+ * BourneShell.quoteOneItem("/test/quoted path\"'abc") = '/test/quoted path"'"'"'abc'
|
|
|
cbe7eb |
*
|
|
|
cbe7eb |
*
|
|
|
cbe7eb |
* @param path not null path.
|
|
|
cbe7eb |
* @return the path unified correctly for the Bourne shell.
|
|
|
cbe7eb |
*/
|
|
|
cbe7eb |
- private static String unifyQuotes( String path )
|
|
|
cbe7eb |
+ protected String quoteOneItem( String path, boolean isExecutable )
|
|
|
cbe7eb |
{
|
|
|
cbe7eb |
if ( path == null )
|
|
|
cbe7eb |
{
|
|
|
cbe7eb |
return null;
|
|
|
cbe7eb |
}
|
|
|
cbe7eb |
|
|
|
cbe7eb |
- if ( path.indexOf( ' ' ) == -1 && path.indexOf( '\'' ) != -1 && path.indexOf( '"' ) == -1 )
|
|
|
cbe7eb |
- {
|
|
|
cbe7eb |
- return StringUtils.escape( path );
|
|
|
cbe7eb |
- }
|
|
|
cbe7eb |
-
|
|
|
cbe7eb |
- return StringUtils.quoteAndEscape( path, '\"', BASH_QUOTING_TRIGGER_CHARS );
|
|
|
cbe7eb |
+ StringBuilder sb = new StringBuilder();
|
|
|
cbe7eb |
+ sb.append( "'" );
|
|
|
cbe7eb |
+ sb.append( path.replace( "'", "'\"'\"'" ) );
|
|
|
cbe7eb |
+ sb.append( "'" );
|
|
|
cbe7eb |
+ return sb.toString();
|
|
|
cbe7eb |
}
|
|
|
cbe7eb |
}
|
|
|
cbe7eb |
diff --git a/src/main/java/org/apache/maven/shared/utils/cli/shell/Shell.java b/src/main/java/org/apache/maven/shared/utils/cli/shell/Shell.java
|
|
|
cbe7eb |
index 6fa2f73..96904cb 100644
|
|
|
cbe7eb |
--- a/src/main/java/org/apache/maven/shared/utils/cli/shell/Shell.java
|
|
|
cbe7eb |
+++ b/src/main/java/org/apache/maven/shared/utils/cli/shell/Shell.java
|
|
|
cbe7eb |
@@ -50,6 +50,8 @@ public class Shell
|
|
|
cbe7eb |
|
|
|
cbe7eb |
private boolean quotedArgumentsEnabled = true;
|
|
|
cbe7eb |
|
|
|
cbe7eb |
+ private boolean unconditionalQuoting = false;
|
|
|
cbe7eb |
+
|
|
|
cbe7eb |
private String executable;
|
|
|
cbe7eb |
|
|
|
cbe7eb |
private String workingDir;
|
|
|
cbe7eb |
@@ -113,6 +115,19 @@ public class Shell
|
|
|
cbe7eb |
}
|
|
|
cbe7eb |
}
|
|
|
cbe7eb |
|
|
|
cbe7eb |
+ protected String quoteOneItem( String inputString, boolean isExecutable )
|
|
|
cbe7eb |
+ {
|
|
|
cbe7eb |
+ char[] escapeChars = getEscapeChars( isSingleQuotedExecutableEscaped(), isDoubleQuotedExecutableEscaped() );
|
|
|
cbe7eb |
+ return StringUtils.quoteAndEscape(
|
|
|
cbe7eb |
+ inputString,
|
|
|
cbe7eb |
+ isExecutable ? getExecutableQuoteDelimiter() : getArgumentQuoteDelimiter(),
|
|
|
cbe7eb |
+ escapeChars,
|
|
|
cbe7eb |
+ getQuotingTriggerChars(),
|
|
|
cbe7eb |
+ '\\',
|
|
|
cbe7eb |
+ unconditionalQuoting
|
|
|
cbe7eb |
+ );
|
|
|
cbe7eb |
+ }
|
|
|
cbe7eb |
+
|
|
|
cbe7eb |
/**
|
|
|
cbe7eb |
* Get the command line for the provided executable and arguments in this shell
|
|
|
cbe7eb |
*
|
|
|
cbe7eb |
@@ -145,15 +160,11 @@ public class Shell
|
|
|
cbe7eb |
|
|
|
cbe7eb |
if ( isQuotedExecutableEnabled() )
|
|
|
cbe7eb |
{
|
|
|
cbe7eb |
- char[] escapeChars =
|
|
|
cbe7eb |
- getEscapeChars( isSingleQuotedExecutableEscaped(), isDoubleQuotedExecutableEscaped() );
|
|
|
cbe7eb |
-
|
|
|
cbe7eb |
- sb.append( StringUtils.quoteAndEscape( getExecutable(), getExecutableQuoteDelimiter(), escapeChars,
|
|
|
cbe7eb |
- getQuotingTriggerChars(), '\\', false ) );
|
|
|
cbe7eb |
+ sb.append( quoteOneItem( executableParameter, true ) );
|
|
|
cbe7eb |
}
|
|
|
cbe7eb |
else
|
|
|
cbe7eb |
{
|
|
|
cbe7eb |
- sb.append( getExecutable() );
|
|
|
cbe7eb |
+ sb.append( executableParameter );
|
|
|
cbe7eb |
}
|
|
|
cbe7eb |
}
|
|
|
cbe7eb |
for ( String argument : argumentsParameter )
|
|
|
cbe7eb |
@@ -165,10 +176,7 @@ public class Shell
|
|
|
cbe7eb |
|
|
|
cbe7eb |
if ( isQuotedArgumentsEnabled() )
|
|
|
cbe7eb |
{
|
|
|
cbe7eb |
- char[] escapeChars = getEscapeChars( isSingleQuotedArgumentEscaped(), isDoubleQuotedArgumentEscaped() );
|
|
|
cbe7eb |
-
|
|
|
cbe7eb |
- sb.append( StringUtils.quoteAndEscape( argument, getArgumentQuoteDelimiter(), escapeChars,
|
|
|
cbe7eb |
- getQuotingTriggerChars(), '\\', false ) );
|
|
|
cbe7eb |
+ sb.append( quoteOneItem( argument, false ) );
|
|
|
cbe7eb |
}
|
|
|
cbe7eb |
else
|
|
|
cbe7eb |
{
|
|
|
cbe7eb |
@@ -285,7 +293,7 @@ public class Shell
|
|
|
cbe7eb |
commandLine.addAll( getShellArgsList() );
|
|
|
cbe7eb |
}
|
|
|
cbe7eb |
|
|
|
cbe7eb |
- commandLine.addAll( getCommandLine( getExecutable(), arguments ) );
|
|
|
cbe7eb |
+ commandLine.addAll( getCommandLine( executable, arguments ) );
|
|
|
cbe7eb |
|
|
|
cbe7eb |
return commandLine;
|
|
|
cbe7eb |
|
|
|
cbe7eb |
@@ -398,4 +406,13 @@ public class Shell
|
|
|
cbe7eb |
this.singleQuotedExecutableEscaped = singleQuotedExecutableEscaped;
|
|
|
cbe7eb |
}
|
|
|
cbe7eb |
|
|
|
cbe7eb |
+ public boolean isUnconditionalQuoting()
|
|
|
cbe7eb |
+ {
|
|
|
cbe7eb |
+ return unconditionalQuoting;
|
|
|
cbe7eb |
+ }
|
|
|
cbe7eb |
+
|
|
|
cbe7eb |
+ public void setUnconditionalQuoting( boolean unconditionalQuoting )
|
|
|
cbe7eb |
+ {
|
|
|
cbe7eb |
+ this.unconditionalQuoting = unconditionalQuoting;
|
|
|
cbe7eb |
+ }
|
|
|
cbe7eb |
}
|
|
|
cbe7eb |
diff --git a/src/test/java/org/apache/maven/shared/utils/cli/shell/BourneShellTest.java b/src/test/java/org/apache/maven/shared/utils/cli/shell/BourneShellTest.java
|
|
|
cbe7eb |
index b5f23d9..f5143c1 100644
|
|
|
cbe7eb |
--- a/src/test/java/org/apache/maven/shared/utils/cli/shell/BourneShellTest.java
|
|
|
cbe7eb |
+++ b/src/test/java/org/apache/maven/shared/utils/cli/shell/BourneShellTest.java
|
|
|
cbe7eb |
@@ -44,7 +44,7 @@ public class BourneShellTest
|
|
|
cbe7eb |
|
|
|
cbe7eb |
String executable = StringUtils.join( sh.getShellCommandLine( new String[]{} ).iterator(), " " );
|
|
|
cbe7eb |
|
|
|
cbe7eb |
- assertEquals( "/bin/sh -c cd /usr/local/bin && chmod", executable );
|
|
|
cbe7eb |
+ assertEquals( "/bin/sh -c cd '/usr/local/bin' && 'chmod'", executable );
|
|
|
cbe7eb |
}
|
|
|
cbe7eb |
|
|
|
cbe7eb |
public void testQuoteWorkingDirectoryAndExecutable_WDPathWithSingleQuotes()
|
|
|
cbe7eb |
@@ -56,7 +56,7 @@ public class BourneShellTest
|
|
|
cbe7eb |
|
|
|
cbe7eb |
String executable = StringUtils.join( sh.getShellCommandLine( new String[]{} ).iterator(), " " );
|
|
|
cbe7eb |
|
|
|
cbe7eb |
- assertEquals( "/bin/sh -c cd \"/usr/local/\'something else\'\" && chmod", executable );
|
|
|
cbe7eb |
+ assertEquals( "/bin/sh -c cd '/usr/local/'\"'\"'something else'\"'\"'' && 'chmod'", executable );
|
|
|
cbe7eb |
}
|
|
|
cbe7eb |
|
|
|
cbe7eb |
public void testQuoteWorkingDirectoryAndExecutable_WDPathWithSingleQuotes_BackslashFileSep()
|
|
|
cbe7eb |
@@ -68,7 +68,7 @@ public class BourneShellTest
|
|
|
cbe7eb |
|
|
|
cbe7eb |
String executable = StringUtils.join( sh.getShellCommandLine( new String[]{} ).iterator(), " " );
|
|
|
cbe7eb |
|
|
|
cbe7eb |
- assertEquals( "/bin/sh -c cd \"\\usr\\local\\\'something else\'\" && chmod", executable );
|
|
|
cbe7eb |
+ assertEquals( "/bin/sh -c cd '\\usr\\local\\'\"'\"'something else'\"'\"'' && 'chmod'", executable );
|
|
|
cbe7eb |
}
|
|
|
cbe7eb |
|
|
|
cbe7eb |
public void testPreserveSingleQuotesOnArgument()
|
|
|
cbe7eb |
@@ -78,13 +78,13 @@ public class BourneShellTest
|
|
|
cbe7eb |
sh.setWorkingDirectory( "/usr/bin" );
|
|
|
cbe7eb |
sh.setExecutable( "chmod" );
|
|
|
cbe7eb |
|
|
|
cbe7eb |
- String[] args = { "\'some arg with spaces\'" };
|
|
|
cbe7eb |
+ String[] args = { "\"some arg with spaces\"" };
|
|
|
cbe7eb |
|
|
|
cbe7eb |
List<String> shellCommandLine = sh.getShellCommandLine( args );
|
|
|
cbe7eb |
|
|
|
cbe7eb |
String cli = StringUtils.join( shellCommandLine.iterator(), " " );
|
|
|
cbe7eb |
System.out.println( cli );
|
|
|
cbe7eb |
- assertTrue( cli.endsWith( args[0] ) );
|
|
|
cbe7eb |
+ assertTrue( cli.endsWith( "'\"some arg with spaces\"'" ) );
|
|
|
cbe7eb |
}
|
|
|
cbe7eb |
|
|
|
cbe7eb |
public void testAddSingleQuotesOnArgumentWithSpaces()
|
|
|
cbe7eb |
@@ -100,7 +100,21 @@ public class BourneShellTest
|
|
|
cbe7eb |
|
|
|
cbe7eb |
String cli = StringUtils.join( shellCommandLine.iterator(), " " );
|
|
|
cbe7eb |
System.out.println( cli );
|
|
|
cbe7eb |
- assertTrue( cli.endsWith( "\'" + args[0] + "\'" ) );
|
|
|
cbe7eb |
+ assertTrue( cli.endsWith("'some arg with spaces'"));
|
|
|
cbe7eb |
+ }
|
|
|
cbe7eb |
+
|
|
|
cbe7eb |
+ public void testAddArgumentWithSingleQuote()
|
|
|
cbe7eb |
+ {
|
|
|
cbe7eb |
+ Shell sh = newShell();
|
|
|
cbe7eb |
+
|
|
|
cbe7eb |
+ sh.setWorkingDirectory( "/usr/bin" );
|
|
|
cbe7eb |
+ sh.setExecutable( "chmod" );
|
|
|
cbe7eb |
+
|
|
|
cbe7eb |
+ String[] args = { "arg'withquote" };
|
|
|
cbe7eb |
+
|
|
|
cbe7eb |
+ List<String> shellCommandLine = sh.getShellCommandLine( args );
|
|
|
cbe7eb |
+
|
|
|
cbe7eb |
+ assertEquals("cd '/usr/bin' && 'chmod' 'arg'\"'\"'withquote'", shellCommandLine.get(shellCommandLine.size() - 1));
|
|
|
cbe7eb |
}
|
|
|
cbe7eb |
|
|
|
cbe7eb |
public void testArgumentsWithsemicolon()
|
|
|
cbe7eb |
@@ -119,7 +133,7 @@ public class BourneShellTest
|
|
|
cbe7eb |
|
|
|
cbe7eb |
String cli = StringUtils.join( shellCommandLine.iterator(), " " );
|
|
|
cbe7eb |
System.out.println( cli );
|
|
|
cbe7eb |
- assertTrue( cli.endsWith( "\'" + args[0] + "\'" ) );
|
|
|
cbe7eb |
+ assertTrue( cli.endsWith( "';some&argwithunix$chars'" ) );
|
|
|
cbe7eb |
|
|
|
cbe7eb |
Commandline commandline = new Commandline( newShell() );
|
|
|
cbe7eb |
commandline.setExecutable( "chmod" );
|
|
|
cbe7eb |
@@ -132,7 +146,7 @@ public class BourneShellTest
|
|
|
cbe7eb |
|
|
|
cbe7eb |
assertEquals( "/bin/sh", lines.get( 0 ) );
|
|
|
cbe7eb |
assertEquals( "-c", lines.get( 1 ) );
|
|
|
cbe7eb |
- assertEquals( "chmod --password ';password'", lines.get( 2 ) );
|
|
|
cbe7eb |
+ assertEquals( "'chmod' '--password' ';password'", lines.get( 2 ) );
|
|
|
cbe7eb |
|
|
|
cbe7eb |
commandline = new Commandline( newShell() );
|
|
|
cbe7eb |
commandline.setExecutable( "chmod" );
|
|
|
cbe7eb |
@@ -144,7 +158,7 @@ public class BourneShellTest
|
|
|
cbe7eb |
|
|
|
cbe7eb |
assertEquals( "/bin/sh", lines.get( 0) );
|
|
|
cbe7eb |
assertEquals( "-c", lines.get( 1 ) );
|
|
|
cbe7eb |
- assertEquals( "chmod --password ';password'", lines.get( 2 ) );
|
|
|
cbe7eb |
+ assertEquals( "'chmod' '--password' ';password'", lines.get( 2 ) );
|
|
|
cbe7eb |
|
|
|
cbe7eb |
commandline = new Commandline( new CmdShell() );
|
|
|
cbe7eb |
commandline.getShell().setQuotedArgumentsEnabled( true );
|
|
|
cbe7eb |
@@ -186,13 +200,14 @@ public class BourneShellTest
|
|
|
cbe7eb |
commandline.createArg().setValue( "{" );
|
|
|
cbe7eb |
commandline.createArg().setValue( "}" );
|
|
|
cbe7eb |
commandline.createArg().setValue( "`" );
|
|
|
cbe7eb |
+ commandline.createArg().setValue( "#" );
|
|
|
cbe7eb |
|
|
|
cbe7eb |
List<String> lines = commandline.getShell().getShellCommandLine( commandline.getArguments() );
|
|
|
cbe7eb |
System.out.println( lines );
|
|
|
cbe7eb |
|
|
|
cbe7eb |
assertEquals( "/bin/sh", lines.get( 0 ) );
|
|
|
cbe7eb |
assertEquals( "-c", lines.get( 1 ) );
|
|
|
cbe7eb |
- assertEquals( "chmod ' ' '|' '&&' '||' ';' ';;' '&' '()' '<' '<<' '>' '>>' '*' '?' '[' ']' '{' '}' '`'",
|
|
|
cbe7eb |
+ assertEquals( "'chmod' ' ' '|' '&&' '||' ';' ';;' '&' '()' '<' '<<' '>' '>>' '*' '?' '[' ']' '{' '}' '`' '#'",
|
|
|
cbe7eb |
lines.get( 2 ) );
|
|
|
cbe7eb |
}
|
|
|
cbe7eb |
|
|
|
cbe7eb |
--
|
|
|
cbe7eb |
2.35.1
|
|
|
cbe7eb |
|