Blame SOURCES/0003-Unconditionally-single-quote-executable-and-argument.patch

af7d84
From c0b225b90d1056e29d681258a2008ae8aff2b508 Mon Sep 17 00:00:00 2001
af7d84
From: Marian Koncek <mkoncek@redhat.com>
af7d84
Date: Tue, 5 Apr 2022 13:56:20 +0200
af7d84
Subject: [PATCH] Unconditionally single quote executable and arguments
af7d84
af7d84
Upstream: https://github.com/apache/maven-shared-utils/pull/40/commits
af7d84
---
af7d84
 .../shared/utils/cli/shell/BourneShell.java   | 48 ++++++++-----------
af7d84
 .../maven/shared/utils/cli/shell/Shell.java   | 39 ++++++++++-----
af7d84
 .../utils/cli/shell/BourneShellTest.java      | 35 ++++++++++----
af7d84
 3 files changed, 72 insertions(+), 50 deletions(-)
af7d84
af7d84
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
af7d84
index 11a447a..f0de631 100644
af7d84
--- a/src/main/java/org/apache/maven/shared/utils/cli/shell/BourneShell.java
af7d84
+++ b/src/main/java/org/apache/maven/shared/utils/cli/shell/BourneShell.java
af7d84
@@ -23,7 +23,6 @@ package org.apache.maven.shared.utils.cli.shell;
af7d84
 import java.util.ArrayList;
af7d84
 import java.util.List;
af7d84
 import org.apache.maven.shared.utils.Os;
af7d84
-import org.apache.maven.shared.utils.StringUtils;
af7d84
 
af7d84
 /**
af7d84
  * @author Jason van Zyl
af7d84
@@ -31,17 +30,15 @@ import org.apache.maven.shared.utils.StringUtils;
af7d84
 public class BourneShell
af7d84
     extends Shell
af7d84
 {
af7d84
-    private static final char[] BASH_QUOTING_TRIGGER_CHARS =
af7d84
-        { ' ', '$', ';', '&', '|', '<', '>', '*', '?', '(', ')', '[', ']', '{', '}', '`' };
af7d84
-
af7d84
     /**
af7d84
-     * Create instance of BournShell.
af7d84
+     * Create instance of BourneShell.
af7d84
      */
af7d84
     public BourneShell()
af7d84
     {
af7d84
+        setUnconditionalQuoting( true );
af7d84
         setShellCommand( "/bin/sh" );
af7d84
         setArgumentQuoteDelimiter( '\'' );
af7d84
-        setExecutableQuoteDelimiter( '\"' );
af7d84
+        setExecutableQuoteDelimiter( '\'' );
af7d84
         setSingleQuotedArgumentEscaped( true );
af7d84
         setSingleQuotedExecutableEscaped( false );
af7d84
         setQuotedExecutableEnabled( true );
af7d84
@@ -57,7 +54,7 @@ public class BourneShell
af7d84
             return super.getExecutable();
af7d84
         }
af7d84
 
af7d84
-        return unifyQuotes( super.getExecutable() );
af7d84
+        return quoteOneItem( super.getExecutable(), true );
af7d84
     }
af7d84
 
af7d84
     /** {@inheritDoc} */
af7d84
@@ -110,47 +107,40 @@ public class BourneShell
af7d84
         StringBuilder sb = new StringBuilder();
af7d84
         sb.append( "cd " );
af7d84
 
af7d84
-        sb.append( unifyQuotes( dir ) );
af7d84
+        sb.append( quoteOneItem( dir, false ) );
af7d84
         sb.append( " && " );
af7d84
 
af7d84
         return sb.toString();
af7d84
     }
af7d84
 
