Secure Coding Flashcards
Solution for SQL Injection
Solution
* Limit input size
* Remove special character
* Remove reserved keywords
* Check the desired pattern
Solution for XML injection
- Limit inputs
- Check it has the pattern that you want
- Never work on or log unvalidated inputs (string, path,…)
Numbers
Overflow
The built-in integer operators in Java secretly wrap the result without
reporting overflow when a mathematical operation cannot be represented
using the provided integer types.
Solution for Number Overflow
Solutions:
* Upcasting: Consider a larger data type if possible
* Prediction testing: Find the boundaries, throw ArithmaticException when needed.
* BigIntger: Convert the inputs into objects of type BigInteger
Numbers
Precision
Solution:
* Use integer
* Use BigDecimal
Methods
Validate input parameters
* Might be costly
* Avoid inconsistent computation, runtime exceptions
Assertion
- test your assumptions about your program
Accessibility
* Method that check security should be private or final.
Defensive Programming
Minimise the scope of variables
Minimise the accessibility of classes
Wrapper methods
* Use private modifiers when it is possible
Conclusive Secure Coding
- Do not trust inputs from users
- Take extra consideration when dealing with sensitive information
- Do not save them in local (log)
- Save them encoded when possible
- Be aware of scopes
- Limit modifier as much as possible
- Limit the scope of each variable
- Be aware of bitwise operations and overflow