Skip to content

Fix IllegalArgumentException when credentials contain $ or \#18

Merged
headius merged 1 commit intojruby:masterfrom
rkoretskiy-cloudlinux:fix/quote-credentials-in-replaceFirst
Mar 27, 2026
Merged

Fix IllegalArgumentException when credentials contain $ or \#18
headius merged 1 commit intojruby:masterfrom
rkoretskiy-cloudlinux:fix/quote-credentials-in-replaceFirst

Conversation

@rkoretskiy-cloudlinux
Copy link
Copy Markdown
Contributor

Problem

MavenGemWagon.withAuthentication() injects server credentials into the repository URL using String.replaceFirst():

url = url.replaceFirst("^(https?://)(.*)$", "$1" + credentials + "@$2");

In Java regex replacements, $ denotes a group reference ($1, $2) and \ is an escape character. If a password contains these characters (e.g. pa$$word), replaceFirst() throws:

java.lang.IllegalArgumentException: Illegal group reference

This breaks any setup where:

  • The Maven settings.xml defines a <server> with credentials for a mavengem mirror
  • The password contains $ (very common in generated passwords) or \

Fix

Wrap the credentials string with Matcher.quoteReplacement() before passing it to replaceFirst(). This escapes $ and \ so they are treated as literals in the replacement string, while keeping the group references $1 and $2 functional for the URL parts.

url = url.replaceFirst("^(https?://)(.*)", "$1" + Matcher.quoteReplacement(credentials) + "@$2");

Context

We discovered this while setting up a Maven mirror for rubygems in a corporate environment (Sonatype Nexus). The settings.xml defines a <server> block with Nexus credentials for the mavengem mirror, and the service account password contains $.

Made with Cursor

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
@rkoretskiy-cloudlinux rkoretskiy-cloudlinux force-pushed the fix/quote-credentials-in-replaceFirst branch from a2e65d8 to 3ed33fe Compare March 27, 2026 17:27
Copy link
Copy Markdown
Member

@headius headius left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good find and simple fix!

@headius
Copy link
Copy Markdown
Member

headius commented Mar 27, 2026

It would be excellent if you could come up with a test case for this, but I'll merge and get something released to avoid blocking you.

@headius headius merged commit 9e862f1 into jruby:master Mar 27, 2026
1 check passed
@headius
Copy link
Copy Markdown
Member

headius commented Mar 30, 2026

mavengem 2.0.3 has been released!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants