A client-selectable sort field changes query structure. Map it to a small server-approved set of fields and directions instead of inserting the supplied text into a database expression.
Values and identifiers need different treatment
A sim free personal sms number API may support message searches with a chosen ordering. Parameter binding protects ordinary query values in its supported context; it does not automatically turn an arbitrary column name or direction into an approved structural choice. Keep those decisions under the server's query design.
OWASP's SQL injection guidance discusses allowlisting for structural elements that cannot be supplied as ordinary bound values. Expose meaningful public choices and map them to fixed internal expressions. Avoid offering a raw expression field and expecting a generic escaping function to make every possible input acceptable.

Make invalid choices predictable
The API can use a documented default or return a clear invalid-parameter result. Whichever contract is chosen, apply it consistently. Silently ignoring one unsupported name while executing another can make clients unable to explain which ordering produced their result.
| Input dimension | Control |
|---|---|
| Field | A fixed approved mapping |
| Direction | A documented finite set |
| Equal values | A supported stable secondary order |
| Resource scope | The existing account and permission filters |
The sort field also needs to belong to the intended public query surface. An operator should not gain information about inaccessible records or hidden attributes merely by selecting an ordering option. A valid ordering never replaces the resource's ordinary authorisation checks.
Keep pagination interpretable
Many messages can share the same timestamp. Where the contract requires a stable boundary, define an appropriate secondary order rather than relying on incidental database output. Keep that order consistent across pages and preserve the supported relationship to any cursor or effective query.
Do not promise that a stable tie-breaker alone creates a snapshot of a changing collection. Ordering, pagination and consistency are related design decisions with separate guarantees. Document what the endpoint actually provides so clients do not infer completeness from a display that merely looks sorted.
Treat export ordering as part of the effective query when an export is expected to match the list. A server-side export should resolve the same approved public choice, not accept a hidden raw expression copied from the browser. Keep the selected order with its filters in the authorised task record. This allows an operator to explain a difference without relaxing the database boundary or exposing internal field names.

Inspect the generated query in controlled tests
Use an authorised environment to exercise known choices, unknown names, unsupported directions and tied values. Verify that invalid input cannot introduce a new query fragment and that valid choices retain the same authorised scope. Review the final data-access path rather than approving only a dropdown's client-side validation.
Before adding another sort option, assess its cost and indexing needs with representative approved data. A choice that works on a tiny backend list may be unsuitable for an unbounded endpoint. Keep slow-query diagnostics limited to the option and request reference; private message bodies are unnecessary for establishing that the structural allowlist and execution plan work as intended.
Reference
OWASP: SQL Injection Prevention
