Skip to content

Feature/fix insecure #12

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

Merged
merged 6 commits into from
Aug 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
### unreleased
Update to support self-signed certificates via insecure? option

### 0.4.3

- **Feature:** Parse link headers from response and put them under `:links` ([#1](https://github.com/martinklepsch/clj-http-lite/pull/1))
Expand Down
1 change: 0 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ A Clojure HTTP library similar to [clj-http](http://github.com/dakrone/clj-http)
- No proxy-ing DELETEs with body
- No multipart form uploads
- No persistent connection support
- No support for insecure HTTPS connection (yet)
- namespace rename clj-http.* -> clj-http.lite.*

## Usage
Expand Down
3 changes: 1 addition & 2 deletions deps.edn
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
{:paths ["src"]
:deps {org.clojure/clojure {:mvn/version "1.6.0"}
slingshot {:mvn/version "0.12.1"}}}
:deps {org.clojure/clojure {:mvn/version "1.10.0"}}}
22 changes: 21 additions & 1 deletion src/clj_http/lite/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
"Core HTTP request/response implementation."
(:require [clojure.java.io :as io])
(:import (java.io ByteArrayOutputStream InputStream IOException)
(java.net URI URL HttpURLConnection)))
(java.net URL HttpURLConnection)
(javax.net.ssl HttpsURLConnection SSLContext TrustManager X509TrustManager HostnameVerifier SSLSession)
(java.security SecureRandom)))

(set! *warn-on-reflection* true)

Expand Down Expand Up @@ -41,6 +43,17 @@
(.flush baos)
(.toByteArray baos)))))

(defn my-host-verifier []
(proxy [HostnameVerifier] []
(verify [^String hostname ^SSLSession session] true)))

(defn trust-invalid-manager []
"This allows the ssl socket to connect with invalid/self-signed SSL certs."
(reify X509TrustManager
(getAcceptedIssuers [this] nil)
(checkClientTrusted [this certs authType])
(checkServerTrusted [this certs authType])))

(defn request
"Executes the HTTP request corresponding to the given Ring request map and
returns the Ring response map corresponding to the resulting HTTP response.
Expand All @@ -55,6 +68,13 @@
(when server-port (str ":" server-port))
uri
(when query-string (str "?" query-string)))
_ (when insecure?
(do (HttpsURLConnection/setDefaultSSLSocketFactory
(.getSocketFactory
(doto (SSLContext/getInstance "SSL")
(.init nil (into-array TrustManager [(trust-invalid-manager)])
(new SecureRandom)))))
(HttpsURLConnection/setDefaultHostnameVerifier (my-host-verifier))))
^HttpURLConnection conn (.openConnection ^URL (URL. http-url))]
(when (and content-type character-encoding)
(.setRequestProperty conn "Content-Type" (str content-type
Expand Down