Skip to content

Commit 6b2a27f

Browse files
authored
[MGPG-136] Windows passphrase corruption (#120)
Since 3.2.0 version we always appended "line separator" to passphrase unless it itself ended with one. But, this caused problem on Windows, as (our assumption is) that GPG uses binary read of STDIN, and on Windows "line separator" is "\r\n", while GPG handles "\n" only, making passphrase corrupted by presence of unwanted "\r". --- https://issues.apache.org/jira/browse/MGPG-136
1 parent 31e87e0 commit 6b2a27f

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

src/main/java/org/apache/maven/plugins/gpg/AbstractGpgMojo.java

+17
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,22 @@ public abstract class AbstractGpgMojo extends AbstractMojo {
270270
@Parameter(property = "gpg.bestPractices", defaultValue = "false")
271271
private boolean bestPractices;
272272

273+
/**
274+
* Whether to terminate the passphrase with LF character or not, as on some systems and some GPG executable combinations
275+
* lack of trailing LF may cause GPG to not detect passphrase on STDIN. Since 3.2.0 it was always appended, unless
276+
* passphrase itself ended with it. Note: before 3.2.7 the "line separator" was used for termination, that on
277+
* other hand caused issues on Windows, where line separator is CRLF while GPG handles LF only.
278+
* This parameter affects ONLY the GPG signer, not the BC signer.
279+
* <p>
280+
* By default, this parameter is {@code true}.
281+
*
282+
* @since 3.2.7
283+
* @see <a href="https://issues.apache.org/jira/browse/MGPG-99">MGPG-99</a>
284+
* @see <a href="https://issues.apache.org/jira/browse/MGPG-136">MGPG-136</a>
285+
*/
286+
@Parameter(property = "gpg.terminatePassphrase", defaultValue = "true")
287+
private boolean terminatePassphrase;
288+
273289
/**
274290
* Current user system settings for use in Maven.
275291
*
@@ -345,6 +361,7 @@ protected AbstractGpgSigner newSigner(MavenProject mavenProject) throws MojoFail
345361
signer.setPublicKeyring(publicKeyring);
346362
signer.setLockMode(lockMode);
347363
signer.setArgs(gpgArguments);
364+
signer.setTerminatePassphrase(terminatePassphrase);
348365

349366
// "new way": env prevails
350367
String passphrase =

src/main/java/org/apache/maven/plugins/gpg/AbstractGpgSigner.java

+6
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ public abstract class AbstractGpgSigner {
4646

4747
protected String passphrase;
4848

49+
protected boolean terminatePassphrase;
50+
4951
private File outputDir;
5052

5153
private File buildDir;
@@ -98,6 +100,10 @@ public void setPassPhrase(String s) {
98100
passphrase = s;
99101
}
100102

103+
public void setTerminatePassphrase(boolean b) {
104+
this.terminatePassphrase = b;
105+
}
106+
101107
public void setOutputDirectory(File out) {
102108
outputDir = out;
103109
}

src/main/java/org/apache/maven/plugins/gpg/GpgSigner.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,10 @@ protected void generateSignatureForFile(File file, File signature) throws MojoEx
112112
cmd.createArg().setValue("--passphrase-fd");
113113
cmd.createArg().setValue("0");
114114

115-
// Prepare the input stream which will be used to pass the passphrase to the executable
116-
if (!passphrase.endsWith(System.lineSeparator())) {
117-
in = new ByteArrayInputStream((passphrase + System.lineSeparator()).getBytes());
115+
// Prepare the STDIN stream which will be used to pass the passphrase to the executable
116+
// but obey terminatePassphrase: append LF if asked for
117+
if (terminatePassphrase && !passphrase.endsWith("\n")) {
118+
in = new ByteArrayInputStream((passphrase + "\n").getBytes());
118119
} else {
119120
in = new ByteArrayInputStream(passphrase.getBytes());
120121
}

0 commit comments

Comments
 (0)