You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by kw...@apache.org on 2025/09/08 15:33:24 UTC

(jackrabbit-filevault) 01/01: JCRVLT-819: Upgrade to JLine 3

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

kwin pushed a commit to branch feature/migrate-to-jline3
in repository https://gitbox.apache.org/repos/asf/jackrabbit-filevault.git

commit 12ec6d1f42062cac195134ee9d23558140076527
Author: Konrad Windszus <kw...@apache.org>
AuthorDate: Mon Sep 8 17:33:13 2025 +0200

    JCRVLT-819: Upgrade to JLine 3
    
    Fixes CVE-2023-50572
---
 vault-cli/pom.xml                                  |  4 +-
 .../apache/jackrabbit/vault/cli/VaultFsApp.java    | 18 +++---
 .../jackrabbit/vault/util/console/Console.java     | 64 ++++++++--------------
 .../vault/util/console/ContextAwareCompleter.java  | 59 ++++++++++++++++++++
 .../vault/util/console/commands/CmdHistory.java    |  3 +-
 5 files changed, 95 insertions(+), 53 deletions(-)

diff --git a/vault-cli/pom.xml b/vault-cli/pom.xml
index a154b79f..e62ae69e 100644
--- a/vault-cli/pom.xml
+++ b/vault-cli/pom.xml
@@ -239,9 +239,9 @@
             <scope>compile</scope>
         </dependency>
         <dependency>
-            <groupId>jline</groupId>
+            <groupId>org.jline</groupId>
             <artifactId>jline</artifactId>
-            <version>1.0</version>
+            <version>3.30.5</version>
             <scope>compile</scope>
         </dependency>
 
diff --git a/vault-cli/src/main/java/org/apache/jackrabbit/vault/cli/VaultFsApp.java b/vault-cli/src/main/java/org/apache/jackrabbit/vault/cli/VaultFsApp.java
index ef320d3b..ff5c0ac4 100644
--- a/vault-cli/src/main/java/org/apache/jackrabbit/vault/cli/VaultFsApp.java
+++ b/vault-cli/src/main/java/org/apache/jackrabbit/vault/cli/VaultFsApp.java
@@ -63,6 +63,7 @@ import org.apache.jackrabbit.vault.vlt.CredentialsStore;
 import org.apache.jackrabbit.vault.vlt.VltContext;
 import org.apache.jackrabbit.vault.vlt.VltDirectory;
 import org.apache.jackrabbit.vault.vlt.meta.MetaDirectory;
