fix: validate sortBy/sortOrder before building ORDER BY clause - #482
Open
aroh3006 wants to merge 1 commit into
Open
fix: validate sortBy/sortOrder before building ORDER BY clause#482aroh3006 wants to merge 1 commit into
aroh3006 wants to merge 1 commit into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #468.
ListWithParamsbuilt the ORDER BY clause withsortBy + " " + sortOrder, and both come straight from thesort_by/sort_orderquery params onGET /api/v1/transcription/listwith no validation. Anything past the intended column name in
sortByreaches the query text as-is.
sortByis now checked against a fixed set of the actual sortablecolumn names (
created_at,updated_at,title,status,audio_path), andsortOrderagainstasc/desc. Anything elsefalls back to the existing default sort (
created_at desc) instead ofreaching the query, same as an empty
sortByalready did before.Tests. Added two tests in
internal/repository/implementations_test.goagainst an in-memory sqlite DB: a
sortByvalue with a trailing SQLfragment 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), confirmingthe injection reaches the database layer.
go vetandgofmtareclean on the changed files, and the full
internal/repositorypackagesuite passes.