fix: prevent SQL injection via sortField parameter in PageRequest - #3
Open
jackieya wants to merge 2 commits into
Open
fix: prevent SQL injection via sortField parameter in PageRequest#3jackieya wants to merge 2 commits into
jackieya wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
Pull request overview
This PR mitigates an ORDER BY SQL injection risk by validating PageRequest.sortField before it is used to build MyBatis-Plus orderBy clauses.
Changes:
- Added whitelist validation in
PageRequest.setSortField()to restrict sortable fields to a safe identifier pattern. - Reused existing
ThrowUtils+ErrorCode.PARAMS_ERRORfor consistent parameter error handling.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| @@ -27,4 +29,12 @@ public class PageRequest { | |||
| * 排序顺序(默认升序) | |||
There was a problem hiding this comment.
Javadoc/注释与实际默认值不一致:这里写“排序顺序(默认升序)”,但 sortOrder 默认值是 "descend"(降序)。建议二选一:要么把默认值改成 "ascend",要么把注释改为“默认降序”,避免误导接口调用方。
Suggested change
| * 排序顺序(默认升序) | |
| * 排序顺序(默认降序) |
Comment on lines
+33
to
+37
| public void setSortField(String sortField) { | ||
| if (sortField != null && !sortField.isEmpty()) { | ||
| ThrowUtils.throwIf(!sortField.matches("^[a-zA-Z][a-zA-Z0-9]*$"), | ||
| ErrorCode.PARAMS_ERROR, "排序字段名称不合法"); | ||
| } |
There was a problem hiding this comment.
这里用 String#matches 每次调用都会重新编译正则,虽然调用频率不高但属于可避免的开销。建议将正则提取为 static final Pattern(或常量)并复用,既能减少重复编译,也更便于后续修改/复用该白名单规则。
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.
漏洞描述
PageRequest.sortField参数未经校验直接拼接到 MyBatis-Plus 的orderBy方法中,导致多个分页查询接口存在 ORDER BY 子句 SQL 注入。受影响接口:
POST /api/picture/list/page/vo(无需认证)POST /api/space/list/page/vo(无需认证)POST /api/user/list/page/vo(需管理员权限)攻击者可利用 time-based 盲注提取数据库中的任意数据。
修复方案
在
PageRequest.setSortField()中添加正则白名单校验^[a-zA-Z][a-zA-Z0-9]*$,仅允许合法列名格式(字母开头,仅含字母和数字)。ThrowUtils+ErrorCode.PARAMS_ERRORPageRequest的子类自动受到保护漏洞复现
bash curl -w "\nTime: %{time_total}s\n" -X POST http://TARGET/api/picture/list/page/vo \ -H "Content-Type: application/json" \ -d '{"sortField":"(SELECT 1 FROM (SELECT SLEEP(3)) t)","sortOrder":"ascend","current":1,"pageSize":10}'