Lecture VI — Working Safely with AI Code
AI-Assisted Programming for PhD Researchers
The Numbers
How Bad Is It?
Independent audits keep landing in the same place.
. . .
- About 45% of AI-generated samples carry OWASP-class vulnerabilities — 80 tasks across 100+ models (Veracode 2025)
- AI-authored pull requests: ~1.7× more issues overall, up to 2.74× for XSS specifically — correlational, n=470 PRs (“AI Vs Human Code Gen Report,” n.d.)
And It Replicates
- This is not a new-model glitch: ~40% of Copilot programs were already vulnerable in the 2022 academic baseline (Pearce et al. 2022)
- Three years, different tools — the rate barely moves
Why?
- Trained on public code — including the insecure half
- Optimized for plausible, not for safe
- No adversarial mindset — it does not ask “who attacks this?”
- Your context rarely says “this will face the internet”
And You’ll Feel Safer Anyway
- Stanford ran a randomized trial with and without an AI assistant
. . .
- Participants with the assistant wrote less secure code, and rated it more secure than the control group did (Perry et al. 2023)
- Overconfidence is the failure mode; the review habits below are the counter
Security Failure Modes
Injection
Plausible, passes tests, ships a hole. Spot the problem:
def get_measurements(conn, station):
query = f"""
SELECT * FROM readings
WHERE station = '{station}'
"""
return conn.execute(query).fetchall(). . .
The station value is pasted straight into SQL. Pass it as a parameter (?), never an f-string.
Secrets in Code
- Hardcoded API keys, tokens, and passwords in tracked files
- Agents echo the patterns they saw in training — including bad ones
- AI-assisted commits leak secrets at 3.2% vs a 1.5% baseline (Nabiullina and Winqwist 2026)
The Secrets Rule
Secrets live in environment variables or your keychain — never in files the agent writes, and never pasted into a prompt.
Hallucinated Dependencies
- The agent imports a package that does not exist
- Squatters register those invented names — “slopsquatting”
- Check every new dependency it adds: real? maintained? license?
The Quiet Ones
- Path traversal: unsanitized filenames escaping a directory
- Unsafe
evalorpickleon data you did not create - Missing input validation on the boring, trusting path
You do not memorize these — you build review habits and checks.
Prompt Injection
- Untrusted content the agent reads (a webpage, a tool result, a file) can carry instructions it then obeys
- The term was coined in 2022 (Willison 2022)
- The rule: never combine the lethal trifecta — private data + untrusted content + external communication (Willison 2025)
Hallucination and Overreliance
Confidently Wrong
- Invented APIs, wrong parameter names, outdated idioms
- Delivered in the same confident tone as correct code
- Training cutoffs mean the model’s world is months old
Countermeasures
- Verify against real docs — MCP docs servers, this afternoon
- Run the code; a claim is not a result
- Prefer boring, well-documented libraries over the clever new one
Licensing and IP
Who Owns AI Output?
- Legally unsettled, and it depends on your jurisdiction
- Courts and institutions are still deciding
- Practical stance: treat it as your code and your responsibility
Three Practical Rules
- Check the licenses of any dependencies it pulls in
- Do not paste licensed code in as context and ask for a “rewrite”
- Know your institution’s policy — it may already bind you
Reviewing AI Code
Read Every Diff
- Non-negotiable: every line, every time
git add -pturns review into a ritual, hunk by hunk- If the diff is too big to review, the step was too big — redo it smaller
Writer and Reviewer
- A fresh agent session reviewing the diff has no attachment to the code
- Review against the spec, not your taste — you arbitrate
- A checklist you are handed does not transfer; it has to be practiced (Bouvier et al. 2025)
- That practice is exactly what Lab 4 is
What Review Catches vs What Tests Catch
| Tests catch | Review catches |
|---|---|
| Behavior regressions | Design smells |
| Broken edge cases | Security holes |
| Wrong outputs | Silent data handling |
You need both — neither substitutes for the other.
Your Data
What Leaves Your Machine
- Every prompt you type
- Every file the agent reads
- Every command output it sees
All of it goes to the model provider’s API. Plain fact, not a scare.
Where It Goes Matters
- Jurisdiction, retention, and whether it trains future models
- Our stack: Mistral, EU-hosted, training opt-out. You set that in setup, check it now
- Zed-hosted models run in the US — know which you are calling
Unpublished Research Data
Before you send it, ask:
- Personal data under GDPR?
- Under embargo or an NDA?
- Owned by a third party?
Any “yes” → use a synthetic sample or a local model. Your data-management plan may already answer this.
Rules of Thumb
Lab data is synthetic, so send it freely. Your own data: check first, sample small, anonymize. Secrets and credentials: never.
Academic Integrity
Disclosure Is the Norm Now
- The rules converged over 2023–2024 and they agree
- Nature and Science: AI cannot be an author, and its use must be disclosed (“Tools Such as ChatGPT Threaten Transparent Science; Here Are Our Ground Rules for Their Use” 2023; Thorp 2023)
The Rules Agree Everywhere
- COPE and ICMJE extend that to thousands of journals (Committee on Publication Ethics 2023; International Committee of Medical Journal Editors 2025)
- DFG, ALLEA, and the EU living guidelines say the same for funders and integrity codes (Deutsche Forschungsgemeinschaft 2023; ALLEA - All European Academies 2023; European Commission 2024)
- A settled expectation, not a frontier
How to Disclose
- In methods or acknowledgments: which tools, for what, and that a human verified the output
- Keep your prompts and session logs — they are your lab notebook
In This Course
- Disclosure is part of your final presentation — the process-reflection section
- It is assessed content, not a confession
Lab 4
Your Mission
In Lab 4 the agent works fast and unsupervised on a branch:
- It builds three features without asking you questions
- You find what is wrong with them before they reach main
- Then you earn the merge through review, not vibes
Continue Your Journey
Next Up
- You can now spot the failure modes and review AI code deliberately
- Lab 4 — Review and Explain starts now
- Lecture VII — Extending the Agent — 13:15
- Course literature and references