SlideShare a Scribd company logo
Code Review for Secure Web
       Applications
       With java samples
Bibliography
• OWASP – Open web applications security
  projects – www.owasp.org
• OWASP Code review guide
Introduction
• Code reviews:
  – Ad hoc reviews
  – Pair programming
  – Walkthrough
  – Team review
  – Inspection
• Purpose – security
Code review strategies
• Automatic
• Manual – use checklists
  – Risk based
  – Most encountered programming mistakes
  – Mitigation of most encountered vulnerabilities
    exploited in the world
  – Security best practices
Checklist based on best practices
•   Authentication
•   Authorization
•   Session management
•   Input validation and output sanitization
Checklist based on best practices
               To be presented next meeting

•   Prevent Cross Site Request Forgery
•   Cryptographic controls
•   Error handling
•   Logging
•   Prevent Race conditions
Authentication
• Check user is not allowed to choose weak
  passwords
Bad:
String password = request.getParameter("Password");
if (password == Null)
    {throw InvalidPasswordException()
    }
Authentication
• Check user is not allowed to choose weak
  passwords
OK:
if password.RegEx([a-z])
    and password.RegEx([A-Z])
    and password.RegEx([0-9])
    and password.RegEx({8-30})
    and password.RexEX([!"£$%^&*()])
    return true;
else
return false;
Authentication
• Password storage strategy: hashing using a
  one-way hash algorithm + salting
OK hashing:
import java.security.MessageDigest;
public byte[] getHash(String password) throws
  NoSuchAlgorithmException {
  MessageDigest digest = MessageDigest.getInstance("SHA-1");
  digest.reset();
  byte[] input = digest.digest(password.getBytes("UTF-8"));
}
Authentication
• Password storage strategy: hashing using a one-way
  hash algorithm + salting
OK salting:
import java.security.MessageDigest;
public byte[] getHash(String password, byte[] salt) throws
  NoSuchAlgorithmException {
  MessageDigest digest = MessageDigest.getInstance("SHA-
  256");
  digest.reset();
  digest.update(salt);
  return digest.digest(password.getBytes("UTF-8"));
}
Authorization
• Check the access roles matrix and make sure it is
  created respecting the need-to-know and least-
  privilege principle
• Check the business logic for errors
Bad:
if user.equals("NormalUser")
    { grantUser(Normal_Permissions);
} else{ //user must be admin/super
    grantUser("Super_Permissions);
}
Authorization
• Check if security by obscurity is used
• Check if authorization is verified for every request
Good:
String action = request.getParameter("action");
 if (action.equals("doStuff"))
boolean permit = session.authTable.isAuthorised(action);
if (permit) doStuff();
else{
    throw new (InvalidRequestException("Unauthorised
    request");
    session.invalidate();
}
Session Management
• Check if only framework’s session manager is
  used
• Check the cryptographic strength, the length of
  the sessions and character pool
• Check that sessionIds coming from clients are
  validated
• Check there is a timeout implemented for idle
  sessions
• Check session is destroyed on logout
Input validation and output
                sanitization
• Ensure 2 separate validations occur: first a
  security validation, then a business validation
• Ensure in the security validation, data are
  canonicalized first
public static void main(String[] args) {
File x = new File("/cmd/" + args[1]);
String absPath = x.getAbsolutePath();
String canonicalPath = x.getCanonicalPath();
}
Input validation and output
               sanitization
• Check that all input that traversed untrusted
  zones is validated, not only user input
• Check that validators or sanitizers are adapted
  for the modules that receives/uses data –
  encode, escape, etc
• Check validators are applied in a safe side
  (never client side)
Input validation and output
                sanitization
public class DoStuff {
public String executeCommand(String userName) {
 try {
   String myUid = userName;
   Runtime rt = Runtime.getRuntime();
   rt.exec("cmd.exe /C doStuff.exe " +”-“ +myUid);
}
catch(Exception e) { e.printStackTrace(); } } }
Input validation and output
               sanitization
String myQuery = “select food from foods where
  name=?”;
String sortOrder=request.getParameter(“order”);
myQuery+=sortOrder;
PreparedStatement preparedStatement =
  connection.prepareStatement(myQuery);
preparedStatement.setString(1, “Shaorma”);
ResultSet resultSet =
  preparedStatement.executeQuery();
Input validation and output
                 sanitization
import java.io.*;
import javax.servlet.http.*;
import javax.servlet.*;
public class HelloServlet extends HttpServlet {
public void doGet (HttpServletRequest req, HttpServletResponse
  res) throws ServletException, IOException {
  String input = req.getHeader(“USERINPUT”);
  PrintWriter out = res.getWriter();
  out.println(Server.HTMLEncode(input));
   out.close();
}
}
Thank you for the interest
Questions?
Prevent Cross Site Script Forgery
Cryptographic controls
Error handling
Logging
Prevent Race Conditions

More Related Content

PPTX
Null meet Code Review
PDF
Java Code Review Checklist
PPTX
Secure coding practices
PDF
Secure Coding in C/C++
PPTX
Static Analysis Security Testing for Dummies... and You
PDF
PPTX
Static Code Analysis
PPTX
DevBeat 2013 - Developer-first Security
Null meet Code Review
Java Code Review Checklist
Secure coding practices
Secure Coding in C/C++
Static Analysis Security Testing for Dummies... and You
Static Code Analysis
DevBeat 2013 - Developer-first Security

What's hot (20)

PPTX
Top 10 static code analysis tool
PPTX
Server Side Template Injection by Mandeep Jadon
PPTX
Code review
PPT
Security Testing
PPTX
PVS-Studio and static code analysis technique
PPTX
Making Security Agile
ODP
OWASP Secure Coding
PDF
Static Analysis Techniques For Testing Application Security - Houston Tech Fest
PDF
Secure Coding - Web Application Security Vulnerabilities and Best Practices
PDF
Neoito — Secure coding practices
PPTX
Static code analysis
PPTX
Static Analysis Primer
PPTX
DevSecOps: Securing Applications with DevOps
PPTX
Java Code Quality Tools
PDF
Secure Coding principles by example: Build Security In from the start - Carlo...
PDF
Simplified Security Code Review Process
PDF
Best Practices of Static Code Analysis in the SDLC
PPTX
Java exception handling
DOCX
Code review guidelines
Top 10 static code analysis tool
Server Side Template Injection by Mandeep Jadon
Code review
Security Testing
PVS-Studio and static code analysis technique
Making Security Agile
OWASP Secure Coding
Static Analysis Techniques For Testing Application Security - Houston Tech Fest
Secure Coding - Web Application Security Vulnerabilities and Best Practices
Neoito — Secure coding practices
Static code analysis
Static Analysis Primer
DevSecOps: Securing Applications with DevOps
Java Code Quality Tools
Secure Coding principles by example: Build Security In from the start - Carlo...
Simplified Security Code Review Process
Best Practices of Static Code Analysis in the SDLC
Java exception handling
Code review guidelines
Ad

Viewers also liked (16)

PPTX
Security asp.net application
PDF
Deploying Static Application Security Testing on a Large Scale
PPTX
Microsoft asp.net identity security
KEY
Security Code Review: Magic or Art?
PPTX
Beefing Up Security In ASP.NET Dot Net Bangalore 3rd meet up on May 16 2015
PPTX
Beefing Up Security In ASP.NET Part 2 Dot Net Bangalore 4th meet up on August...
ODP
Secure coding in C#
PDF
Security Code Review for .NET - Sherif Koussa (OWASP Ottawa)
ODP
Practical Security Testing for Developers using OWASP ZAP at Dot Net Bangalor...
PPTX
PCI security requirements secure coding and code review 2014
PPTX
ASP.NET Core Security
PPTX
ASP.NET Web Security
PDF
Secure Code Review 101
PDF
Sass Code Reviews - How one code review changed my life #SassConf2015
PDF
«Вредоносные браузерные расширения и борьба с ними», Александра Сватикова (Од...
PDF
«Android Activity Hijacking», Евгений Блашко, Юрий Шабалин (АО «Сбербанк-Тех...
Security asp.net application
Deploying Static Application Security Testing on a Large Scale
Microsoft asp.net identity security
Security Code Review: Magic or Art?
Beefing Up Security In ASP.NET Dot Net Bangalore 3rd meet up on May 16 2015
Beefing Up Security In ASP.NET Part 2 Dot Net Bangalore 4th meet up on August...
Secure coding in C#
Security Code Review for .NET - Sherif Koussa (OWASP Ottawa)
Practical Security Testing for Developers using OWASP ZAP at Dot Net Bangalor...
PCI security requirements secure coding and code review 2014
ASP.NET Core Security
ASP.NET Web Security
Secure Code Review 101
Sass Code Reviews - How one code review changed my life #SassConf2015
«Вредоносные браузерные расширения и борьба с ними», Александра Сватикова (Од...
«Android Activity Hijacking», Евгений Блашко, Юрий Шабалин (АО «Сбербанк-Тех...
Ad

Similar to Code review for secure web applications (20)

PDF
Steps to mitigate Top 5 OWASP Vulnerabilities 2013
PPTX
Secure coding - Balgan - Tiago Henriques
PPTX
Ebu class edgescan-2017
PPTX
Code Review Cybersecurity: Comprehensive Guide to Secure Code Evaluation & B...
PPTX
Defending web applications v.1.0
PDF
WhiteList Checker: An Eclipse Plugin to Improve Application Security
PDF
How to avoid top 10 security risks in Java EE applications and how to avoid them
PDF
42 minutes to secure your code....
PDF
2015 09-18-jug summer camp
PDF
9 Ways to Hack a Web App
PDF
Secure code
PPT
Secure code practices
PPTX
The path of secure software by Katy Anton
PPTX
Writing secure code
PDF
Protecting web apps
PDF
WhiteList Checker: An Eclipse Plugin to Improve Application Security
PDF
Owasp tds
ODP
Java zone ASVS 2015
PDF
SecurityBSides London - Agnitio: it's static analysis but not as we know it
PPT
Web application development_dos_and_donts
Steps to mitigate Top 5 OWASP Vulnerabilities 2013
Secure coding - Balgan - Tiago Henriques
Ebu class edgescan-2017
Code Review Cybersecurity: Comprehensive Guide to Secure Code Evaluation & B...
Defending web applications v.1.0
WhiteList Checker: An Eclipse Plugin to Improve Application Security
How to avoid top 10 security risks in Java EE applications and how to avoid them
42 minutes to secure your code....
2015 09-18-jug summer camp
9 Ways to Hack a Web App
Secure code
Secure code practices
The path of secure software by Katy Anton
Writing secure code
Protecting web apps
WhiteList Checker: An Eclipse Plugin to Improve Application Security
Owasp tds
Java zone ASVS 2015
SecurityBSides London - Agnitio: it's static analysis but not as we know it
Web application development_dos_and_donts

Recently uploaded (20)

PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
Cloud computing and distributed systems.
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
KodekX | Application Modernization Development
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
Big Data Technologies - Introduction.pptx
PDF
Approach and Philosophy of On baking technology
PDF
Advanced Soft Computing BINUS July 2025.pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Modernizing your data center with Dell and AMD
Diabetes mellitus diagnosis method based random forest with bat algorithm
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Per capita expenditure prediction using model stacking based on satellite ima...
Cloud computing and distributed systems.
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
KodekX | Application Modernization Development
Advanced methodologies resolving dimensionality complications for autism neur...
Chapter 3 Spatial Domain Image Processing.pdf
The Rise and Fall of 3GPP – Time for a Sabbatical?
The AUB Centre for AI in Media Proposal.docx
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
20250228 LYD VKU AI Blended-Learning.pptx
Big Data Technologies - Introduction.pptx
Approach and Philosophy of On baking technology
Advanced Soft Computing BINUS July 2025.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
Modernizing your data center with Dell and AMD

Code review for secure web applications

  • 1. Code Review for Secure Web Applications With java samples
  • 2. Bibliography • OWASP – Open web applications security projects – www.owasp.org • OWASP Code review guide
  • 3. Introduction • Code reviews: – Ad hoc reviews – Pair programming – Walkthrough – Team review – Inspection • Purpose – security
  • 4. Code review strategies • Automatic • Manual – use checklists – Risk based – Most encountered programming mistakes – Mitigation of most encountered vulnerabilities exploited in the world – Security best practices
  • 5. Checklist based on best practices • Authentication • Authorization • Session management • Input validation and output sanitization
  • 6. Checklist based on best practices To be presented next meeting • Prevent Cross Site Request Forgery • Cryptographic controls • Error handling • Logging • Prevent Race conditions
  • 7. Authentication • Check user is not allowed to choose weak passwords Bad: String password = request.getParameter("Password"); if (password == Null) {throw InvalidPasswordException() }
  • 8. Authentication • Check user is not allowed to choose weak passwords OK: if password.RegEx([a-z]) and password.RegEx([A-Z]) and password.RegEx([0-9]) and password.RegEx({8-30}) and password.RexEX([!"£$%^&*()]) return true; else return false;
  • 9. Authentication • Password storage strategy: hashing using a one-way hash algorithm + salting OK hashing: import java.security.MessageDigest; public byte[] getHash(String password) throws NoSuchAlgorithmException { MessageDigest digest = MessageDigest.getInstance("SHA-1"); digest.reset(); byte[] input = digest.digest(password.getBytes("UTF-8")); }
  • 10. Authentication • Password storage strategy: hashing using a one-way hash algorithm + salting OK salting: import java.security.MessageDigest; public byte[] getHash(String password, byte[] salt) throws NoSuchAlgorithmException { MessageDigest digest = MessageDigest.getInstance("SHA- 256"); digest.reset(); digest.update(salt); return digest.digest(password.getBytes("UTF-8")); }
  • 11. Authorization • Check the access roles matrix and make sure it is created respecting the need-to-know and least- privilege principle • Check the business logic for errors Bad: if user.equals("NormalUser") { grantUser(Normal_Permissions); } else{ //user must be admin/super grantUser("Super_Permissions); }
  • 12. Authorization • Check if security by obscurity is used • Check if authorization is verified for every request Good: String action = request.getParameter("action"); if (action.equals("doStuff")) boolean permit = session.authTable.isAuthorised(action); if (permit) doStuff(); else{ throw new (InvalidRequestException("Unauthorised request"); session.invalidate(); }
  • 13. Session Management • Check if only framework’s session manager is used • Check the cryptographic strength, the length of the sessions and character pool • Check that sessionIds coming from clients are validated • Check there is a timeout implemented for idle sessions • Check session is destroyed on logout
  • 14. Input validation and output sanitization • Ensure 2 separate validations occur: first a security validation, then a business validation • Ensure in the security validation, data are canonicalized first public static void main(String[] args) { File x = new File("/cmd/" + args[1]); String absPath = x.getAbsolutePath(); String canonicalPath = x.getCanonicalPath(); }
  • 15. Input validation and output sanitization • Check that all input that traversed untrusted zones is validated, not only user input • Check that validators or sanitizers are adapted for the modules that receives/uses data – encode, escape, etc • Check validators are applied in a safe side (never client side)
  • 16. Input validation and output sanitization public class DoStuff { public String executeCommand(String userName) { try { String myUid = userName; Runtime rt = Runtime.getRuntime(); rt.exec("cmd.exe /C doStuff.exe " +”-“ +myUid); } catch(Exception e) { e.printStackTrace(); } } }
  • 17. Input validation and output sanitization String myQuery = “select food from foods where name=?”; String sortOrder=request.getParameter(“order”); myQuery+=sortOrder; PreparedStatement preparedStatement = connection.prepareStatement(myQuery); preparedStatement.setString(1, “Shaorma”); ResultSet resultSet = preparedStatement.executeQuery();
  • 18. Input validation and output sanitization import java.io.*; import javax.servlet.http.*; import javax.servlet.*; public class HelloServlet extends HttpServlet { public void doGet (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { String input = req.getHeader(“USERINPUT”); PrintWriter out = res.getWriter(); out.println(Server.HTMLEncode(input)); out.close(); } }
  • 19. Thank you for the interest Questions?
  • 20. Prevent Cross Site Script Forgery