Skip to content

Commit 704b8df

Browse files
mminellacppwfs
authored andcommitted
Added sort validation
This commit now validates that the value passed via a PageRequest to sort the results by is a valid value. Resolves #739 Fixed to allow for all letter cases
1 parent bb7d235 commit 704b8df

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

spring-cloud-task-core/src/main/java/org/springframework/cloud/task/repository/dao/JdbcTaskExecutionDao.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2019 the original author or authors.
2+
* Copyright 2015-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@
2222
import java.util.ArrayList;
2323
import java.util.Collections;
2424
import java.util.Date;
25+
import java.util.HashSet;
2526
import java.util.LinkedHashMap;
2627
import java.util.List;
2728
import java.util.Map;
@@ -58,6 +59,7 @@
5859
* @author Gunnar Hillert
5960
* @author David Turanski
6061
* @author Ilayaperumal Gopinathan
62+
* @author Michael Minella
6163
*/
6264
public class JdbcTaskExecutionDao implements TaskExecutionDao {
6365

@@ -161,6 +163,21 @@ public class JdbcTaskExecutionDao implements TaskExecutionDao {
161163

162164
private DataFieldMaxValueIncrementer taskIncrementer;
163165

166+
private static final Set<String> validSortColumns = new HashSet<>(10);
167+
168+
static {
169+
validSortColumns.add("TASK_EXECUTION_ID");
170+
validSortColumns.add("START_TIME");
171+
validSortColumns.add("END_TIME");
172+
validSortColumns.add("TASK_NAME");
173+
validSortColumns.add("EXIT_CODE");
174+
validSortColumns.add("EXIT_MESSAGE");
175+
validSortColumns.add("ERROR_MESSAGE");
176+
validSortColumns.add("LAST_UPDATED");
177+
validSortColumns.add("EXTERNAL_EXECUTION_ID");
178+
validSortColumns.add("PARENT_EXECUTION_ID");
179+
}
180+
164181
/**
165182
* Initializes the JdbcTaskExecutionDao.
166183
* @param dataSource used by the dao to execute queries and update the tables.
@@ -511,8 +528,13 @@ private Page<TaskExecution> queryForPageableResults(Pageable pageable,
511528

512529
if (sort != null) {
513530
for (Sort.Order sortOrder : sort) {
514-
sortOrderMap.put(sortOrder.getProperty(),
531+
if (validSortColumns.contains(sortOrder.getProperty().toUpperCase())) {
532+
sortOrderMap.put(sortOrder.getProperty(),
515533
sortOrder.isAscending() ? Order.ASCENDING : Order.DESCENDING);
534+
}
535+
else {
536+
throw new IllegalArgumentException(String.format("Invalid sort option selected: %s", sortOrder.getProperty()));
537+
}
516538
}
517539
}
518540

spring-cloud-task-core/src/test/java/org/springframework/cloud/task/repository/dao/JdbcTaskExecutionDaoTests.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2019 the original author or authors.
2+
* Copyright 2015-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -46,12 +46,14 @@
4646

4747
import static org.assertj.core.api.Assertions.assertThat;
4848
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
49+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
4950

5051
/**
5152
* Executes unit tests on JdbcTaskExecutionDao.
5253
*
5354
* @author Glenn Renfro
5455
* @author Gunnar Hillert
56+
* @author Michael Minella
5557
*/
5658
@ExtendWith(SpringExtension.class)
5759
@ContextConfiguration(
@@ -209,6 +211,31 @@ public void testStartExecutionWithNullExternalExecutionIdNonExisting() {
209211
expectedTaskExecution.getExecutionId()));
210212
}
211213

214+
@Test
215+
@DirtiesContext
216+
public void testFindRunningTaskExecutions() {
217+
initializeRepositoryNotInOrderWithMultipleTaskExecutions();
218+
assertThat(this.dao.findRunningTaskExecutions("FOO1", PageRequest.of(1, Integer.MAX_VALUE, Sort.by("START_TIME"))).getTotalElements())
219+
.isEqualTo(4);
220+
}
221+
222+
@Test
223+
@DirtiesContext
224+
public void testFindRunningTaskExecutionsIllegalSort() {
225+
initializeRepositoryNotInOrderWithMultipleTaskExecutions();
226+
assertThatThrownBy(() -> this.dao.findRunningTaskExecutions("FOO1", PageRequest.of(1, Integer.MAX_VALUE, Sort.by("ILLEGAL_SORT"))).getTotalElements())
227+
.isInstanceOf(IllegalArgumentException.class)
228+
.hasMessage("Invalid sort option selected: ILLEGAL_SORT");
229+
}
230+
231+
@Test
232+
@DirtiesContext
233+
public void testFindRunningTaskExecutionsSortWithDifferentCase() {
234+
initializeRepositoryNotInOrderWithMultipleTaskExecutions();
235+
assertThat(this.dao.findRunningTaskExecutions("FOO1", PageRequest.of(1, Integer.MAX_VALUE, Sort.by("StArT_TiMe"))).getTotalElements())
236+
.isEqualTo(4);
237+
}
238+
212239
private TaskExecution initializeTaskExecutionWithExternalExecutionId() {
213240
TaskExecution expectedTaskExecution = TestVerifierUtils
214241
.createSampleTaskExecutionNoArg();

0 commit comments

Comments
 (0)