Thursday 19 January 2012


Who Should Read:
  1. All java architects/developers/designers.
  2. All who are interested to understand the best practices to avoid secuirty loop holes in a web application.



7 Coding/Design practices to secure your web application

  1. Input Validation:
    • Prefer to have white list validation instead of black list validation.Idea is to accept input data which belongs to the set of known good values.
    • Regular Expression can be a good choice for implementing while list validations.For e.g
      public boolean validatePostCode(String code) {
        return (code != null && 
        Pattern.matches("/^(((2|8|9)\d{2})|((02|08|09)\d{2})|
        ([1-9]\d{3}))$/",code)) ? true : false;
      }
    • One should always prefer third party while list validatiors. Apache Commons validatior can be a good choice for input validation.
     
  1. Secure File Upload:
    • Try to upload files on some dedicated file area instead of directly storing in database or on some location in website tree.
    • Also apply validation checks for size, mime type and file type.
    • Java Mime magic library can be a good choice to validate mime type.
    • Also use to scan uploaded files. You can invoke antivirus CLI via Java Run Time to scan files on the fly.
  1. Output Encoding ( Escaping):
    • Escape html before inserting data into html elements for e.g. <body> escapeHTML(data) </body> .
    • Encode following 5 characters into html entities (& to &amp; > to &lt; < to &gt; “ to &qot; to &#x27; / to &#x2f
    • Escape java script before putting any data in java script elements for e.g. <script>alert(‘escapeJavaScript(data)’ </script>
    • Escape URLs e.g < a href="escapeURL(url)" />
    • Escape XML
    • You can use a apache commons StringEscapeUtils class to perform all above encodings. There are readymade methods like escapeHtml, escapeJavaScript, escapeXML, escapeURL
  1. Exception Handling:
    • Exception stack traces should not be displayed on browser.
    • Never let any exception leak any sensitive information to user/browser.
    • Catch each and every exception on the server and translate exception in a relevant error message.
    • Global exception handler can work for all uncaught errors.
  1. Logging:
    • What to log: All security related events like login, accessing a URL, changing role, assessing a resource.
    • What not to log: Any confidential or sensitive information like passwords, user credit card details etc.
  1. Security API:
  1. Avoid SQL injection:
    • Always use prepared statement to support parametrized queries.
    • Avoid string concatenation or string replacement to form queries.

8 comments:

  1. Very good Blog with relevant examples

    ReplyDelete
    Replies
    1. Please feel free to post some more practices and examples here .I will keep updating this article.

      Delete
  2. Very good post . Not only developers , testers can also benefit from these points.

    ReplyDelete
  3. I dislike the title of this article. Even if you implement all these suggestions then your application won't necessarily be secured. You would have reduced the risk of a successful attack but to say it's secured is an overstatement. For instance you're application could be very vulnerable to a (d)dos attack.

    ReplyDelete
    Replies
    1. Intent of this blog is to present 7 important practices to secure a web application. I fully agree with you that these guide lines may not be sufficient to ensure 100% security. Even any highly secure website can be vulnerable now a days. Only way is to keep evolving.

      Delete
  4. This blog is also avaliable on server side @ http://www.theserverside.com/discussions/thread.tss?thread_id=63580

    ReplyDelete
  5. Very nice article, well explained in a simple way. These are really loop holes in web security that we need to consider while designing application.

    ReplyDelete
  6. You should also share guidelines for code security

    ReplyDelete

A practical guide for java developers to take care of web application vulnerabilities.