Skip to content

armgit / nutch

Public Imported from GitHub
0

Apache Nutch is an extensible and scalable web crawler

NUTCH-2648 Make configurable whether TLS/SSL certificates are checked by protocol plugins - enable/disable validation of certs by property `http.tls.certificates.check` (default: false/disabled)

Sebastian Nagel3f64083ed38c
diff --git a/conf/nutch-default.xml b/conf/nutch-default.xml
index 9f57af26e..065ed86fe 100644
--- a/conf/nutch-default.xml
+++ b/conf/nutch-default.xml
@@ -249,6 +249,18 @@
</description>
</property>
+<property>
+ <name>http.tls.certificates.check</name>
+ <value>false</value>
+ <description>
+ Whether to check the TLS/SSL server certificates for validity.
+ If true invalid (e.g., self-signed or expired) certificates are
+ rejected and the https connection is failed. If false insecure
+ TLS/SSL connections are allowed. Note that this property is
+ currently not supported by all http/https protocol plugins.
+ </description>
+</property>
+
<property>
<name>http.proxy.host</name>
<value></value>
diff --git a/src/plugin/lib-http/src/java/org/apache/nutch/protocol/http/api/HttpBase.java b/src/plugin/lib-http/src/java/org/apache/nutch/protocol/http/api/HttpBase.java
index 42f479312..a5c0a90f1 100644
--- a/src/plugin/lib-http/src/java/org/apache/nutch/protocol/http/api/HttpBase.java
+++ b/src/plugin/lib-http/src/java/org/apache/nutch/protocol/http/api/HttpBase.java
@@ -157,6 +157,9 @@ public abstract class HttpBase implements Protocol {
/** Skip page if Crawl-Delay longer than this value. */
protected long maxCrawlDelay = -1L;
+ /** Whether to check TLS/SSL certificates */
+ protected boolean tlsCheckCertificate = false;
+
/** Which TLS/SSL protocols to support */
protected Set<String> tlsPreferredProtocols;
@@ -206,6 +209,8 @@ public abstract class HttpBase implements Protocol {
// backward-compatible default setting
this.useHttp11 = conf.getBoolean("http.useHttp11", true);
this.useHttp2 = conf.getBoolean("http.useHttp2", false);
+ this.tlsCheckCertificate = conf.getBoolean("http.tls.certificates.check",
+ false);
this.responseTime = conf.getBoolean("http.store.responsetime", true);
this.storeIPAddress = conf.getBoolean("store.ip.address", false);
this.storeHttpRequest = conf.getBoolean("store.http.request", false);
@@ -496,6 +501,10 @@ public abstract class HttpBase implements Protocol {
return useHttp11;
}
+ public boolean isTlsCheckCertificates() {
+ return tlsCheckCertificate;
+ }
+
public Set<String> getTlsPreferredCipherSuites() {
return tlsPreferredCipherSuites;
}
diff --git a/src/plugin/protocol-http/src/java/org/apache/nutch/protocol/http/HttpResponse.java b/src/plugin/protocol-http/src/java/org/apache/nutch/protocol/http/HttpResponse.java
index 95ae35248..b4d3fbcb9 100644
--- a/src/plugin/protocol-http/src/java/org/apache/nutch/protocol/http/HttpResponse.java
+++ b/src/plugin/protocol-http/src/java/org/apache/nutch/protocol/http/HttpResponse.java
@@ -355,10 +355,17 @@ public class HttpResponse implements Response {
* -------------------------
*/
- private SSLSocket getSSLSocket(Socket socket, String sockHost, int sockPort) throws Exception {
- SSLContext sslContext = SSLContext.getInstance("TLS");
- sslContext.init(null, new TrustManager[]{new DummyX509TrustManager(null)}, null);
- SSLSocketFactory factory = sslContext.getSocketFactory();
+ private SSLSocket getSSLSocket(Socket socket, String sockHost, int sockPort)
+ throws Exception {
+ SSLSocketFactory factory;
+ if (http.isTlsCheckCertificates()) {
+ factory = (SSLSocketFactory) SSLSocketFactory.getDefault();
+ } else {
+ SSLContext sslContext = SSLContext.getInstance("TLS");
+ sslContext.init(null,
+ new TrustManager[] { new DummyX509TrustManager(null) }, null);
+ factory = sslContext.getSocketFactory();
+ }
SSLSocket sslsocket = (SSLSocket) factory
.createSocket(socket, sockHost, sockPort, true);
diff --git a/src/plugin/protocol-httpclient/src/java/org/apache/nutch/protocol/httpclient/Http.java b/src/plugin/protocol-httpclient/src/java/org/apache/nutch/protocol/httpclient/Http.java
index c185f9bdc..2cd29d3e9 100644
--- a/src/plugin/protocol-httpclient/src/java/org/apache/nutch/protocol/httpclient/Http.java
+++ b/src/plugin/protocol-httpclient/src/java/org/apache/nutch/protocol/httpclient/Http.java
@@ -47,7 +47,7 @@ import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
import org.apache.commons.httpclient.protocol.Protocol;
import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
-
+import org.apache.commons.httpclient.protocol.SSLProtocolSocketFactory;
import org.apache.commons.lang.StringUtils;
import org.apache.nutch.crawl.CrawlDatum;
import org.apache.nutch.net.protocols.Response;
@@ -184,8 +184,12 @@ public class Http extends HttpBase {
private void configureClient() {
// Set up an HTTPS socket factory that accepts self-signed certs.
- // ProtocolSocketFactory factory = new SSLProtocolSocketFactory();
- ProtocolSocketFactory factory = new DummySSLProtocolSocketFactory();
+ ProtocolSocketFactory factory;
+ if (tlsCheckCertificate) {
+ factory = new SSLProtocolSocketFactory();
+ } else {
+ factory = new DummySSLProtocolSocketFactory();
+ }
Protocol https = new Protocol("https", factory, 443);
Protocol.registerProtocol("https", https);