Skip to content

Commit a2e65d8

Browse files
Fix IllegalArgumentException when credentials contain $ or \
The withAuthentication() method uses String.replaceFirst() to inject credentials into the repository URL. The credentials string is used directly as the replacement argument, but in Java regex replacements $ and \ are special characters (group references and escape sequences). If a password contains $ (e.g. "pa$$word"), replaceFirst() throws java.lang.IllegalArgumentException: Illegal group reference. Fix: wrap credentials with Matcher.quoteReplacement() to escape any regex-special characters before using them in the replacement string. Made-with: Cursor
1 parent 8ad40ad commit a2e65d8

1 file changed

Lines changed: 2 additions & 1 deletion

File tree

mavengem-wagon/src/main/java/org/torquebox/mojo/mavengem/wagon/MavenGemWagon.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.net.SocketAddress;
2525
import java.net.URL;
2626
import java.net.URLConnection;
27+
import java.util.regex.Matcher;
2728

2829
public class MavenGemWagon extends StreamWagon {
2930

@@ -115,7 +116,7 @@ private URL withAuthentication(String url)
115116
throws MalformedURLException {
116117
if (authenticationInfo != null && authenticationInfo.getUserName() != null) {
117118
String credentials = authenticationInfo.getUserName() + ":" + authenticationInfo.getPassword();
118-
url = url.replaceFirst("^(https?://)(.*)$", "$1" + credentials + "@$2");
119+
url = url.replaceFirst("^(https?://)(.*)", "$1" + Matcher.quoteReplacement(credentials) + "@$2");
119120
}
120121
return new URL(url);
121122
}

0 commit comments

Comments
 (0)