Skip to content

feat: wire configuration/httpHeaders into Authentication for token auth (#14203) - #14205

Open
gnodet wants to merge 1 commit into
apache:masterfrom
gnodet:port/14203-token-auth-master
Open

gnodet wants to merge 1 commit into
apache:masterfrom
gnodet:port/14203-token-auth-master

Conversation

@gnodet

@gnodet gnodet commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Port of #14203 (maven-3.10.x) to master.

Summary

Wire <configuration><httpHeaders> entries into AuthenticationBuilder token headers, so that bearer tokens and custom HTTP authentication headers stored in settings.xml are:

  • Origin-scoped by the Resolver's addBearerToken/addTokenHeader path, so tokens are never sent to cross-origin redirects
  • RFC 9110 validated by HttpTransporterUtils.isValidTokenHeaderName before being added to the Authentication
  • Moved authSelector.add() to after the headers loop so token headers are included in the built Authentication object

Changes

  1. impl/maven-core — DefaultRepositorySystemSessionFactory: wire token headers from <configuration><httpHeaders> into AuthenticationBuilder, with guards for empty bearer tokens, duplicate headers, and malformed timeout values
  2. compat/maven-settings-builder — DefaultSettingsDecrypter: add decryptHttpHeaders() to decrypt <configuration><httpHeaders> property values in-place. Maven 3 embedders use the compat layer directly (bypassing DefaultSettingsBuilder's DecryptingSettingsTransformer), so without this change encrypted httpHeaders values would not be decrypted when going through the compat path.
  3. pom.xml: bump resolverVersion to 2.0.27 (required for isValidTokenHeaderName, addBearerToken, addTokenHeader)

Difference from 3.10.x port (#14203)

The impl/maven-impl's DefaultSettingsBuilder already decrypts all configuration XmlNode values via its DecryptingSettingsTransformer, which walks the DOM tree recursively and calls transform(String) on every leaf value — including httpHeaders property values. This master port therefore needs an additional change in compat/maven-settings-builder to bring the same decryption to the Maven 3 compat layer.

Sequencing note: The companion PR #14203 (maven-3.10.x port) is still open. The compat/maven-settings-builder change here mirrors what will land in Maven 3.x via #14203 — the sequencing is intentional (master first, then backport), but #14203 should land shortly after this PR merges.

Licensed under the Apache License, Version 2.0. I confirm that my contribution is made under the terms of the Apache License Version 2.0.

@gnodet gnodet added enhancement New feature or request backport-to-4.0.x security Security issue or fix. Report undisclosed vulnerabilities to security@apache.org, not here. labels Dec 1, 2026
@gnodet
gnodet requested review from cstamas and rmannibucau December 1, 2026 07:46

@gnodet-bot gnodet-bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Core logic is correct — moving authSelector.add() to after the headers loop is the right fix to include token headers in the built Authentication. Two points worth addressing before merge:

1. authSelector.add() skipped on malformed timeout value (behavior regression)

The PR moves authSelector.add(server.getId(), authBuilder.build()) from before the configuration block to after it. This means Integer.parseInt(connectTimeoutXml.getValue()) and Integer.parseInt(requestTimeoutXml.getValue()) now execute between building the auth and registering it. If a settings.xml has a non-numeric <connectTimeout> or <requestTimeout>, a NumberFormatException will propagate up and authSelector.add() will never be called — leaving the server without registered authentication. Previously the auth was registered unconditionally before config parsing, so malformed timeout values only broke the timeout configuration, not authentication itself.

Consider wrapping the timeout-parsing block in a try/catch, or move authSelector.add() to immediately after the headers loop (before the timeout block):

// Add to auth selector after token headers are wired in, before timeout parsing
authSelector.add(server.getId(), authBuilder.build());

PlexusConfiguration connectTimeoutXml = config.getChild("connectTimeout", false);
// ...

2. Empty bearer token admitted silently

authBuilder.addBearerToken(value.substring("Bearer ".length()))

If a user configures <value>Bearer </value> (the prefix with no actual token), substring returns "". addBearerToken("") calls addTokenHeader(AUTHORIZATION, "Bearer ") — a blank Authorization header will be sent to the repository, likely causing a 401. A guard would catch the misconfiguration early:

String token = value.substring("Bearer ".length());
if (!token.isEmpty()) {
    authBuilder.addBearerToken(token);
}

3. No test coverage for the new auth wiring

The new code path (token headers in Authentication) is security-sensitive and has zero test coverage. A unit test on DefaultRepositorySystemSessionFactory verifying that a Bearer <token> header in settings.xml ends up in the AuthenticationSelector would catch regressions without requiring a full integration setup.


This review was generated by an AI agent, Hermès on behalf of @gnodet.

@gnodet-bot gnodet-bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM on the structural fix — moving authSelector.add() after the headers loop is the correct fix for the race where token headers would have been excluded from the built Authentication. The approach (reusing the existing <httpHeaders> path, delegating validation to isValidTokenHeaderName) is clean.

Two issues need attention before this can ship:

Missing test for the new auth-wiring path. The existing httpConfigurationWithHttpHeadersTest only asserts the HTTP_HEADERS config map — it does not verify that bearer/token headers are wired into the Authentication object. The new code path (addBearerToken/addTokenHeader) is completely untested.

Uncaught IllegalArgumentException from duplicate headers. AuthenticationBuilder.addTokenHeader/addBearerToken throw IllegalArgumentException if the same header name is added twice (case-insensitive — enforced by tokenValueKey()). A malformed settings.xml with two Authorization properties would propagate this exception uncaught through newRepositorySessionBuilder, crashing Maven startup with a stack trace rather than a user-friendly diagnostic.

This review was generated by an AI agent, Hermès on behalf of @gnodet.

Comment thread pom.xml
<plexusTestingVersion>2.2.0</plexusTestingVersion>
<plexusXmlVersion>4.2.1</plexusXmlVersion>
<resolverVersion>2.0.26</resolverVersion>
<resolverVersion>2.0.27-SNAPSHOT</resolverVersion>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

💡 2.0.27-SNAPSHOT cannot ship in a Maven release. Update to 2.0.27 once apache/maven-resolver#2372 is released (the resolver PR was merged 2026-11-30 so the release should follow shortly).

@gnodet

gnodet commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

The integration-tests (macos-latest, 17) failure is unrelated to this PR — the failed ITs (MavenITmng3684, MavenITmng7353, MavenITmng0461) all hit HTTP 429 rate-limiting from Maven Central, with 30-60s timeouts. Network flakiness on the macOS runner, not a code regression.

…th (apache#14203)

Port of apache#14203 (maven-3.10.x) to master.

Wire `<configuration><httpHeaders>` entries into `AuthenticationBuilder`
token headers, so bearer tokens and custom HTTP authentication headers
stored in `settings.xml` are:

- Origin-scoped by the Resolver's `addBearerToken`/`addTokenHeader` path
- RFC 9110 validated by `HttpTransporterUtils.isValidTokenHeaderName`
- Moved `authSelector.add()` to after the headers loop so token headers
  are included in the built Authentication object

No decryption change needed here: `DefaultSettingsBuilder` already
decrypts all `configuration` XmlNode values via its
`DecryptingSettingsTransformer`, which walks the DOM tree recursively.

Requires maven-resolver 2.0.27-SNAPSHOT for `isValidTokenHeaderName`,
`addBearerToken`, and `addTokenHeader`.
@gnodet
gnodet force-pushed the port/14203-token-auth-master branch from fe937dc to 0b046d9 Compare December 2, 2026 15:44
@gnodet

gnodet commented Dec 2, 2026

Copy link
Copy Markdown
Contributor Author

Addressed the gnodet-bot findings and pushed updated branch:

  • Empty bearer token guard: added if (!token.isEmpty()) check before calling addBearerToken() to skip a bare Bearer prefix with no token value.
  • Uncaught IllegalArgumentException on duplicate headers: wrapped addBearerToken/addTokenHeader in a try/catch that logs a WARN instead of propagating the exception.
  • Missing test coverage: added three new tests in DefaultRepositorySystemSessionFactoryTest covering bearer token wiring, custom token header wiring, and graceful handling of duplicate headers.
  • DefaultSettingsDecrypter http-header decryption: added decryptHttpHeaders() to decrypt <configuration><httpHeaders> values in-place (with tests in DefaultSettingsDecrypterTest).
  • Timeout parsing: wrapped connectTimeout/requestTimeout Integer.parseInt() calls in try/catch (NumberFormatException) with WARN logging.

The 2.0.27-SNAPSHOT dependency thread is left open pending the maven-resolver 2.0.27 release.

@gnodet-bot gnodet-bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Following up on my earlier review — the previous three findings are addressed:

  • Empty bearer token guard (!token.isEmpty()) ✅
  • IllegalArgumentException on duplicate headers (try/catch with logger.warn) ✅
  • Test coverage (3 tests in DefaultRepositorySystemSessionFactoryTest, 3 in DefaultSettingsDecrypterTest) ✅
  • Timeout parsing (NumberFormatException caught with logger.warn) ✅

One issue with this round:

PR description contradicts the actual diff. The "Difference from 3.10.x port" section states:

No change to compat/maven-settings-builder or any decryption code needed here. The only changes are: (1) impl/maven-core — DefaultRepositorySystemSessionFactory and (2) pom.xml.

The diff shows a third change: compat/maven-settings-builder/DefaultSettingsDecrypter.java now gains decryptHttpHeaders(). The compat change is functionally correct — Maven 3 embedders using the compat layer bypass DefaultSettingsBuilder and its DecryptingSettingsTransformer, so they would miss httpHeaders decryption without this addition. But the description is stale and will mislead future readers.

Additionally, per the compat-layer review rule, behaviour changes in compat/maven-settings-builder should be paired with the equivalent change already landed in Maven 3.x. The companion PR #14203 (maven-3.10.x port) is still open — the compat change here is ahead of the Maven 3.x port. This isn't a blocker if the sequencing is intentional, but it should be called out.

Please update the PR description to list the compat change as a third modification, and note the sequencing dependency on #14203.

The resolverVersion>2.0.27-SNAPSHOT blocker is already tracked in the open review thread.

This review was generated by an AI agent, Hermès on behalf of @gnodet.

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

Labels

backport-to-4.0.x enhancement New feature or request security Security issue or fix. Report undisclosed vulnerabilities to security@apache.org, not here.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants