coding4 min read

To Write Secure Code, Be Less Gullible Than Your AI

Learn how to write secure code by critically evaluating AI-generated solutions, focusing on tooling, readability, and best practices.

Kevin Liu profile picture

Kevin Liu

November 10, 2025

To Write Secure Code, Be Less Gullible Than Your AI

Can AI-Generated Code Be Trusted for Security?

In the dynamic world of software development, the rise of AI tools in coding has sparked a mix of excitement and concern. Can these AI-generated code snippets be trusted for security? Ryan and Greg Foster, the CTO of Graphite, delve into this critical issue, urging developers to critically evaluate AI-generated code.

What Are the Limitations of AI in Generating Secure Code?

AI has revolutionized code writing, yet it falls short in crucial areas. These tools often miss the security context essential for secure applications. Key limitations include:

  • Lack of Context Awareness: AI fails to fully understand the code it generates.
  • Introduction of Security Vulnerabilities: AI might add security flaws that a human developer would typically avoid.
  • Complexity in Readability: AI-generated code can be hard to decipher for humans.

How Reliable Is AI-Generated Code?

So, how much trust should developers place in AI-generated code? The answer requires a balanced view. While AI boosts efficiency, its limitations cannot be ignored. To improve code security, consider these strategies:

  1. Review and Refactor: Always scrutinize AI-generated code for security gaps and make necessary adjustments.
  2. Leverage Static Analysis Tools: Use tools like ESLint or SonarQube to identify security issues early.
  3. Implement Best Practices: Adhere to secure coding standards, such as the OWASP Top Ten.
  4. Conduct Thorough Testing: Use unit and integration tests to ensure code security and functionality.

Why Is Tooling Essential for Secure Coding?

Greg Foster highlights the significance of tooling in code security. Regardless of whether developers use AI-assisted tools or traditional methods, selecting the right technologies is crucial. Essential tools include:

Essential Tools for Developers

  • Snyk: Finds and fixes vulnerabilities in dependencies.
  • OWASP Dependency-Check: Detects known vulnerabilities in project dependencies.
  • Burp Suite: Offers comprehensive web application security testing.

How Does Readability Contribute to Security?

Readability plays a vital role in secure coding. Clear code allows developers to spot potential issues more easily. Greg and Ryan suggest ways to enhance readability:

  • Adopt Naming Conventions: Choose clear, descriptive names for variables and functions.
  • Comment Thoughtfully: Use comments to clarify complex logic or highlight security considerations.
  • Maintain Consistent Formatting: Keep a uniform style throughout the codebase for better clarity.

Secure Coding Example in React

Consider a React example to illustrate secure form submission:

import React, { useState } from 'react';

const SecureForm = () => {
  const [input, setInput] = useState('');

  const handleSubmit = (event) => {
    event.preventDefault();
    // Validate input to prevent XSS
    const sanitizedInput = input.replace(/<[^>]*>/g, '');
    console.log('Submitting:', sanitizedInput);
  };

  return (
    <form onSubmit={handleSubmit}>
      <input
        type="text"
        value={input}
        onChange={(e) => setInput(e.target.value)}
        placeholder="Enter your input"
      />
      <button type="submit">Submit</button>
    </form>
  );
};

export default SecureForm;

This example demonstrates the importance of validating and sanitizing data to prevent cross-site scripting (XSS) attacks, showcasing secure coding practices.

Conclusion: Trust with Caution

In summary, securing code in an AI-dominated era demands a judicious approach. Developers should leverage AI for its efficiency but remain cautious of its limitations. By integrating AI tools with stringent security measures, developers can secure their code while ensuring it remains readable.

The aim is to create code that is not only functional but also secure, maintainable, and clear. Trust your judgment, critically assess AI outputs, and always prioritize security.

Related Articles