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 security issues (correlational, n=470 PRs) (Loker 2025)
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 Risks
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. You get a way to check claims against real sources 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
Review vs Tests: What Each Catches
| 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
Could Anyone Tell?
- Suppose you skip disclosure, could anyone prove a model wrote your text?
- Since August 2026: probably, yes
- The idea behind this is not very complicated, so let’s see how it works
How a Watermark Hides in Plain Text
- Before each word, the model secretly splits its vocabulary into a green and a red half which is reshuffled at every word
- It then softly prefers green words; the text still reads normally
- By chance, you write ~half green words, but the model writes far more of these
Who Can Actually Detect It
- Detection is simply counting: one paragraph can push chance below \(10^{-13}\) (Kirchenbauer et al. 2023)
- But only if you know the secret key that decided which half was green, so in practice only the provider can run it
When Did the Watermark Appear?
- EU AI Act, Article 50: since 2 August 2026 providers must mark AI content machine-readably (European Commission 2026)
- Claude models released since then watermark all text output; detection tooling is still forthcoming from Anthropic (Anthropic 2026)
- Google has watermarked Gemini’s text output with SynthID-Text since 2024 (Dathathri et al. 2024)
What a Mark Proves (and Does Not)
- Proves: a model processed this text, but not who did the work and not whether a human verified it
- Heavy editing or paraphrasing can wash it out; short snippets may not carry it, hence an absence proves nothing
- Presence without disclosure, though, looks exactly like what it is
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
- When you show your project on Day 3, say which tool you used and for what
- It is part of the work, 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