You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2022/09/03 02:17:49 UTC

[groovy] 01/03: GROOVY-10739: Improve error message for malformed grab coordinate

This is an automated email from the ASF dual-hosted git repository.

paulk pushed a commit to branch GROOVY_4_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit 52662654a7edd89e89958cd4856c054286c1fe3f
Author: Paul King <pa...@asert.com.au>
AuthorDate: Thu Sep 1 23:41:44 2022 +1000

    GROOVY-10739: Improve error message for malformed grab coordinate
---
 src/main/groovy/groovy/grape/GrapeIvy.groovy | 33 ++++++++++++++++++++--------
 src/test/groovy/grape/GrapeIvyTest.groovy    | 26 ++++++++++++++++++++++
 2 files changed, 50 insertions(+), 9 deletions(-)

diff --git a/src/main/groovy/groovy/grape/GrapeIvy.groovy b/src/main/groovy/groovy/grape/GrapeIvy.groovy
index bfc00258f1..f942fc84de 100644
--- a/src/main/groovy/groovy/grape/GrapeIvy.groovy
+++ b/src/main/groovy/groovy/grape/GrapeIvy.groovy
@@ -198,6 +198,30 @@ class GrapeIvy implements GrapeEngine {
             throw new RuntimeException('grab requires at least a module: or artifactId: or artifact: argument')
         }
 
+        // check for malformed components of the coordinates
+        dep.each { k, v ->
+            if (v instanceof CharSequence) {
+                if (k.toString().contains('v')) { // revision, version, rev
+                    if (!(v ==~ '[^\\/:"<>|]*')) {
+                        throw new RuntimeException("Grab: invalid value of '$v' for $k: should not contain any of / \\ : \" < > |")
+                    }
+                } else {
+                    if (!(v ==~ '[-._a-zA-Z0-9]*')) {
+                        throw new RuntimeException("Grab: invalid value of '$v' for $k: should only contain - . _ a-z A-Z 0-9")
+                    }
+                }
+            }
+        }
+
+        // check for mutually exclusive arguments
+        Set<String> keys = (Set<String>) dep.keySet()
+        keys.each { key ->
+            Set<String> badArgs = MUTUALLY_EXCLUSIVE_KEYS[key]
+            if (badArgs && !badArgs.disjoint(keys)) {
+                throw new RuntimeException("Grab: mutually exclusive arguments: ${keys.intersect(badArgs) + key}")
+            }
+        }
+
         String groupId = dep.group ?: dep.groupId ?: dep.organisation ?: dep.organization ?: dep.org ?: ''
         // TODO: accept ranges and decode them?  except '1.0.0'..<'2.0.0' won't work in groovy
         String version = dep.version ?: dep.revision ?: dep.rev ?: '*'
@@ -593,15 +617,6 @@ class GrapeIvy implements GrapeEngine {
     }
 
     URI[] resolve(ClassLoader loader, Map args, List depsInfo, Map... dependencies) {
-        // check for mutually exclusive arguments
-        Set<String> keys = (Set<String>) args.keySet()
-        keys.each { key ->
-            Set<String> badArgs = MUTUALLY_EXCLUSIVE_KEYS[key]
-            if (badArgs && !badArgs.disjoint(keys)) {
-                throw new RuntimeException("Mutually exclusive arguments passed into grab: ${keys.intersect(badArgs) + key}")
-            }
-        }
-
         // check the kill switch
         if (!enableGrapes) {
             return new URI[0]
diff --git a/src/test/groovy/grape/GrapeIvyTest.groovy b/src/test/groovy/grape/GrapeIvyTest.groovy
index 1fc93c30b2..9096ab2fab 100644
--- a/src/test/groovy/grape/GrapeIvyTest.groovy
+++ b/src/test/groovy/grape/GrapeIvyTest.groovy
@@ -374,6 +374,32 @@ final class GrapeIvyTest {
         '''
     }
 
+    @Test
+    void testInvalidGroup() {
+        def ex = shouldFail '''
+            @Grab('org/ejml:ejml-simple:0.41')
+            import org.ejml.simple.SimpleMatrix
+        '''
+        assert ex.message.contains("Grab: invalid value of 'org/ejml' for group")
+    }
+
+    @Test
+    void testInvalidVersion() {
+        def ex = shouldFail '''
+            @Grab('org.ejml:ejml-simple:0.41|')
+            import org.ejml.simple.SimpleMatrix
+        '''
+        assert ex.message.contains("Grab: invalid value of '0.41|' for version")
+    }
+
+    @Test
+    void testInvalidMutuallyExclusiveArgs() {
+        def ex = shouldFail '''
+            groovy.grape.Grape.grab(group: 'org.ejml', groupId: 'org.ejml', module: 'ejml-simple', version: '0.41')
+        '''
+        assert ex.message.contains('Grab: mutually exclusive arguments: [groupId, group]')
+    }
+
     @Test
     void testTransitiveShorthandExpectFailure() {
         shouldFail MissingPropertyException, '''