DEV Community

Cover image for Top 8 Mistakes New Coders Make (and How to Avoid Them Like a Pro)
Gaurav Chaudhary
Gaurav Chaudhary

Posted on

Top 8 Mistakes New Coders Make (and How to Avoid Them Like a Pro)

Ah, the wild world of web development. It's a land of endless possibilities, fueled by caffeine and fueled by a burning desire to create something awesome. But for new coders, this thrilling journey can also be fraught with hidden dangers – security vulnerabilities lurking in the shadows of your code. Fear not, grasshopper coders! We've all been there, and I'm here to share the eight most common security mistakes that can leave your website vulnerable like a sandcastle during a hurricane.

1. Insecure Input Validation: Leaving the Backdoor Wide Open

Imagine this scenario: You've built a login system for your brand new e-commerce website. Users can sign in with a username and password. But what happens if a malicious attacker tries to enter more than just a username and password?

The Mistake: Let's say your login form only expects a username and password, but it doesn't validate what the user actually types in those fields. An attacker could try entering code into the username field that exploits a vulnerability called SQL injection. This code could trick your website into revealing sensitive information or even taking control of your entire database!

How to Avoid It: Always validate all user input! Make sure the data entered matches the expected format. For instance, a username field should only accept letters, numbers, and underscores, and it shouldn't be longer than a certain character limit. Here's an example of insecure code in PHP:

PHP

$username = $_POST['username'];
$password = $_POST['password'];

$sql = "SELECT * FROM users WHERE username = '$username' AND password = '$password'";

$result = mysqli_query($conn, $sql);

Enter fullscreen mode Exit fullscreen mode

This code simply takes the username and password from the login form and inserts them directly into a SQL query. An attacker could exploit this by entering a username like ' OR '1'='1'-- - this might seem like gibberish, but it tricks the SQL statement into returning all users from the database, potentially giving the attacker access to usernames and passwords!

Improved Code: To prevent this, we can use prepared statements:

PHP

$stmt = mysqli_prepare($conn, "SELECT * FROM users WHERE username = ? AND password = ?");
mysqli_stmt_bind_param($stmt, "ss", $username, $password);
mysqli_stmt_execute($stmt);

$result = mysqli_stmt_get_result($stmt);

Enter fullscreen mode Exit fullscreen mode

Prepared statements separate the data from the SQL query, preventing malicious code injection.

2. Weak Passwords: The Key to Your Kingdom (Given Away for Free)

The Mistake: Let's say you allow users to create passwords for your website, but there are no restrictions on password length or complexity. Users might choose passwords like "password123" or their pet's name. These passwords are easy to guess or crack using brute-force attacks, leaving your user accounts vulnerable.

How to Avoid It: Enforce strong password policies. Here are some tips:

  • Minimum password length: Require passwords to be at least 12 characters long.
  • Character requirements: Insist on a combination of uppercase and lowercase letters, numbers, and symbols.
  • Password history: Prevent users from reusing their old passwords.
  • Hashing: Store passwords securely using a hashing algorithm. Hashing transforms a password into a scrambled string that cannot be easily reversed.

3. Encryption? Never Heard of Her: Keeping Your Data in Plain Sight

Imagine this: Your e-commerce website collects customer credit card information for purchases. But unfortunately, this data is stored on your server in plain text – anyone who hacks into your system can see it all!

The Mistake: Neglecting to encrypt sensitive data like credit card details, login credentials, and other private information leaves it vulnerable to theft.

How to Avoid It: Always encrypt sensitive data! Here's what you need to do:

  • HTTPS: Use HTTPS (Hypertext Transfer Protocol Secure) to encrypt communication between your website and users' browsers. This scrambles data in transit, making it unreadable to anyone intercepting it.
  • Encryption Algorithms: Use strong encryption algorithms like AES (Advanced Encryption Standard) to encrypt data at rest on your server. This ensures that even if an attacker gains access to your database, they cannot decrypt the sensitive information.

4. Outdated Software: A Feast for Hackers

The Mistake: Let's say you built your website using a specific development framework. Over time, vulnerabilities are discovered in that framework, and security patches are released. But you never update the framework because you think "it's working fine, why mess with it?" This is a dangerous misconception.

How to Avoid It: Make updating software a priority! Here's how to stay on top of things:

  • Enable Automatic Updates: Whenever possible, enable automatic updates for your development environment, frameworks, libraries, and plugins. This ensures you have the latest security patches as soon as they become available.
  • Regular Reviews: Schedule regular reviews to check for updates for all the software you use. Don't wait until something goes wrong!

5. DIY Security: Don't Be a Hero, Use Established Tools

The Mistake: You're building a new social media platform and decide to write your own custom login system from scratch. You think it'll be a fun challenge, and how hard can it be, right? Well, security is a complex field, and building secure systems from the ground up is a recipe for disaster.

How to Avoid It: There's no need to reinvent the wheel! Use established security libraries and frameworks that have been rigorously tested and battle-proven by experts. These libraries handle common security tasks like authentication, authorization, and encryption, saving you time and effort while boosting your website's security posture.

6. Ignoring Security Best Practices: Knowledge is Power (and Protection)

The Mistake: You're laser-focused on building new features and functionalities for your website, and security just isn't on your radar. There are so many resources available online about web security best practices, but you just haven't had the time to delve into them.

How to Avoid It: Invest time in learning about web security! Here are some resources to get you started:

  • OWASP (Open Web Application Security Project): https://owasp.org/ A nonprofit organization that provides free resources, tools, and articles on web security best practices.
  • SANS Institute: https://www.sans.org/ - Offers a variety of security courses and certifications for developers.
  • Security Blogs and Podcasts: Stay up-to-date on the latest security threats and vulnerabilities by following security blogs and podcasts from reputable sources.

7. Blind Trust in Third-Party Code: Not Everything Sparkles is Gold

The Mistake: Building a new e-commerce website, you find a free plugin that adds a shopping cart functionality. It seems perfect, and you quickly integrate it into your website without giving it much thought. The problem? This plugin might have hidden vulnerabilities that attackers can exploit to compromise your website.

How to Avoid It: Approach third-party code with a healthy dose of skepticism. Here are some steps to take:

  • Research the Developer: Check the reputation of the developer who created the plugin. Look for reviews and see if they have a history of maintaining and patching their code.
  • Security Audits: If possible, see if the code has been audited by a security professional.
  • Stay Updated: Keep third-party plugins and libraries up-to-date to benefit from the latest security fixes.

8. Forgetting to Test, Test, Test: Security Vulnerabilities Love to Hide

The Mistake: You've finally finished building your website and launch it with pride. Everything seems to be working perfectly! But have you considered security? You haven't conducted any security testing, so you might have vulnerabilities lurking beneath the surface.

How to Avoid It: Make security testing an integral part of your development process. Here are some ways to find and fix vulnerabilities:

  • Security Scans: Use automated security scanners to identify potential vulnerabilities in your code.
  • Penetration Testing: Hire a penetration tester (ethical hacker) to simulate real-world attacks and try to break into your website.
  • Manual Code Reviews: Regularly review your code for common security vulnerabilities.

Remember, security is an ongoing process, not a one-time fix. By following these tips and avoiding common mistakes, you can build websites that are not only functional and beautiful but also fortresses against the ever-present threats of the digital world. Happy coding, and code securely!

Top comments (0)