Skip to content

Commit b4cfdd5

Browse files
committed
[Refactor]: 정렬 조건 sql 인젝션 방지용 화이트리스트 추가
1 parent 47dd172 commit b4cfdd5

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

back/src/main/java/com/back/domain/post/repository/PostRepositoryCustomFullTextImpl.java

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import java.util.ArrayList;
1818
import java.util.List;
19+
import java.util.Map;
1920

2021
@Profile("prod")
2122
@RequiredArgsConstructor
@@ -147,19 +148,32 @@ private String buildDataQuery(PostSearchCondition condition, Pageable pageable)
147148
}
148149

149150
private String buildOrderByColumns(Pageable pageable) {
151+
// 허용된 정렬 컬럼만 화이트리스트로 관리
152+
Map<String, String> allowedColumns = Map.of(
153+
"createdDate", "p.created_date",
154+
"likeCount", "p.like_count"
155+
);
156+
150157
List<String> orders = new ArrayList<>();
151158

152159
for (Sort.Order order : pageable.getSort()) {
153-
String column = switch (order.getProperty()) {
154-
case "createdDate" -> "p.created_date";
155-
case "likeCount" -> "p.like_count";
156-
default -> "p.created_date";
157-
};
160+
String property = order.getProperty();
161+
162+
// 화이트리스트에 없는 컬럼은 무시
163+
if (!allowedColumns.containsKey(property)) {
164+
continue; // 또는 예외 발생
165+
}
166+
167+
String column = allowedColumns.get(property);
168+
169+
// direction도 명시적으로 검증
158170
String direction = order.isAscending() ? "ASC" : "DESC";
171+
159172
orders.add(column + " " + direction);
160173
}
161174

162-
return String.join(", ", orders);
175+
// 정렬 조건이 없으면 기본값 반환
176+
return orders.isEmpty() ? "p.created_date DESC" : String.join(", ", orders);
163177
}
164178

165179
/**

0 commit comments

Comments
 (0)