-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Sanitize URLs in file fields to handle invalid pipe characters ('|') #12156
Open
hitalo-siriano
wants to merge
7
commits into
JabRef:main
Choose a base branch
from
hitalo-siriano:fix-for-issue-11876
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
63f9562
Sanitize URLs in file fields to handle invalid pipe characters ('|') …
hitalo-siriano 0f1ea59
Sanitize URLs in file fields to handle invalid pipe characters ('|') …
hitalo-siriano e12d3d6
Merge branch 'main' into fix-for-issue-11876
koppor 547300d
Discard changes to src/main/java/module-info.java
koppor f392c2b
Sanitize URLs in file fields to handle invalid pipe characters ('|') …
hitalo-siriano 3551dc2
Merge remote-tracking branch 'origin/fix-for-issue-11876' into fix-fo…
hitalo-siriano 2f390d4
Sanitize URLs in file fields to handle invalid pipe characters ('|') …
hitalo-siriano File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
import java.net.MalformedURLException; | ||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.net.URL; | ||
import java.net.URLDecoder; | ||
import java.nio.charset.StandardCharsets; | ||
|
@@ -22,10 +23,9 @@ private URLUtil() { | |
|
||
/** | ||
* Cleans URLs returned by Google search. | ||
* | ||
* <example> | ||
* If you copy links from search results from Google, all links will be enriched with search meta data, e.g. | ||
* https://www.google.de/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&&url=http%3A%2F%2Fwww.inrg.csie.ntu.edu.tw%2Falgorithm2014%2Fhomework%2FWagner-74.pdf&ei=DifeVYHkDYWqU5W0j6gD&usg=AFQjCNFl638rl5KVta1jIMWLyb4CPSZidg&sig2=0hSSMw9XZXL3HJWwEcJtOg | ||
* If you copy links from search results from Google, all links will be enriched with search meta data, e.g. | ||
* https://www.google.de/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&&url=http%3A%2F%2Fwww.inrg.csie.ntu.edu.tw%2Falgorithm2014%2Fhomework%2FWagner-74.pdf&ei=DifeVYHkDYWqU5W0j6gD&usg=AFQjCNFl638rl5KVta1jIMWLyb4CPSZidg&sig2=0hSSMw9XZXL3HJWwEcJtOg | ||
* </example> | ||
* | ||
* @param url the Google search URL string | ||
|
@@ -39,7 +39,7 @@ public static String cleanGoogleSearchURL(String url) { | |
} | ||
// Extract destination URL | ||
try { | ||
URL searchURL = URI.create(url).toURL(); | ||
URL searchURL = URLUtil.create(url); | ||
// URL parameters | ||
String query = searchURL.getQuery(); | ||
// no parameters | ||
|
@@ -77,7 +77,7 @@ public static String cleanGoogleSearchURL(String url) { | |
*/ | ||
public static boolean isURL(String url) { | ||
try { | ||
URI.create(url).toURL(); | ||
URLUtil.create(url); | ||
return true; | ||
} catch (MalformedURLException | IllegalArgumentException e) { | ||
return false; | ||
|
@@ -96,7 +96,7 @@ public static Optional<String> getSuffix(final String link, ExternalApplications | |
String strippedLink = link; | ||
try { | ||
// Try to strip the query string, if any, to get the correct suffix: | ||
URL url = URI.create(link).toURL(); | ||
URL url = URLUtil.create(link); | ||
if ((url.getQuery() != null) && (url.getQuery().length() < (link.length() - 1))) { | ||
strippedLink = link.substring(0, link.length() - url.getQuery().length() - 1); | ||
} | ||
|
@@ -138,4 +138,37 @@ public static Optional<String> getSuffix(final String link, ExternalApplications | |
return Optional.ofNullable(suffix); | ||
} | ||
} | ||
|
||
/** | ||
* Creates a {@link URL} object from the given string URL. | ||
* | ||
* @param url the URL string to be converted into a {@link URL}. | ||
* @return the {@link URL} object created from the string URL. | ||
* @throws MalformedURLException if the URL is malformed and cannot be converted to a {@link URL}. | ||
*/ | ||
public static URL create(String url) throws MalformedURLException { | ||
return URLUtil.createUri(url).toURL(); | ||
} | ||
|
||
/** | ||
* Creates a {@link URI} object from the given string URL. | ||
* This method attempts to convert the given URL string into a {@link URI} object. | ||
* The pipe character ('|') is replaced with its percent-encoded equivalent ("%7C") because the pipe character | ||
* is not a valid character in certain parts of a URI (specifically, in the path or query components). | ||
* According to the URI specification (RFC 3986), certain characters must be percent-encoded when used in specific contexts. | ||
* | ||
* @param url the URL string to be converted into a {@link URI}. | ||
* @return the {@link URI} object created from the string URL. | ||
* @throws IllegalArgumentException if the string URL is not a valid URI or if the URI format is incorrect. | ||
* @throws URISyntaxException if the string URL has an invalid syntax and cannot be converted into a {@link URI}. | ||
*/ | ||
public static URI createUri(String url) { | ||
try { | ||
// Replace '|' character with its percent-encoded representation '%7C'. | ||
String urlFormat = url.replace("|", "%7C"); | ||
return new URI(urlFormat); | ||
} catch (URISyntaxException e) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add |
||
throw new IllegalArgumentException(e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a comment explaining why we are doing this and the relevant references.