Skip to content

Fix SQL Injection in GET /users/v1/{username} (API8:2019 Injection) - #66

Closed
beanbeah wants to merge 1 commit into
OWASP-CTF:dc34-ctffrom
beanbeah:ctf/challenge-1-sql-injection-username-lookup
Closed

beanbeah wants to merge 1 commit into
OWASP-CTF:dc34-ctffrom
beanbeah:ctf/challenge-1-sql-injection-username-lookup

Conversation

@beanbeah

@beanbeah beanbeah commented Aug 9, 2026

Copy link
Copy Markdown

Vulnerability

GET /users/v1/{username} built a raw SQL statement via f-string interpolation of the username path parameter (models/user_model.py, User.get_user()) and executed it with db.session.execute(text(user_query)), allowing classic UNION/boolean-based SQL injection.

Fix

Use a parameterized query: build the SQL with a named bind placeholder (:username) via SQLAlchemy's text() and pass the untrusted value as a parameter dict, so it is always treated as data and never concatenated into the SQL string.

Testing

Ran the app locally (vulnerable=1) and confirmed:

  • Injection payloads against GET /users/v1/{username} (name1' OR '1'='1, a UNION SELECT attempting to dump the admin row, and a bare trailing single quote) all now return {"status": "fail", "message": "User not found"} with no SQL error / no data leak.
  • Legitimate lookups still work: name1, name2, and admin all return their correct {"username":..., "email":...} JSON.
  • A genuinely nonexistent username still returns the same "User not found" response as before.

🤖 Generated with Claude Code

User.get_user() built a raw SQL string via f-string interpolation of
the username path param and executed it directly, allowing classic
UNION/boolean-based SQL injection (API8:2019 Injection).

Switch to a parameterized query using SQLAlchemy's text() bound
parameter (:username) so the value is always passed as data, never
concatenated into the SQL statement.

Verified locally: UNION/boolean injection payloads against
GET /users/v1/{username} (e.g. "name1' OR '1'='1", UNION SELECT
dumping the admin row, and a bare trailing quote) all now resolve as
'User not found' with no SQL error, while legitimate lookups for
name1, name2, and admin still return the correct user JSON.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Aug 9, 2026

Copy link
Copy Markdown

🏆 VAmPI — CTF Patch Score

███░░░░░░░░░░░░░░░░░  2 / 16 pts  (13%)

1 / 9 challenges patched

Per-challenge detail is withheld — it would reveal the rubric.

Commit: 5693e86 · scoring run

🎉 Your result is on the leaderboard — see where you rank! 🏆

@beanbeah

beanbeah commented Aug 9, 2026

Copy link
Copy Markdown
Author

Superseded by consolidated PR #74 (#74), which merges all validated challenge fixes into one branch. Closing this individual PR.

@beanbeah beanbeah closed this Aug 9, 2026
@beanbeah
beanbeah deleted the ctf/challenge-1-sql-injection-username-lookup branch August 9, 2026 13:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant