diff --git a/CHANGELOG.md b/CHANGELOG.md index a1b7599d741..c8b8fa1aaaf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -86,6 +86,7 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv - We changed to syntax for the websearch to the one of the main search bar. [#13607](https://github.com/JabRef/jabref/issues/13607) - We improved the for the web search tab in the preferences dialog [#13791](https://github.com/JabRef/jabref/pull/13791) - We improved the event viewer for debugging [#13783](https://github.com/JabRef/jabref/pull/13783). +- We improved "REDACTED" replacement of API key value in web fetcher search URL [#13796](https://github.com/JabRef/jabref/issues/13796) ### Fixed diff --git a/jablib/src/main/java/org/jabref/logic/importer/FetcherException.java b/jablib/src/main/java/org/jabref/logic/importer/FetcherException.java index 0bb693449fd..f5c77333fb4 100644 --- a/jablib/src/main/java/org/jabref/logic/importer/FetcherException.java +++ b/jablib/src/main/java/org/jabref/logic/importer/FetcherException.java @@ -14,8 +14,9 @@ public class FetcherException extends JabRefException { private static final Logger LOGGER = LoggerFactory.getLogger(FetcherException.class); - private static final Pattern API_KEY_PATTERN = Pattern.compile("(?i)(api|key|api[-_]?key)=[^&]*"); - private static String REDACTED_STRING = "[REDACTED]"; + private static final String API_KEY_PARAM_NAME = "apiKeyParamName"; + private static final Pattern API_KEY_PATTERN = Pattern.compile("(?i)(?<" + API_KEY_PARAM_NAME + ">api|key|api[-_]?key)=[^&]*"); + private static final String REDACTED_STRING = "[REDACTED]"; private final String url; private final SimpleHttpResponse httpResponse; @@ -85,12 +86,12 @@ public String getLocalizedMessage() { } } - private String getRedactedUrl() { - return API_KEY_PATTERN.matcher(url).replaceAll(REDACTED_STRING); + String getRedactedUrl() { + return getRedactedUrl(url); } - public static Object getRedactedUrl(URL source) { - return API_KEY_PATTERN.matcher(source.toString()).replaceAll(REDACTED_STRING); + public static String getRedactedUrl(String source) { + return API_KEY_PATTERN.matcher(source).replaceAll("${" + API_KEY_PARAM_NAME + "}=" + REDACTED_STRING); } private String getPrefix() { diff --git a/jablib/src/main/java/org/jabref/logic/net/URLDownload.java b/jablib/src/main/java/org/jabref/logic/net/URLDownload.java index c7842d293ef..7eff72b29ef 100644 --- a/jablib/src/main/java/org/jabref/logic/net/URLDownload.java +++ b/jablib/src/main/java/org/jabref/logic/net/URLDownload.java @@ -382,7 +382,7 @@ public URLConnection openConnection() throws FetcherException { } else if (status >= 400) { // in case of an error, propagate the error message SimpleHttpResponse httpResponse = new SimpleHttpResponse(httpURLConnection); - LOGGER.info("{}: {}", FetcherException.getRedactedUrl(this.source), httpResponse); + LOGGER.info("{}: {}", FetcherException.getRedactedUrl(this.source.toString()), httpResponse); if (status < 500) { throw new FetcherClientException(this.source, httpResponse); } else { diff --git a/jablib/src/test/java/org/jabref/logic/importer/FetcherExceptionTest.java b/jablib/src/test/java/org/jabref/logic/importer/FetcherExceptionTest.java new file mode 100644 index 00000000000..b35cfa3502a --- /dev/null +++ b/jablib/src/test/java/org/jabref/logic/importer/FetcherExceptionTest.java @@ -0,0 +1,21 @@ +package org.jabref.logic.importer; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class FetcherExceptionTest { + + @ParameterizedTest + @CsvSource({ + "https://api.springernature.com/meta/v1/json?q=anything&api_key=abc&s=1&p=20, https://api.springernature.com/meta/v1/json?q=anything&api_key=[REDACTED]&s=1&p=20", + "https://api.springernature.com/meta/v1/json?q=anything&API_KEY=abc, https://api.springernature.com/meta/v1/json?q=anything&API_KEY=[REDACTED]", + "https://api.springernature.com/meta/v1/json?q=anything&apikey=abc123ABC, https://api.springernature.com/meta/v1/json?q=anything&apikey=[REDACTED]", + "https://api.springernature.com/meta/v1/json?q=anything, https://api.springernature.com/meta/v1/json?q=anything", + "https://api.springernature.com/meta/v1/json, https://api.springernature.com/meta/v1/json" + }) + void getRedactedUrl(String url, String redactedUrl) { + assertEquals(redactedUrl, FetcherException.getRedactedUrl(url)); + } +}