== review_agent.sh | model: gpt-oss:20b
== time: 2026-11-20 13:53:12 UTC | 2026-11-20 08:53:12 EST
== inputs (1):
   pr-42.diff  sha256=f7080abce9e6ebc770b21ca606c8b9c207c56590bce36c4eeaace7ea66816983  lines=343
== question: Review this change for security defects. Separate what the code does from what you infer.
== answer:
**Security concerns – ordered by severity**

| # | File & line | Class of defect | How to reproduce | What tool would see it? | Finding‑type |
|---|-------------|-----------------|------------------|------------------------|--------------|
| 1 | `app.py` line ≈ 68‑73 (`aid_status`) | *Authorization bypass – information disclosure* | 1. Log in as a valid user (`/login` → cookie).  <br>2. Issue a request to `/aid/status?student_id=student_b` while authenticated as `student_a`.  <br>3. Observe that the response contains `student_id: student_b` and the full aid letter for `student_b`. | Static‑analysis rule that checks that every endpoint that can read user‑specific data must enforce that the data belongs to the authenticated principal.  <br>Manual review needed (needs‑reproduction). | Needs‑reproduction |
| 2 | `render.py` line ≈ 17 (`render_letter_image`) | *Expression‑based injection via unchecked badge expression* | 1. Authenticate as any user.  <br>2. Call `/aid/status?format=image&badge=A+and+1==1` (or any expression that evaluates to a truthy value).  <br>3. Observe that the call is processed by `ImageMath.eval` and could potentially evaluate arbitrary arithmetic or boolean expressions. | A static‑analysis rule that flags use of `ImageMath.eval` (or any eval‑like API) with data that originates from the request without validation. | Finding‑candidate |
| 3 | `config.py` line ≈ 11 (`DB_PASSWORD = os.environ.get("NB_DB_PASSWORD")`) | *Missing default – potential runtime failure but not a secret leak* | 1. Run the service with `NB_DB_PASSWORD` unset.  <br>2. Observe that the DB password is `None`, causing the DB connection to fail and the service to crash. | Dependency‑check / environment‑validation tool that warns when a required secret is not provided. | Needs‑reproduction |
| 4 | `db.py` line ≈ 16 (`SORT_COLUMNS = ("title", "category", "year")`) | *No defect – defensive coding is present* | – | – | – |
| 5 | `requirements.txt` & `requirements.lock` | *New dependency on Pillow* | 1. Verify that the image rendering functionality is only invoked when a client requests `format=image`.  <br>2. Confirm that the PIL import is lazy and does not cause failures when Pillow is missing. | Dependency‑check would flag the addition of Pillow but that is expected for the new feature. | – |

### What the change actually does

* Moves the database password out of the source code and makes it mandatory to be supplied via the `NB_DB_PASSWORD` environment variable.  
* Adds a new `/aid/status` endpoint that returns a student’s financial‑aid letter.  
* Allows the letter to be returned as plain text or rendered as an image; the image rendering uses Pillow’s `ImageMath.eval` to overlay a badge on a template.  
* Extends the search endpoint to accept an optional `sort` query parameter and validates it against a whitelist.  
* Adds logging for search queries.  

### What the reviewer infers

* The authorization logic in `aid_status` does **not** check that the `student_id` requested (when supplied in the query string) matches the authenticated user. An attacker who is logged in can read any other student’s aid letter.  
* The badge expression supplied in the query string is passed straight to `ImageMath.eval`, an expression evaluator. Because the expression is not sanitized, it could be used to execute arbitrary arithmetic or boolean logic; in the worst case, if Pillow’s internals expose any side‑effects, it could be a vector for code execution.  
* The service now requires `NB_DB_PASSWORD`; running without it will cause a crash. While not a secret leak, it is a runtime failure that may reveal sensitive data via error messages or stack traces.  

### Checks that are missing or should be added

* **Authorization checks**: Add a guard that ensures `student_id` (if supplied) equals the authenticated `signed_in` ID, or otherwise reject with a `403`.  
* **Input validation for `badge_expr`**: Either whitelist allowed badge expressions or use a safe parser/evaluator that rejects dangerous constructs.  
* **Environment validation**: Add a startup check that `NB_DB_PASSWORD` is set and fail early with a clear error message.  
* **Static‑analysis rule for eval‑like APIs**: Flag usage of `ImageMath.eval` with request data.  
* **Dependency review**: Ensure that adding Pillow does not introduce CVEs (e.g., check CVE database for Pillow 8.4.0).  

These are the primary security concerns that arise from the diff. The rest of the changes are benign or already mitigated by defensive coding.

[stats] model=gpt-oss:20b prompt_tokens=3544 context_length=32768 seconds=20.9
