Secure Coding Best Practices for 2022
Written by Sigal Zigelboim   
Monday, 19 September 2022
Article Index
Secure Coding Best Practices for 2022
Securing the CI/CD Pipeline

In this era of vulnerability to cyber attack developers DevOps teams need to step up. Here we explore the  concept of secure coding and provide several best practices that can help developers level up their skills and adopt secure coding.


What Is Secure Coding?

Secure coding involves practices, techniques, and tools that prevent accidental or malicious introduction of security vulnerabilities into software code. Threat actors often look for defects, logic flaws, and bugs they can exploit in software applications.  

Identifying insecure coding practices that lead to these errors, and implementing secure coding practices, can reduce vulnerabilities and eliminate security problems long before software is deployed to production. This can reduce the attack surface, minimize risks, and reduce the impact of a breach even in case of a successful attack. 

Secure Coding Statistics

Secure Code Warrior has released the results of its 2022 survey on the state of developer-centric security. The findings suggest that developers have conflicting security attitudes and behaviors. Most developers recognize the importance of a security-centric approach to software development; 86% don’t prioritize application security when coding. 

According to the survey, over half of the developers failed to secure their code against the top seven vulnerabilities. Secure Code Warrior conducted this study to evaluate how developers can adopt proactive, secure coding practices. 

Developers must address several priorities, with many administrative barriers preventing them from writing secure code early in the SDLC. Developers often prioritize deadlines or lack the guidance and training to write secure code. 

Training significantly impacts the development team’s adoption of secure coding practices, with 81% of developers applying the knowledge gained from training in their daily work. Despite implementing the mechanisms from training, 67% of developers knowingly shipped vulnerable code. Many developers expressed a desire for further training. 

Secure Coding Best Practices

Here are critical best practices developers must know to ensure their code and the surrounding environment are secure. 

Shift-left Security

Shifting security left enables prioritizing security across the entire software development lifecycle (SDLC). It helps discover and remediate flaws early on, minimizing the probability of personnel ignoring security issues because they are forced to meet unforgiving time schedules.  

Implementing security as an integral part of the development process enables teams to assess the risks of dependencies and open source components before including them. It also helps ensure versioning is consistent throughout the project.  

Teams can leverage information from sources like the OWASP Top 10 list to get professional guidance on vulnerability management and use tools to make this process faster and more efficient. Here are common shift-left security tools: 

  • Static application security testing (SAST)—teams can use SAST tools to scan source code for known security vulnerabilities. SAST tools scan code at rest, early in the SDLC, typically before the code is compiled. The goal is to pinpoint issues to help teams fix them while coding.

  • Dynamic application security testing (DAST)—unlike SAST tools that scan code at rest, DAST tools can analyze source code during runtime or after compilation. Teams can use SAST tools to detect security issues missed by static scans. It can help identify security vulnerabilities like runtime errors and SQL injection (SQLi).

  • Software composition analysis (SCA)—these automated tools scan source code to identify open source components and third-party libraries. SCA tools can detect known vulnerabilities and notify teams of available updates or patches. 

Validation of Inputs and Encoding of Outputs

User inputs and encoding outputs can cause severe vulnerabilities. Here are common best practices to help mitigate this threat: 

  • Validate all user inputs according to syntax and semantic criteria.

  • Parameterize queries or identify them as user input. 

  • Use allow-listing for validation through comparison to regular expressions. 

  • Encode outputs via entities like HTML or JavaScript hex to nullify scripting commands and eliminate potentially harmful characters. 

  • Use server-side validation and encoding to maintain coverage of all components, including back-end web services and user devices. Combined, these practices can help prevent cross-site scripting (XSS) and code injection attacks.  

Error Handling and Logging

Error handling processes deal with unexpected outcomes that occur when a program is given unusual input. Error handling problems can expose sensitive information. Teams can mitigate this risk by using logging to maintain a record of changes made to applications.  

For example, a threat actor can search for individuals using the same credentials so they could use these credentials to obtain access to many accounts. If there is no logging or tracking of these event occurrences, actors can keep performing these attacks using new credentials. 

Here are common errors handling and logging approaches: 

  • Always use error handlers that do not display stack trace or debugging information.

  • Free allocated memory properly when error conditions occur.

  • Use logging controls that support the success as well as failure of specified security events.

  • Log all input validation failures and authentication attempts.

  • Implement a cryptographic hash function to validate the integrity of log entries.

  • Define generic error messages and set up custom error pages. 

sec code warrior

Last Updated ( Friday, 07 October 2022 )