Copilot commented on code in PR #1815:
URL: https://github.com/apache/struts/pull/1815#discussion_r3654152197


##########
core/src/test/java/org/apache/struts2/interceptor/ActionFileUploadInterceptorTest.java:
##########
@@ -864,6 +887,125 @@ public void testWildcardMatchingWithDynamicParameters() 
throws Exception {
         assertThat(files.get(1).getContentType()).startsWith("image/");
     }
 
+    public void testConcurrentDynamicPoliciesStayIsolatedPerRequest() throws 
Exception {
+        ActionFileUploadInterceptor controlInterceptor = new 
ActionFileUploadInterceptor();
+        container.inject(controlInterceptor);
+
+        MyDynamicFileUploadAction controlAction = new 
MyDynamicFileUploadAction();
+        controlAction.setAllowedMimeTypes("text/plain");
+        container.inject(controlAction);
+
+        try {
+            runUploadAttempt(
+                    controlInterceptor,
+                    controlAction,
+                    createUploadRequest("control.html", "text/html", 
htmlContent),
+                    controlAction.getAllowedMimeTypes()
+            );
+
+            assertThat(controlAction.getUploadFiles()).isNull();
+            assertThat(controlAction.getFieldErrors()).containsKey("file");
+        } finally {
+            controlInterceptor.destroy();
+        }
+
+        CoordinatedActionFileUploadInterceptor sharedInterceptor = new 
CoordinatedActionFileUploadInterceptor();
+        container.inject(sharedInterceptor);
+
+        MyDynamicFileUploadAction plainPolicyAction = new 
MyDynamicFileUploadAction();
+        plainPolicyAction.setAllowedMimeTypes("text/plain");
+        container.inject(plainPolicyAction);
+
+        MyDynamicFileUploadAction htmlPolicyAction = new 
MyDynamicFileUploadAction();
+        htmlPolicyAction.setAllowedMimeTypes("text/html");
+        container.inject(htmlPolicyAction);
+
+        ExecutorService executor = Executors.newFixedThreadPool(2);
+        try {
+            Future<String> plainResult = executor.submit(() -> 
runUploadAttempt(
+                    sharedInterceptor,
+                    plainPolicyAction,
+                    createUploadRequest("plain-policy.html", "text/html", 
htmlContent),
+                    plainPolicyAction.getAllowedMimeTypes()
+            ));
+
+            assertThat(sharedInterceptor.awaitFirstValidation()).isTrue();
+
+            Future<String> htmlResult = executor.submit(() -> runUploadAttempt(
+                    sharedInterceptor,
+                    htmlPolicyAction,
+                    createUploadRequest("html-policy.html", "text/html", 
htmlContent),
+                    htmlPolicyAction.getAllowedMimeTypes()
+            ));
+
+            assertThat(htmlResult.get(10, 
TimeUnit.SECONDS)).isEqualTo("success");
+            sharedInterceptor.releaseFirstValidation();
+            assertThat(plainResult.get(10, 
TimeUnit.SECONDS)).isEqualTo("success");
+        } finally {
+            sharedInterceptor.releaseFirstValidation();
+            executor.shutdownNow();
+            sharedInterceptor.destroy();
+        }
+
+        
assertThat(plainPolicyAction.getAllowedMimeTypes()).isEqualTo("text/plain");
+        assertThat(plainPolicyAction.getUploadFiles()).isNull();
+        assertThat(plainPolicyAction.getFieldErrors()).containsKey("file");
+
+        assertThat(htmlPolicyAction.hasFieldErrors()).isFalse();
+        assertThat(htmlPolicyAction.getUploadFiles()).isNotNull().hasSize(1);
+        
assertThat(htmlPolicyAction.getUploadFiles().get(0).getOriginalName()).isEqualTo("html-policy.html");
+        
assertThat(htmlPolicyAction.getUploadFiles().get(0).getContentType()).isEqualTo("text/html");
+    }
+
+    private String runUploadAttempt(ActionFileUploadInterceptor 
actionFileUploadInterceptor,
+                                    MyDynamicFileUploadAction action,
+                                    MockHttpServletRequest uploadRequest,
+                                    String allowedTypes) throws Exception {
+        MultiPartRequestWrapper multiPartRequest = 
createMultipartRequest(uploadRequest, -1, -1, 3, -1);
+        ValueStack valueStack = 
container.getInstance(ValueStackFactory.class).createValueStack();
+        valueStack.push(action);
+

Review Comment:
   `runUploadAttempt(..., String allowedTypes)` never uses the `allowedTypes` 
parameter, which makes the helper harder to follow and can mask mistakes in the 
call sites (e.g., passing the wrong policy). Either remove the parameter (and 
update callers) or use it to assert the action’s current upload policy before 
injecting lazy params.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to