How to review code you copied from an AI assistant or a forum
Copied code solves someone else's problem in someone else's project, often for a different library version. AI assistants add two more risks: they can call functions that do not exist, and they often rewrite more than you asked for.
The example
A Python function fetches exchange rates. It sometimes fails with timeouts and certificate errors, so the developer asks an AI assistant to fix it. The original:
import requests
def fetch_rates(base_url, api_key):
response = requests.get(
f"{base_url}/rates",
headers={"Authorization": f"Bearer {api_key}"},
timeout=10,
)
response.raise_for_status()
return response.json()
The suggested fix:
import requests
def fetch_rates(base_url, api_key):
try:
response = requests.get(
f"{base_url}/rates",
headers={"Authorization": f"Bearer {api_key}"},
verify=False,
)
return response.json()
except:
return {}
It runs and the errors stop. That is exactly why it is dangerous.
Step 1: diff it against your version
Do not paste the suggestion over your file. Put both versions in Compare Codes. With default settings it looks like a full rewrite, because the new try block indented every line:
@@ -1,10 +1,12 @@ import requests def fetch_rates(base_url, api_key):- response = requests.get(- f"{base_url}/rates",- headers={"Authorization": f"Bearer {api_key}"},- timeout=10,- )- response.raise_for_status()- return response.json()+ try:+ response = requests.get(+ f"{base_url}/rates",+ headers={"Authorization": f"Bearer {api_key}"},+ verify=False,+ )+ return response.json()+ except:+ return {}
Turn on Ignore whitespace and only five rows remain: try: added, timeout=10, changed to verify=False,, raise_for_status() deleted, and except: and return {} added. Python uses indentation as structure, so turn the option off once more before accepting anything. Those five rows hold four problems:
verify=Falseturns off certificate checking. The request, API key included, can now be read and changed by anyone who can intercept it. The error is gone because the check is gone.timeout=10was removed. requests has no default timeout, so the call can now wait forever.raise_for_status()was removed. A 401 or 500 error page is now parsed as if it were rates.- The bare
except:catches everything, even a typo or Ctrl+C. The caller cannot tell a failure from a day with no rates.
None of this is visible when you only read the suggestion. All of it is visible in the diff.
Step 2: check that every function and package exists
- Look up each unfamiliar function and option in the official documentation for your version. AI assistants sometimes merge two real APIs into one that does not exist.
- Check minimum versions.
ArgumentException.ThrowIfNullOrEmptyneeds .NET 7,str.removeprefixneeds Python 3.9, andArray.prototype.toSortedneeds ES2023 support. - Find every new dependency on its official registry (npm, PyPI, NuGet, Packagist) before installing. Attackers register package names that AI tools tend to invent, so a name that almost matches is a warning sign.
Step 3: search for risky patterns
Quick fixes often use these because they make an error message go away:
| Risk | Search for | Safer approach |
|---|---|---|
| Certificate checks off | verify=False, rejectUnauthorized: false, CURLOPT_SSL_VERIFYPEER false, curl -k | Fix the certificate chain, or point the client at the right CA bundle. |
| SQL built from strings | "... WHERE id = " + id, f-strings or $"..." in a query | Parameterized queries, such as $pdo->prepare("... WHERE id = ?"). |
| Errors swallowed | except: pass, catch (Exception) { }, .catch(() => {}) | Catch the specific exceptions you expect and log or raise the rest. |
| Text run as code | eval(, exec(, shell=True, shell_exec( | Pass arguments as a list and never build commands from user input. |
| Unsafe data loading | pickle.loads, yaml.load(, PHP unserialize( | json or yaml.safe_load. |
| Weak random secrets | Math.random(), Python random for tokens | crypto.getRandomValues(), the Python secrets module. |
| Weak password storage | md5( or sha1( on passwords | password_hash() in PHP, bcrypt or Argon2 elsewhere. |
| Hardcoded secrets | api_key =, password =, Bearer with a literal value | Environment variables or a secrets manager. |
| Access opened too wide | chmod 777, AllowAnyOrigin() | List the exact origins, users or paths that need access. |
Step 4: test it with your own inputs
Include an empty value, a missing value and a very long value; accented and non Latin text such as Zoƫ or Arabic; zero, one and duplicate items; a server that times out or returns an error; and dates near midnight in your users' time zones.
Step 5: check the license and keep a source note
Stack Overflow code is shared under CC BY-SA, which requires attribution. GitHub code carries its repository's license. For AI generated code, follow your organization's policy. A one line # Adapted from https://... helps the next reader, so keep it when you clean up other comments.
Step 6: write the version you will keep
For the example, a real fix keeps the timeout and the status check, retries only errors worth retrying, and tells the caller clearly when it gives up:
import requests
class RatesUnavailable(Exception):
pass
def fetch_rates(base_url, api_key, retries=2):
last_error = None
for attempt in range(retries + 1):
try:
response = requests.get(
f"{base_url}/rates",
headers={"Authorization": f"Bearer {api_key}"},
timeout=10,
)
response.raise_for_status()
return response.json()
except (requests.Timeout, requests.ConnectionError) as error:
last_error = error
raise RatesUnavailable(f"Rates API failed after {retries + 1} attempts") from last_error
requests raises SSLError as a kind of ConnectionError, so certificate errors are retried too. If they keep happening, the certificate chain is the real problem to fix. Before committing, remove comments that only repeat the code and write a commit message that says what changed and why.