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

jbonofre pushed a commit to branch karaf-4.4.x
in repository https://gitbox.apache.org/repos/asf/karaf.git


The following commit(s) were added to refs/heads/karaf-4.4.x by this push:
     new 68ec7bbd50 fix(shell): add a command ACL for the groovy scope (#3064)
68ec7bbd50 is described below

commit 68ec7bbd501fdc21c2a4fde812eae3fdcb0f0c72
Author: JB Onofré <[email protected]>
AuthorDate: Wed Nov 11 05:27:11 2026 +0000

    fix(shell): add a command ACL for the groovy scope (#3064)
    
    When the groovy feature is installed, the shell feature installs
    org.apache.karaf.shell.groovy, which provides groovy:exec and
    groovy:exec-file. The groovy scope had no org.apache.karaf.command.acl.*
    configuration, and the command guard allows every command of a scope
    without ACL (unless karaf.secured.command.compulsory.roles is set), so
    any user who can log in to the console, viewer included, could evaluate
    Groovy code in the Karaf JVM.
    
    Restrict both commands to admin, consistent with shell:exec and
    shell:java. The ACL is provided by a <config> element under the groovy
    conditional of the shell feature (same mechanism as the kar, jdbc and
    jms scopes).
    
    Pass the admin role in GroovyTest, add an SSH command security itest
    for the groovy scope, and document the ACL in the user guide.
    
    Reported-by: MUTED CYBERSECURITY S.L.
---
 .../features/standard/src/main/feature/feature.xml | 32 +++++++++++
 .../src/test/filtered-resources/etc/feature.xml    | 10 ++++
 .../java/org/apache/karaf/itests/GroovyTest.java   | 10 ++--
 .../itests/ssh/GroovySshCommandSecurityTest.java   | 63 ++++++++++++++++++++++
 manual/src/main/asciidoc/user-guide/security.adoc  |  2 +
 5 files changed, 114 insertions(+), 3 deletions(-)

diff --git a/assemblies/features/standard/src/main/feature/feature.xml 
b/assemblies/features/standard/src/main/feature/feature.xml
index bf03466cae..c9a2eb0c57 100644
--- a/assemblies/features/standard/src/main/feature/feature.xml
+++ b/assemblies/features/standard/src/main/feature/feature.xml
@@ -392,6 +392,38 @@ disableEofExit = false
         <bundle 
start-level="30">mvn:org.apache.karaf.shell/org.apache.karaf.shell.commands/${project.version}</bundle>
         <conditional>
             <condition>groovy</condition>
+            <config name="org.apache.karaf.command.acl.groovy">
+################################################################################
+#
+#    Licensed to the Apache Software Foundation (ASF) under one or more
+#    contributor license agreements.  See the NOTICE file distributed with
+#    this work for additional information regarding copyright ownership.
+#    The ASF licenses this file to You under the Apache License, Version 2.0
+#    (the "License"); you may not use this file except in compliance with
+#    the License.  You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+#    Unless required by applicable law or agreed to in writing, software
+#    distributed under the License is distributed on an "AS IS" BASIS,
+#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#    See the License for the specific language governing permissions and
+#    limitations under the License.
+#
+################################################################################
+
+#
+# This configuration file defines the ACLs for commands in the groovy subshell
+#
+# For an explanation of the syntax of this file, see the file:
+#   org.apache.karaf.command.acl.system.cfg
+#
+# 'exec' and 'exec-file' evaluate arbitrary Groovy code in the Karaf JVM, so 
they
+# are restricted to 'admin', consistent with shell:exec and shell:java.
+#
+exec = admin
+exec-file = admin
+            </config>
             <bundle 
start-level="30">mvn:org.apache.karaf.shell/org.apache.karaf.shell.groovy/${project.version}</bundle>
         </conditional>
     </feature>
diff --git a/itests/test/src/test/filtered-resources/etc/feature.xml 
b/itests/test/src/test/filtered-resources/etc/feature.xml
index 03726969db..fa003f3c0e 100644
--- a/itests/test/src/test/filtered-resources/etc/feature.xml
+++ b/itests/test/src/test/filtered-resources/etc/feature.xml
@@ -260,6 +260,16 @@
         <bundle 
start-level="30">mvn:org.apache.karaf.shell/org.apache.karaf.shell.commands/${project.version}</bundle>
          <conditional>
             <condition>groovy</condition>
+            <config name="org.apache.karaf.command.acl.groovy">
+                #
+                # This configuration file defines the ACLs for commands in the 
groovy subshell
+                #
+                # For an explanation of the syntax of this file, see the file:
+                #   org.apache.karaf.command.acl.system.cfg
+                #
+                exec = admin
+                exec-file = admin
+            </config>
             <bundle 
start-level="30">mvn:org.apache.karaf.shell/org.apache.karaf.shell.groovy/${project.version}</bundle>
         </conditional>
     </feature>
diff --git a/itests/test/src/test/java/org/apache/karaf/itests/GroovyTest.java 
b/itests/test/src/test/java/org/apache/karaf/itests/GroovyTest.java
index ebee38ca81..cf6faba35e 100644
--- a/itests/test/src/test/java/org/apache/karaf/itests/GroovyTest.java
+++ b/itests/test/src/test/java/org/apache/karaf/itests/GroovyTest.java
@@ -13,6 +13,7 @@
  */
 package org.apache.karaf.itests;
 
+import org.apache.karaf.jaas.boot.principal.RolePrincipal;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -32,6 +33,9 @@ import static 
org.ops4j.pax.exam.karaf.options.KarafDistributionOption.replaceCo
 @ExamReactorStrategy(PerClass.class)
 public class GroovyTest extends BaseTest {
 
+    // groovy:exec and groovy:exec-file are restricted to admin by the groovy 
command ACL
+    private static final RolePrincipal ADMIN = new RolePrincipal("admin");
+
     @Configuration
     public Option[] config() {
         List<Option> result = new LinkedList<>(Arrays.asList(super.config()));
@@ -51,16 +55,16 @@ public class GroovyTest extends BaseTest {
                 + "def add(x, y) {\n"
                 + "  return x + y \n"
                 + "}\n"
-                + "println add(1, 2)\"");
+                + "println add(1, 2)\"", ADMIN);
         assertContains("3", resultNoArgs);
 
-        String resultTwoArgs = executeCommand("groovy:exec \"(x as int) + (y 
as int)\" x=1 y=2");
+        String resultTwoArgs = executeCommand("groovy:exec \"(x as int) + (y 
as int)\" x=1 y=2", ADMIN);
         assertContains("3", resultTwoArgs);
     }
 
     @Test
     public void execGroovyFileCommand() throws Exception {
-        String result = executeCommand("groovy:exec-file script.groovy");
+        String result = executeCommand("groovy:exec-file script.groovy", 
ADMIN);
         assertContains("3", result);
     }
 }
