Skip to content

#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere

Remove unnecessary attributes from ad-hoc pipeline run requests (#8679)

James Brunton7ec1c974624b
diff --git a/app/proprietary/src/main/java/stirling/software/proprietary/policy/controller/AdHocRunRequest.java b/app/proprietary/src/main/java/stirling/software/proprietary/policy/controller/AdHocRunRequest.java
new file mode 100644
index 0000000000..327c8b85fd
--- /dev/null
+++ b/app/proprietary/src/main/java/stirling/software/proprietary/policy/controller/AdHocRunRequest.java
@@ -0,0 +1,15 @@
+package stirling.software.proprietary.policy.controller;
+
+import java.util.List;
+
+import stirling.software.proprietary.policy.model.OutputSpec;
+import stirling.software.proprietary.policy.model.PipelineDefinition;
+import stirling.software.proprietary.policy.model.PipelineStep;
+
+/** The pipeline an ad-hoc run executes, as a client may state it. */
+public record AdHocRunRequest(String name, List<PipelineStep> steps, List<OutputSpec> outputs) {
+
+ PipelineDefinition toDefinition() {
+ return new PipelineDefinition(name, steps, outputs);
+ }
+}
diff --git a/app/proprietary/src/main/java/stirling/software/proprietary/policy/controller/PolicyController.java b/app/proprietary/src/main/java/stirling/software/proprietary/policy/controller/PolicyController.java
index 5cad2d4ff2..19651ab9eb 100644
--- a/app/proprietary/src/main/java/stirling/software/proprietary/policy/controller/PolicyController.java
+++ b/app/proprietary/src/main/java/stirling/software/proprietary/policy/controller/PolicyController.java
@@ -132,10 +132,11 @@ public class PolicyController {
+ " in order asynchronously and returns a run id. Poll the run status"
+ " endpoint and download outputs via /api/v1/general/files/{id}.")
public ResponseEntity<JobResponse<Void>> run(
- @RequestPart("json") PipelineDefinition definition,
+ @RequestPart("json") AdHocRunRequest request,
@RequestParam(value = "policyId", required = false) String policyId,
@Valid @ModelAttribute PolicyRunFiles files)
throws IOException {
+ PipelineDefinition definition = request.toDefinition();
stampPolicyAudit(definition);
requireRunnable(definition);
validateAdHocRun(definition);
@@ -154,10 +155,11 @@ public class PolicyController {
+ " starts and completes, then a terminal 'completed', 'failed',"
+ " 'cancelled', or 'waiting' event carrying the final run view.")
public SseEmitter runStream(
- @RequestPart("json") PipelineDefinition definition,
+ @RequestPart("json") AdHocRunRequest request,
@RequestParam(value = "policyId", required = false) String policyId,
@Valid @ModelAttribute PolicyRunFiles files)
throws IOException {
+ PipelineDefinition definition = request.toDefinition();
stampPolicyAudit(definition);
requireRunnable(definition);
validateAdHocRun(definition);
diff --git a/app/proprietary/src/test/java/stirling/software/proprietary/policy/controller/PolicyControllerTest.java b/app/proprietary/src/test/java/stirling/software/proprietary/policy/controller/PolicyControllerTest.java
index 29e976e89f..cba7bdde00 100644
--- a/app/proprietary/src/test/java/stirling/software/proprietary/policy/controller/PolicyControllerTest.java
+++ b/app/proprietary/src/test/java/stirling/software/proprietary/policy/controller/PolicyControllerTest.java
@@ -71,6 +71,9 @@ import stirling.software.proprietary.policy.source.SourceStore;
import stirling.software.proprietary.policy.trigger.PolicyTriggerManager;
import stirling.software.proprietary.util.SecretMasker;
+import tools.jackson.databind.DeserializationFeature;
+import tools.jackson.databind.json.JsonMapper;
+
@ExtendWith(MockitoExtension.class)
@DisplayName("PolicyController")
class PolicyControllerTest {
@@ -212,7 +215,11 @@ class PolicyControllerTest {
}
private static PipelineDefinition definitionWithStep() {
- return new PipelineDefinition(
+ return requestWithStep().toDefinition();
+ }
+
+ private static AdHocRunRequest requestWithStep() {
+ return new AdHocRunRequest(
"pipe", List.of(new PipelineStep("/api/v1/misc/compress-pdf", null)), List.of());
}
@@ -247,12 +254,42 @@ class PolicyControllerTest {
.thenReturn(handle("run-1"));
ResponseEntity<JobResponse<Void>> response =
- controller.run(definitionWithStep(), null, new PolicyRunFiles());
+ controller.run(requestWithStep(), null, new PolicyRunFiles());
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.ACCEPTED);
assertThat(response.getBody().getJobId()).isEqualTo("run-1");
}
+ @Test
+ @DisplayName("never hands client-supplied routing to the engine")
+ void dropsClientSuppliedRouting() throws Exception {
+ // A routed destination is delivered without an access check, so a request must not be
+ // able to carry one. The app's mapper ignores unknown keys, as this one does.
+ JsonMapper mapper =
+ JsonMapper.builder()
+ .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
+ .build();
+ String json =
+ """
+ {"name": "pipe",
+ "steps": [{"operation": "/api/v1/misc/compress-pdf"}],
+ "routing": [{"rule": {"field": "document.extension", "operator": "matches-any",
+ "values": ["pdf"], "outputId": "unused"},
+ "destination": {"type": "s3", "options": {"connectionId": 1}}}]}
+ """;
+ assertThat(mapper.readValue(json, PipelineDefinition.class).routing()).hasSize(1);
+ when(policyRunner.runAdHoc(any(), any(), eq(PolicyProgressListener.NOOP)))
+ .thenReturn(handle("run-1"));
+
+ controller.run(
+ mapper.readValue(json, AdHocRunRequest.class), null, new PolicyRunFiles());
+
+ ArgumentCaptor<PipelineDefinition> submitted =
+ ArgumentCaptor.forClass(PipelineDefinition.class);
+ verify(policyRunner).runAdHoc(submitted.capture(), any(), any());
+ assertThat(submitted.getValue().routing()).isEmpty();
+ }
+
@Test
@DisplayName("feeds the editor source, scoped to the caller's team")
void adHocRunFeedsTheEditorSource() throws Exception {
@@ -260,7 +297,7 @@ class PolicyControllerTest {
.thenReturn(handle("run-1"));
when(sourceAccessGuard.currentTeamId()).thenReturn(3L);
- controller.run(definitionWithStep(), null, new PolicyRunFiles());
+ controller.run(requestWithStep(), null, new PolicyRunFiles());
verify(docCounter).record(EditorSource.counterKey(3L), 0L);
}
@@ -268,7 +305,7 @@ class PolicyControllerTest {
@Test
@DisplayName("rejects a pipeline with no steps")
void rejectsEmptyPipeline() {
- PipelineDefinition empty = new PipelineDefinition("pipe", List.of(), List.of());
+ AdHocRunRequest empty = new AdHocRunRequest("pipe", List.of(), List.of());
assertThatThrownBy(() -> controller.run(empty, null, new PolicyRunFiles()))
.isInstanceOf(ResponseStatusException.class)
@@ -283,16 +320,16 @@ class PolicyControllerTest {
void rejectsUnauthorizedAdHocOutput() {
// The confused-deputy guard: an S3 output referencing a connection the caller may not
// use is validated here (principal present) and refused before any worker dispatch.
- PipelineDefinition definition =
- new PipelineDefinition(
+ AdHocRunRequest request =
+ new AdHocRunRequest(
"pipe",
List.of(new PipelineStep("/api/v1/misc/compress-pdf", null)),
- new OutputSpec("s3", Map.of("connectionId", 999)));
+ List.of(new OutputSpec("s3", Map.of("connectionId", 999))));
doThrow(new IllegalArgumentException("unknown or inaccessible s3 connection"))
.when(policyValidator)
.validateOutput(any(), any());
- assertThatThrownBy(() -> controller.run(definition, null, new PolicyRunFiles()))
+ assertThatThrownBy(() -> controller.run(request, null, new PolicyRunFiles()))
.isInstanceOf(ResponseStatusException.class)
.satisfies(
e ->
@@ -312,7 +349,7 @@ class PolicyControllerTest {
when(policyRunner.runAdHoc(any(), any(), eq(PolicyProgressListener.NOOP)))
.thenReturn(handle("run-1"));
- controller.run(definitionWithStep(), "pol-1", new PolicyRunFiles());
+ controller.run(requestWithStep(), "pol-1", new PolicyRunFiles());
verify(assetResolver).resolve(eq(p), any());
}
@@ -328,7 +365,7 @@ class PolicyControllerTest {
when(policyRunner.runAdHoc(any(), any(), eq(PolicyProgressListener.NOOP)))
.thenReturn(handle("run-1"));
- controller.run(definitionWithStep(), "pol-1", new PolicyRunFiles());
+ controller.run(requestWithStep(), "pol-1", new PolicyRunFiles());
verify(assetResolver, never()).resolve(any(), any());
verify(policyStore, never()).get(any());
@@ -346,7 +383,7 @@ class PolicyControllerTest {
when(policyRunner.runAdHoc(any(), any(), eq(PolicyProgressListener.NOOP)))
.thenReturn(handle("run-1"));
- controller.run(definitionWithStep(), "pol-1", new PolicyRunFiles());
+ controller.run(requestWithStep(), "pol-1", new PolicyRunFiles());
verify(assetResolver).resolve(eq(p), any());
}
@@ -362,7 +399,7 @@ class PolicyControllerTest {
when(policyRunner.runAdHoc(any(), any(), any())).thenReturn(handle("run-2"));
SseEmitter emitter =
- controller.runStream(definitionWithStep(), null, new PolicyRunFiles());
+ controller.runStream(requestWithStep(), null, new PolicyRunFiles());
assertThat(emitter).isNotNull();
}
@@ -370,7 +407,7 @@ class PolicyControllerTest {
@Test
@DisplayName("rejects a pipeline with no steps")
void rejectsEmpty() {
- PipelineDefinition empty = new PipelineDefinition("pipe", List.of(), List.of());
+ AdHocRunRequest empty = new AdHocRunRequest("pipe", List.of(), List.of());
assertThatThrownBy(() -> controller.runStream(empty, null, new PolicyRunFiles()))
.isInstanceOf(ResponseStatusException.class);