Skip to content
This repository was archived by the owner on May 14, 2025. It is now read-only.

Commit 60c5bcf

Browse files
committed
Validate sort keys for task executions
- Now simply does a check for sort field that it is what can possibly work and alignes i.e. what we can request from UI. - Fixes #4319 Use correct assert as andReturn()... doesn't work Support both cases
1 parent 6814d7c commit 60c5bcf

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

spring-cloud-dataflow-server-core/src/main/java/org/springframework/cloud/dataflow/server/controller/TaskExecutionController.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2019 the original author or authors.
2+
* Copyright 2016-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.
@@ -43,6 +43,7 @@
4343
import org.springframework.data.domain.Page;
4444
import org.springframework.data.domain.PageImpl;
4545
import org.springframework.data.domain.Pageable;
46+
import org.springframework.data.domain.Sort;
4647
import org.springframework.data.web.PagedResourcesAssembler;
4748
import org.springframework.hateoas.PagedModel;
4849
import org.springframework.hateoas.server.ExposesResourceFor;
@@ -87,6 +88,9 @@ public class TaskExecutionController {
8788

8889
private final TaskSanitizer taskSanitizer = new TaskSanitizer();
8990

91+
private static final List<String> allowedSorts = Arrays.asList("task_execution_id", "task_name", "start_time",
92+
"end_time", "exit_code");
93+
9094
/**
9195
* Creates a {@code TaskExecutionController} that retrieves Task Execution information
9296
* from a the {@link TaskExplorer}
@@ -124,6 +128,7 @@ public TaskExecutionController(TaskExplorer explorer, TaskExecutionService taskE
124128
@ResponseStatus(HttpStatus.OK)
125129
public PagedModel<TaskExecutionResource> list(Pageable pageable,
126130
PagedResourcesAssembler<TaskJobExecutionRel> assembler) {
131+
validatePageable(pageable);
127132
Page<TaskExecution> taskExecutions = this.explorer.findAll(pageable);
128133
Page<TaskJobExecutionRel> result = getPageableRelationships(taskExecutions, pageable);
129134
return assembler.toModel(result, this.taskAssembler);
@@ -141,6 +146,7 @@ public PagedModel<TaskExecutionResource> list(Pageable pageable,
141146
@ResponseStatus(HttpStatus.OK)
142147
public PagedModel<TaskExecutionResource> retrieveTasksByName(@RequestParam("name") String taskName,
143148
Pageable pageable, PagedResourcesAssembler<TaskJobExecutionRel> assembler) {
149+
validatePageable(pageable);
144150
this.taskDefinitionRepository.findById(taskName)
145151
.orElseThrow(() -> new NoSuchTaskDefinitionException(taskName));
146152
Page<TaskExecution> taskExecutions = this.explorer.findTaskExecutionsByName(taskName, pageable);
@@ -257,6 +263,20 @@ private Page<TaskJobExecutionRel> getPageableRelationships(Page<TaskExecution> t
257263
return new PageImpl<>(taskJobExecutionRels, pageable, taskExecutions.getTotalElements());
258264
}
259265

266+
private static void validatePageable(Pageable pageable) {
267+
if (pageable != null) {
268+
Sort sort = pageable.getSort();
269+
if (sort != null) {
270+
for (Sort.Order order : sort) {
271+
String property = order.getProperty();
272+
if (property != null && !allowedSorts.contains(property.toLowerCase())) {
273+
throw new IllegalArgumentException("Sorting column " + order.getProperty() + " not allowed");
274+
}
275+
}
276+
}
277+
}
278+
}
279+
260280
/**
261281
* {@link org.springframework.hateoas.server.RepresentationModelAssembler} implementation that converts
262282
* {@link TaskJobExecutionRel}s to {@link TaskExecutionResource}s.

spring-cloud-dataflow-server-core/src/test/java/org/springframework/cloud/dataflow/server/controller/TaskExecutionControllerTests.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
6363
import org.springframework.web.context.WebApplicationContext;
6464

65+
import static org.hamcrest.CoreMatchers.containsString;
6566
import static org.hamcrest.Matchers.containsInAnyOrder;
6667
import static org.hamcrest.Matchers.hasSize;
6768
import static org.hamcrest.Matchers.is;
@@ -365,4 +366,19 @@ private ResultActions verifyTaskArgs(List<String> expectedArgs, String prefix, R
365366
}
366367
return ra;
367368
}
369+
370+
@Test
371+
public void testSorting() throws Exception {
372+
mockMvc.perform(get("/tasks/executions").param("sort", "TASK_EXECUTION_ID").accept(MediaType.APPLICATION_JSON))
373+
.andExpect(status().isOk());
374+
mockMvc.perform(get("/tasks/executions").param("sort", "task_execution_id").accept(MediaType.APPLICATION_JSON))
375+
.andExpect(status().isOk());
376+
377+
mockMvc.perform(get("/tasks/executions").param("sort", "WRONG_FIELD").accept(MediaType.APPLICATION_JSON))
378+
.andExpect(status().is5xxServerError())
379+
.andExpect(content().string(containsString("Sorting column WRONG_FIELD not allowed")));
380+
mockMvc.perform(get("/tasks/executions").param("sort", "wrong_field").accept(MediaType.APPLICATION_JSON))
381+
.andExpect(status().is5xxServerError())
382+
.andExpect(content().string(containsString("Sorting column wrong_field not allowed")));
383+
}
368384
}

0 commit comments

Comments
 (0)