Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
import org.eclipse.aether.repository.WorkspaceReader;
import org.eclipse.aether.resolution.ResolutionErrorPolicy;
import org.eclipse.aether.util.ConfigUtils;
import org.eclipse.aether.util.connector.transport.http.HttpTransporterUtils;
import org.eclipse.aether.util.graph.manager.TransitiveDependencyManager;
import org.eclipse.aether.util.listener.ChainedRepositoryListener;
import org.eclipse.aether.util.repository.AuthenticationBuilder;
Expand Down Expand Up @@ -384,7 +385,6 @@ public RepositorySystemSession.SessionBuilder newRepositorySessionBuilder(MavenE
AuthenticationBuilder authBuilder = new AuthenticationBuilder();
authBuilder.addUsername(server.getUsername()).addPassword(server.getPassword());
authBuilder.addPrivateKey(server.getPrivateKey(), server.getPassphrase());
authSelector.add(server.getId(), authBuilder.build());

// origins the operator bound to this id explicitly, added to the ones collected above from the
// mirrors and the repositories of the request
Expand Down Expand Up @@ -418,9 +418,19 @@ public RepositorySystemSession.SessionBuilder newRepositorySessionBuilder(MavenE
if (properties != null && properties.length > 0) {
headers = new HashMap<>();
for (PlexusConfiguration property : properties) {
headers.put(
property.getChild("name").getValue(),
property.getChild("value").getValue());
String name = property.getChild("name").getValue();
String value = property.getChild("value").getValue();
headers.put(name, value);
// Wire token headers into Authentication so the resolver enforces
// origin-scoping, preemptive sending, and RFC 9110 validation.
// Values were already decrypted by DefaultSettingsDecrypter.
if (name != null && value != null && HttpTransporterUtils.isValidTokenHeaderName(name)) {
if ("Authorization".equalsIgnoreCase(name) && value.startsWith("Bearer ")) {

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.

we need a configurable list of these (i'd say just header name) since there are a lot of patterns and not all are bearer so maybe something like:

Authorization
PRIVATE-TOKEN
JOB-TOKEN
Circle-Token
X-Buildkite-Token
X-Gitlab-Token
X-API-Key
API-Key
apikey
api_key
X-API-Token
Ocp-Apim-Subscription-Key
X-Goog-Api-Key
X-Auth-Token
X-AUTH-TOKEN
X-Auth-Key
X-Access-Token
X-Forwarded-Access-Token
X-Token
Access-Token
Private-Token
Client-Token
Client-Secret
X-Client-Secret
X-Client-Key
DPoP
X-Jenkins-Crumb
X-Original-Authorization

authBuilder.addBearerToken(value.substring("Bearer ".length()));
} else {
authBuilder.addTokenHeader(name, value);
}
}
}
}
}
Expand Down Expand Up @@ -477,6 +487,7 @@ public RepositorySystemSession.SessionBuilder newRepositorySessionBuilder(MavenE

configProps.put("aether.transport.wagon.perms.fileMode." + server.getId(), server.getFilePermissions());
configProps.put("aether.transport.wagon.perms.dirMode." + server.getId(), server.getDirectoryPermissions());
authSelector.add(server.getId(), authBuilder.build());
}
String credentialScope = ConfigUtils.getString(
configProps, OriginBoundAuthenticationSelector.SCOPE_ORIGIN, MAVEN_REPOSITORY_CREDENTIAL_SCOPE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.apache.maven.settings.building.DefaultSettingsProblem;
import org.apache.maven.settings.building.SettingsProblem;
import org.apache.maven.settings.building.SettingsProblem.Severity;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.sonatype.plexus.components.sec.dispatcher.SecDispatcher;
import org.sonatype.plexus.components.sec.dispatcher.SecDispatcherException;

Expand Down Expand Up @@ -82,6 +83,8 @@ public SettingsDecryptionResult decrypt(SettingsDecryptionRequest request) {
-1,
e));
}

decryptHttpHeaders(server, problems);
}

List<Proxy> proxies = new ArrayList<>();
Expand All @@ -107,6 +110,42 @@ public SettingsDecryptionResult decrypt(SettingsDecryptionRequest request) {
return new DefaultSettingsDecryptionResult(servers, proxies, problems);
}

/**
* Decrypts the values of {@code <configuration><httpHeaders><property><value>} entries in-place
* on the given (already cloned) server, so that encrypted header values such as bearer tokens
* stored with {@code mvn --encrypt-password} are usable by the HTTP transports.
*/
private void decryptHttpHeaders(Server server, List<SettingsProblem> problems) {
if (server.getConfiguration() == null) {
return;
}
Xpp3Dom config = (Xpp3Dom) server.getConfiguration();
Xpp3Dom httpHeaders = config.getChild("httpHeaders");
if (httpHeaders == null) {
return;
}
for (Xpp3Dom property : httpHeaders.getChildren("property")) {
Xpp3Dom nameNode = property.getChild("name");
Xpp3Dom valueNode = property.getChild("value");
if (nameNode == null || valueNode == null || valueNode.getValue() == null) {
continue;
}
try {
String decrypted = decrypt(valueNode.getValue());
valueNode.setValue(decrypted);
} catch (SecDispatcherException e) {
problems.add(new DefaultSettingsProblem(
"Failed to decrypt HTTP header '" + nameNode.getValue() + "' for server " + server.getId()
+ ": " + e.getMessage(),
Severity.ERROR,
"server: " + server.getId() + ", configuration/httpHeaders/" + nameNode.getValue(),
-1,
-1,
e));
}
}
}

