gnodet-bot commented on code in PR #26782:
URL: https://github.com/apache/camel/pull/26782#discussion_r4102897852


##########
components/camel-pqc/src/test/java/org/apache/camel/component/pqc/lifecycle/FileBasedKeyLifecycleManagerPathTest.java:
##########
@@ -0,0 +1,84 @@
+/*
+ * 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.apache.camel.component.pqc.lifecycle;
+
+import java.nio.file.Files;
+import java.nio.file.Path;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * keyId values reach the file-based key store from message headers 
(CamelPQCKeyId / CamelPQCNewKeyId). The manager must
+ * keep every key file inside its configured directory regardless of the 
supplied keyId.
+ */
+class FileBasedKeyLifecycleManagerPathTest {
+
+    @Test
+    void rejectsKeyIdWithParentTraversal(@TempDir Path tempDir) throws 
Exception {
+        Path keyDir = tempDir.resolve("keys");
+        FileBasedKeyLifecycleManager manager = new 
FileBasedKeyLifecycleManager(keyDir.toString());
+
+        // A sentinel file one level above the key directory that an 
unconfined "../victim" keyId would target
+        Path sentinel = tempDir.resolve("victim.private.json");
+        Files.writeString(sentinel, "{}");
+
+        assertThrows(IllegalArgumentException.class, () -> 
manager.getKey("../victim"));
+        assertThrows(IllegalArgumentException.class, () -> 
manager.deleteKey("../victim"));
+
+        assertTrue(Files.exists(sentinel), "a file outside the key directory 
must not be reachable via keyId");
+    }
+
+    @Test
+    void rejectsAbsoluteSeparatorAndBlankKeyIds(@TempDir Path tempDir) throws 
Exception {
+        Path keyDir = tempDir.resolve("keys");
+        FileBasedKeyLifecycleManager manager = new 
FileBasedKeyLifecycleManager(keyDir.toString());
+
+        assertThrows(IllegalArgumentException.class, () -> 
manager.getKeyMetadata("sub/evil"));
+        assertThrows(IllegalArgumentException.class, () -> 
manager.getKeyMetadata("/etc/evil"));
+        assertThrows(IllegalArgumentException.class,
+                () -> 
manager.getKeyMetadata(tempDir.resolve("abs").toString()));
+        assertThrows(IllegalArgumentException.class, () -> 
manager.getKeyMetadata("sub/evil"));
+        assertThrows(IllegalArgumentException.class, () -> 
manager.getKeyMetadata("/etc/evil"));
+        assertThrows(IllegalArgumentException.class,
+                () -> 
manager.getKeyMetadata(tempDir.resolve("abs").toString()));
+        assertThrows(IllegalArgumentException.class, () -> 
manager.getKeyMetadata(""));

Review Comment:
   🔍 **Nit — duplicate assertions.** Lines 58–60 are exact copies of lines 
55–57 (same `"sub/evil"`, `"/etc/evil"`, `tempDir.resolve("abs")`). Looks like 
the suggestion block was applied on top of the existing lines rather than 
replacing them. Harmless but noisy — drop the duplicates when convenient.



##########
components/camel-pqc/src/test/java/org/apache/camel/component/pqc/lifecycle/FileBasedKeyLifecycleManagerPathTest.java:
##########
@@ -0,0 +1,84 @@
+/*
+ * 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.apache.camel.component.pqc.lifecycle;
+
+import java.nio.file.Files;
+import java.nio.file.Path;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * keyId values reach the file-based key store from message headers 
(CamelPQCKeyId / CamelPQCNewKeyId). The manager must
+ * keep every key file inside its configured directory regardless of the 
supplied keyId.
+ */
+class FileBasedKeyLifecycleManagerPathTest {
+
+    @Test
+    void rejectsKeyIdWithParentTraversal(@TempDir Path tempDir) throws 
Exception {
+        Path keyDir = tempDir.resolve("keys");
+        FileBasedKeyLifecycleManager manager = new 
FileBasedKeyLifecycleManager(keyDir.toString());
+
+        // A sentinel file one level above the key directory that an 
unconfined "../victim" keyId would target
+        Path sentinel = tempDir.resolve("victim.private.json");
+        Files.writeString(sentinel, "{}");
+
+        assertThrows(IllegalArgumentException.class, () -> 
manager.getKey("../victim"));
+        assertThrows(IllegalArgumentException.class, () -> 
manager.deleteKey("../victim"));
+
+        assertTrue(Files.exists(sentinel), "a file outside the key directory 
must not be reachable via keyId");
+    }
+
+    @Test
+    void rejectsAbsoluteSeparatorAndBlankKeyIds(@TempDir Path tempDir) throws 
Exception {
+        Path keyDir = tempDir.resolve("keys");
+        FileBasedKeyLifecycleManager manager = new 
FileBasedKeyLifecycleManager(keyDir.toString());
+
+        assertThrows(IllegalArgumentException.class, () -> 
manager.getKeyMetadata("sub/evil"));
+        assertThrows(IllegalArgumentException.class, () -> 
manager.getKeyMetadata("/etc/evil"));
+        assertThrows(IllegalArgumentException.class,
+                () -> 
manager.getKeyMetadata(tempDir.resolve("abs").toString()));
+        assertThrows(IllegalArgumentException.class, () -> 
manager.getKeyMetadata("sub/evil"));
+        assertThrows(IllegalArgumentException.class, () -> 
manager.getKeyMetadata("/etc/evil"));
+        assertThrows(IllegalArgumentException.class,
+                () -> 
manager.getKeyMetadata(tempDir.resolve("abs").toString()));
+        assertThrows(IllegalArgumentException.class, () -> 
manager.getKeyMetadata(""));
+        // NUL character — caught by character guard
+        assertThrows(IllegalArgumentException.class, () -> 
manager.getKeyMetadata("evil\0inject"));
+    }
+
+    @Test
+    void allowsPlainKeyId(@TempDir Path tempDir) throws Exception {
+        Path keyDir = tempDir.resolve("keys");
+        FileBasedKeyLifecycleManager manager = new 
FileBasedKeyLifecycleManager(keyDir.toString());
+
+        // A normal flat keyId is accepted: metadata for an absent key returns 
null rather than being rejected

Review Comment:
   🔍 **Nit — duplicate comment.** This line is an exact copy of line 72. Same 
copy-paste artefact as above.



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