af7d84
-    /** {@inheritDoc} */
af7d84
-    protected char[] getQuotingTriggerChars()
af7d84
-    {
af7d84
-        return BASH_QUOTING_TRIGGER_CHARS;
af7d84
-    }
af7d84
-
af7d84
     /**
af7d84
      * 

Unify quotes in a path for the Bourne Shell.

af7d84
      * 

af7d84
      * 
af7d84
-     * BourneShell.unifyQuotes(null)                       = null
af7d84
-     * BourneShell.unifyQuotes("")                         = (empty)
af7d84
-     * BourneShell.unifyQuotes("/test/quotedpath'abc")     = /test/quotedpath\'abc
af7d84
-     * BourneShell.unifyQuotes("/test/quoted path'abc")    = "/test/quoted path'abc"
af7d84
-     * BourneShell.unifyQuotes("/test/quotedpath\"abc")    = "/test/quotedpath\"abc"
af7d84
-     * BourneShell.unifyQuotes("/test/quoted path\"abc")   = "/test/quoted path\"abc"
af7d84
-     * BourneShell.unifyQuotes("/test/quotedpath\"'abc")   = "/test/quotedpath\"'abc"
af7d84
-     * BourneShell.unifyQuotes("/test/quoted path\"'abc")  = "/test/quoted path\"'abc"
af7d84
+     * BourneShell.quoteOneItem(null)                       = null
af7d84
+     * BourneShell.quoteOneItem("")                         = ''
af7d84
+     * BourneShell.quoteOneItem("/test/quotedpath'abc")     = '/test/quotedpath'"'"'abc'
af7d84
+     * BourneShell.quoteOneItem("/test/quoted path'abc")    = '/test/quoted pat'"'"'habc'
af7d84
+     * BourneShell.quoteOneItem("/test/quotedpath\"abc")    = '/test/quotedpath"abc'
af7d84
+     * BourneShell.quoteOneItem("/test/quoted path\"abc")   = '/test/quoted path"abc'
af7d84
+     * BourneShell.quoteOneItem("/test/quotedpath\"'abc")   = '/test/quotedpath"'"'"'abc'
af7d84
+     * BourneShell.quoteOneItem("/test/quoted path\"'abc")  = '/test/quoted path"'"'"'abc'
af7d84
      * 
af7d84
      *
af7d84
      * @param path not null path.
af7d84
      * @return the path unified correctly for the Bourne shell.
af7d84
      */
af7d84
-    private static String unifyQuotes( String path )
af7d84
+    protected String quoteOneItem( String path, boolean isExecutable )
af7d84
     {
af7d84
         if ( path == null )
af7d84
         {
af7d84
             return null;
af7d84
         }
af7d84
 
af7d84
-        if ( path.indexOf( ' ' ) == -1 && path.indexOf( '\'' ) != -1 && path.indexOf( '"' ) == -1 )
af7d84
-        {
af7d84
-            return StringUtils.escape( path );
af7d84
-        }
af7d84
-
af7d84
-        return StringUtils.quoteAndEscape( path, '\"', BASH_QUOTING_TRIGGER_CHARS );
af7d84
+        StringBuilder sb = new StringBuilder();
af7d84
+        sb.append( "'" );
af7d84
+        sb.append( path.replace( "'", "'\"'\"'" ) );
af7d84
+        sb.append( "'" );
af7d84
+        return sb.toString();
af7d84
     }
af7d84
 }
af7d84
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
af7d84
index 6fa2f73..96904cb 100644
af7d84
--- a/src/main/java/org/apache/maven/shared/utils/cli/shell/Shell.java
af7d84
+++ b/src/main/java/org/apache/maven/shared/utils/cli/shell/Shell.java
af7d84
@@ -50,6 +50,8 @@ public class Shell
af7d84
 
af7d84
     private boolean quotedArgumentsEnabled = true;
af7d84
 
af7d84
+    private boolean unconditionalQuoting = false;
af7d84
+
af7d84
     private String executable;
af7d84
 
af7d84
     private String workingDir;
af7d84
@@ -113,6 +115,19 @@ public class Shell
af7d84
         }
af7d84
     }
af7d84
 
