keepcalculator

06 Numbers and Powers

How to Keep AI Coding Tools From Introducing Security Risks

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

AI coding tools can speed up development, but their suggestions may contain vulnerable patterns or expose sensitive information. A review-and-test workflow helps you use generated code while keeping security decisions under your control; you will need access to your project’s tests and a way to review changes before merging.

What You’ll Need

  • A version-control repository with a clean working tree, so generated changes are easy to inspect and undo.
  • A current development environment and the project’s normal test commands.
  • Dependency and security checks appropriate to your language, such as a package audit or static-analysis tool.
  • A clear policy for what information may be sent to the coding tool. Check your organization’s rules before using it with work code.

Step-by-Step

  1. Set boundaries before prompting. Identify the files and task the tool may work on. Do not provide passwords, API keys, private keys, production data, customer records, or confidential code unless your organization has approved that use and the tool’s data handling is understood. Prefer a small, specific request such as “Add input validation to this function” over asking for a broad rewrite.
  2. Start from a clean, reviewable state. Commit or otherwise save existing work, then check the repository status. Use a separate branch when practical. This makes it possible to see exactly what the tool changed and revert its work without losing unrelated edits.
  3. Limit the change. Ask for one feature or fix at a time, and name relevant constraints: preserve authentication checks, use parameterized database queries, validate inputs, and avoid adding dependencies unless needed. Treat these instructions as guidance, not proof that the resulting code is safe.
  4. Inspect every changed line. Review the full diff, including configuration, tests, generated files, and dependency manifests. Look for unexpected network calls, shell commands, file access, permission changes, weakened checks, disabled tests, and secrets. Read surrounding code to confirm the change follows the application’s security model.
  5. Check risky code paths deliberately. Pay special attention to user input, authentication and authorization, database access, file uploads, deserialization, cryptography, and output rendered in a browser. Confirm that access checks happen on the server, inputs are validated for their intended use, and database queries do not join user data into executable SQL. Do not accept custom cryptography or security-sensitive code just because it looks plausible.
  6. Verify dependencies before accepting them. Ask whether a new package is necessary. Check its name, source, version, maintenance, and known vulnerabilities using your normal dependency review process. A plausible-looking package name can be a typo or an unintended substitute. Keep lockfiles consistent and avoid unexplained version changes.
  7. Run tests and security checks. Use the project’s documented test suite, formatter or linter, static analysis, and dependency scanning where available. Add tests for the security behavior, not only the happy path: for example, verify that an unauthenticated user is denied and invalid input is rejected. A passing test suite reduces uncertainty but cannot prove the absence of vulnerabilities.
  8. Review configuration and secrets separately. Ensure credentials are read from approved secret storage or environment configuration rather than committed in source. Check that debug settings, permissive cross-origin rules, verbose errors, and development-only shortcuts are not enabled in production configuration. If a real secret was exposed, revoke or rotate it; deleting it from the latest file alone may not remove it from repository history or logs.
  9. Ask for an explanation, then verify it. Have the tool summarize its changes and assumptions, including security-relevant decisions. Use that summary as a review aid, not evidence. Confirm claims against the code, tests, documentation, and trusted project guidance before merging.
  10. Get an independent review for high-impact changes. Changes involving login, permissions, payments, sensitive data, cryptography, or deployment deserve review by a qualified maintainer or security reviewer. Merge only after required checks pass and unresolved concerns are addressed.

Common Mistakes

  • Accepting a large generated patch without reading the diff. Broad changes can hide weakened checks or unrelated edits.
  • Assuming that a confident explanation, passing tests, or a clean scan proves the code is secure. Each catches only some kinds of problems.
  • Sharing secrets in prompts, code snippets, screenshots, or logs. Removing them afterward may not undo exposure.
  • Allowing generated code to disable a failing test instead of investigating the failure. Treat removed or skipped security tests as a warning.
  • Adding a dependency without checking what it does or whether it is actually needed.

Tips

  • Keep prompts narrow and provide only the minimum relevant code. Use sanitized examples when a real snippet contains sensitive details.
  • Use repository protections such as required reviews and automated checks where available. Do not give a coding tool permission to merge or deploy changes unless that access is explicitly approved and tightly controlled.
  • Keep security requirements in the project’s contribution guidance, and make the same checks apply to generated and human-written code.
  • For a security-sensitive change, write the expected behavior and negative cases before generating code. This makes the review more concrete.

Troubleshooting

A test or scanner fails: Do not suppress the warning just to make the build pass. Identify the affected code and determine whether the finding is valid. Fix the underlying issue, update a test if its expected behavior was genuinely wrong, or document a false positive through your normal review process.

The tool makes unrelated edits: Stop and inspect the diff. Revert unrelated files or discard the patch and ask for a smaller change. Avoid applying a second broad prompt before understanding the first result.

You cannot tell whether a pattern is safe: Pause the merge. Consult the project’s security guidance, authoritative documentation for the relevant framework, or a qualified reviewer. Do not ship a pattern you cannot explain in a sensitive code path.

A credential may have been exposed: Revoke or rotate it promptly, check relevant access logs, and follow your incident-response process. Remove the value from code and address repository history or other copies as appropriate; a simple deletion is not sufficient containment.

The tool repeatedly generates unsafe code: Narrow the task, add explicit constraints, and implement or review the sensitive part manually. If the behavior continues, stop using the tool for that task and report the issue through the appropriate project process.

Frequently Asked Questions

Can AI-generated code be secure?
It can be, but generated code needs the same review, testing, and security checks as any other contribution. No tool can guarantee that a change is vulnerability-free.

Should I paste private code into a coding tool?
Only if your organization permits it and you understand the tool’s data handling. Otherwise, use a sanitized, minimal example or work locally without sending confidential material.

Are automated security scans enough?
No. Scanners can find known patterns and dependency issues, but may miss logic flaws such as incorrect authorization. Combine them with tests and human review.

What should I do if generated code includes a secret?
Do not merge it. If the secret was real and may have been exposed, revoke or rotate it, then check for copies in history, logs, and shared prompts.

Is a small change safe to merge without review?
Small changes can still affect access control or data handling. Follow your project’s review rules and assess risk based on what the code does, not only its size.

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 →