Web App Security: Best Practices for Junior Developers

Kenny Wolf
Geek Talk
Published in
6 min readJan 20, 2024
Web Application Security Cover Image

In this article, we’re going to talk about making sure your web applications are safe and secure.

Specifically designed for Fullstack Developers, especially those who are just starting out, we’ll look into simple ways to write code that keeps your web application safe. We’ll also break down the idea of keeping your information private through encryption and share some practical advice to protect your projects from common online problems. By the end of this quick guide, you’ll have a good understanding of how to keep your websites secure.

I’m a Junior Software Engineer working in Switzerland, and I’ll share some real-world tips and examples to help you make your websites safe and strong. Count on this information to make your applications stronger and improve your skills in creating safe and reliable websites.

Secure Coding Principles

In the world of web development, secure coding is like building a sturdy fortress around your application to keep it safe from potential threats.

It’s not just about making things work; it’s about making them work safely.

Secure coding principles form the backbone of a robust defense system for your web application. To understand its significance, think of secure coding as using a strong lock on the door of your house. You wouldn’t want just any lock; you’d want one that’s tough to pick or break.

Similarly, in web development, secure coding ensures that the code you write is resilient against attacks.

For instance, in Java, a common insecure coding practice is using string concatenation to build SQL queries, which can make your application vulnerable to SQL injection. Instead, secure coding promotes the use of parameterized queries, acting like a fortified lock to prevent unauthorized access to your database.

public class InsecureCodeExample {

public String getUserData(String username) {
String query = "SELECT * FROM users WHERE username = '" + username + "'";
// Execute the query and retrieve user data
// ...
return userData;
}

}

In the above insecure example, the SQL query is constructed by directly concatenating the user input (username) into the query string. This approach is susceptible to SQL injection, where malicious users can manipulate the input to execute unauthorized SQL commands.

public class SecureCodeExample {

public String getUserData(String username) {
String query = "SELECT * FROM users WHERE username = ?";
try (Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/database", "user", "password");
PreparedStatement preparedStatement = connection.prepareStatement(query)) {

preparedStatement.setString(1, username);
ResultSet resultSet = preparedStatement.executeQuery();

// Process the result set and retrieve user data
// ...

return userData;

} catch (SQLException e) {
e.printStackTrace();
// Handle the exception
}

return null;
}

}

In the secure example, a parameterized query is used with a PreparedStatement. This approach separates user input from the SQL query, making it immune to SQL injection attacks. The setString method is employed to safely bind the user input to the query. This secure coding practice enhances the security of the web application by preventing unauthorized database access through manipulation of input data.

Now, let’s explore a bit more.

Secure coding also means avoiding the temptation of using hardcoded passwords in your code. It’s like leaving the key under the doormat — it might be convenient, but it’s a huge security risk. Instead, you can use secure methods like hashing and salting to protect sensitive information.

By adopting these secure coding practices, you’re essentially building a digital stronghold for your web application, making it a much tougher target for cyber threats.

Data Encryption Essentials

Protection Against Common Web Vulnerabilities

In the world of web development, understanding and addressing common vulnerabilities is crucial for ensuring the safety of your applications.

Two prevalent threats, Cross-Site Scripting (XSS) and SQL injection, pose serious risks to web applications if not properly guarded against.

Cross-Site Scripting (XSS)

Cross-Site Scripting occurs when attackers inject malicious scripts into web pages viewed by other users.

Imagine a scenario where a user inputs a comment on a blog, and if the application doesn’t validate and sanitize that input, an attacker could insert harmful scripts that run when another user views the comment. In Java, one preventive measure is to use libraries like OWASP Java Encoder, which sanitizes user input to eliminate potential XSS threats.

By incorporating such tools and validating user input, developers can create a shield against XSS attacks.

SQL Injection

SQL injection is another common vulnerability where attackers manipulate input to execute unauthorized SQL commands, gaining access to sensitive data.

In Java, consider a scenario where user input is used to construct an SQL query, similar to the insecure example mentioned earlier. To prevent SQL injection, developers should employ parameterized queries using prepared statements, as shown in the secure code example in a previous section.

This practice ensures that user input is treated as data, not as executable code, fortifying the application against SQL injection attacks.

By addressing these vulnerabilities with practical solutions like input validation, data sanitization, and proper coding practices, developers can significantly enhance the security of their web applications and protect user data from potential exploits.

If you want to know about Common Web Vulnerabilities, the OWASP Top 10 is a great place to start. I wrote a 2-part article series of OWASP which you can find here.

Practical Tools for Implementation

Now that we’ve explored essential security practices, let’s delve into some practical tools that can make implementing these measures a smoother process for Junior Developers.

One such tool is the OWASP Dependency-Check for Java, which helps identify and manage project dependencies with known vulnerabilities. It acts like a vigilant guardian, notifying developers of potential risks associated with the libraries they use. By regularly running Dependency-Check, Junior Developers can ensure they are using secure and up-to-date dependencies, fortifying their applications against known vulnerabilities.

Integrating Security Tools into Development Workflow

Integrating security tools into your development workflow doesn’t have to be a daunting task.

Take the OWASP ZAP (Zed Attack Proxy) tool, for instance. This tool can be seamlessly integrated into your development process to identify security vulnerabilities during testing. By automating security scans with tools like ZAP, Junior Developers can catch potential threats early in the development cycle, allowing them to address issues before they become major concerns.

The user-friendly interface of ZAP makes it accessible for developers, even those who are just starting out, ensuring that security becomes an integral part of their coding routine.

Emphasizing Ease of Use and Effectiveness: A Developer’s Ally

For Junior Developers, tools like SonarQube provide an effective means to enhance code quality and security.

It analyzes code for potential vulnerabilities, providing clear and actionable feedback. Imagine it as a helpful mentor, guiding developers towards writing cleaner and more secure code. With its easy-to-understand reports, SonarQube becomes an ally for Junior Developers, supporting them in building applications that not only function well but also stand strong against potential security threats.

By incorporating these tools into their toolkit, Junior Developers can bolster their applications’ defenses without feeling overwhelmed.

Conclusion

In wrapping up our exploration of web application security for Fullstack Developers, it’s crucial to recognize that while many frameworks do incorporate robust security measures, understanding common vulnerabilities and best practices remains fundamental for developers.

The insights shared in this article serve as a foundational guide for both seasoned developers and those just embarking on their coding journey.

While we’ve covered key aspects like secure coding principles, protection against common web vulnerabilities, and practical tools for implementation, it’s essential to acknowledge the vast landscape of web security. Numerous other vulnerabilities and best practices exist, but delving into each would make this article exhaustive.

Remember, web development is a dynamic field, and staying informed about security is an ongoing process.

Even as frameworks evolve, having a solid grasp of the fundamentals allows developers to make informed decisions and contribute to the overall resilience of their applications. By embracing secure coding practices, understanding potential threats, and incorporating practical tools into their workflows, developers can actively contribute to a more secure online environment.

As we navigate the ever-evolving landscape of web development, let this article serve as a compass, guiding you toward creating web applications that are not only functional and innovative but also robustly secure.

--

--

Kenny Wolf
Geek Talk

I write about tech, software development and hacking for non-techies and geeks 🤓 | Software Developer 👾 | Interested in pentesting 👹