af7d84
+    protected String quoteOneItem( String inputString, boolean isExecutable )
af7d84
+    {
af7d84
+        char[] escapeChars = getEscapeChars( isSingleQuotedExecutableEscaped(), isDoubleQuotedExecutableEscaped() );
af7d84
+        return StringUtils.quoteAndEscape(
af7d84
+                inputString,
af7d84
+                isExecutable ? getExecutableQuoteDelimiter() : getArgumentQuoteDelimiter(),
af7d84
+                escapeChars,
af7d84
+                getQuotingTriggerChars(),
af7d84
+                '\\',
af7d84
+                unconditionalQuoting
af7d84
+        );
af7d84
+    }
af7d84
+
af7d84
     /**
af7d84
      * Get the command line for the provided executable and arguments in this shell
af7d84
      *
af7d84
@@ -145,15 +160,11 @@ public class Shell
af7d84
 
af7d84
             if ( isQuotedExecutableEnabled() )
af7d84
             {
af7d84
-                char[] escapeChars =
af7d84
-                    getEscapeChars( isSingleQuotedExecutableEscaped(), isDoubleQuotedExecutableEscaped() );
af7d84
-
af7d84
-                sb.append( StringUtils.quoteAndEscape( getExecutable(), getExecutableQuoteDelimiter(), escapeChars,
af7d84
-                                                       getQuotingTriggerChars(), '\\', false ) );
af7d84
+                sb.append( quoteOneItem( executableParameter, true ) );
af7d84
             }
af7d84
             else
af7d84
             {
af7d84
-                sb.append( getExecutable() );
af7d84
+                sb.append( executableParameter );
af7d84
             }
af7d84
         }
af7d84
         for ( String argument : argumentsParameter )
af7d84
@@ -165,10 +176,7 @@ public class Shell
af7d84
 
af7d84
             if ( isQuotedArgumentsEnabled() )
af7d84
             {
af7d84
-                char[] escapeChars = getEscapeChars( isSingleQuotedArgumentEscaped(), isDoubleQuotedArgumentEscaped() );
af7d84
-
af7d84
-                sb.append( StringUtils.quoteAndEscape( argument, getArgumentQuoteDelimiter(), escapeChars,
af7d84
-                                                       getQuotingTriggerChars(), '\\', false ) );
af7d84
+                sb.append( quoteOneItem( argument, false ) );
af7d84
             }
af7d84
             else
af7d84
             {
af7d84
@@ -285,7 +293,7 @@ public class Shell
af7d84
             commandLine.addAll( getShellArgsList() );
af7d84
         }
af7d84
 
af7d84
-        commandLine.addAll( getCommandLine( getExecutable(), arguments ) );
af7d84
+        commandLine.addAll( getCommandLine( executable, arguments ) );
af7d84
 
af7d84
         return commandLine;
af7d84
 
af7d84
@@ -398,4 +406,13 @@ public class Shell
af7d84
         this.singleQuotedExecutableEscaped = singleQuotedExecutableEscaped;
af7d84
     }
af7d84
 
af7d84
+    public boolean isUnconditionalQuoting()
af7d84
+    {
af7d84
+        return unconditionalQuoting;
af7d84
+    }
af7d84
+
af7d84
+    public void setUnconditionalQuoting( boolean unconditionalQuoting )
af7d84
+    {
af7d84
+        this.unconditionalQuoting = unconditionalQuoting;
af7d84
+    }
af7d84
 }
af7d84
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
af7d84
index b5f23d9..f5143c1 100644
af7d84
--- a/src/test/java/org/apache/maven/shared/utils/cli/shell/BourneShellTest.java
af7d84
+++ b/src/test/java/org/apache/maven/shared/utils/cli/shell/BourneShellTest.java
af7d84
@@ -44,7 +44,7 @@ public class BourneShellTest
af7d84
 
af7d84
         String executable = StringUtils.join( sh.getShellCommandLine( new String[]{} ).iterator(), " " );
af7d84
 
af7d84
-        assertEquals( "/bin/sh -c cd /usr/local/bin && chmod", executable );
af7d84
+        assertEquals( "/bin/sh -c cd '/usr/local/bin' && 'chmod'", executable );
af7d84
     }
af7d84
 
af7d84
     public void testQuoteWorkingDirectoryAndExecutable_WDPathWithSingleQuotes()
af7d84
@@ -56,7 +56,7 @@ public class BourneShellTest
af7d84
 
af7d84
         String executable = StringUtils.join( sh.getShellCommandLine( new String[]{} ).iterator(), " " );
af7d84
 
af7d84
-        assertEquals( "/bin/sh -c cd \"/usr/local/\'something else\'\" && chmod", executable );
af7d84
+        assertEquals( "/bin/sh -c cd '/usr/local/'\"'\"'something else'\"'\"'' && 'chmod'", executable );
af7d84
     }
af7d84
 
af7d84
     public void testQuoteWorkingDirectoryAndExecutable_WDPathWithSingleQuotes_BackslashFileSep()
af7d84
@@ -68,7 +68,7 @@ public class BourneShellTest
af7d84
 
af7d84
         String executable = StringUtils.join( sh.getShellCommandLine( new String[]{} ).iterator(), " " );
af7d84
 
af7d84
-        assertEquals( "/bin/sh -c cd \"\\usr\\local\\\'something else\'\" && chmod", executable );
af7d84
+        assertEquals( "/bin/sh -c cd '\\usr\\local\\'\"'\"'something else'\"'\"'' && 'chmod'", executable );
af7d84
     }
af7d84
 
af7d84
     public void testPreserveSingleQuotesOnArgument()
af7d84
@@ -78,13 +78,13 @@ public class BourneShellTest
af7d84
         sh.setWorkingDirectory( "/usr/bin" );
af7d84
         sh.setExecutable( "chmod" );
af7d84
 
af7d84
-        String[] args = { "\'some arg with spaces\'" };
af7d84
+        String[] args = { "\"some arg with spaces\"" };
af7d84
 
af7d84
         List<String> shellCommandLine = sh.getShellCommandLine( args );
af7d84
 
af7d84
         String cli = StringUtils.join( shellCommandLine.iterator(), " " );
af7d84
         System.out.println( cli );
af7d84
-        assertTrue( cli.endsWith( args[0] ) );
af7d84
+        assertTrue( cli.endsWith( "'\"some arg with spaces\"'" ) );
af7d84
     }
af7d84
 
af7d84
     public void testAddSingleQuotesOnArgumentWithSpaces()
af7d84
@@ -100,7 +100,21 @@ public class BourneShellTest
af7d84
 
af7d84
         String cli = StringUtils.join( shellCommandLine.iterator(), " " );
af7d84
         System.out.println( cli );
af7d84
-        assertTrue( cli.endsWith( "\'" + args[0] + "\'" ) );
af7d84
+        assertTrue( cli.endsWith("'some arg with spaces'"));
af7d84
+    }
af7d84
+
af7d84
+    public void testAddArgumentWithSingleQuote()
af7d84
+    {
af7d84
+        Shell sh = newShell();
af7d84
+
af7d84
+        sh.setWorkingDirectory( "/usr/bin" );
af7d84
+        sh.setExecutable( "chmod" );
af7d84
+
af7d84
+        String[] args = { "arg'withquote" };
af7d84
+
af7d84
+        List<String> shellCommandLine = sh.getShellCommandLine( args );
af7d84
+
af7d84
+        assertEquals("cd '/usr/bin' && 'chmod' 'arg'\"'\"'withquote'", shellCommandLine.get(shellCommandLine.size() - 1));
af7d84
     }
af7d84
 
af7d84
     public void testArgumentsWithsemicolon()
af7d84
@@ -119,7 +133,7 @@ public class BourneShellTest
af7d84
 
af7d84
         String cli = StringUtils.join( shellCommandLine.iterator(), " " );
af7d84
         System.out.println( cli );
af7d84
-        assertTrue( cli.endsWith( "\'" + args[0] + "\'" ) );
af7d84
+        assertTrue( cli.endsWith( "';some&argwithunix$chars'" ) );
af7d84
 
af7d84
         Commandline commandline = new Commandline( newShell() );
af7d84
         commandline.setExecutable( "chmod" );
af7d84
@@ -132,7 +146,7 @@ public class BourneShellTest
af7d84
 
af7d84
         assertEquals( "/bin/sh", lines.get( 0 ) );
af7d84
         assertEquals( "-c", lines.get( 1 ) );
af7d84
-        assertEquals( "chmod --password ';password'", lines.get( 2 ) );
af7d84
+        assertEquals( "'chmod' '--password' ';password'", lines.get( 2 ) );
af7d84
 
af7d84
         commandline = new Commandline( newShell() );
af7d84
         commandline.setExecutable( "chmod" );
af7d84
@@ -144,7 +158,7 @@ public class BourneShellTest
af7d84
 
af7d84
         assertEquals( "/bin/sh", lines.get( 0) );
af7d84
         assertEquals( "-c", lines.get( 1 ) );
af7d84
-        assertEquals( "chmod --password ';password'", lines.get( 2 ) );
af7d84
+        assertEquals( "'chmod' '--password' ';password'", lines.get( 2 ) );
af7d84
 
af7d84
         commandline = new Commandline( new CmdShell() );
af7d84
         commandline.getShell().setQuotedArgumentsEnabled( true );
af7d84
@@ -186,13 +200,14 @@ public class BourneShellTest
af7d84
         commandline.createArg().setValue( "{" );
af7d84
         commandline.createArg().setValue( "}" );
af7d84
         commandline.createArg().setValue( "`" );
af7d84
+        commandline.createArg().setValue( "#" );
af7d84
 
af7d84
         List<String> lines = commandline.getShell().getShellCommandLine( commandline.getArguments() );
af7d84
         System.out.println( lines  );
af7d84
 
af7d84
         assertEquals( "/bin/sh", lines.get( 0 ) );
af7d84
         assertEquals( "-c", lines.get( 1 ) );
af7d84
-        assertEquals( "chmod ' ' '|' '&&' '||' ';' ';;' '&' '()' '<' '<<' '>' '>>' '*' '?' '[' ']' '{' '}' '`'",
af7d84
+        assertEquals( "'chmod' ' ' '|' '&&' '||' ';' ';;' '&' '()' '<' '<<' '>' '>>' '*' '?' '[' ']' '{' '}' '`' '#'",
af7d84
                       lines.get( 2 ) );
af7d84
     }
af7d84
 
af7d84
-- 
af7d84
2.35.1
af7d84