This is an automated email from the ASF dual-hosted git repository.

slachiewicz pushed a commit to branch maven-3.9.x
in repository https://gitbox.apache.org/repos/asf/maven.git

commit eb91c8b1fbcb4e85059ef7ac8cdb7838ba4c3615
Author: Slawomir Jaranowski <[email protected]>
AuthorDate: Sat Sep 26 21:04:19 2026 +0200

    Issue #13265: let a settings server declare the repository origins its 
credentials may be used with
    
    Server credentials are scoped to the origin (scheme, host and port) of the
    repositories and mirrors declared with the same server id. Repositories
    declared inside a settings profile only reach that set when the profile id 
is
    listed in <activeProfiles>; a profile activated through <activation> or -P
    contributes no origin, so its credentials are warned about under the default
    origin scope and refused under strict, with nothing the operator can do 
about
    it. The session is built before any project exists, so profile activation
    cannot be evaluated there.
    
    A <server> can now declare those origins explicitly:
    
      <server>
        <id>internal</id>
        <username>u</username>
        <password>p</password>
        <repositoryOrigins>
          <repositoryOrigin>https://repo.example.org</repositoryOrigin>
          <repositoryOrigin>https://mirror.example.org:8443</repositoryOrigin>
        </repositoryOrigins>
      </server>
    
    The declared origins are added to the ones Maven derives on its own; nothing
    is replaced. Values are bare origins, not repository URLs, and are 
validated:
    a value no origin can be derived from is an error, while a full repository 
URL
    is accepted with a warning naming the origin actually used. Values with a
    property placeholder are skipped, as validation runs before interpolation.
    
    The warnings of the origin binding now name <repositoryOrigins> as the way 
to
    declare a missing origin.
    
    (cherry picked from commit ae59501ad8e2944c387124d71d3bea28554d4ba3)
    
    Backport of #13265 to maven-3.9.x, which already binds server credentials
    to repository origins (aeb0a05540) but had no way to declare an origin on
    the server itself. Adapted for 3.9.x:
    DefaultRepositorySystemSessionFactoryTest injects
    DefaultRepositorySystemSessionFactory and uses newRepositorySession(), as
    3.9.x has no RepositorySystemSessionFactory interface.
---
 .../DefaultRepositorySystemSessionFactory.java     |  25 +++-
 .../aether/OriginBoundAuthenticationSelector.java  |  30 ++--
 .../DefaultRepositorySystemSessionFactoryTest.java | 154 +++++++++++++++++++++
 .../validation/DefaultSettingsValidator.java       |  79 +++++++++++
 .../DefaultSettingsBuilderFactoryTest.java         |  23 +++
 .../validation/DefaultSettingsValidatorTest.java   |  69 +++++++++
 .../settings/factory/settings-servers-4.xml        |  38 +++++
 maven-settings/src/main/mdo/settings.mdo           |  19 +++
 8 files changed, 422 insertions(+), 15 deletions(-)

diff --git 
a/maven-core/src/main/java/org/apache/maven/internal/aether/DefaultRepositorySystemSessionFactory.java
 
