Join Codacy Product Showcase on July 9th to learn about product updates

Group 370

Meet us at WeAreDevelopers World Congress in Berlin

Group 370

Spotlight Whitepaper by IDC on Importance of Automated Code Review Technologies

Group 370

OWASP Explained: Secure Coding Best Practices

In this article:
Subscribe to our blog:

From global finance to daily communication, software underpins every aspect of modern life. That's why a single data breach can shatter user trust, cripple businesses, and even disrupt world peace. Adopting security standards is essential to prevent and mitigate these risks. These frameworks help structure and systemize how you build secure software.

One of the most popular and influential communities on software security standards is the Open Worldwide Application Security Project (OWASP), a global network of experts and practitioners who share their insights and tips on building secure software. Following their recommendations and guidance can help avoid common pitfalls and security vulnerabilities plaguing software products.

In this article, we will explain what the OWASP is, its significance in the software industry, its most popular OWASP Top 10 publication, and how to implement secure coding practices using their recommendations. 

What Is OWASP?

OWASP is a nonprofit organization that provides resources and guidance for developers and security experts to build, test, and maintain secure software applications. 

OWASP was founded in 2001 by Mark Curphey, a software security consultant, who saw the need for a collaborative project to document and share web application security knowledge and best practices. 

Since 2001, OWASP has grown into a global community of thousands of volunteers contributing to various software security projects and research. These include the popular OWASP Top 10, a list of the most critical web application security risks, and the OWASP Application Security Verification Standard (ASVS) Project, which provides a basis for testing web application security controls and also provides developers with a list of requirements for secure development. 

OWASP helps developers, security professionals, and organizations understand potential software threats and adopt security best practices.  


The OWASP Top 10 Software Security Vulnerabilities

The OWASP Top 10 is a standard awareness document on software application security for developers and engineers. The latest version, released in 2021, represents a broad consensus about the most critical security risks facing software applications obtained from community research. 

A01:2021-Broken Access Control

Broken access control is a security risk when web applications fail to properly enforce authorization checks on users' actions and requests. Access control “breaks” when attackers can successfully request access to something they shouldn't have access to. 

Broken access control allows unauthorized users to access, modify, or delete sensitive data or resources, perform administrative functions, or take over other users' accounts.

Some of the best protection strategies for preventing broken access control are:

  • Grant users the minimum access level required for their tasks. Don't provide administrative access by default.

  • Implement role-based access control (RBAC) to assign granular permissions based on user roles and responsibilities.

  • Validate and sanitize all user inputs on the server before making access control decisions.

  • Use random or encrypted identifiers for resources, such as user profiles, documents, or transactions. 

Consider a scenario where users can add and edit comments on a blog post. You want to make sure that they can only edit comments that belong to them:

class User:
    def __init__(self, username, role):
        self.username = username
        self.role = role

class Comment:
    def __init__(self, content, author):
        self.content = content
        self.author = author