diff --git 
a/itests/test/src/test/java/org/apache/karaf/itests/ssh/GroovySshCommandSecurityTest.java
 
b/itests/test/src/test/java/org/apache/karaf/itests/ssh/GroovySshCommandSecurityTest.java
new file mode 100644
index 0000000000..13f5b99344
--- /dev/null
+++ 
b/itests/test/src/test/java/org/apache/karaf/itests/ssh/GroovySshCommandSecurityTest.java
@@ -0,0 +1,63 @@
+/*
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.karaf.itests.ssh;
+
+import org.junit.Assume;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.ops4j.pax.exam.junit.PaxExam;
+import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
+import org.ops4j.pax.exam.spi.reactors.PerClass;
+
+/**
+ * This test exercises the Shell Command ACL for the groovy scope commands as 
defined in the
+ * {@code org.apache.karaf.command.acl.groovy} configuration provided by the 
{@code shell} feature
+ * when the {@code groovy} feature is installed.
+ *
+ * <p>{@code groovy:exec} and {@code groovy:exec-file} evaluate arbitrary 
Groovy code in the Karaf
+ * JVM, so they are restricted to the {@code admin} role, like {@code 
shell:exec} and
+ * {@code shell:java}.</p>
+ */
+@RunWith(PaxExam.class)
+@ExamReactorStrategy(PerClass.class)
+public class GroovySshCommandSecurityTest extends SshCommandTestBase {
+
+    private static int counter = 0;
+
+    @Test
+    public void testGroovyCommandSecurityViaSsh() throws Exception {
+        // Skip on Windows where PTY output can be garbled,
+        // when upgrading to Junit5, this can be replaced with 
@DisabledOnOs(OS.WINDOWS)
+        Assume.assumeFalse(System.getProperty("os.name", 
"").toLowerCase().contains("win"));
+
+        installAndAssertFeature("groovy");
+
+        String manageruser = "man" + System.nanoTime() + "_" + counter++;
+        String vieweruser = "view" + System.nanoTime() + "_" + counter++;
+
+        addUsers(manageruser, vieweruser);
+
+        // the admin user can see and run the groovy commands (a bare 
invocation reports a
+        // missing-argument error, not "Command not found")
+        assertCommand("karaf", "groovy:exec", Result.OK);
+        assertCommand("karaf", "groovy:exec-file", Result.OK);
+
+        // exec / exec-file are admin-only: a viewer and a manager must not 
even see the commands
+        assertCommand(vieweruser, "groovy:exec", Result.NOT_FOUND);
+        assertCommand(vieweruser, "groovy:exec-file", Result.NOT_FOUND);
+
+        assertCommand(manageruser, "groovy:exec", Result.NOT_FOUND);
+        assertCommand(manageruser, "groovy:exec-file", Result.NOT_FOUND);
+    }
+}
diff --git a/manual/src/main/asciidoc/user-guide/security.adoc 
b/manual/src/main/asciidoc/user-guide/security.adoc
index fdc6817bbe..9111e38753 100644
--- a/manual/src/main/asciidoc/user-guide/security.adoc
+++ b/manual/src/main/asciidoc/user-guide/security.adoc
@@ -511,6 +511,8 @@ By default, Apache Karaf defines the following commands 
ACLs:
  As `jdbc:ds-create` stores an arbitrary JDBC URL that pax-jdbc-config turns 