private String decrypt(String str) throws SecDispatcherException {
return (str == null) ? null : securityDispatcher.decrypt(str);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.settings.crypto;

import java.util.Collections;

import org.apache.maven.settings.Server;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.junit.jupiter.api.Test;
import org.sonatype.plexus.components.sec.dispatcher.SecDispatcher;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* Unit tests for {@link DefaultSettingsDecrypter}, focused on httpHeaders decryption.
*/
class DefaultSettingsDecrypterTest {

/** A dispatcher that strips braces to simulate decryption. */
private static final SecDispatcher SIMULATED = str -> {
if (str != null && str.startsWith("{") && str.endsWith("}")) {
return "decrypted:" + str.substring(1, str.length() - 1);
}
return str;
};

/**
* Verifies that httpHeaders property values are decrypted by the settings decrypter,
* so that bearer tokens and custom token headers stored with {@code mvn --encrypt-password}
* are usable by the HTTP transports without a settings schema change.
*/
@Test
void httpHeaderValuesAreDecrypted() {
DefaultSettingsDecrypter decrypter = new DefaultSettingsDecrypter(SIMULATED);

Server server = serverWithHeader("gitlab", "Private-Token", "{my-encrypted-token}");

DefaultSettingsDecryptionRequest request = new DefaultSettingsDecryptionRequest();
request.setServers(Collections.singletonList(server));

SettingsDecryptionResult result = decrypter.decrypt(request);

assertTrue(result.getProblems().isEmpty(), "no decryption problems expected");
assertEquals(
"decrypted:my-encrypted-token",
headerValue(result.getServers().get(0), "Private-Token"),
"header value should be decrypted");
}

/**
* Verifies that plain-text httpHeaders values pass through unchanged and no problems are reported.
*/
@Test
void plainHttpHeaderValuesPassThrough() {
DefaultSettingsDecrypter decrypter = new DefaultSettingsDecrypter(SIMULATED);

Server server = serverWithHeader("internal", "Authorization", "Bearer plain-token");

DefaultSettingsDecryptionRequest request = new DefaultSettingsDecryptionRequest();
request.setServers(Collections.singletonList(server));

SettingsDecryptionResult result = decrypter.decrypt(request);

assertTrue(result.getProblems().isEmpty());
assertEquals(
"Bearer plain-token",
headerValue(result.getServers().get(0), "Authorization"),
"plain value should pass through unchanged");
}

/**
* Verifies that a server without httpHeaders configuration is left untouched.
*/
@Test
void serverWithoutHttpHeadersIsNotAffected() {
DefaultSettingsDecrypter decrypter = new DefaultSettingsDecrypter(SIMULATED);

Server server = new Server();
server.setId("simple");
server.setPassword("{encrypted-password}");

DefaultSettingsDecryptionRequest request = new DefaultSettingsDecryptionRequest();
request.setServers(Collections.singletonList(server));

SettingsDecryptionResult result = decrypter.decrypt(request);

assertTrue(result.getProblems().isEmpty());
assertEquals("decrypted:encrypted-password", result.getServers().get(0).getPassword());
}

private static Server serverWithHeader(String id, String headerName, String headerValue) {
Server server = new Server();
server.setId(id);

Xpp3Dom config = new Xpp3Dom("configuration");
Xpp3Dom httpHeaders = new Xpp3Dom("httpHeaders");
config.addChild(httpHeaders);

Xpp3Dom property = new Xpp3Dom("property");
Xpp3Dom nameNode = new Xpp3Dom("name");
nameNode.setValue(headerName);
Xpp3Dom valueNode = new Xpp3Dom("value");
valueNode.setValue(headerValue);
property.addChild(nameNode);
property.addChild(valueNode);
httpHeaders.addChild(property);

server.setConfiguration(config);
return server;
}

private static String headerValue(Server server, String headerName) {
Xpp3Dom config = (Xpp3Dom) server.getConfiguration();
for (Xpp3Dom property : config.getChild("httpHeaders").getChildren("property")) {
if (headerName.equals(property.getChild("name").getValue())) {
return property.getChild("value").getValue();
}
}
return null;
}
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ under the License.
<!-- plexus-cipher 2.1.0 uses an incompatible KDF; ciphertext encrypted with 2.0 will no longer decrypt — see #12811. Do not bump on this branch. -->
<cipherVersion>2.0</cipherVersion>
<jxpathVersion>1.4.0</jxpathVersion>
<resolverVersion>2.0.26</resolverVersion>
<resolverVersion>2.0.27-SNAPSHOT</resolverVersion>
<slf4jVersion>2.0.20</slf4jVersion>
<xmlunitVersion>2.14.0</xmlunitVersion>
<powermockVersion>2.0.9</powermockVersion>
Expand Down
Loading