You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by el...@apache.org on 2026/08/04 11:31:18 UTC
(maven-shared-io) branch master updated: fix #89: reconstruct baseUrl from URL components to avoid query/fragment corruption (#109)
This is an automated email from the ASF dual-hosted git repository.
elharo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-shared-io.git
The following commit(s) were added to refs/heads/master by this push:
new 3a878c0 fix #89: reconstruct baseUrl from URL components to avoid query/fragment corruption (#109)
3a878c0 is described below
commit 3a878c0058ac38f1ef3bc9321a891ce456c812d1
Author: Phaneendra Injarapu <in...@infosys.com>
AuthorDate: Tue Aug 4 17:01:13 2026 +0530
fix #89: reconstruct baseUrl from URL components to avoid query/fragment corruption (#109)
* fix #89: reconstruct baseUrl from URL components to avoid query/fragment corruption
When DefaultDownloadManager split a download URL into baseUrl and
remotePath, it used string arithmetic:
String remotePath = sourceUrl.getPath();
String baseUrl = url.substring(0, url.length() - remotePath.length());
sourceUrl.getPath() returns only the path portion (e.g. /path/file.jar),
stripping any query string or fragment. The full 'url' string still
carries those suffixes, so the substring cut lands in the middle of the
path, producing a completely wrong baseUrl and breaking Wagon
connectivity (e.g. http://host/path/file.j instead of http://host).
Fix: reconstruct baseUrl explicitly from the URL object's own
protocol/userInfo/host/port components, which are never contaminated
by query strings or fragments.
Adds two regression tests (query string and fragment) that capture
the Repository argument passed to wagon.connect() and assert the URL
equals exactly "http://example.com" with no trailing path or query.
* Potential fix for pull request finding
Co-authored-by: Copilot Autofix powered by AI <17...@users.noreply.github.com>
* fix #89: guard authority-less URLs and cover baseUrl reconstruction with tests
---------
Co-authored-by: pinjarapu <ph...@apple.com>
Co-authored-by: Elliotte Rusty Harold <el...@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <17...@users.noreply.github.com>
---
.../shared/io/download/DefaultDownloadManager.java | 13 +-
.../io/download/DefaultDownloadManagerTest.java | 154 +++++++++++++++++++++
2 files changed, 166 insertions(+), 1 deletion(-)
diff --git a/src/main/java/org/apache/maven/shared/io/download/DefaultDownloadManager.java b/src/main/java/org/apache/maven/shared/io/download/DefaultDownloadManager.java
index e50ec49..b9efbc1 100644
--- a/src/main/java/org/apache/maven/shared/io/download/DefaultDownloadManager.java
+++ b/src/main/java/org/apache/maven/shared/io/download/DefaultDownloadManager.java
@@ -116,7 +116,18 @@ public class DefaultDownloadManager implements DownloadManager {
// split the download URL into base URL and remote path for connecting, then retrieving.
String remotePath = sourceUrl.getPath();
- String baseUrl = url.substring(0, url.length() - remotePath.length());
+ String authority = sourceUrl.getAuthority();
+
+ // Repository derives host and port by re-parsing this URL, and tolerates a missing
+ // authority only for file: URLs. Fail fast instead of letting its parser guess.
+ if ((authority == null || authority.isEmpty()) && !"file".equalsIgnoreCase(sourceUrl.getProtocol())) {
+ throw new DownloadFailedException(
+ url, "Download failed due to URL without an authority component (expected protocol://host/path).");
+ }
+
+ // Assemble from components instead of substring(url), which lets a query string or fragment leak in.
+ // Authority is copied verbatim so port and bracketed IPv6 host survive.
+ String baseUrl = sourceUrl.getProtocol() + ":" + (authority != null ? "//" + authority : "");
for (Iterator<TransferListener> it = transferListeners.iterator(); it.hasNext(); ) {
wagon.addTransferListener(it.next());
diff --git a/src/test/java/org/apache/maven/shared/io/download/DefaultDownloadManagerTest.java b/src/test/java/org/apache/maven/shared/io/download/DefaultDownloadManagerTest.java
index cd6f964..b0fb7af 100644
--- a/src/test/java/org/apache/maven/shared/io/download/DefaultDownloadManagerTest.java
+++ b/src/test/java/org/apache/maven/shared/io/download/DefaultDownloadManagerTest.java
@@ -42,14 +42,17 @@ import org.apache.maven.wagon.authorization.AuthorizationException;
import org.apache.maven.wagon.events.TransferListener;
import org.apache.maven.wagon.proxy.ProxyInfo;
import org.apache.maven.wagon.repository.Repository;
+import org.easymock.Capture;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.easymock.EasyMock.anyObject;
import static org.easymock.EasyMock.anyString;
+import static org.easymock.EasyMock.capture;
import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.expectLastCall;
+import static org.easymock.EasyMock.newCapture;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
@@ -318,6 +321,157 @@ class DefaultDownloadManagerTest {
verify(wagon, wagonManager);
}
+ @Test
+ void shouldUseCorrectBaseUrlWhenUrlHasQueryString() throws Exception {
+ String urlWithQuery = "http://example.com/path/file.jar?token=abc";
+
+ Capture<Repository> repoCapture = newCapture();
+
+ expect(wagonManager.getWagon("http")).andReturn(wagon);
+ expect(wagonManager.getAuthenticationInfo(anyString())).andReturn(null);
+ expect(wagonManager.getProxy(anyString())).andReturn(null);
+ wagon.connect(capture(repoCapture), anyObject(AuthenticationInfo.class), anyObject(ProxyInfo.class));
+ wagon.get(anyString(), anyObject(File.class));
+ wagon.disconnect();
+
+ replay(wagon, wagonManager);
+
+ DownloadManager downloadManager = new DefaultDownloadManager(wagonManager);
+ downloadManager.download(urlWithQuery, new DefaultMessageHolder());
+
+ verify(wagon, wagonManager);
+
+ assertEquals("http://example.com", repoCapture.getValue().getUrl());
+ }
+
+ @Test
+ void shouldUseCorrectBaseUrlWhenUrlHasFragment() throws Exception {
+ String urlWithFragment = "http://example.com/path/file.jar#section";
+
+ Capture<Repository> repoCapture = newCapture();
+
+ expect(wagonManager.getWagon("http")).andReturn(wagon);
+ expect(wagonManager.getAuthenticationInfo(anyString())).andReturn(null);
+ expect(wagonManager.getProxy(anyString())).andReturn(null);
+ wagon.connect(capture(repoCapture), anyObject(AuthenticationInfo.class), anyObject(ProxyInfo.class));
+ wagon.get(anyString(), anyObject(File.class));
+ wagon.disconnect();
+
+ replay(wagon, wagonManager);
+
+ DownloadManager downloadManager = new DefaultDownloadManager(wagonManager);
+ downloadManager.download(urlWithFragment, new DefaultMessageHolder());
+
+ verify(wagon, wagonManager);
+
+ assertEquals("http://example.com", repoCapture.getValue().getUrl());
+ }
+
+ @Test
+ void shouldPreserveUserInfoAndPortInBaseUrl() throws Exception {
+ String urlWithCredentials = "http://user:secret@example.com:8080/path/file.jar?token=abc";
+
+ Capture<Repository> repoCapture = newCapture();
+ Capture<String> pathCapture = newCapture();
+
+ expect(wagonManager.getWagon("http")).andReturn(wagon);
+ expect(wagonManager.getAuthenticationInfo(anyString())).andReturn(null);
+ expect(wagonManager.getProxy(anyString())).andReturn(null);
+ wagon.connect(capture(repoCapture), anyObject(AuthenticationInfo.class), anyObject(ProxyInfo.class));
+ wagon.get(capture(pathCapture), anyObject(File.class));
+ wagon.disconnect();
+
+ replay(wagon, wagonManager);
+
+ DownloadManager downloadManager = new DefaultDownloadManager(wagonManager);
+ downloadManager.download(urlWithCredentials, new DefaultMessageHolder());
+
+ verify(wagon, wagonManager);
+
+ Repository repo = repoCapture.getValue();
+ assertEquals("example.com", repo.getHost());
+ assertEquals(8080, repo.getPort());
+ assertEquals("user", repo.getUsername());
+ assertEquals("secret", repo.getPassword());
+ // Repository strips the credentials from the URL once it has parsed them out.
+ assertEquals("http://example.com:8080", repo.getUrl());
+ assertEquals("/path/file.jar", pathCapture.getValue());
+ }
+
+ @Test
+ void shouldPreserveBracketedIpv6HostAndPortInBaseUrl() throws Exception {
+ String ipv6Url = "http://[::1]:8081/path/file.jar#section";
+
+ Capture<Repository> repoCapture = newCapture();
+ Capture<String> pathCapture = newCapture();
+
+ expect(wagonManager.getWagon("http")).andReturn(wagon);
+ expect(wagonManager.getAuthenticationInfo(anyString())).andReturn(null);
+ expect(wagonManager.getProxy(anyString())).andReturn(null);
+ wagon.connect(capture(repoCapture), anyObject(AuthenticationInfo.class), anyObject(ProxyInfo.class));
+ wagon.get(capture(pathCapture), anyObject(File.class));
+ wagon.disconnect();
+
+ replay(wagon, wagonManager);
+
+ DownloadManager downloadManager = new DefaultDownloadManager(wagonManager);
+ downloadManager.download(ipv6Url, new DefaultMessageHolder());
+
+ verify(wagon, wagonManager);
+
+ Repository repo = repoCapture.getValue();
+ assertEquals("::1", repo.getHost());
+ assertEquals(8081, repo.getPort());
+ assertEquals("http://[::1]:8081", repo.getUrl());
+ assertEquals("/path/file.jar", pathCapture.getValue());
+ }
+
+ @Test
+ void shouldFailToDownloadNonFileUrlWithoutAuthority() throws Exception {
+ // The wagon is resolved before the base URL is built, so this gets past the protocol check.
+ expect(wagonManager.getWagon("http")).andReturn(wagon);
+
+ replay(wagon, wagonManager);
+
+ DownloadManager downloadManager = new DefaultDownloadManager(wagonManager);
+
+ try {
+ downloadManager.download("http:/path/file.jar", new DefaultMessageHolder());
+
+ fail("Should not download a URL without an authority component.");
+ } catch (DownloadFailedException e) {
+ assertTrue(e.getMessage().contains("without an authority component"));
+ }
+
+ verify(wagon, wagonManager);
+ }
+
+ @Test
+ void shouldDownloadFileUrlWithoutAuthority() throws Exception {
+ File tempFile = Files.createTempFile("download-source", "test").toFile();
+ tempFile.deleteOnExit();
+
+ Capture<Repository> repoCapture = newCapture();
+
+ expect(wagonManager.getWagon("file")).andReturn(wagon);
+ expect(wagonManager.getAuthenticationInfo(anyString())).andReturn(null);
+ expect(wagonManager.getProxy(anyString())).andReturn(null);
+ wagon.connect(capture(repoCapture), anyObject(AuthenticationInfo.class), anyObject(ProxyInfo.class));
+ wagon.get(anyString(), anyObject(File.class));
+ wagon.disconnect();
+
+ replay(wagon, wagonManager);
+
+ DownloadManager downloadManager = new DefaultDownloadManager(wagonManager);
+
+ // File URLs have no authority (file:/tmp/...); wagon resolves these against localhost.
+ downloadManager.download("file:" + tempFile.getAbsolutePath(), new DefaultMessageHolder());
+
+ verify(wagon, wagonManager);
+
+ assertEquals("file:", repoCapture.getValue().getUrl());
+ }
+
@Test
void shouldDownloadConcurrentlyAndCacheResults() throws Exception {
File tempFile = Files.createTempFile("download-source", "test").toFile();