Secure Coding Practices in Java

OWASP, or the Open Web Application Security Project, stands as a vital resource in the realm of web application security. Established as an online community with a nonprofit foundation, its mission revolves around creating freely accessible knowledge, methodologies, documentation, tools, and technologies to bolster the security of web applications.

The recent Java Zero-day attack, which occurred on January 18, 2022, served as a stark reminder of the persistent threats web applications face. This particular exploit was found in the widely used “log4j” library, underscoring the importance of vigilance in safeguarding our digital assets. By adhering to the standards outlined in documents like the Top 10 awareness principles for web application security, developers can mitigate potential vulnerabilities and protect their applications.

Java, with its popular frameworks like Quarkus, Spring, and SpringBoot, plays a pivotal role in web application development. It is crucial to pay heed to security considerations while using these frameworks. Guarding against injection attacks, validating inputs, and preventing inclusion attacks are essential measures every developer should be well-versed in.

Avoid injection:

  • We recommend canonicalizing inputs, validating only one representation and converting to data type to force an implicit validation. For example, a number test with int or long or float whether case it is. In text, we need to convert one encoding type, like UTF-8.
  • Validate input: Encoding can bypass a blocklist. Use the allowed list over the blocklist.

An inclusion attack is a web that includes a malicious resource to be executed on that platform.

  • Don’t load untrusted input to load resources.
  • Load resources locally,  using an allowed list.
  • Load remote resources over HTTPS.
  • Use secure third-party software. 

Java and denial of service.

How to mitigate a denial of service.

  • Define and enforce limits. For example, Hashmap (application caching) could be a memory leak for the long term. This action helps to define a limit in the memory ram that the process consumes or the heap size memory. Don’t load files or stream in memory without a limit on granularity definition. Overflow on data types. Things and design reactive to forecast the increment in data for a variable. So we could take action to avoid overflow on data types.
  •  Protect against overflow. For example, trunk the resources or fail the request, forcing the end to avoid overflow.
  • Clean up or Pool resources. Memory management. Database connection, accessing a file. Remember to close the resource.

Spring Boot is a Java framework used to create web applications, cron job applications, REST API applications, and GraphQl applications. It is easy to configure and lets you do the work in a more agile way.

Spring Boot lets you add headers to use the standard content-security-policy to prevent cross-site scripting. Each layer acts like a middleware or filter for each HTTP request, so if we use “strict http firewall check” the web application will reject URLs that are not normalized to avoid bypassing security constraints, Rejects HTTP methods that are not allowed, and implements rules that enforce added security to sanitize and protect us against potentially harmful crafted URLs.

These filters are integrated into Spring Boot, and you also have the flexibility to tailor and enforce your own rules and security restrictions for each HTTP request by leveraging the Spring Boot Security library. This is made possible by the robust security architecture inherent in Spring Boot applications.

For instance, you have the capability to construct and fine-tune your custom filters. As illustrated in the diagram below, each HTTP request must meet specific conditions to successfully traverse the security filter, ultimately reaching the dispatcher responsible for generating an HTTP response. This level of customization empowers you to define and implement the security measures that align with your application’s requirements.

Lastly, it is imperative to remember the significance of TLS/SSL in securing data communication. By using encryption protocols like TLS/SSL, we can safeguard sensitive information, thwart potential man-in-the-middle attacks, and maintain the privacy of data shared with both clients and third-party software. 

In a rapidly evolving digital landscape, staying informed and proactive in web application security is an ongoing commitment. The resources and practices outlined in this article, combined with the mission of organizations like OWASP, provide a strong foundation for developers to build and maintain secure web applications.

You may also like

Spring Boot

Spring Boot 1 to Spring Boot 2

Modifiability

Modifiability – What is it? And some Java Solutions

How to Build Custom Java Annotations

Menu