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