Secure Coding Best Practices Handbook Veracode Guide
Secure Coding Best Practices Handbook Veracode Guide
CODI N G BEST
P R ACTI CES
H AN DBO OK
A D E V E LO P E R ’ S G U I D E
TO P R OAC T I V E CO N T R O L S
SECUR ITY SK IL L S A RE W H AT ’ S I N S I D E
NO LO N GE R O P T I O N A L B E ST P R ACTI CE S
FOR DE V E LOP ERS #01 Verify for Security Early and Often
#02 Parameterize Queries
#03 Encode Data
As cybersecurity risks steadily increase, application security
#04 Validate All Inputs
has become an absolute necessity. That means secure
coding practices must be part of every developer’s skill set. #05 Implement Identity and
How you write code, and the steps you take to update and Authentication Controls
monitor it, have a big impact on your applications, your
#06 Implement Access Controls
organization, and your ability to do your job well.
#07 Protect Data
This guide will give you practical tips in using secure
#08 Implement Logging
coding best practices. It’s based on the OWASP Top 10
and Intrusion Detection
Proactive Controls — widely considered the gold standard
for application security — but translated into a concise, #09 Leverage Security
easy-to-use format. You’ll get a brief overview of each Frameworks and Libraries
control, along with coding examples, actionable advice, #10 Monitor Error and Exception
and further resources to help you create secure software. Handling
It used to be standard practice for the security team to do security testing near RISKS ADDRESSED
the end of a project and then hand the results over to developers for remediation.
But tackling a laundry list of fixes just before the application is scheduled to go
to production isn’t acceptable anymore. It also increases the risk of a breach. You
need the tools and processes for manual and automated testing during coding.
All the OWASP
Top 10 Risks
SECURITY TIPS
• Scrum with the security team to ensure testing methods fix any defects. Veracode Application Security Platform
• Consider data protections from the beginning. Include security up front Veracode Greenlight
when agreeing upon the definition of “done” for a project.
Learn more
Secure Coding Best Practices Handbook:
A Developer’s Guide to Proactive Controls |3
BEST
P R AC T I C E 2 Parameterize Queries
SQL injection is one of the most dangerous application risks, partly because RISKS ADDRESSED
attackers can use open source attack tools to exploit these common vulnerabilities.
You can control this risk using query parameterization. This type of query specifies
placeholders for parameters, so the database will always treat them as data, rather
than part of a SQL command. You can use prepared statements, and a growing
SQL
number of frameworks, including Rails, Django, and Node.js, use object relational
injection
mappers to abstract communication with a database.
• Defend against SQL injection using proper database management system RESOURCES
configuration.
Veracode SQL Injection Cheat Sheet
Encoding translates potentially dangerous special characters into an equivalent RISKS ADDRESSED
form that renders the threat ineffective. This technique is applicable for a
variety of platforms and injection methods, including UNIX command encoding,
Windows command encoding, and cross-site scripting (XSS). Encoding
addresses the three main classes of XSS: persistent, reflected, and DOM-based.
SQL Cross-site Client-side
injection scripting injection
SECURITY TIPS
• Develop relevant encoding to address the spectrum of attack methods, Veracode Static Analysis
including injection attacks.
It's vitally important to ensure that all data is syntactically and semantically RISKS ADDRESSED
valid as it arrives and enters a system. As you approach the task, assume that
all data and variables can’t be trusted, and provide security controls regardless
of the source of that data. Valid syntax means that the data is in the form that's
expected — including the correct number of characters or digits. Semantic
SQL Cross-site Unvalidated redirects
validity means that the data has actual meaning and is valid for the interaction
injection scripting and forwards
or transaction. Whitelisting is the recommended validation method.
You can avoid security breaches by confirming user identity up front and RISKS ADDRESSED
building strong authentication controls into code and systems. These controls
must extend beyond a basic username and password. You’ll want to include both
session management and identity management controls to provide the highest
level of protection.
Broken authentication
and session management
SECURITY TIPS
You can dramatically improve protection and resiliency in your applications by RISKS ADDRESSED
building authorization or access controls into your applications in the initial
stages of application development. Note that authorization is not the same
as authentication. According to OWASP, authorization is the “process where
requests to access a particular feature or resource should be granted or denied.”
Insecure direct Missing function-
When appropriate, authorization should include a multi-tenancy and horizontal
object references level access control
(data specific) access control.
• Consider denying all access for features that haven’t been configured OWASP Testing Guide for Authorization
for access control.
• Code to the principle of least privilege. Allocate the minimum privilege and
time span required to perform an action for each user or system component.
• Adopt a framework that supports server-side trusted data for driving access
control. Key elements of the framework include user identity and log-in
state, user entitlements, overall access control policy, the feature and data
requested, along with time and geolocation.
Improve protection
Consider using the following string.
if (user.hasAccess("DELETE_ACCOUNT")) {
and resiliency in
your applications by
deleteAccount();
}
building authorization
or access controls
during the initial
stages of application
development.
Organizations have a duty to protect sensitive data within applications. To that RISKS ADDRESSED
end, you must encrypt critical data while it’s at rest and in transit. This includes
financial transactions, web data, browser data, and information residing in
mobile apps. Regulations like the EU General Data Protection Regulation make
data protection a serious compliance issue.
Sensitive data
exposure
SECURITY TIPS
• Don’t neglect the more difficult aspects of applied crypto, such as key
RESOURCES
management, overall cryptographic architecture design, tiering, and trust
issues in complex software. Existing crypto hardware, such as a Hardware Encryption and Decryption
Security Module (HSM), can make your job easier. in Java Cryptography
• Avoid using an inadequate key, or storing the key along with the encrypted data. Cryptographically Secure
Pseudo-Random Number Generators
• Don’t make confidential or sensitive data accessible in memory, or allow it to be
written into temporary storage locations or log files that an attacker can view. OWASP Cryptographic Storage
Cheat Sheet
• Use transport layer security (TLS) to encrypt data in transit.
OWASP Password Storage Cheat Sheet
In Java, this is the most secure way to create a randomizer object on Windows:
Coding secure
SecureRandom secRan = SecureRandom.getInstance("Windows-PRNG") ;
byte[] b = new byte[NO_OF_RANDOM_BYTES] ;
secRan.nextBytes(b);
crypto can be difficult
due to the number
On Unix-like systems, use this example: of parameters
SecureRandom secRan = new SecureRandom() ;
byte[] b = new byte[NO_OF_RANDOM_BYTES] ;
that you need to
secRan.nextBytes(b);
configure. Even a
tiny misconfiguration
will leave an entire
crypto-system open
to attacks.
Logging should be used for more than just debugging and troubleshooting. RISKS ADDRESSED
Logging and tracking security events and metrics helps to enable what’s
known as attack-driven defense, which considers the scenarios for
real-world attacks against your system. For example, if a server-side
validation catches a change to a non-editable, throw an alert or take some
All the OWASP
other action to protect your system. Focus on four key areas: application
Top 10 Risks
monitoring; business analytics and insight; activity auditing and compliance
monitoring; and system intrusion detection and forensics.
RESOURCE
SECURITY TIPS OWASP Logging Cheat Sheet
• Use an extensible logging framework like SLF4J with Logback,
or Apache Log4j2, to ensure that all log entries are consistent.
• Keep various audit and transaction logs separate for both security
and auditing purposes.
• Always log the timestamp and identifying information, like source IP and user ID.
• Log at an optimal level. Too much or too little logging heightens risk.
You can waste a lot of time — and unintentionally create security flaws — by RISKS ADDRESSED
developing security controls from scratch for every web application you’re
working on. To avoid that, take advantage of established security frameworks
and, when necessary, respected third-party libraries that provide tested and
proven security controls.
All common web
application vulnerabilities
SECURITY TIPS
• Use existing secure framework features rather than using new tools,
SOLUTION
such as third-party libraries.
Veracode Software Composition Analysis
• Because some frameworks have security flaws, build in additional controls
or security protections as needed.
By understanding not just the status of the component but whether or not a
vulnerable method is being called, organizations can pinpoint their component
risk and prioritize fixes based on the riskiest uses of components.
Learn more
Secure Coding Best Practices Handbook:
A Developer’s Guide to Proactive Controls | 14
Monitor Error and
BEST
P R AC T I C E 10 Exception Handling
Error and exception handling isn’t exciting, but like input validation, it is a crucial RISKS ADDRESSED
element of defensive coding. Mistakes in error and exception handling can cause
leakage of information to attackers, who can use it to better understand your
platform or design. Even small mistakes in error handling have been found to
cause catastrophic failures in distributed systems.
All the OWASP
Top 10 Risks
SECURITY TIPS
• Conduct careful code reviews and use negative testing, including exploratory
SOLUTION
testing and pen testing, fuzzing, and fault injection, to identify problems in
error handling. Veracode Manual Penetration Testing
A B O U T V E R ACO D E
Veracode, is a leader in helping organizations secure the software that powers their world. Veracode’s SaaS
platform and integrated solutions help security teams and software developers find and fix security-related
defects at all points in the software development lifecycle, before they can be exploited by hackers. Our complete
set of offerings help customers reduce the risk of data breaches, increase the speed of secure software delivery,
meet compliance requirements, and cost effectively secure their software assets – whether that’s software
they make, buy or sell. Veracode serves over a thousand customers across a wide range of industries, including
nearly one-third of the Fortune 100, three of the top four U.S. commercial banks and more than 20 of the Forbes
100 Most Valuable Brands. Learn more at veracode.com, on the Veracode Blog, and on Twitter.