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

slachiewicz pushed a commit to branch maven-3.9.x
in repository https://gitbox.apache.org/repos/asf/maven.git

commit 3d8c20128ed83c31c4d086842ec565e19c240711
Author: Arturo Bernal <[email protected]>
AuthorDate: Tue Oct 20 17:13:21 2026 +0200

    Read the password to encrypt from the standard input when there is no 
console (fix #10230) (#13589)
    
    `mvn --encrypt-master-password` and `mvn --encrypt-password` read the 
password with `System.console()`. When the streams are redirected there is no 
console, the password stayed `null`, and the command printed an empty `{}`, 
which makes it unusable from another program (#10230, MNG-8398). I could 
reproduce it on 3.9.16: `echo myMasterPassword | mvn --encrypt-master-password` 
prints `{}`.
    
    A new `MavenCli.readPassword` reads the password from the console and, when 
there is none, from the first line of the standard input. When no password can 
be read, or it is empty, the command now fails with `No password was provided` 
instead of encrypting nothing. The standard input is not closed.
    
    This is for 3.10.x. Maven 4 does not have these options any more, it has 
`mvnenc`.
    
    `MavenCliTest` covers reading from a redirected input and the missing and 
empty cases. The console path cannot be mocked, because `java.io.Console` is 
final. I also built the distribution of this branch: `echo myMasterPassword | 
mvn --encrypt-master-password` prints the encrypted `{...}` value, and with an 
empty input it fails with the new message. `maven-embedder` and its 
dependencies pass (45 tests) and spotless is clean. I did not run the 
integration tests.
    
    ---
    
    To make clear that you license your contribution under
    the [Apache License Version 2.0, January 
2004](https://www.apache.org/licenses/LICENSE-2.0)
    check one of the following:
    
    - [x] I hereby declare this contribution to be licensed under the [Apache 
License Version 2.0, January 2004](https://www.apache.org/licenses/LICENSE-2.0)
    - [x] I hereby declare that I have filed an [Apache Individual Contributor 
License Agreement](https://www.apache.org/licenses/icla.pdf).
---
 .../main/java/org/apache/maven/cli/MavenCli.java   | 56 +++++++++++++++-------
 .../java/org/apache/maven/cli/MavenCliTest.java    | 29 +++++++++++
 2 files changed, 67 insertions(+), 18 deletions(-)

diff --git a/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java 
b/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java
index 0fa53a26af..e41028e0a7 100644
--- a/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java
+++ b/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java
@@ -26,6 +26,8 @@
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
 import java.io.PrintStream;
 import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
@@ -845,6 +847,40 @@ private List<File> parseExtClasspath(CliRequest 
cliRequest) {
         return jars;
     }
 
+    /**
+     * Reads a password from the console and, when there is none because the 
streams are redirected, from the first
+     * line of the given input.
+     *
+     * @param console the console, or {@code null} if the streams are 
redirected
+     * @param in the input to read the first line from when there is no console
+     * @param prompt the prompt shown by the console; it is not shown when the 
password is read from the input, as
+     *               the caller is then not interactive and the prompt would 
only pollute its output
+     * @return the password, never empty
+     * @throws IOException if the input cannot be read
+     * @throws IllegalStateException if no password could be read, so that an 
empty one is not encrypted
+     */
+    static String readPassword(Console console, InputStream in, String prompt) 
throws IOException {
+        String password;
+        if (console != null) {
+            char[] chars = console.readPassword(prompt);
+            password = (chars == null) ? null : String.copyValueOf(chars);
+
+            if (chars != null) {
+                // Sun/Oracle advises to empty the char array
+                java.util.Arrays.fill(chars, ' ');
+            }
+        } else {
+            // do not close the reader, that would close the standard input
+            password = new BufferedReader(new InputStreamReader(in, 
StandardCharsets.UTF_8)).readLine();
+        }
+
+        if (password == null || password.isEmpty()) {
+            throw new IllegalStateException("No password was provided");
+        }
+
+        return password;
+    }
+
     //
     // This should probably be a separate tool and not be baked into Maven.
     //
@@ -853,15 +889,7 @@ private void encryption(CliRequest cliRequest) throws 
Exception {
             String passwd = 
cliRequest.commandLine.getOptionValue(CLIManager.ENCRYPT_MASTER_PASSWORD);
 
             if (passwd == null) {
-                Console cons = System.console();
-                char[] password = (cons == null) ? null : 
cons.readPassword("Master password: ");
-                if (password != null) {
-                    // Cipher uses Strings
-                    passwd = String.copyValueOf(password);
-
-                    // Sun/Oracle advises to empty the char array
-                    java.util.Arrays.fill(password, ' ');
-                }
+                passwd = readPassword(System.console(), System.in, "Master 
password: ");
             }
 
             DefaultPlexusCipher cipher = new DefaultPlexusCipher();
@@ -873,15 +901,7 @@ private void encryption(CliRequest cliRequest) throws 
Exception {
             String passwd = 
cliRequest.commandLine.getOptionValue(CLIManager.ENCRYPT_PASSWORD);
 
             if (passwd == null) {
-                Console cons = System.console();
-                char[] password = (cons == null) ? null : 
cons.readPassword("Password: ");
-                if (password != null) {
-                    // Cipher uses Strings
-                    passwd = String.copyValueOf(password);
-
-                    // Sun/Oracle advises to empty the char array
-                    java.util.Arrays.fill(password, ' ');
-                }
+                passwd = readPassword(System.console(), System.in, "Password: 
");
             }
 
             String configurationFile = dispatcher.getConfigurationFile();
diff --git 
a/maven-embedder/src/test/java/org/apache/maven/cli/MavenCliTest.java 
b/maven-embedder/src/test/java/org/apache/maven/cli/MavenCliTest.java
index 564b1fb465..abcf479709 100644
--- a/maven-embedder/src/test/java/org/apache/maven/cli/MavenCliTest.java
+++ b/maven-embedder/src/test/java/org/apache/maven/cli/MavenCliTest.java
@@ -72,6 +72,35 @@ public void tearDown() throws Exception {
         }
     }
 
+    /**
+     * With redirected streams there is no console, and the password is read 
from the standard input, as the
+     * encryption printed an empty result otherwise (MNG-8398).
+     */
+    @Test
+    public void testReadPasswordFromRedirectedInput() throws Exception {
+        ByteArrayInputStream in = new 
ByteArrayInputStream("secret\nignored\n".getBytes(StandardCharsets.UTF_8));
+
+        assertEquals("secret", MavenCli.readPassword(null, in, "Password: "));
+    }
+
+    @Test
+    public void testReadPasswordFromRedirectedInputIsDecodedAsUtf8() throws 
Exception {
+        ByteArrayInputStream in = new 
ByteArrayInputStream("p\u00e4ss\u20ac\n".getBytes(StandardCharsets.UTF_8));
+
+        assertEquals("p\u00e4ss\u20ac", MavenCli.readPassword(null, in, 
"Password: "));
+    }
+
+    @Test
+    public void testReadPasswordRejectsMissingPassword() {
+        assertThrows(
+                IllegalStateException.class,
+                () -> MavenCli.readPassword(null, new ByteArrayInputStream(new 
byte[0]), "Password: "));
+        assertThrows(
+                IllegalStateException.class,
+                () -> MavenCli.readPassword(
+                        null, new 
ByteArrayInputStream("\n".getBytes(StandardCharsets.UTF_8)), "Password: "));
+    }
+
     @Test
     public void testCalculateDegreeOfConcurrency() {
         assertThrows(IllegalArgumentException.class, () -> 
cli.calculateDegreeOfConcurrency("0"));

Reply via email to