into a live datasource (several drivers run code at connection time, for 
instance H2 `INIT=RUNSCRIPT`), and `jdbc:execute` and `jdbc:query` run 
arbitrary SQL, these commands together with `jdbc:ds-delete` are restricted to 
the users with the `admin` role. The read-only `jdbc:*` commands require the 
`viewer` role.
 * `etc/org.apache.karaf.command.acl.jms.cfg` configuration file (provided by 
the `jms` feature) defines the ACL for `jms:*` commands.
  As `jms:create` stores an arbitrary broker URL that pax-jms-config turns into 
a live connection factory, and `jms:send`, `jms:consume` and `jms:move` write 
to or destructively read from broker destinations, these commands together with 
`jms:delete` are restricted to the users with the `admin` role. The read-only 
`jms:*` commands require the `viewer` role.
+* `etc/org.apache.karaf.command.acl.groovy.cfg` configuration file (provided 
by the `shell` feature when the `groovy` feature is installed) defines the ACL 
for `groovy:*` commands.
+ As `groovy:exec` and `groovy:exec-file` evaluate arbitrary Groovy code in the 
Karaf JVM, they are restricted to the users with the `admin` role, like 
`shell:exec` and `shell:java`.
 
 You can change these default ACLs, and add your own ACLs for additional 
command scopes (for instance `etc/org.apache.karaf.command.acl.cluster.cfg` for
 Apache Karaf Cellar, `etc/org.apache.karaf.command.acl.camel.cfg` from Apache 
Camel, ...).

Reply via email to