+import org.jline.reader.LineReaderBuilder;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -792,16 +793,13 @@ public class VaultFsApp extends AbstractApplication {
         public Credentials getCredentials(RepositoryAddress mountpoint) {
             Credentials creds = base.getCredentials(mountpoint);
             if (creds instanceof SimpleCredentials) {
-                try {
-                    SimpleCredentials simpleCredentials = (SimpleCredentials) creds;
-                    if (simpleCredentials.getPassword().length == 0) {
-                        System.out.printf(Locale.ENGLISH, "Please enter password for user %s connecting to %s: ",
-                                simpleCredentials.getUserID(), mountpoint);
-                        String password = new jline.ConsoleReader().readLine('*');
-                        creds = new SimpleCredentials(simpleCredentials.getUserID(), password.toCharArray());
-                    }
-                } catch (IOException e) {
-                    log.error("Error while opening console for reading password" + e);
+                SimpleCredentials simpleCredentials = (SimpleCredentials) creds;
+                if (simpleCredentials.getPassword().length == 0) {
+                    System.out.printf(Locale.ENGLISH, "Please enter password for user %s connecting to %s: ",
+                            simpleCredentials.getUserID(), mountpoint);
+                    // ensure JLine is initialized
+                    String password = LineReaderBuilder.builder().build().readLine('*');
+                    creds = new SimpleCredentials(simpleCredentials.getUserID(), password.toCharArray());
                 }
             }
             return creds;
diff --git a/vault-cli/src/main/java/org/apache/jackrabbit/vault/util/console/Console.java b/vault-cli/src/main/java/org/apache/jackrabbit/vault/util/console/Console.java
index c1659de6..c02bfabe 100644
--- a/vault-cli/src/main/java/org/apache/jackrabbit/vault/util/console/Console.java
+++ b/vault-cli/src/main/java/org/apache/jackrabbit/vault/util/console/Console.java
@@ -23,19 +23,22 @@ import java.util.Map;
 import java.util.Set;
 
 import org.apache.jackrabbit.vault.util.console.util.Table;
+import org.jline.reader.History;
+import org.jline.reader.LineReader;
+import org.jline.reader.LineReaderBuilder;
+import org.jline.reader.impl.completer.StringsCompleter;
+import org.jline.reader.impl.history.DefaultHistory;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import jline.Completor;
-import jline.ConsoleReader;
-import jline.History;
-import jline.SimpleCompletor;
 
 /**
  * {@code Console}...
  */
 public class Console {
 
+    static final String VARIABLE_CONTEXT_NAME = "CONTEXT";
+
     /**
      * the default logger
      */
@@ -43,11 +46,11 @@ public class Console {
 
     private ConsoleExecutionContext currentCtx;
 
-    private final Map contexts = new HashMap();
+    private final Map<String, ConsoleExecutionContext> contexts = new HashMap<>();
 
     private final AbstractApplication app;
 
-    private ConsoleReader reader;
+    private LineReader reader;
 
     /**
      * indicates console is running
@@ -65,7 +68,7 @@ public class Console {
         }
         contexts.put(ctx.getName(), ctx);
         ctx.attach(this);
-        currentCtx = ctx;
+        switchContext(ctx.getName());
     }
 
     public void removeContext(ConsoleExecutionContext ctx) {
@@ -79,19 +82,9 @@ public class Console {
         switchContext(ctx.getName());
     }
 
-    private void setCompletor() {
-        // always remove existing
-        Iterator iter = reader.getCompletors().iterator();
-        while (iter.hasNext()) {
-            reader.removeCompletor((Completor) iter.next());
-            iter = reader.getCompletors().iterator();
-        }
-        Set triggers = currentCtx.getCommandsGroup().getTriggers();
-        reader.addCompletor(new SimpleCompletor((String[]) triggers.toArray(new String[triggers.size()])));
-    }
-
     public void switchContext(String name) {
-        if (name == null) {
+        if (name == null) { 
+            // lists all contexts
             Iterator iter = contexts.keySet().iterator();
             Table t = new Table(2);
             while (iter.hasNext()) {
@@ -111,8 +104,8 @@ public class Console {
             if (!contexts.containsKey(name)) {
                 throw new ExecutionException("No such context: " + name);
             }
-            currentCtx = (ConsoleExecutionContext) contexts.get(name);
-            setCompletor();
+            currentCtx = contexts.get(name);
+            reader.setVariable(VARIABLE_CONTEXT_NAME, name);
             log.info("Switched to context '{}'", name);
         }
     }
@@ -124,25 +117,17 @@ public class Console {
         return app;
     }
 
-    protected void initJLine() {
-        History history = new History();
-        /*
-        try {
-            history = new History(new File(".consolehistory"));
-        } catch (IOException e) {
-            log.warn("Cannot read or write file for storing command line history: " + e.getMessage() + "");
-            history = new History();
-        }
-        */
-        reader.setHistory(history);
-        reader.setUseHistory(true);
-        setCompletor();
+    protected LineReader createJLineReader() {
+        History history = new DefaultHistory();
+        return LineReaderBuilder.builder()
+            .history(history)
+            .completer(new ContextAwareCompleter(contexts))
+            .build();
     }
 
     public void run() throws IOException {
 
-        reader = new ConsoleReader();
-        initJLine();
+        reader = createJLineReader();
         // setup and start
         setup();
 
@@ -159,13 +144,12 @@ public class Console {
                             // re-execute event from history
                             String oldLine;
                             try {
-                                int historyIndex = Integer.valueOf(line.substring(1).trim()).intValue();
-                                oldLine = (String) reader.getHistory().getHistoryList().get(historyIndex - 1);
+                                oldLine = reader.getHistory().get(reader.getHistory().last());
                             } catch (Exception e) {
                                 System.out.println("  " + line + ": event not found");
                                 continue;
                             }
-                            reader.getHistory().addToHistory(oldLine);
+                            reader.getHistory().add(oldLine);
                             System.out.println("Executing '" + oldLine + "'");
                             line = oldLine;
                         }
@@ -182,7 +166,7 @@ public class Console {
         close();
     }
 
-    public ConsoleReader getReader() {
+    public LineReader getReader() {
         return reader;
     }
 
diff --git a/vault-cli/src/main/java/org/apache/jackrabbit/vault/util/console/ContextAwareCompleter.java b/vault-cli/src/main/java/org/apache/jackrabbit/vault/util/console/ContextAwareCompleter.java
new file mode 100644
index 00000000..6bac1a16
--- /dev/null
+++ b/vault-cli/src/main/java/org/apache/jackrabbit/vault/util/console/ContextAwareCompleter.java
@@ -0,0 +1,59 @@
+/*
+ * 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.
+ */
+package org.apache.jackrabbit.vault.util.console;
+
+import java.util.List;
+import java.util.Map;
+
+import org.jline.reader.Candidate;
+import org.jline.reader.Completer;
+import org.jline.reader.LineReader;
+import org.jline.reader.ParsedLine;
+import org.jline.reader.impl.completer.StringsCompleter;
+
+public class ContextAwareCompleter implements Completer {
+
+    private final Map<String, Completer> contextCompleters;
+
+    public ContextAwareCompleter(Map<String, ConsoleExecutionContext> contexts) {
+        if (contexts == null || contexts.isEmpty()) {
+            throw new IllegalArgumentException("No contexts provided for ContextAwareCompleter");
+        }
+        contextCompleters = new java.util.HashMap<>();
+        for (Map.Entry<String, ConsoleExecutionContext> entry : contexts.entrySet()) {
+            Iterable<String> commands = entry.getValue().getCommandsGroup().getTriggers();
+            contextCompleters.put(entry.getKey(), new StringsCompleter(commands));
+        }
+    }
+
+    @Override
+    public void complete(LineReader reader, ParsedLine line, List<Candidate> candidates) {
+        // Get current context from reader variables
+        String context = (String) reader.getVariable(Console.VARIABLE_CONTEXT_NAME);
+        if (context == null) {
+            throw new IllegalStateException("No context set in reader variables");
+        }
+        // Use the appropriate completer for this context
+        Completer contextCompleter = contextCompleters.get(context);
+        // If no completer found for context, fallback to a default behavior
+        if (contextCompleter == null) {
+            throw new IllegalStateException("No completer found for context: " + context);
+        }
+        contextCompleter.complete(reader, line, candidates);
+    }
+
+}
diff --git a/vault-cli/src/main/java/org/apache/jackrabbit/vault/util/console/commands/CmdHistory.java b/vault-cli/src/main/java/org/apache/jackrabbit/vault/util/console/commands/CmdHistory.java
index 15355d9e..cff296aa 100644
--- a/vault-cli/src/main/java/org/apache/jackrabbit/vault/util/console/commands/CmdHistory.java
+++ b/vault-cli/src/main/java/org/apache/jackrabbit/vault/util/console/commands/CmdHistory.java
@@ -22,6 +22,7 @@ import org.apache.commons.cli2.CommandLine;
 import org.apache.commons.cli2.builder.CommandBuilder;
 import org.apache.commons.cli2.option.Command;
 import org.apache.jackrabbit.vault.util.console.ConsoleExecutionContext;
+import org.jline.reader.History;
 
 /**
  */
@@ -31,7 +32,7 @@ public class CmdHistory extends AbstractConsoleCommand {
             throws Exception {
 
         // print the history
-        Iterator iter = ctx.getConsole().getReader().getHistory().getHistoryList().iterator();
+        Iterator<History.Entry> iter = ctx.getConsole().getReader().getHistory().iterator();
         int i=1;
         while (iter.hasNext()) {
             System.out.println("  " + i + "  " + iter.next());