Skip to content

Commit 0cee243

Browse files
timtebeekTim te Beekclaude
authored
Fix MavenPomDownloader.htmlIndexToVersioning() to handle HTML title attributes (#6745)
When parsing HTML directory listings, the method searched for "\">" to delimit the href attribute value. This overshoots when anchor tags include additional attributes like title="...", causing corrupted version strings such as "1.0.0/\" title=\"1.0.0" instead of "1.0.0/". The fix changes the search to find the closing quote directly, which correctly terminates at the href attribute boundary regardless of subsequent attributes. Includes a test case covering this scenario. Co-authored-by: Tim te Beek <tim@mac.home> Co-authored-by: Claude Haiku 4.5 <noreply@anthropic.com>
1 parent 4d3b85b commit 0cee243

2 files changed

Lines changed: 37 additions & 1 deletion

File tree

rewrite-maven/src/main/java/org/openrewrite/maven/internal/MavenPomDownloader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ public MavenMetadata downloadMetadata(GroupArtifactVersion gav, @Nullable Resolv
411411
int start = responseBody.indexOf("<a href=\"");
412412
while (start > 0) {
413413
start += 9;
414-
int end = responseBody.indexOf("\">", start);
414+
int end = responseBody.indexOf("\"", start);
415415
if (end < 0) {
416416
break;
417417
}

rewrite-maven/src/test/java/org/openrewrite/maven/internal/MavenPomDownloaderTest.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,42 @@ void deriveMetaDataFromHtmlBasedRepository() {
626626
new MavenPomDownloader(emptyMap(), ctx).downloadMetadata(new GroupArtifact("does.definitely.not", "exist"), null, List.of(repository)));
627627
}
628628

629+
@Issue("https://github.com/openrewrite/rewrite/issues/6739")
630+
@Test
631+
void deriveMetaDataFromHtmlWithTitleAttributes() throws Exception {
632+
try (MockWebServer server = new MockWebServer()) {
633+
server.setDispatcher(new Dispatcher() {
634+
@Override
635+
public MockResponse dispatch(RecordedRequest request) {
636+
if (request.getPath() != null && request.getPath().endsWith("maven-metadata.xml")) {
637+
return new MockResponse().setResponseCode(404);
638+
}
639+
return new MockResponse().setResponseCode(200).setBody(
640+
"""
641+
<html><body>
642+
<a href="../" title="../">../</a>
643+
<a href="1.0.0/" title="1.0.0/">1.0.0/</a>
644+
<a href="1.1.0/" title="1.1.0/">1.1.0/</a>
645+
<a href="2.0.0/" title="2.0.0/">2.0.0/</a>
646+
</body></html>
647+
"""
648+
);
649+
}
650+
});
651+
server.start();
652+
653+
MavenRepository repository = MavenRepository.builder()
654+
.id("html-with-title")
655+
.uri("http://%s:%d/".formatted(server.getHostName(), server.getPort()))
656+
.knownToExist(true)
657+
.deriveMetadataIfMissing(true)
658+
.build();
659+
MavenMetadata metaData = new MavenPomDownloader(emptyMap(), ctx)
660+
.downloadMetadata(new GroupArtifact("fred", "fred"), null, List.of(repository));
661+
assertThat(metaData.getVersioning().getVersions()).containsExactlyInAnyOrder("1.0.0", "1.1.0", "2.0.0");
662+
}
663+
}
664+
629665
@SuppressWarnings("ConstantConditions")
630666
@Test
631667
void mergeMetadata() throws Exception {

0 commit comments

Comments
 (0)