Clojure Ring
Java HTTP Server
Ring Server Adapter
request
response
fn
request
response
https://github.com/ring-clojure/ring/blob/master/SPEC
Slide 5
Slide 5 text
Ring Requests & Responses
(def request {:uri "/"
:request-method :get
:headers {"Accept"
"text/plain"}})
(defn example-app [req]
{:status 200
:body (str "Hello " (:uri req) "!")})
(ring.adapter.jetty/run-jetty
example-app {:port 4000})
webapp
Java HTTP Server
Ring Server Adapter
Slide 6
Slide 6 text
Ring Middleware
modi ed request
(i.e. added session info)
modi ed response
(i.e. added headers)
request
response
middleware
modi ed request
modi ed response
response modi ed request
response
request
condition?
true
false
(defn middleware [handler]
(fn [request]
;; Modify request before sending it to the web app
(let [response (handler request)]
;; modify response before returning it to the user
response)))
Putting it all together
(def webapp
(-> app-routes
auth-middleware
(wrap-defaults
secure-site-defaults)))
(ring.adapter.jetty/run-jetty
webapp {:port 4000})
auth?
handle no
auth
ring secure
defaults
GET
/:user
POST
/
GET
/
PUT
/:user
DELETE
/:user not-found
list users add user get user update
user
delete
user
handle
not found
Java HTTP Server
Ring Server Adapter
Where is the security?
auth?
handle no
auth
ring secure
defaults
GET
/:user
POST
/
GET
/
PUT
/:user
DELETE
/:user not-found
list users add user get user update
user
delete
user
handle
not found
Java HTTP Server
Ring Server Adapter
Slide 12
Slide 12 text
We have to add the last piece!
Hans‑Peter Gauster
Slide 13
Slide 13 text
Security = Teamwork + Review
(cc-by Kevin Dooley - flic.kr/p/dxCnzT)
Slide 14
Slide 14 text
How to Write a Secure Web
Application
Maintain your application
Stay informed! Register for Security Advisories
KISS
Know what you are doing
Monitor your Application
Slide 15
Slide 15 text
OWASP Top 10 2017
Injection
Broken Authentication / Session Management
XSS
Broken Access Control
Security Misconfigurations
Sensitive Data Exposure
Insufficient Attack Protection
Cross Site Request Forgery
Using Components with Known Vulnerabilities
Underprotected APIs
Slide 16
Slide 16 text
HTTPS
Just a friendly reminder to use SSL!
(def options
{:port 3000
:join? false
:ssl? true
:ssl-port 4000
:keystore "ssl/keystore"
:key-password "somesecret"
:host "example.com"})
(ring.adapter.jetty/run-jetty webapp options)
Validate Input - Escape Output
Templating Language Escaping by default? # GitHub References Last Updated
>2.0.0-alpha1 19481 15 Jan 2017
Yes 7412 21 Sep 2015
Yes 6465 28 Jun 2017
Yes 1177 17 Jun 2017
hiccup
enlive
Selmer
hoplon
Statistics taken on 17 July 2017
Slide 22
Slide 22 text
CSRF - Cross-site Request Forgery
Slide 23
Slide 23 text
Protect Against CSRF
Don't use GET requests to change state!
SameSite=Strict cookie attribute
Ring-Anti-Forgery
Authentication & Authorization
How do I know if the user is who he says he is?
How do I know if the user is allowed to access a
resource?
Slide 29
Slide 29 text
Introducing Buddy
Slide 30
Slide 30 text
Buddy-Auth
Ring Middleware Integration
Authentication information saved in :identity in the
request map
Different Backends: HTTP Basic, Session, Token,
Signed JWT, Encrypted JWT
Possibility to implement custom authentication
backend
In Summary...
Use HTTPS!
Validate user input and escape output
Templating library with automatic HTML escaping!!!
Use site-defaults or secure-site-
defaults for
for authentication and authorization
ring defaults
Buddy
Slide 37
Slide 37 text
Example Web Application:
(WIP)
https://github.com/innoq/quackers
Joy Clark
joy.clark@innoq.com
@iamjoyclark