keepcalculator

06 Numbers and Powers

Prevent Security Vulnerabilities in AI-Written Code

Card No. 06·56379 · Updated · Standard formula, shown below

AI-written code can speed up development, but it may also contain unsafe assumptions, outdated patterns, or flaws that are difficult to spot in a quick review. Use the steps below to treat generated code as untrusted, check it systematically, and reduce the chance that a defect reaches production. You will need access to the code, its dependencies, and the project’s normal test and review tools.

What You’ll Need

  • The generated code and its surrounding project, including configuration and dependency files.
  • A development environment where you can run tests without exposing production data or secrets.
  • Code review and version-control tools, plus available static analysis, dependency scanning, and secret-scanning checks.
  • A clear description of what the code should do, which data it handles, and who or what can access it.

Step-by-Step

  1. Define the security requirements first. Before asking for or accepting code, describe its inputs, outputs, users, permissions, and trust boundaries. Specify relevant requirements such as authentication, authorization, data validation, privacy, and error handling. Identify sensitive data and the consequences of misuse. This gives reviewers something concrete to compare against; code that merely passes a functional example may still violate a security requirement.
  2. Keep generated code out of production until reviewed. Work on a branch or in an isolated development environment. Do not paste real credentials, private keys, personal records, or proprietary data into a tool unless its approved handling rules explicitly permit it. Use synthetic examples instead. Treat suggestions from a model, including claims that a library or function is safe, as unverified until you check authoritative project documentation.
  3. Inspect the whole change, not just the new function. Read the code line by line and compare the diff with the original. Check imports, configuration changes, build scripts, and dependency-file edits as well as the main logic. Ask what happens with empty, malformed, unusually large, or unexpected input; whether callers can bypass a check; and whether errors reveal sensitive details. Remove code you cannot explain or justify.
  4. Review common high-risk patterns. Confirm that untrusted input is validated at the system boundary and safely encoded or parameterized for its destination. For database queries, prefer parameterized APIs over string concatenation. For web output, use context-appropriate escaping and established framework protections. Check file paths against traversal, avoid unsafe deserialization and dynamic code execution, and do not construct shell commands from user-controlled strings. Use maintained cryptographic libraries rather than custom algorithms, and verify that authorization is enforced on the server for each protected operation.
  5. Protect secrets and access. Search the change and repository for passwords, tokens, private keys, and connection strings. Read secrets from an approved secret-management mechanism or protected runtime configuration, not from source code. If a real secret was committed or shared, remove it from future code and rotate or revoke it; deleting the visible line alone does not invalidate a leaked credential. Give the code and its service account only the permissions they need.
  6. Check dependencies and generated commands. Review every new or changed package: verify its identity, purpose, source, maintenance status, and version against trusted project or ecosystem information. Prefer established dependencies already used by the project when they meet the need. Run the repository’s dependency vulnerability checks and review their results. Do not execute a generated shell command, migration, or installation script blindly; understand its effects and test it in an isolated environment first.
  7. Run layered checks. Run the project’s tests, including tests for invalid input, denied access, and failure paths. Add focused tests for security requirements rather than relying only on happy-path tests. Run available static analysis, dependency scanning, and secret scanning; review warnings instead of suppressing them automatically. These tools help identify risks but cannot prove code is secure, so keep human review in the loop.
  8. Use a review gate before release. Ask a qualified reviewer to inspect security-sensitive changes, especially authentication, authorization, cryptography, data access, and input handling. Record unresolved findings and fix or formally assess them before merging. Deploy through the normal release process, monitor relevant errors and access patterns, and keep a rollback plan. Recheck generated changes when requirements, dependencies, or surrounding code change.

Common Mistakes

  • Trusting confident explanations. A plausible description is not evidence. Verify behavior in code, tests, and documentation.
  • Reviewing only the visible snippet. Vulnerabilities can come from a new dependency, changed permissions, unsafe configuration, or how another component calls the code.
  • Treating tests or scanners as a guarantee. Tests cover only chosen cases, and scanners have blind spots and false positives. Combine automated checks with review.
  • Silencing warnings without investigation. Confirm whether a finding is applicable, document the reason, and choose a safer fix where needed.
  • Leaving exposed credentials active. Removing a secret from the latest commit does not undo exposure. Revoke or rotate it and assess where it may have been copied.

Tips

  • Ask for small, focused changes with explicit security constraints, then review each change separately.
  • Reuse the project’s established validation, authentication, logging, and database patterns instead of inventing alternatives.
  • Use test data that resembles edge cases without containing real sensitive information.
  • Keep dependencies and security checks integrated into the normal development workflow so they run consistently.
  • For complex or high-impact code, involve a security specialist before deployment rather than relying on a prompt or checklist alone.

Troubleshooting

A scanner reports a vulnerability: identify the affected file, dependency, and reachable code path. Update or replace a vulnerable dependency where practical, then rerun tests and scans. If the finding appears irrelevant, document evidence and have a reviewer confirm the assessment; do not suppress it just to obtain a clean report.

Tests pass but the code still feels unsafe: check whether tests cover authorization failures, malformed input, boundary conditions, and error handling. Add tests for the missing requirement and review how the code behaves in its real calling context. Passing tests demonstrate only the cases they exercise.

You cannot explain a generated section: pause before merging. Ask for a simpler implementation or rewrite it using familiar project patterns, then review and test the replacement. Unclear code is harder to maintain and audit.

A secret or unsafe change has already been shared: restrict access to the affected repository or environment, rotate exposed credentials, and notify the appropriate project or security contact. Preserve enough information to investigate, then remove the unsafe change and verify the fix in the release path.

Frequently Asked Questions

Can AI-generated code be secure?
It can be made safer through sound design, careful review, testing, and maintenance, but its origin does not guarantee security.

Is a security scan enough before release?
No. Scans can miss logic flaws and context-specific problems. Combine them with tests, dependency review, and human code review.

Should I paste private code into an AI tool?
Only if your organization’s policies and the tool’s approved data-handling terms allow it. Otherwise, use synthetic examples or an approved environment.

What should I do if generated code adds a package?
Confirm the package’s identity and purpose, check its maintenance and known vulnerabilities, and decide whether the project truly needs it before accepting the change.

How should I handle a false-positive security finding?
Verify the affected code path and document why the issue does not apply. Get an appropriate reviewer to confirm before recording an exception or suppressing the warning.

Does human review make the code safe?
Review reduces risk but cannot guarantee it. Keep automated checks, testing, least-privilege access, and ongoing updates in place.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →