Summary
A SQL injection vulnerability in the PageRequest handling of yu-picture's PictureServiceImpl allows untrusted sortField input to influence dynamically constructed database queries in MyBatis-Plus.
When an attacker can control the sortField parameter over a remote interface, they may manipulate the generated SQL, potentially accessing or modifying data beyond intended permissions.
Affected context
The issue occurs in yu-picture's backend within PictureServiceImpl.PageRequest, where user-supplied pagination and sorting parameters are translated into MyBatis-Plus query wrappers.
The vulnerability is present when:
- The
sortFieldargument is accepted from client input (e.g., HTTP query/body), - It is used to build query or ORDER BY clauses without strict validation or whitelisting,
- And the ORM layer (MyBatis-Plus) is configured to pass such values into SQL without proper parameterization.
Under these conditions, hostile input can affect the final SQL statement issued to the database.
Exploit preconditions
- Attack surface: Remote API endpoints or web handlers that accept pagination or sorting parameters (especially
sortField) and forward them toPictureServiceImpl.PageRequest. - Required access: Network access to the exposed yu-picture backend; no special privileges are needed beyond the ability to send crafted requests if the vulnerable endpoint is unauthenticated.
- Trigger point: Invocation of the paginated query in
PictureServiceImplwhere thesortFieldvalue influences the SQL fragment (for example, an ORDER BY clause) generated by MyBatis-Plus. - Why exploitation is possible: The implementation treats
sortFieldas a trusted expression or column fragment rather than as data, and it is not strictly validated against an allowlist of legitimate column names. This leads to unsafe concatenation or interpolation into SQL, consistent with CWE-89 (Improper Neutralization of Special Elements used in an SQL Command) and CWE-74 (Improper Neutralization of Special Elements in Output).
Why this happens
The root cause is an insecure pattern for constructing database queries based on client-controlled input in the PageRequest logic.
Instead of mapping user input to a small, predefined set of column names or query options, the code allows sortField to propagate directly into the query-building phase of MyBatis-Plus.
When an ORM or query builder accepts raw column or expression strings, these become part of the generated SQL text.
Without robust neutralization and strict whitelisting, attackers can inject additional SQL tokens or clauses.
This behavior aligns with CWE-89: Improper Neutralization of Special Elements used in an SQL Command and is also related to CWE-74, since untrusted data is incorporated into output that is later interpreted as SQL by the database engine.
Potential impact
- Unauthorized reading of database records that should be restricted to other users or roles.
- Manipulation of query logic to bypass application-level filters and access sensitive metadata.
- Tampering with stored data, including modification or deletion of picture records or associated metadata, depending on the exact injection point.
- Potential execution of stacked or additional SQL statements if the underlying database and driver configuration allow it.
- Indirect disclosure of database structure and schema information through error responses or timing behavior.
General mitigation guidance
Effective mitigation centers on eliminating the ability for untrusted input to control SQL structure:
- Strictly whitelist sortable fields: Map incoming
sortFieldvalues to a fixed enumeration of known, safe column names. Ignore or reject any value that does not match the whitelist. - Separate data from query structure: Ensure that only static, server-defined strings make up SQL keywords, column names, and ordering expressions. User input should never be concatenated into SQL fragments.
- Leverage ORM-safe patterns: Use MyBatis-Plus APIs that accept column identifiers indirectly (e.g., via lambda or pre-defined constants) rather than raw strings derived from user data.
- Validate and normalize input: Enforce validation rules on pagination and sorting parameters (length limits, allowed characters, and valid options) before invoking query-building logic.
- Harden error handling: Avoid echoing raw database error messages to clients to reduce the feedback that attackers can use to refine SQL injection attempts.
- Apply least privilege at the database tier: Use database accounts with minimal permissions so that, in the event of injection, the blast radius is reduced.
How teams detect issues like this
Detection can combine code-level review and runtime monitoring:
- Static analysis: Configure SAST tools to flag concatenation of user-controlled values into query strings or ORM APIs that accept raw SQL/column fragments. Custom rules can focus on methods that build MyBatis-Plus
Pageor query wrappers using request parameters. - Code review checklists: During reviews of data access layers, explicitly verify that pagination, filtering, and sorting parameters are mapped through whitelists and never injected directly into SQL fragments.
- Security unit/integration tests: Add automated tests that send crafted
sortFieldinputs to the affected endpoints and verify that the resulting SQL (or behavior) is not susceptible to injection (e.g., no unexpected rows returned, no syntax errors from malformed SQL). - Database and application logging: Monitor for anomalous queries containing suspicious patterns (like unexpected SQL keywords in column/ORDER BY fields) and correlate them with incoming request parameters.
- Runtime protection policies: Where appropriate, employ a database firewall or web application firewall (WAF) with rules tuned to detect and block common SQL injection payload patterns on sorting and pagination parameters.

