Skip to content

fix: validate sortBy/sortOrder before building ORDER BY clause - #482

Open
aroh3006 wants to merge 1 commit into
rishikanthc:mainfrom
aroh3006:fix/sortby-sql-injection
Open

fix: validate sortBy/sortOrder before building ORDER BY clause#482
aroh3006 wants to merge 1 commit into
rishikanthc:mainfrom
aroh3006:fix/sortby-sql-injection

Conversation

@aroh3006

@aroh3006 aroh3006 commented Sep 3, 2026

Copy link
Copy Markdown

Fixes #468.

ListWithParams built the ORDER BY clause with
sortBy + " " + sortOrder, and both come straight from the
sort_by/sort_order query params on GET /api/v1/transcription/list
with no validation. Anything past the intended column name in sortBy
reaches the query text as-is.

sortBy is now checked against a fixed set of the actual sortable
column names (created_at, updated_at, title, status,
audio_path), and sortOrder against asc/desc. Anything else
falls back to the existing default sort (created_at desc) instead of
reaching the query, same as an empty sortBy already did before.

Tests. Added two tests in internal/repository/implementations_test.go
against an in-memory sqlite DB: a sortBy value with a trailing SQL
fragment is rejected and falls back to the default sort instead of
reaching the query, and a real column name still sorts correctly. Ran
the first one against the old code first, and the driver actually
received the injected text as part of the query
(ORDER BY id; DROP TABLE transcription_jobs; -- desc), confirming
the injection reaches the database layer. go vet and gofmt are
clean on the changed files, and the full internal/repository package
suite passes.

ListWithParams built the ORDER BY clause with sortBy + " " + sortOrder,
both taken straight from the sort_by and sort_order query params on
GET /api/v1/transcription/list with no validation. Anything after the
column name in sortBy reaches the query as-is.

sortBy is now checked against a fixed set of actual column names, and
sortOrder against asc/desc. Anything else falls back to the existing
default sort (created_at desc) instead of reaching the query.

Added tests: a sortBy value with a trailing SQL fragment is rejected
(falls back to default sort rather than erroring or building the
query), and a real column name still sorts correctly. Confirmed the
first test fails against the old code, the driver actually receives
the injected ORDER BY text as part of the query.
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.

[Security] ORDER BY SQL Injection via sortBy parameter (CWE-89)

1 participant