gitgabrio commented on code in PR #3868:
URL: 
https://github.com/apache/incubator-kie-kogito-runtimes/pull/3868#discussion_r2068076736


##########
addons/common/persistence/filesystem/src/test/java/org/kie/persistence/filesystem/PathUtilsTest.java:
##########
@@ -0,0 +1,78 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.kie.persistence.filesystem;
+
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+import org.junit.jupiter.api.Test;
+import org.kie.kogito.persistence.filesystem.PathUtils;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+class PathUtilsTest {
+
+    @Test
+    void 
testResolveSecure_ThrowsSecurityException_WhenPathDoesNotStartWithBase() {

Review Comment:
   👍 😃 



##########
addons/common/process-svg/src/test/java/org/kie/kogito/svg/AbstractProcessSvgServiceTest.java:
##########
@@ -0,0 +1,82 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.kie.kogito.svg;
+
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.Optional;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.kie.kogito.svg.dataindex.DataIndexClient;
+
+import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
+import static 
org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType;
+
+class AbstractProcessSvgServiceTest {
+    private TestProcessSvgService service;
+
+    @BeforeEach
+    void setUp() {
+        Path securePath = 
Paths.get("src/test/resources/META-INF/processSVG").toAbsolutePath();
+        service = new TestProcessSvgService(null, 
Optional.of(securePath.toString()),
+                "#C0C0C0", "#030303", "#FF0000");
+    }
+
+    @Test
+    void testResolveSecureAcceptsValidPath() {
+        Path base = 
Paths.get("src/test/resources/META-INF/processSVG").toAbsolutePath();
+        Path result = service.callResolveSecure(base, "travels.svg");
+        assertThat(result.startsWith(base)).isTrue();
+    }
+
+    @Test
+    void testResolveSecureRejectsTraversal() {
+        Path base = 
Paths.get("src/test/resources/META-INF/processSVG").toAbsolutePath();
+        assertThatExceptionOfType(SecurityException.class)
+                .isThrownBy(() -> service.callResolveSecure(base, 
"../outside.svg"))
+                .withMessageContaining("Attempted path traversal");
+    }
+
+    @Test
+    void testGetProcessSvgFromValidPath() {
+        Optional<String> svg = service.getProcessSvg("travels");
+        assertThat(svg).isPresent();
+        assertThat(svg.get()).contains("<svg");
+    }
+
+    @Test
+    void testGetProcessSvgFromInvalidPath() {
+        Optional<String> svg = service.getProcessSvg("nonexistent");
+        assertThat(svg).isEmpty();
+    }
+
+    /**
+     * Helper subclass to expose protected resolveSecure() method
+     */
+    static class TestProcessSvgService extends AbstractProcessSvgService {

Review Comment:
   @bncriju Please remove this class and its usage: it is not needed anymore



##########
addons/common/process-svg/src/test/java/org/kie/kogito/svg/PathUtilsTest.java:
##########
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.kie.kogito.svg;
+
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import static org.assertj.core.api.Assertions.*;
+
+class PathUtilsTest {

Review Comment:
   @bncriju
   IINW, this is a duplicated of 
org.kie.kogito.persistence.filesystem.PathUtilsTest: please remove this one 
(see previous comment)



##########
addons/common/process-svg/src/main/java/org/kie/kogito/svg/AbstractProcessSvgService.java:
##########
@@ -68,7 +68,7 @@ public void setSvgResourcesPath(Optional<String> 
svgResourcesPath) {
     public Optional<String> getProcessSvg(String processId) {
         if (svgResourcesPath.isPresent()) {
             Path path = Paths.get(svgResourcesPath.get(), processId + ".svg");
-            if (Files.exists(path)) {
+            if (path.toFile().exists()) {

Review Comment:
   See previous comment.



##########
addons/common/persistence/filesystem/src/main/java/org/kie/kogito/persistence/filesystem/PathUtils.java:
##########
@@ -0,0 +1,33 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.kie.kogito.persistence.filesystem;
+
+import java.nio.file.Path;
+
+public class PathUtils {
+
+    public static Path resolveSecure(Path base, String userProvided) {

Review Comment:
   👍 😃 



##########
addons/common/process-svg/src/main/java/org/kie/kogito/svg/PathUtils.java:
##########
@@ -0,0 +1,33 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.kie.kogito.svg;
+
+import java.nio.file.Path;
+
+public class PathUtils {

Review Comment:
   @bncriju 
   IINW, this is a duplicated of 
org.kie.kogito.persistence.filesystem.PathUtils: please remove this one, and 
fix code accordingly



##########
addons/common/persistence/filesystem/src/main/java/org/kie/kogito/persistence/filesystem/FileSystemProcessInstances.java:
##########
@@ -100,7 +100,7 @@ public boolean exists(String id) {
     @Override
     public void create(String id, ProcessInstance instance) {
         if (isActive(instance)) {
-            Path processInstanceStorage = Paths.get(storage.toString(), id);
+            Path processInstanceStorage = PathUtils.resolveSecure(storage, id);
             if (Files.exists(processInstanceStorage)) {

Review Comment:
   IINW,  `Files.exists()` is one of the critical methods, and it is still 
there: am I missing something ? 



##########
addons/common/persistence/filesystem/src/main/java/org/kie/kogito/persistence/filesystem/FileSystemProcessInstances.java:
##########
@@ -69,7 +69,7 @@ public FileSystemProcessInstances(Process<?> process, Path 
storage, ProcessInsta
     public Optional findById(String id, ProcessInstanceReadMode mode) {
         Path processInstanceStorage = Paths.get(storage.toString(), id);
 
-        if (Files.notExists(processInstanceStorage)) {
+        if (!processInstanceStorage.toFile().exists()) {

Review Comment:
   
[Path.toFIle()](https://docs.oracle.com/javase/8/docs/api/java/nio/file/Path.html#toFile--)
 returns a file, then this method fallback to 
[File.exists()](https://docs.oracle.com/javase/8/docs/api/java/io/File.html#exists--),
 IINW, and then I'm afraid the issue is still there 🤔 



-- 
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]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to