Skip to content

Commit c7b4f7f

Browse files
authored
Apply :insecure? ssl opt only to current request (#47)
Was formerly also being applied to all subsequent requests. Includes a small work-around for bb compatibility. Closes #45
1 parent 7c4f254 commit c7b4f7f

File tree

4 files changed

+39
-26
lines changed

4 files changed

+39
-26
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
- If specified, request's body encoding is now applied, else defaults to UTF-8 ([#18](https://github.com/clj-commons/clj-http-lite/issues/18)) ([@lread](https://github.com/lread))
44
- User info from request URL now applied to basic auth ([#34](https://github.com/clj-commons/clj-http-lite/issues/34)) ([@lread](https://github.com/lread))
55
- Nested query and form parameters are now automatically flattened ([#43](https://github.com/clj-commons/clj-http-lite/issues/43)) ([@lread](https://github.com/lread))
6+
- The `:insecure?` option is now applied only to the current request ([#45](https://github.com/clj-commons/clj-http-lite/issues/45)) ([@lread](https://github.com/lread))
67
- Quality
7-
- Docstrings reviewed and updated
8+
- Docstrings and README reviewed and updated
89
- Automated CI testing added for Windows ([#21](https://github.com/clj-commons/clj-http-lite/issues/21)) ([@lread](https://github.com/lread))
910

1011
### 0.4.384

bb/clj_http/lite/client_test.clj

+7
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,10 @@
2929
(is false "should not reach here")
3030
(catch Exception e
3131
(is (:headers (ex-data e))))))
32+
33+
(deftest insecure-test
34+
(is (thrown? Exception
35+
(client/get "https://expired.badssl.com")))
36+
(is (= 200 (:status (client/get "https://expired.badssl.com" {:insecure? true}))))
37+
(is (thrown? Exception
38+
(client/get "https://expired.badssl.com"))))

src/clj_http/lite/core.clj

+26-24
Original file line numberDiff line numberDiff line change
@@ -41,35 +41,37 @@
4141
(.flush baos)
4242
(.toByteArray baos)))))
4343

44-
(def ^:private insecure-mode
45-
(delay (throw (ex-info "insecure? option not supported in this environment"
46-
{}))))
44+
(defn- trust-all-ssl!
45+
[_conn]
46+
(throw (ex-info "insecure? option not supported in this environment"
47+
{})))
4748

4849
(defmacro ^:private def-insecure []
4950
(when (try (import '[javax.net.ssl
5051
HttpsURLConnection SSLContext TrustManager X509TrustManager HostnameVerifier SSLSession])
5152
(catch Exception _))
5253
'(do
53-
(defn- my-host-verifier []
54-
(proxy [HostnameVerifier] []
55-
(verify [^String hostname ^javax.net.ssl.SSLSession session] true)))
56-
57-
(defn trust-invalid-manager
58-
"This allows the ssl socket to connect with invalid/self-signed SSL certs."
59-
[]
60-
(reify javax.net.ssl.X509TrustManager
61-
(getAcceptedIssuers [this] nil)
62-
(checkClientTrusted [this certs authType])
63-
(checkServerTrusted [this certs authType])))
54+
(def ^:private trust-all-hostname-verifier
55+
(delay
56+
(proxy [HostnameVerifier] []
57+
(verify [^String hostname ^SSLSession session] true))))
6458

65-
(def ^:private insecure-mode
59+
(def ^:private trust-all-ssl-socket-factory
6660
(delay
67-
(HttpsURLConnection/setDefaultSSLSocketFactory
68-
(.getSocketFactory
69-
(doto (SSLContext/getInstance "SSL")
70-
(.init nil (into-array TrustManager [(trust-invalid-manager)])
71-
(new java.security.SecureRandom)))))
72-
(HttpsURLConnection/setDefaultHostnameVerifier (my-host-verifier)))))))
61+
(.getSocketFactory
62+
(doto (SSLContext/getInstance "SSL")
63+
(.init nil (into-array TrustManager [(reify X509TrustManager
64+
(getAcceptedIssuers [this] nil)
65+
(checkClientTrusted [this certs authType])
66+
(checkServerTrusted [this certs authType]))])
67+
(new java.security.SecureRandom))))))
68+
69+
(defn- trust-all-ssl!
70+
[conn]
71+
(when (instance? HttpsURLConnection conn)
72+
(let [^HttpsURLConnection ssl-conn conn]
73+
(.setHostnameVerifier ssl-conn @trust-all-hostname-verifier)
74+
(.setSSLSocketFactory ssl-conn @trust-all-ssl-socket-factory)))))))
7375

7476
(def-insecure)
7577

@@ -84,9 +86,9 @@
8486
(when server-port (str ":" server-port))
8587
uri
8688
(when query-string (str "?" query-string)))
87-
_ (when insecure?
88-
@insecure-mode)
8989
^HttpURLConnection conn (.openConnection (URL. http-url))]
90+
(when insecure?
91+
(trust-all-ssl! conn))
9092
(when (and content-type character-encoding)
9193
(.setRequestProperty conn "Content-Type" (str content-type
9294
"; charset="
@@ -116,4 +118,4 @@
116118
(coerce-body-entity req conn))}
117119
(when save-request?
118120
{:request (assoc (dissoc req :save-request?)
119-
:http-url http-url)}))))
121+
:http-url http-url)}))))

test/clj_http/test/core_test.clj

+4-1
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,10 @@
172172
(request client-opts)))
173173
(let [resp (request (assoc client-opts :insecure? true))]
174174
(is (= 200 (:status resp)))
175-
(is (= "get" (slurp-body resp))))))
175+
(is (= "get" (slurp-body resp))))
176+
(is (thrown? javax.net.ssl.SSLException
177+
(request client-opts))
178+
"subsequent bad cert fetch throws")))
176179

177180
(deftest ^{:integration true} t-save-request-obj
178181
(let [resp (request {:request-method :post :uri "/post"

0 commit comments

Comments
 (0)