Skip to content

[Junie]: fix: resolve IllegalArgumentException for non-absolute URIs #13669

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv

- We fixed an issue where the Pagetotal column was sorting the values alphabetically instead of numerically. [#12533](https://github.com/JabRef/jabref/issues/12533)
- We fixed dark mode of BibTeX Source dialog in Citation Relations tab. [#13599](https://github.com/JabRef/jabref/issues/13599)
- We fixed an issue where URLs starting with "www." (without a protocol) in file fields caused an `IllegalArgumentException: URI is not absolute` error. [#12186](https://github.com/JabRef/jabref/issues/12186)
- We fixed an issue where the LibreOffice integration did not support citation keys containing Unicode characters. [#13301](https://github.com/JabRef/jabref/issues/13301)
- We fixed an issue where the "Search ShortScience" action did not convert LaTeX-formatted titles to Unicode. [#13418](https://github.com/JabRef/jabref/issues/13418)
- We fixed an issue where LaTeX file directories were not properly shared between different users on the same host. [#9990](https://github.com/JabRef/jabref/issues/9990)
Expand Down
19 changes: 18 additions & 1 deletion jablib/src/main/java/org/jabref/logic/util/URLUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ public static String cleanGoogleSearchURL(String url) {
/// @param url the String to check for a URL
/// @return true if `url` contains a valid URL
public static boolean isURL(String url) {
if (url == null || url.trim().isEmpty()) {
return false;
}

// Check if the URL has a protocol (http://, https://, ftp://)
Copy link

Choose a reason for hiding this comment

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

Comment is redundant as it merely restates what is obvious from the code. The URL_PATTERN.matcher() check clearly indicates protocol verification.

if (!URL_PATTERN.matcher(url).matches()) {
return false;
}

try {
create(url);
return true;
Expand All @@ -100,8 +109,16 @@ public static URL create(String url) throws MalformedURLException {
if (url == null || url.trim().isEmpty()) {
throw new IllegalArgumentException("URL must not be null or empty.");
}

String trimmedUrl = url.trim();

// Add http:// prefix to URLs starting with www. to make them absolute
if (trimmedUrl.startsWith("www.")) {
trimmedUrl = "http://" + trimmedUrl;
}

try {
URI parsedUri = new URI(url.trim());
URI parsedUri = new URI(trimmedUrl);
if (!parsedUri.isAbsolute()) {
throw new MalformedURLException("URI is not absolute: " + url);
}
Expand Down
13 changes: 11 additions & 2 deletions jablib/src/test/java/org/jabref/logic/net/URLUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,18 @@ void emptyUrl() {
}

@Test
void uriMissingScheme() {
void urlStartingWithWww() throws MalformedURLException {
// URLs starting with www. should be prefixed with http://
URL result = URLUtil.create("www.example.com");
assertNotNull(result);
assertEquals("http://www.example.com", result.toString());
}

@Test
void uriMissingSchemeAndNotStartingWithWww() {
// URLs not starting with www. and without a scheme should still throw an exception
MalformedURLException exception = assertThrows(MalformedURLException.class, () ->
URLUtil.create("www.example.com"));
URLUtil.create("example.com"));
assertTrue(exception.getMessage().contains("not absolute"));
}

Expand Down
Loading