From 2b37ec73f876b964d69e585abe2b22d18a8bfb4e Mon Sep 17 00:00:00 2001 From: Ryyyc <1429846365@qq.com> Date: Wed, 21 Apr 2021 14:58:38 +0800 Subject: [PATCH 1/6] One solution for issue7616 --- .../gui/fieldeditors/LinkedFileViewModel.java | 30 +++++++++++++++++++ .../org/jabref/logic/net/URLDownload.java | 15 ++++++++++ src/main/resources/l10n/JabRef_en.properties | 2 ++ 3 files changed, 47 insertions(+) diff --git a/src/main/java/org/jabref/gui/fieldeditors/LinkedFileViewModel.java b/src/main/java/org/jabref/gui/fieldeditors/LinkedFileViewModel.java index cdc9562e978..44a3c151d64 100644 --- a/src/main/java/org/jabref/gui/fieldeditors/LinkedFileViewModel.java +++ b/src/main/java/org/jabref/gui/fieldeditors/LinkedFileViewModel.java @@ -10,6 +10,9 @@ import java.util.Optional; import java.util.function.BiPredicate; +import javax.net.ssl.HostnameVerifier; +import javax.net.ssl.HttpsURLConnection; +import javax.net.ssl.SSLSocketFactory; import javax.xml.transform.TransformerException; import javafx.beans.Observable; @@ -425,6 +428,10 @@ public void download() { } URLDownload urlDownload = new URLDownload(linkedFile.getLink()); + if (!checkSSLHandshake(urlDownload)) { + return; + } + BackgroundTask downloadTask = prepareDownloadTask(targetDirectory.get(), urlDownload); downloadTask.onSuccess(destination -> { LinkedFile newLinkedFile = LinkedFilesEditorViewModel.fromFile(destination, databaseContext.getFileDirectories(filePreferences), externalFileTypes); @@ -463,7 +470,29 @@ public void download() { } } + public boolean checkSSLHandshake(URLDownload urlDownload) { + try { + urlDownload.canBeReached(); + } catch (kong.unirest.UnirestException ex) { + if (ex.getCause() instanceof javax.net.ssl.SSLHandshakeException) { + if (dialogService.showConfirmationDialogAndWait(Localization.lang("Download file"), + Localization.lang("Unable to find valid certification path to requested target(%0), download anyway?", + urlDownload.getSource().toString()))) { + URLDownload.bypassSSLVerification(); + return true; + } else { + dialogService.showInformationDialogAndWait(Localization.lang("Download file"), + Localization.lang("Operation canceled.")); + } + } + return false; + } + return true; + } + public BackgroundTask prepareDownloadTask(Path targetDirectory, URLDownload urlDownload) { + SSLSocketFactory defaultSSLSocketFactory = HttpsURLConnection.getDefaultSSLSocketFactory(); + HostnameVerifier defaultHostnameVerifier = HttpsURLConnection.getDefaultHostnameVerifier(); BackgroundTask downloadTask = BackgroundTask .wrap(() -> { Optional suggestedType = inferFileType(urlDownload); @@ -476,6 +505,7 @@ public BackgroundTask prepareDownloadTask(Path targetDirectory, URLDownloa return targetDirectory.resolve(fulltextDir).resolve(suggestedName); }) .then(destination -> new FileDownloadTask(urlDownload.getSource(), destination)) + .onFinished(() -> URLDownload.setSSLVerification(defaultSSLSocketFactory, defaultHostnameVerifier)) .onFailure(exception -> dialogService.showErrorDialogAndWait("Download failed", exception)); return downloadTask; } diff --git a/src/main/java/org/jabref/logic/net/URLDownload.java b/src/main/java/org/jabref/logic/net/URLDownload.java index 99887c7ac93..8ba913b168a 100644 --- a/src/main/java/org/jabref/logic/net/URLDownload.java +++ b/src/main/java/org/jabref/logic/net/URLDownload.java @@ -38,6 +38,7 @@ import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; +import javax.net.ssl.SSLSocketFactory; import org.jabref.logic.util.io.FileUtil; import org.jabref.model.util.FileHelper; @@ -132,6 +133,20 @@ public X509Certificate[] getAcceptedIssuers() { } } + /** + * + * @param sf trust manager + * @param v host verifier + */ + public static void setSSLVerification(SSLSocketFactory sf, HostnameVerifier v){ + try { + HttpsURLConnection.setDefaultSSLSocketFactory(sf); + HttpsURLConnection.setDefaultHostnameVerifier(v); + } catch (Exception e) { + LOGGER.error("A problem occurred when reset SSL verification", e); + } + } + public URL getSource() { return source; } diff --git a/src/main/resources/l10n/JabRef_en.properties b/src/main/resources/l10n/JabRef_en.properties index c75a24158dd..8d72c51f88d 100644 --- a/src/main/resources/l10n/JabRef_en.properties +++ b/src/main/resources/l10n/JabRef_en.properties @@ -2292,3 +2292,5 @@ Import\ settings=Import settings Custom\ DOI\ URI=Custom DOI URI Customization=Customization Use\ custom\ DOI\ base\ URI\ for\ article\ access=Use custom DOI base URI for article access + +Unable\ to\ find\ valid\ certification\ path\ to\ requested\ target(%0),\ download\ anyway?=Unable to find valid certification path to requested target(%0), download anyway? From df384cc164ce4217c5194d2fcc2f34a71987f3f3 Mon Sep 17 00:00:00 2001 From: Ryyyc <1429846365@qq.com> Date: Wed, 21 Apr 2021 16:07:56 +0800 Subject: [PATCH 2/6] Add log to CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 97603ecfcc6..5ebfe5f62a1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve - We added two new fields to track the creation and most recent modification date and time for each entry. [koppor#130](https://github.com/koppor/jabref/issues/130) - We added a feature that allows the user to copy highlighted text in the preview window. [#6962](https://github.com/JabRef/jabref/issues/6962) - We added a feature that allows you to create new BibEntry via paste arxivId [#2292](https://github.com/JabRef/jabref/issues/2292) +- We added a feature that allows the user to choose whether to trust the target site when unable to find a valid certification path from the file download site. [#7616](https://github.com/JabRef/jabref/issues/7616) ### Changed From 5c19e5c3dc497841f390bcf22087e3738004894a Mon Sep 17 00:00:00 2001 From: Ryyyc <1429846365@qq.com> Date: Wed, 21 Apr 2021 19:22:11 +0800 Subject: [PATCH 3/6] Update code style. --- src/main/java/org/jabref/logic/net/URLDownload.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/jabref/logic/net/URLDownload.java b/src/main/java/org/jabref/logic/net/URLDownload.java index 8ba913b168a..c7285868b1a 100644 --- a/src/main/java/org/jabref/logic/net/URLDownload.java +++ b/src/main/java/org/jabref/logic/net/URLDownload.java @@ -36,9 +36,9 @@ import javax.net.ssl.HostnameVerifier; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLSocketFactory; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; -import javax.net.ssl.SSLSocketFactory; import org.jabref.logic.util.io.FileUtil; import org.jabref.model.util.FileHelper; @@ -138,7 +138,7 @@ public X509Certificate[] getAcceptedIssuers() { * @param sf trust manager * @param v host verifier */ - public static void setSSLVerification(SSLSocketFactory sf, HostnameVerifier v){ + public static void setSSLVerification(SSLSocketFactory sf, HostnameVerifier v) { try { HttpsURLConnection.setDefaultSSLSocketFactory(sf); HttpsURLConnection.setDefaultHostnameVerifier(v); From 4416c203c17929009cfd66f5715a92a88537500e Mon Sep 17 00:00:00 2001 From: Ryyyc <1429846365@qq.com> Date: Wed, 21 Apr 2021 19:57:53 +0800 Subject: [PATCH 4/6] The user chooses to cancel the download, use Notify instead of InformationDialog to prompt the user. --- .../java/org/jabref/gui/fieldeditors/LinkedFileViewModel.java | 3 +-- src/main/resources/l10n/JabRef_en.properties | 1 + 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/jabref/gui/fieldeditors/LinkedFileViewModel.java b/src/main/java/org/jabref/gui/fieldeditors/LinkedFileViewModel.java index 44a3c151d64..87e71366a6d 100644 --- a/src/main/java/org/jabref/gui/fieldeditors/LinkedFileViewModel.java +++ b/src/main/java/org/jabref/gui/fieldeditors/LinkedFileViewModel.java @@ -481,8 +481,7 @@ public boolean checkSSLHandshake(URLDownload urlDownload) { URLDownload.bypassSSLVerification(); return true; } else { - dialogService.showInformationDialogAndWait(Localization.lang("Download file"), - Localization.lang("Operation canceled.")); + dialogService.notify(Localization.lang("Download operation canceled.")); } } return false; diff --git a/src/main/resources/l10n/JabRef_en.properties b/src/main/resources/l10n/JabRef_en.properties index 8d72c51f88d..56f6d51106f 100644 --- a/src/main/resources/l10n/JabRef_en.properties +++ b/src/main/resources/l10n/JabRef_en.properties @@ -2294,3 +2294,4 @@ Customization=Customization Use\ custom\ DOI\ base\ URI\ for\ article\ access=Use custom DOI base URI for article access Unable\ to\ find\ valid\ certification\ path\ to\ requested\ target(%0),\ download\ anyway?=Unable to find valid certification path to requested target(%0), download anyway? +Download\ operation\ canceled.=Download operation canceled. From aac49cc3b079f05d37e4ea54f69b73c08f729a85 Mon Sep 17 00:00:00 2001 From: Ruan <47767371+Ryyyc@users.noreply.github.com> Date: Wed, 21 Apr 2021 17:22:42 -0700 Subject: [PATCH 5/6] Update the variable naming in src/main/java/org/jabref/logic/net/URLDownload.java Update the variable naming in src/main/java/org/jabref/logic/net/URLDownload.java, function setSSLVerification(). Co-authored-by: Christoph --- src/main/java/org/jabref/logic/net/URLDownload.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/jabref/logic/net/URLDownload.java b/src/main/java/org/jabref/logic/net/URLDownload.java index c7285868b1a..cfeb63cd8c1 100644 --- a/src/main/java/org/jabref/logic/net/URLDownload.java +++ b/src/main/java/org/jabref/logic/net/URLDownload.java @@ -138,7 +138,7 @@ public X509Certificate[] getAcceptedIssuers() { * @param sf trust manager * @param v host verifier */ - public static void setSSLVerification(SSLSocketFactory sf, HostnameVerifier v) { + public static void setSSLVerification(SSLSocketFactory socketFactory, HostnameVerifier verifier) { try { HttpsURLConnection.setDefaultSSLSocketFactory(sf); HttpsURLConnection.setDefaultHostnameVerifier(v); From 21f69ff6417a6ec92cc77bc9eb1932f39bf48943 Mon Sep 17 00:00:00 2001 From: Ruan <47767371+Ryyyc@users.noreply.github.com> Date: Thu, 22 Apr 2021 08:30:49 +0800 Subject: [PATCH 6/6] Fix the bug of the variable name change Fix the bug of the variable name change. --- src/main/java/org/jabref/logic/net/URLDownload.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/jabref/logic/net/URLDownload.java b/src/main/java/org/jabref/logic/net/URLDownload.java index cfeb63cd8c1..de94af68dc5 100644 --- a/src/main/java/org/jabref/logic/net/URLDownload.java +++ b/src/main/java/org/jabref/logic/net/URLDownload.java @@ -135,13 +135,13 @@ public X509Certificate[] getAcceptedIssuers() { /** * - * @param sf trust manager - * @param v host verifier + * @param socketFactory trust manager + * @param verifier host verifier */ public static void setSSLVerification(SSLSocketFactory socketFactory, HostnameVerifier verifier) { try { - HttpsURLConnection.setDefaultSSLSocketFactory(sf); - HttpsURLConnection.setDefaultHostnameVerifier(v); + HttpsURLConnection.setDefaultSSLSocketFactory(socketFactory); + HttpsURLConnection.setDefaultHostnameVerifier(verifier); } catch (Exception e) { LOGGER.error("A problem occurred when reset SSL verification", e); }