Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Real World Web Security

Sponsored · SiteGround - Reliable hosting with speed, security, and support you can count on. →

Real World Web Security

Developer Essentials
adorsys – 2016-03-18

Avatar for Florian Hirsch

Florian Hirsch

March 18, 2016

More Decks by Florian Hirsch

Other Decks in Programming

Transcript

  1. REAL WORLD WEB SECURITY public class SecurityFilter implements ContainerRequestFilter {

    @Override public void filter(ContainerRequestContext requestContext) throws IOException { // TODO implement security } }
  2. A FORM <form action="https://adorsys.de/shop/carts" method="GET"> Shopping cart of: <input type="text"

    name="customerId"> <input type="submit"> </form> https://adorsys.de/shop/carts?customerId=4711
  3. SESSION-CONFIG (WEB.XML) <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"

    metadata-complete="true" version="3.0"> <session-config> <tracking-mode>URL</tracking-mode> <tracking-mode>COOKIE</tracking-mode> <tracking-mode>SSL</tracking-mode> </session-config> </web-app>
  4. @WebServlet("/login") public class LoginServlet extends HttpServlet { @Override protected void

    doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { if (login(req.getParameter("user"), req.getParameter("pw"))) { response.sendRedirect("/home"); } else { response.sendRedirect("/login-failed"); } } }
  5. RENEW THE SESSION // Java EE 7 request.changeSessionId(); // Jave

    EE 6 request.getSession(false).invalidate(); request.getSession(true); Session fixation
  6. CSRF VIA POST <form action="https://adorsys.de/money-transfer" method="POST" id="form"> <input type="hidden" name="to"

    value="4dcfbb3c6a4f1d4c4a000012"> <input type="hidden" name="amount" value="5000"> <input type="submit" value="Show me Kittens!"> </form> <img src="kittens.png" onmouseover="document.getElementById('form').submit(); return false;">
  7. CSRF ON A JSON API? <form enctype="text/plain"> -> @Consumes("application/json") XHR

    requests -> Access-Control-Allow-Origin: https://api.com
  8. SOP

  9. CSP HEADER Content-Security-Policy: default-src 'self'; script-src 'self' 'https://apis.google.com'; connect-src 'self'

    'https://my.api.com'; img-src 'self' 'https://static.cdn.com'; Content Security Policy Reference
  10. COOKIELESS LOGIN @Path("/login") public class LoginResource { @POST public Response

    login(@Context HttpServletRequest req) { if (basicAuth(req)) { return Response.ok() .header("X-AUTH-TOKEN", UUID.randomUUID()).build(); } else { return Response.status(403).build(); } } }
  11. TOKEN STORAGE $http.post('/login', credentials).then(function(response) { $httpProvider.defaults.headers.common['X-AUTH-TOKEN'] = response.data.token; // for

    page reloads... $cookies.put('X-AUTH-TOKEN', response.data.token); $window.localStorage.setItem('X-AUTH-TOKEN', response.data.token); $window.sessionStorage.setItem('X-AUTH-TOKEN', response.data.token); });
  12. <%@ page contentType="text/html; charset=utf-8" %> <!doctype html> <html> <head> <title>A

    JSP</title> </head> <body> <h1>Hello ${param.name}</h1> </body> </html>
  13. XSS PREVENTION (ANGULAR) // $sce enabled per default since 1.2,

    but: $translateProvider.useSanitizeValueStrategy('sanitize'); DOM based XSS Angular $sce Angular Translate Security
  14. MONGO SQL INJECTION { "username": {"$gt": ""}, "password": {"$gt": ""}

    } // also possible with Java Driver BasicDBObject query = (BasicDBObject) JSON.parse(json); Hacking NodeJS and MongoDB
  15. public Response importCsv(InputStream stream) throws Exception { BufferedReader reader =

    new BufferedReader(new InputStreamReader(inputStream, "UTF-8")); List<ErrorDetail> errors = new ArrayList<>(); String line; while ((line = reader.readLine())) != null) { if (!importCsvLine(line)) { errors.add(new ErrorDetail(line)); } } reader.close(); return errors.isEmpty() ? Response.ok().build() : Response.status(400).entity(errors).build(); }