class BlogPost:
    def __init__(self, title, author):
        self.title = title
        self.author = author
        self.comments = []
    def add_comment(self, comment):

    def can_edit_comment(self, user, comment):
    if(user.username == comment.author.username# User can edit their own comments
          return True
    return False

Implementing real-world RBAC can be more complex, but you can find robust libraries that are well-maintained and appropriate for your web application's framework.

A02:2021-Cryptographic Failures

A cryptographic failure occurs when a system makes sensitive data accessible to potentially malicious attackers. Cryptographic failures usually result from the improper use or implementation of cryptographic techniques, such as encryption, hashing, signing, or secrets management, in software applications. 

The best protection strategy for cryptographic failures is to use cryptography correctly and securely throughout the application. This means:

  • Use secure and reliable cryptographic libraries or services that are well-tested, well-reviewed, and well-maintained, like PyCryptodome, Cryptography (Python), JSEncrypt, Forge (JavaScript), Bouncy Castle, and Web3j (Java). Avoid writing your own cryptographic code.

  • Store keys securely using hardware security modules (HSMs) or dedicated key management systems.

  • Enforce HTTPS for all communication, especially when transmitting sensitive data.

  • Regularly rotate keys to minimize the impact of potential compromises. Implement automated key rotation processes where feasible.


Injection happens when an attacker sends invalid data into an application to make it do something it was not supposed to do. SQL injection is a popular code injection technique. It happens when an attacker inserts malicious SQL code into user input that gets processed by the application and executed on the database.

Some of the best protection strategies for preventing SQL injection are:

  • Validate and sanitize all user inputs before sending them to a code interpreter. Use parameterized queries or prepared statements, which separate the query or command structure from the user input.

    For example, in a scenario where users can add comments to a blog post, you want to ensure they do not inject malicious SQL statements, which could be harmful to the database. Here, you can make use of parameterized queries using (?) and then supply the values using a safe method: 

cursor.execute(query, (user_id, content))

This way, the user input will not affect the structure of the SQL statement and will prevent SQL injection attacks.

def add_comment(user_id, content):
        conn = sqlite3.connect("mydb.sqlite")
        cursor = conn.cursor()
            # Use a parameterized query
            query = "INSERT INTO comments (user_id, content) VALUES (?, ?)"
            cursor.execute(query, (user_id, content))
            print(f"Comment added successfully for user '{user_id}'!")

    except sqlite3.Error as e:
        print(f"Error: {e}")

# Example usage
if __name__ == "__main__":
    user_id = "alice@example.com"
    comment_content = "Great article! I enjoyed reading it."

    add_comment(user_id, comment_content)
  • Regularly update your database software, application frameworks, and libraries to benefit from security patches.

  • Limit the database access based on individual user roles and functions.

A04:2021-Insecure Design

Insecure design is a security weakness that stems from the lack of security considerations or principles in the design phase of web applications. Insecure design can introduce flaws or vulnerabilities that are hard or impossible to fix in the later stages of development or deployment.

Preventing insecure design requires a proactive approach throughout the development lifecycle. Here are some key strategies:

  • Integrate security considerations from the very beginning, aligning them with functional requirements.
  • Utilize static code analysis tools such as Codacy to identify potential security vulnerabilities in your codebase early on.

  • Use well-proven and secure design patterns like Defense in Depth, Least Privilege, and Secure Direct Object References.

  • Conduct dynamic application security testing (DAST) to uncover runtime vulnerabilities and weaknesses in your application's behavior.

  • Automate deployment processes to minimize human error

A05:2021-Security Misconfiguration

Security misconfiguration is a security weakness that occurs when an application or its components are not configured correctly or securely, leaving them vulnerable to attacks. Plain and simple, any technical issue across any component that may arise from the app's configuration settings and not bad code is classified as a security misconfiguration vulnerability.

As a developer, you play a critical role in preventing them by implementing these key strategies:

  • Limit access to configuration files and interfaces to authorized personnel only.

  • Utilize tools like Chef, Puppet, or Ansible to automate configuration deployment and management across environments.

  • Regularly review and update cloud service configurations to adhere to security best practices.

  • Document configuration settings clearly and maintain detailed records.

A06:2021-Vulnerable and Outdated Components

Vulnerable and outdated components are security weaknesses resulting from using web application components, such as libraries, modules, plugins, or APIs, that have known vulnerabilities or are no longer supported or maintained.

To prevent attacks from vulnerable and outdated components, follow these tips: 

  • Employ tools like Maven, npm, or NuGet to automatically track and manage dependencies, simplifying vulnerability identification and updates.

  • Obtain components from official sources or trusted repositories to minimize the risk of malware or malicious code.

  • Create a comprehensive list of all software components used in your application, including their versions, dependencies, and origin sources. This provides a clear picture of your attack surface.

A07:2021-Identification and Authentication Failures

Identification and authentication failures are security weaknesses that occur when an application does not properly verify or validate the identity or credentials of users or entities. Broken authentication allows hackers to bypass authentication controls, steal someone's identity to log in, or break other security mechanisms. 

Here are some key strategies to prevent these failures:

  • Utilize rate limiting on login attempts to thwart automated attacks. Consider CAPTCHAs or risk-based authentication for suspicious activity.

  • Enforce strong password policies, including minimum length, complexity, and regular rotation. Encourage multi-factor authentication (MFA) for added security.

  • Secure APIs with strong authentication and authorization mechanisms like OAuth or API keys.

  • Monitor login attempts and system activity for suspicious behavior. Logs can help identify and respond to potential attacks.

A08:2021-Software and Data Integrity Failures

Software and data integrity failures relate to code and infrastructure that do not correctly protect or verify the integrity or authenticity of software or data. This vulnerability can lead to severe consequences, including data corruption, loss of customer trust, and regulatory non-compliance.

Preventing software and data integrity failures is crucial for maintaining the reliability and trustworthiness of systems and information. Here are some strategies to mitigate the risk of such failures:

  • Enforce strict access controls to limit access to sensitive data and critical system components.

  • Encrypt sensitive data stored on disks or databases to prevent unauthorized access and tampering.

  • Establish a comprehensive backup and recovery plan to ensure data availability and integrity in case of incidents.

A09:2021-Security Logging and Monitoring Failures

Security logging and monitoring failures cover the lack of proper logging and monitoring mechanisms in web applications, which can prevent or delay the detection and response to security incidents. Failing to log crucial events like login attempts, access control changes, and server errors leaves you blind to potential attacks.

Here are some tips to prevent security logging and monitoring failures: 

  • Capture essential activities like logins, failed attempts, file changes, database queries, and API calls.

  • Leverage centralized log management systems such as Elasticsearch and Splunk for efficient analysis, storage, and search capabilities.

  • Utilize tools to analyze logs for suspicious activity based on user behavior, access patterns, and known attack signatures.

A10:2021-Server-Side Request Forgery

Server-side request forgery (SSRF) is a web security vulnerability that allows an attacker to cause the server-side application to make requests to an unintended location. In a typical SSRF attack:

  1. An attacker sends a malicious request containing a URL or an instruction for the server to a vulnerable application.

  2. The server trusts the request and tries to access the specified resource, which could be internal or external.

  3. The attacker gains unauthorized access to the resource and can steal, modify, or compromise data or systems.

SSRF attacks are dangerous because they are used to bypass security controls and access resources that the attacker shouldn't be able to reach. This can lead to security incidents such as data breaches and service disruptions.

To prevent SSRF, developers should: 

  • Validate and sanitize user input before constructing or executing server-side requests.

  • Maintain a whitelist of allowed URLs or domains the application can access, preventing requests to unauthorized or internal resources.

  • Monitor your application's outbound traffic for suspicious requests, looking for unusual destinations, protocols, or data patterns.

The code snippet below demonstrates a vulnerability to a full SSRF attack. It directly constructs a URL using untrusted input from an HTTP request parameter. By setting the target value to “bad.com#,” the resulting URL becomes “https://bad.com#.example.com/data/.” The snippet also provides a solution by allowing the user to select a known fixed string instead.

import requests
from flask import Flask, request

app = Flask(__name__)

def full_ssrf():
    target = request.args["target"]

    # Vulnerable: User input directly constructs the URL
    resp = requests.get("https://" + target + ".example.com/data/")

    # Secure: Use a fixed string based on user input
    subdomain = "europe" if target == "EU" else "world"
    resp = requests.get("https://" + subdomain + ".example.com/data/")

How Codacy Can Help Prevent OWASP Top 10 Vulnerabilities

Codacy can help prevent OWASP Top 10 vulnerabilities by providing automated code analysis, quality, and security tools that scan over 40 programming languages, such as JavaScript, Python, Java, C#, and PHP. Codacy can help developers:

Using Codacy, you can reduce the time and effort required to prevent OWASP Top 10 vulnerabilities and focus on delivering secure and robust web applications. 

Learn more about how Codacy can help you prevent OWASP's Top 10 vulnerabilities by signing up for a free trial.


How to Ensure Security Compliance in Modern Software Development
Non-compliance is a concept that should send a chill down the spine of any product leader. It can lead to genuinely company-ending ramifications. In...
Why OWASP Matters In Modern Web Development
Threats are pervasive in modern software development. These threats don’t just come from 1337 h4x0rz; they come from you. The sophistication of modern...
A Deep Dive Into OWASP Dependency-Check
Modern software heavily relies on open-source libraries and tools. GitHub reports that over 90% of modern applications leverage them to accelerate...

Automate code
reviews on your commits and pull request

Group 13