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

Web Security in Node.js Applications

Scott Smith
November 23, 2014

Web Security in Node.js Applications

With billions of people using the Internet, the potential for nefarious or outright criminal users hitting your site is very high. With attacks ranging from MITM, CSRF, Script Injection, Clickjacking to name a few, it is imperative that we as developers understand these exploits, how they work, how they can be stopped, and how to implement the code or setup to do so. As developers, we tend to focus on the core of what our application does. Unfortunately, security tends to be overlooked or at best an afterthought.

In this talk we will cover best practices for securing your web applications. The focus will be on Express web applications in Node, but the principles shown can be applied to any framework or environment.

Some, but not all, of the topics we will cover are: securing your web traffic, proper cookie handling, preventing CSRF, stopping Script Injection, sanitizing data input and output, handling sensitive data, implementing CSP, HSTS, X-Frame-Options, preventing vulnerabilities in node modules, and more.

At the end, you will leave with a diverse understanding of how best to secure your application from most of the attacks that can occur.

Scott Smith

November 23, 2014
Tweet

More Decks by Scott Smith

Other Decks in Programming

Transcript

  1. I’m Scott Smith VP of Product Development by Day Full

    Stack Node & .NET Developer by Night
  2. • Injection • Broken Authentication and Session Management • Cross

    Site Scripting (XSS) • Cross Site Request Forgery (CSRF) • Using Components with Known Vulnerabilities
  3. • Injection • Broken Authentication and Session Management • Cross

    Site Scripting (XSS) • Cross Site Request Forgery (CSRF) • Using Components with Known Vulnerabilities
  4. • Sharing of link grants others full access • Stored

    on cache servers https://bank.com/account?sessionid=1234567
  5. • Sharing of link grants others full access • Stored

    on cache servers • Stored in browser history https://bank.com/account?sessionid=1234567
  6. • Sharing of link grants others full access • Stored

    on cache servers • Stored in browser history • Leaked through the Referer header https://bank.com/account?sessionid=1234567
  7. • Sharing of link grants others full access • Stored

    on cache servers • Stored in browser history • Leaked through the Referer header • Leaded through logs not properly protected https://bank.com/account?sessionid=1234567
  8. • Sharing of link grants others full access • Stored

    on cache servers • Stored in browser history • Leaked through the Referer header • Leaded through logs not properly protected • Much more visible and dangerous https://bank.com/account?sessionid=1234567
  9. • Injection • Broken Authentication and Session Management • Cross

    Site Scripting (XSS) • Cross Site Request Forgery (CSRF) • Using Components with Known Vulnerabilities
  10. Attacker Victim Site GET /index.html HTTP/1.1 200 OK <html> <a

    href=“https://site.com/search? q=<script>alert(‘hacked’)<script>”> Click Here </a> </html> Non-persistent
  11. Attacker Victim Site GET /index.html HTTP/1.1 200 OK <html> <a

    href=“https://site.com/search? q=<script>alert(‘hacked’)<script>”> Click Here </a> </html> GET /search?q=… Non-persistent
  12. Attacker Victim Site GET /index.html HTTP/1.1 200 OK <html> <a

    href=“https://site.com/search? q=<script>alert(‘hacked’)<script>”> Click Here </a> </html> GET /search?q=… Non-persistent HTTP/1.1 200 OK Payload with injected script
  13. Attacker Victim Site GET /index.html HTTP/1.1 200 OK <html> <a

    href=“https://site.com/search? q=<script>alert(‘hacked’)<script>”> Click Here </a> </html> GET /search?q=… Non-persistent HTTP/1.1 200 OK Payload with injected script Send valuable data
  14. Attacker Victim Site GET /comment?id=1 Persistent HTTP/1.1 200 OK Payload

    with injected script POST /comment Payload with script
  15. Attacker Victim Site GET /comment?id=1 Persistent HTTP/1.1 200 OK Payload

    with injected script Send valuable data POST /comment Payload with script
  16. Content Security Policy (CSP) Response header telling the browser the

    domains it should consider as valid sources of content
  17. • Injection • Broken Authentication and Session Management • Cross

    Site Scripting (XSS) • Cross Site Request Forgery (CSRF) • Using Components with Known Vulnerabilities
  18. Attacker Victim Bank POST /login HTTP/1.1 200 OK Set-Cookie: SessionId=1234

    GET /index.html HTTP/1.1 200 OK <html> <img src=“https://bank.com/transfer? to=12345&dollars=1000000” width=“0” height=“0”> </html>
  19. Attacker Victim Bank POST /login HTTP/1.1 200 OK Set-Cookie: SessionId=1234

    GET /index.html HTTP/1.1 200 OK <html> <img src=“https://bank.com/transfer? to=12345&dollars=1000000” width=“0” height=“0”> </html> GET /transfer?to… Cookie: SessionId=1234
  20. Attacker Victim Bank POST /login HTTP/1.1 200 OK Set-Cookie: SessionId=1234

    GET /index.html HTTP/1.1 200 OK <html> <img src=“https://bank.com/transfer? to=12345&dollars=1000000” width=“0” height=“0”> </html> HTTP/1.1 200 OK GET /transfer?to… Cookie: SessionId=1234
  21. Attacker Victim Bank POST /login HTTP/1.1 200 OK Set-Cookie: SessionId=1234

    GET /index.html HTTP/1.1 200 OK <form name=“bad” method=“post” action=“https://bank.com/transfer”> <input type=“hidden” name=“to” value=“12345”> <input type=“hidden” name=“dollars” value=“1000000”> </form> <script>document.bad.submit()</script>
  22. Attacker Victim Bank POST /login HTTP/1.1 200 OK Set-Cookie: SessionId=1234

    GET /index.html HTTP/1.1 200 OK <form name=“bad” method=“post” action=“https://bank.com/transfer”> <input type=“hidden” name=“to” value=“12345”> <input type=“hidden” name=“dollars” value=“1000000”> </form> <script>document.bad.submit()</script> POST /transfer Cookie: SessionId=1234
  23. Attacker Victim Bank POST /login HTTP/1.1 200 OK Set-Cookie: SessionId=1234

    GET /index.html HTTP/1.1 200 OK <form name=“bad” method=“post” action=“https://bank.com/transfer”> <input type=“hidden” name=“to” value=“12345”> <input type=“hidden” name=“dollars” value=“1000000”> </form> <script>document.bad.submit()</script> HTTP/1.1 200 OK POST /transfer Cookie: SessionId=1234
  24. Attacker Victim Bank POST /login HTTP/1.1 200 OK Set-Cookie: SessionId=1234

    GET /index.html HTTP/1.1 200 OK <iframe src=“https://bank.com/transfer? to=12345&dollars=1000000”>
  25. Attacker Victim Bank POST /login HTTP/1.1 200 OK Set-Cookie: SessionId=1234

    GET /index.html HTTP/1.1 200 OK <iframe src=“https://bank.com/transfer? to=12345&dollars=1000000”> GET /transfer?to=… Cookie: SessionId=1234
  26. Attacker Victim Bank POST /login HTTP/1.1 200 OK Set-Cookie: SessionId=1234

    GET /index.html HTTP/1.1 200 OK <iframe src=“https://bank.com/transfer? to=12345&dollars=1000000”> GET /transfer?to=… Cookie: SessionId=1234 HTTP/1.1 200 OK <form method=“post”> <input type=“text” name=“to” value=“”> <input type=“text” name=“dollars” value=“”> <input type=“hidden” name=“csrf” value=“a0d73b12”> </form>
  27. Attacker Victim Bank POST /login HTTP/1.1 200 OK Set-Cookie: SessionId=1234

    GET /index.html HTTP/1.1 200 OK <iframe src=“https://bank.com/transfer? to=12345&dollars=1000000”> GET /transfer?to=… Cookie: SessionId=1234 HTTP/1.1 200 OK <form method=“post”> <input type=“text” name=“to” value=“”> <input type=“text” name=“dollars” value=“”> <input type=“hidden” name=“csrf” value=“a0d73b12”> </form> POST /transfer?to=… via Clickjacking
  28. • Injection • Broken Authentication and Session Management • Cross

    Site Scripting (XSS) • Cross Site Request Forgery (CSRF) • Using Components with Known Vulnerabilities
  29. Other OWASP top 10 • Insecure Direct Object Reference •

    Security Misconfigurations • Sensitive Data Exposure • Missing Function Level Access Control • Unvalidated Redirects and Forwards
  30. npm packages • express-session • bcrypt-nodejs • helmet (check out

    Lusca as well) • express-validator • csurf • nsp and grunt-nap-package • david