This is an automated email from the ASF dual-hosted git repository.
slachiewicz pushed a commit to branch maven-4.0.x
in repository https://gitbox.apache.org/repos/asf/maven.git
The following commit(s) were added to refs/heads/maven-4.0.x by this push:
new 9802e6cea9 Read the password to encrypt from the standard input when
there is no console (fix #10230) (#13589)
9802e6cea9 is described below
commit 9802e6cea971448b53e93491d275aab1dc59a385
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)
## What and why
`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 `{}`.
## How
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`.
## Tests
`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).
(cherry picked from commit 73e6521070a518de45e5938ac85953dd68d4249b)
Ported to Maven 4: only --encrypt-password reads a password here, as
--encrypt-master-password already fails with "not supported anymore";
the code is in compat/maven-embedder. The mvn launcher of Maven 4 does not
have these options; mvnenc is the tool for it.
---
.../main/java/org/apache/maven/cli/MavenCli.java | 46 +++++++++++++++++-----
.../java/org/apache/maven/cli/MavenCliTest.java | 30 ++++++++++++++
2 files changed, 67 insertions(+), 9 deletions(-)
diff --git
a/compat/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java
b/compat/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java
index e9f2cfc03b..c1dc6c3da2 100644
--- a/compat/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java
+++ b/compat/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java
@@ -20,12 +20,14 @@
import javax.xml.stream.XMLStreamException;
+import java.io.BufferedReader;
import java.io.Console;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
+import java.io.InputStreamReader;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystem;
@@ -973,6 +975,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.
//
@@ -984,15 +1020,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:
");
}
System.out.println(dispatcher.encrypt(passwd, null));
throw new ExitException(0);
diff --git
a/compat/maven-embedder/src/test/java/org/apache/maven/cli/MavenCliTest.java
b/compat/maven-embedder/src/test/java/org/apache/maven/cli/MavenCliTest.java
index 44c8ce6212..04d8ee2c9e 100644
--- a/compat/maven-embedder/src/test/java/org/apache/maven/cli/MavenCliTest.java
+++ b/compat/maven-embedder/src/test/java/org/apache/maven/cli/MavenCliTest.java
@@ -18,6 +18,7 @@
*/
package org.apache.maven.cli;
+import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
@@ -107,6 +108,35 @@ 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
void testPerformProfileActivation() throws ParseException {
final CommandLineParser parser = new DefaultParser();