b/maven-core/src/main/java/org/apache/maven/internal/aether/DefaultRepositorySystemSessionFactory.java
index bff8148778..35737ac84c 100644
--- 
a/maven-core/src/main/java/org/apache/maven/internal/aether/DefaultRepositorySystemSessionFactory.java
+++ 
b/maven-core/src/main/java/org/apache/maven/internal/aether/DefaultRepositorySystemSessionFactory.java
@@ -116,15 +116,20 @@ public class DefaultRepositorySystemSessionFactory {
      * User property selecting how server credentials configured in settings 
are scoped to repositories:
      * <ul>
      *     <li>{@code origin} (default): credentials for a server id are only 
used with a repository whose
-     *     origin (protocol, host and port) matches a repository or mirror 
declared with the same id in
-     *     settings or on the command line. For server ids without any such 
declared repository (for
-     *     example pure deployment servers whose URL comes from the project's
+     *     origin (protocol, host and port) matches an origin declared for the 
same id, either by a
+     *     repository or mirror declared with that id in settings or on the 
command line, or by the
+     *     {@code <repositoryOrigins>} of that server in settings. For server 
ids without any such declared
+     *     origin (for example pure deployment servers whose URL comes from 
the project's
      *     {@code distributionManagement}), credentials are used as before, 
but a warning identifying the
      *     target origin is emitted.</li>
      *     <li>{@code strict}: like {@code origin}, but credentials are 
refused for server ids that have no
-     *     repository or mirror declared in settings or on the command 
line.</li>
+     *     declared origin at all.</li>
      *     <li>{@code id}: legacy behavior, credentials are matched by server 
id only.</li>
      * </ul>
+     * <p>
+     * Repositories declared inside a settings {@code <profile>} only 
contribute an origin when the profile
+     * id is listed in {@code <activeProfiles>}; profiles activated through 
{@code <activation>} or
+     * {@code -P} contribute none, which is what {@code 
<server><repositoryOrigins>} is for.
      *
      * @since 3.9.10
      */
@@ -229,8 +234,9 @@ public DefaultRepositorySystemSession 
newRepositorySession(MavenExecutionRequest
             }
         }
 
-        // origins of the repositories and mirrors the operator declared for a 
given server id, used below
-        // to scope that id's credentials to the origin(s) it was actually 
configured for
+        // origins of the repositories and mirrors the operator declared for a 
given server id, completed
+        // below with the origins declared on the servers themselves, used to 
scope that id's credentials
+        // to the origin(s) it was actually configured for
         Map<String, Set<String>> declaredRepositoryOrigins = new HashMap<>();
 
         DefaultMirrorSelector mirrorSelector = new DefaultMirrorSelector();
@@ -273,6 +279,13 @@ public DefaultRepositorySystemSession 
newRepositorySession(MavenExecutionRequest
             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
+            for (String repositoryOrigin : server.getRepositoryOrigins()) {
+                OriginBoundAuthenticationSelector.addOrigin(
+                        declaredRepositoryOrigins, server.getId(), 
repositoryOrigin);
+            }
+
             if (server.getConfiguration() != null) {
                 Xpp3Dom dom = (Xpp3Dom) server.getConfiguration();
                 for (int i = dom.getChildCount() - 1; i >= 0; i--) {
diff --git 
a/maven-core/src/main/java/org/apache/maven/internal/aether/OriginBoundAuthenticationSelector.java
 
b/maven-core/src/main/java/org/apache/maven/internal/aether/OriginBoundAuthenticationSelector.java
index 1898bf9666..9ba8eb5f89 100644
--- 
a/maven-core/src/main/java/org/apache/maven/internal/aether/OriginBoundAuthenticationSelector.java
+++ 
b/maven-core/src/main/java/org/apache/maven/internal/aether/OriginBoundAuthenticationSelector.java
@@ -38,10 +38,11 @@
  * port) of the repository or mirror the operator declared for the same server 
id.
  * <p>
  * A repository's id and its origin are independent: this selector serves a 
server id's credentials
- * only to a repository whose origin matches one the operator declared for 
that id, in settings or on
- * the command line. Ids with no operator-declared origin keep the previous 
behaviour unless
- * {@code strict} scope is requested, and a warning naming the target origin 
is emitted once per
- * id/origin pair.
+ * only to a repository whose origin matches one the operator declared for 
that id, either by declaring
+ * a repository or mirror with that id in settings or on the command line, or 
by listing the origin in
+ * that server's {@code <repositoryOrigins>} in settings. Ids with no 
operator-declared origin keep the
+ * previous behaviour unless {@code strict} scope is requested, and a warning 
naming the target origin
+ * is emitted once per id/origin pair.
  *
  * @see DefaultRepositorySystemSessionFactory#MAVEN_REPOSITORY_CREDENTIAL_SCOPE
  */
@@ -135,8 +136,10 @@ public Authentication getAuthentication(RemoteRepository 
repository) {
                     id,
                     origin,
                     "Not using credentials of server '" + id + "' for 
repository " + repository.getUrl()
-                            + ": the repository or mirror declared for this id 
resides at " + origins
-                            + ". Set "
+                            + ": the origins declared for this id are " + 
origins
+                            + ". Add " + originHint(origin)
+                            + " to <repositoryOrigins> of that server in 
settings if these credentials belong"
+                            + " there, or set "
                             + 
DefaultRepositorySystemSessionFactory.MAVEN_REPOSITORY_CREDENTIAL_SCOPE + "="
                             + SCOPE_ID + " to restore legacy id-only 
credential matching.");
             return null;
@@ -147,8 +150,10 @@ public Authentication getAuthentication(RemoteRepository 
repository) {
                     origin,
                     "Not using credentials of server '" + id + "' for 
repository " + repository.getUrl()
                             + ": no repository or mirror with this id is 
declared in settings or on the command"
-                            + " line, and " + 
DefaultRepositorySystemSessionFactory.MAVEN_REPOSITORY_CREDENTIAL_SCOPE
-                            + "=" + SCOPE_STRICT + " is in effect.");
+                            + " line and the server declares no 
<repositoryOrigins>, and "
+                            + 
DefaultRepositorySystemSessionFactory.MAVEN_REPOSITORY_CREDENTIAL_SCOPE
+                            + "=" + SCOPE_STRICT + " is in effect. Declare " + 
originHint(origin)
+                            + " in <repositoryOrigins> of that server in 
settings to allow it.");
             return null;
         }
         warnOnce(
@@ -156,12 +161,19 @@ public Authentication getAuthentication(RemoteRepository 
repository) {
                 origin,
                 "Using credentials of server '" + id + "' for repository " + 
repository.getUrl()
                         + ", although no repository or mirror with this id is 
declared in settings or on the"
-                        + " command line. Set "
+                        + " command line and the server declares no 
<repositoryOrigins>. Declare "
+                        + originHint(origin)
+                        + " in <repositoryOrigins> of that server in settings 
to bind these credentials"
+                        + " explicitly, or set "
                         + 
DefaultRepositorySystemSessionFactory.MAVEN_REPOSITORY_CREDENTIAL_SCOPE
                         + "=" + SCOPE_STRICT + " to refuse such credential 
use.");
         return auth;
     }
 
+    private static String originHint(String origin) {
+        return origin != null ? "'" + origin + "'" : "its origin";
+    }
+
     private void warnOnce(String id, String origin, String message) {
         if (reported.add(id + "->" + origin)) {
             logger.warn(message);
diff --git 
a/maven-core/src/test/java/org/apache/maven/internal/aether/DefaultRepositorySystemSessionFactoryTest.java
 
b/maven-core/src/test/java/org/apache/maven/internal/aether/DefaultRepositorySystemSessionFactoryTest.java
new file mode 100644
index 0000000000..8e4b2e6d31
--- /dev/null
+++ 
b/maven-core/src/test/java/org/apache/maven/internal/aether/DefaultRepositorySystemSessionFactoryTest.java
@@ -0,0 +1,154 @@
+/*
+ * 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.internal.aether;
+
+import javax.inject.Inject;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Properties;
+
+import org.apache.maven.artifact.repository.ArtifactRepository;
+import org.apache.maven.bridge.MavenRepositorySystem;
+import org.apache.maven.execution.DefaultMavenExecutionRequest;
+import org.apache.maven.execution.MavenExecutionRequest;
+import org.apache.maven.settings.Mirror;
+import org.apache.maven.settings.Server;
+import org.codehaus.plexus.testing.PlexusTest;
+import org.eclipse.aether.repository.AuthenticationSelector;
+import org.eclipse.aether.repository.RemoteRepository;
+import org.junit.jupiter.api.Test;
+
+import static org.codehaus.plexus.testing.PlexusExtension.getBasedir;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+/**
+ * UT for {@link DefaultRepositorySystemSessionFactory}, focused on how server 
credentials are scoped
+ * to repository origins.
+ */
+@PlexusTest
+class DefaultRepositorySystemSessionFactoryTest {
+
+    @Inject
+    private DefaultRepositorySystemSessionFactory systemSessionFactory;
+
+    @Inject
+    private MavenRepositorySystem mavenRepositorySystem;
+
+    @Test
+    void credentialsServedForOriginDeclaredOnlyByServer() throws Exception {
+        MavenExecutionRequest request = requestWithServer(
+                serverWithRepositoryOrigins("internal", 
"https://repo.example.org";, "https://mirror.example.org:8443";));
+
+        AuthenticationSelector selector = authenticationSelector(request);
+
+        assertNotNull(selector.getAuthentication(repository("internal", 
"https://repo.example.org/releases/";)));
+        assertNotNull(selector.getAuthentication(repository("internal", 
"https://mirror.example.org:8443/repo/";)));
+        assertNull(selector.getAuthentication(repository("internal", 
"https://evil.example.org/releases/";)));
+    }
+
+    @Test
+    void credentialsServedForOriginDeclaredOnlyByServerInStrictScope() throws 
Exception {
+        MavenExecutionRequest request =
+                requestWithServer(serverWithRepositoryOrigins("internal", 
"https://repo.example.org";));
+        request.setSystemProperties(strictCredentialScope());
+
+        AuthenticationSelector selector = authenticationSelector(request);
+
+        assertNotNull(selector.getAuthentication(repository("internal", 
"https://repo.example.org/releases/";)));
+        assertNull(selector.getAuthentication(repository("internal", 
"https://evil.example.org/releases/";)));
+    }
+
+    @Test
+    void serverWithoutRepositoryOriginsIsRefusedInStrictScope() throws 
Exception {
+        MavenExecutionRequest request = 
requestWithServer(serverWithRepositoryOrigins("internal"));
+        request.setSystemProperties(strictCredentialScope());
+
+        AuthenticationSelector selector = authenticationSelector(request);
+
+        assertNull(selector.getAuthentication(repository("internal", 
"https://repo.example.org/releases/";)));
+    }
+
+    @Test
+    void serverRepositoryOriginsAddToMirrorOrigins() throws Exception {
+        MavenExecutionRequest request =
+                requestWithServer(serverWithRepositoryOrigins("internal", 
"https://repo.example.org";));
+        Mirror mirror = new Mirror();
+        mirror.setId("internal");
+        mirror.setUrl("https://mirror.example.org/repo/";);
+        mirror.setMirrorOf("*");
+        request.setMirrors(new ArrayList<>(Collections.singletonList(mirror)));
+
+        AuthenticationSelector selector = authenticationSelector(request);
+
+        assertNotNull(selector.getAuthentication(repository("internal", 
"https://mirror.example.org/repo/";)));
+        assertNotNull(selector.getAuthentication(repository("internal", 
"https://repo.example.org/releases/";)));
+        assertNull(selector.getAuthentication(repository("internal", 
"https://evil.example.org/releases/";)));
+    }
+
+    @Test
+    void malformedServerRepositoryOriginIsIgnored() throws Exception {
+        MavenExecutionRequest request = 
requestWithServer(serverWithRepositoryOrigins("internal", "not an origin"));
+        request.setSystemProperties(strictCredentialScope());
+
+        AuthenticationSelector selector = authenticationSelector(request);
+
+        assertNull(selector.getAuthentication(repository("internal", 
"https://repo.example.org/releases/";)));
+    }
+
+    private static Properties strictCredentialScope() {
+        Properties properties = new Properties();
+        properties.put(
+                
DefaultRepositorySystemSessionFactory.MAVEN_REPOSITORY_CREDENTIAL_SCOPE,
+                OriginBoundAuthenticationSelector.SCOPE_STRICT);
+        return properties;
+    }
+
+    private static Server serverWithRepositoryOrigins(String id, String... 
repositoryOrigins) {
+        Server server = new Server();
+        server.setId(id);
+        server.setUsername("jason");
+        server.setPassword("abc123");
+        server.setRepositoryOrigins(new 
ArrayList<>(Arrays.asList(repositoryOrigins)));
+        return server;
+    }
+
+    private MavenExecutionRequest requestWithServer(Server server) throws 
Exception {
+        MavenExecutionRequest request = new DefaultMavenExecutionRequest();
+        request.setLocalRepository(getLocalRepository());
+        request.setServers(new ArrayList<>(Collections.singletonList(server)));
+        return request;
+    }
+
+    private AuthenticationSelector 
authenticationSelector(MavenExecutionRequest request) {
+        return 
systemSessionFactory.newRepositorySession(request).getAuthenticationSelector();
+    }
+
+    private static RemoteRepository repository(String id, String url) {
+        return new RemoteRepository.Builder(id, "default", url).build();
+    }
+
+    private ArtifactRepository getLocalRepository() throws Exception {
+        File repoDir = new File(getBasedir(), 
"target/local-repo").getAbsoluteFile();
+        return mavenRepositorySystem.createLocalRepository(null, repoDir);
+    }
+}
diff --git 
a/maven-settings-builder/src/main/java/org/apache/maven/settings/validation/DefaultSettingsValidator.java
 
b/maven-settings-builder/src/main/java/org/apache/maven/settings/validation/DefaultSettingsValidator.java
index 5d32a14055..057067f43a 100644
--- 
a/maven-settings-builder/src/main/java/org/apache/maven/settings/validation/DefaultSettingsValidator.java
+++ 
b/maven-settings-builder/src/main/java/org/apache/maven/settings/validation/DefaultSettingsValidator.java
@@ -21,6 +21,8 @@
 import javax.inject.Named;
 import javax.inject.Singleton;
 
+import java.net.URI;
+import java.net.URISyntaxException;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
@@ -109,6 +111,8 @@ public void validate(Settings settings, 
SettingsProblemCollector problems) {
                                 "must be unique across all server ids and 
aliases but found duplicate alias " + alias);
                     }
                 }
+
+                validateRepositoryOrigins(problems, server, i);
             }
         }
 
@@ -222,6 +226,81 @@ private void validateRepositories(SettingsProblemCollector 
problems, List<Reposi
         }
     }
 
+    private static void validateRepositoryOrigins(SettingsProblemCollector 
problems, Server server, int index) {
+        for (int o = 0; o < server.getRepositoryOrigins().size(); o++) {
+            String repositoryOrigin = server.getRepositoryOrigins().get(o);
+            String fieldName = "servers.server[" + index + 
"].repositoryOrigins[" + o + "]";
+
+            if (!validateStringNotEmpty(problems, fieldName, repositoryOrigin, 
server.getId())) {
+                continue;
+            }
+
+            // settings are validated before they are interpolated here, so a 
placeholder is not something
+            // this validator can judge
+            if (repositoryOrigin.contains("${")) {
+                continue;
+            }
+
+            String invalid = invalidRepositoryOriginReason(repositoryOrigin);
+            if (invalid != null) {
+                addViolation(problems, Severity.ERROR, fieldName, 
server.getId(), invalid);
+                continue;
+            }
+
+            String ignored = 
ignoredRepositoryOriginPartsReason(repositoryOrigin);
+            if (ignored != null) {
+                addViolation(problems, Severity.WARNING, fieldName, 
server.getId(), ignored);
+            }
+        }
+    }
+
+    /**
+     * Parses a {@code <server><repositoryOrigins>} value the way the 
credential scoping does; see
+     * {@code OriginBoundAuthenticationSelector#originOf(String)} in 
maven-core, which is the authority
+     * on what an origin is. Returns {@code null} when the value is not a URI 
at all.
+     */
+    private static URI parseRepositoryOrigin(String value) {
+        try {
+            return new URI(value).parseServerAuthority();
+        } catch (URISyntaxException e) {
+            return null;
+        }
+    }
+
+    /**
+     * @return the reason why no origin can be derived from the given value, 
or {@code null} if one can
+     */
+    private static String invalidRepositoryOriginReason(String value) {
+        URI uri = parseRepositoryOrigin(value);
+        if (uri == null) {
+            return "must be a repository origin of the form 
scheme://host[:port] but found '" + value + "'";
+        }
+        if (uri.getScheme() == null) {
+            return "must start with a scheme, for example 
https://repo.example.org, but found '" + value + "'";
+        }
+        if (uri.getHost() == null) {
+            return "must name a host, for example https://repo.example.org, 
but found '" + value + "'";
+        }
+        if (uri.getUserInfo() != null) {
+            return "must not carry user information but found '" + value + "'";
+        }
+        return null;
+    }
+
+    /**
+     * @return the reason why parts of the given value are ignored, or {@code 
null} if it is a bare origin
+     */
+    private static String ignoredRepositoryOriginPartsReason(String value) {
+        URI uri = parseRepositoryOrigin(value);
+        String path = uri.getRawPath();
+        boolean extraPath = path != null && !path.isEmpty() && 
!"/".equals(path);
+        if (!extraPath && uri.getRawQuery() == null && uri.getRawFragment() == 
null) {
+            return null;
+        }
+        return "is a repository origin, not a repository URL; only '" + 
uri.getScheme() + "://" + uri.getAuthority()
+                + "' of '" + value + "' is used";
+    }
+
     // ----------------------------------------------------------------------
     // Field validation
     // ----------------------------------------------------------------------
diff --git 
a/maven-settings-builder/src/test/java/org/apache/maven/settings/building/DefaultSettingsBuilderFactoryTest.java
 
b/maven-settings-builder/src/test/java/org/apache/maven/settings/building/DefaultSettingsBuilderFactoryTest.java
index 8ef3b03500..512119a6f9 100644
--- 
a/maven-settings-builder/src/test/java/org/apache/maven/settings/building/DefaultSettingsBuilderFactoryTest.java
+++ 
b/maven-settings-builder/src/test/java/org/apache/maven/settings/building/DefaultSettingsBuilderFactoryTest.java
@@ -130,6 +130,29 @@ public void testSettingsWithServersAndAliases() throws 
Exception {
         assertTrue(server3.getAliases().isEmpty());
     }
 
+    @Test
+    void testSettingsWithServerRepositoryOrigins() throws Exception {
+        SettingsBuilder builder = new 
DefaultSettingsBuilderFactory().newInstance();
+
+        DefaultSettingsBuildingRequest request = new 
DefaultSettingsBuildingRequest();
+        request.setSystemProperties(System.getProperties());
+        request.setUserSettingsFile(getSettings("settings-servers-4"));
+
+        Settings settings = builder.build(request).getEffectiveSettings();
+
+        List<Server> servers = settings.getServers();
+        assertEquals(2, servers.size());
+
+        List<String> repositoryOrigins = 
Arrays.asList("https://repo.example.org";, "https://mirror.example.org:8443";);
+
+        Server server1 = getServerById(servers, "server-1");
+        assertEquals(repositoryOrigins, server1.getRepositoryOrigins());
+
+        // an alias is the same credentials under another id, so it is bound 
to the same origins
+        Server server11 = getServerById(servers, "server-11");
+        assertEquals(repositoryOrigins, server11.getRepositoryOrigins());
+    }
+
     private Server getServerById(List<Server> servers, String id) {
         return servers.stream()
                 .filter(s -> s.getId().equals(id))
diff --git 
a/maven-settings-builder/src/test/java/org/apache/maven/settings/validation/DefaultSettingsValidatorTest.java
 
b/maven-settings-builder/src/test/java/org/apache/maven/settings/validation/DefaultSettingsValidatorTest.java
index 628ecc6ea4..073f0ca392 100644
--- 
a/maven-settings-builder/src/test/java/org/apache/maven/settings/validation/DefaultSettingsValidatorTest.java
+++ 
b/maven-settings-builder/src/test/java/org/apache/maven/settings/validation/DefaultSettingsValidatorTest.java
@@ -266,6 +266,75 @@ void testValidateServerIdAliasesWithEmptyValue() {
         assertContains(problems.messages.get(0), 
"'servers.server[0].aliases[0]' for server-1 is missing");
     }
 
+    @Test
+    void testValidateServerRepositoryOrigins() {
+        SimpleProblemCollector problems = validateRepositoryOrigins(
+                "https://repo.example.org";, "https://mirror.example.org:8443";, 
"HTTP://Repo.Example.Org:80");
+        assertEquals(0, problems.messages.size());
+    }
+
+    @Test
+    void testValidateServerRepositoryOriginWithoutScheme() {
+        SimpleProblemCollector problems = 
validateRepositoryOrigins("repo.example.org");
+        assertEquals(1, problems.messages.size());
+        assertContains(
+                problems.messages.get(0),
+                "'servers.server[0].repositoryOrigins[0]' for server-1 must 
start with a scheme,"
+                        + " for example https://repo.example.org, but found 
'repo.example.org'");
+    }
+
+    @Test
+    void testValidateServerRepositoryOriginWithoutHost() {
+        SimpleProblemCollector problems = 
validateRepositoryOrigins("file:/tmp/repo");
+        assertEquals(1, problems.messages.size());
+        assertContains(problems.messages.get(0), "must name a host");
+    }
+
+    @Test
+    void testValidateServerRepositoryOriginWithUserInfo() {
+        SimpleProblemCollector problems = 
validateRepositoryOrigins("https://user:[email protected]";);
+        assertEquals(1, problems.messages.size());
+        assertContains(problems.messages.get(0), "must not carry user 
information");
+    }
+
+    @Test
+    void testValidateServerRepositoryOriginEmpty() {
+        SimpleProblemCollector problems = validateRepositoryOrigins("");
+        assertEquals(1, problems.messages.size());
+        assertContains(problems.messages.get(0), 
"'servers.server[0].repositoryOrigins[0]' for server-1 is missing");
+    }
+
+    @Test
+    void testValidateServerRepositoryOriginWithPath() {
+        SimpleProblemCollector problems = 
validateRepositoryOrigins("https://repo.example.org/releases/";);
+        assertEquals(1, problems.messages.size());
+        assertContains(
+                problems.messages.get(0),
+                "is a repository origin, not a repository URL; only 
'https://repo.example.org'"
+                        + " of 'https://repo.example.org/releases/' is used");
+    }
+
+    @Test
+    void testValidateServerRepositoryOriginWithPlaceholderIsNotValidated() {
+        // this validator runs before the settings are interpolated
+        SimpleProblemCollector problems = 
validateRepositoryOrigins("${env.REPO_URL}");
+        assertEquals(0, problems.messages.size());
+    }
+
+    private SimpleProblemCollector validateRepositoryOrigins(String... 
repositoryOrigins) {
+        Settings settings = new Settings();
+        Server server = new Server();
+        server.setId("server-1");
+        for (String repositoryOrigin : repositoryOrigins) {
+            server.addRepositoryOrigin(repositoryOrigin);
+        }
+        settings.addServer(server);
+
+        SimpleProblemCollector problems = new SimpleProblemCollector();
+        validator.validate(settings, problems);
+        return problems;
+    }
+
     private static class SimpleProblemCollector implements 
SettingsProblemCollector {
 
         private final List<String> messages = new ArrayList<>();
diff --git 
a/maven-settings-builder/src/test/resources/settings/factory/settings-servers-4.xml
 
b/maven-settings-builder/src/test/resources/settings/factory/settings-servers-4.xml
new file mode 100644
index 0000000000..4c3923b1ef
--- /dev/null
+++ 
b/maven-settings-builder/src/test/resources/settings/factory/settings-servers-4.xml
@@ -0,0 +1,38 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+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.
+-->
+
+<settings>
+  <servers>
+    <server>
+      <id>server-1</id>
+      <username>username1</username>
+      <password>password1</password>
+      <aliases>
+        <alias>server-11</alias>
+      </aliases>
+      <repositoryOrigins>
+        <repositoryOrigin>https://repo.example.org</repositoryOrigin>
+        <repositoryOrigin>https://mirror.example.org:8443</repositoryOrigin>
+      </repositoryOrigins>
+    </server>
+  </servers>
+
+</settings>
diff --git a/maven-settings/src/main/mdo/settings.mdo 
b/maven-settings/src/main/mdo/settings.mdo
index 575a42c87f..89cc72a0c5 100644
--- a/maven-settings/src/main/mdo/settings.mdo
+++ b/maven-settings/src/main/mdo/settings.mdo
@@ -593,6 +593,25 @@
             <multiplicity>*</multiplicity>
           </association>
         </field>
+        <field>
+          <name>repositoryOrigins</name>
+          <version>1.3.0+</version>
+          <description>List of repository origins these credentials may be 
used with, each written as
+            {@code scheme://host} or {@code scheme://host:port}, for example 
{@code https://repo.example.org}
+            or {@code https://mirror.example.org:8443} - an origin, not a full 
repository URL.
+            Maven binds the credentials of a server to the origins of the 
repositories and mirrors declared
+            with the same id, see the {@code maven.repository.credentialScope} 
user property. The origins
+            listed here are added to those; nothing is replaced. Declaring 
them is needed when the repository
+            using these credentials is not known when the session is created, 
for example a repository
+            declared in a {@code settings.xml} profile that is not listed in 
{@code &lt;activeProfiles&gt;}
+            but activated through {@code &lt;activation&gt;} or {@code -P}.
+            Comparison is case insensitive and the default ports 80 (http) and 
443 (https) are equivalent to
+            no port.</description>
+          <association>
+            <type>String</type>
+            <multiplicity>*</multiplicity>
+          </association>
+        </field>
       </fields>
     </class>
     <class>

Reply via email to