This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch camel-4.18.x
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-4.18.x by this push:
new 77ac3c6fb944 [backport camel-4.18.x] CAMEL-24937: camel-pqc - confine
FileBasedKeyLifecycleManager key file resolution to the configured directory
(#29868)
77ac3c6fb944 is described below
commit 77ac3c6fb944f84d57e710a05b0b86d349843824
Author: Guillaume Nodet <[email protected]>
AuthorDate: Fri Nov 13 20:43:43 2026 +0100
[backport camel-4.18.x] CAMEL-24937: camel-pqc - confine
FileBasedKeyLifecycleManager key file resolution to the configured directory
(#29868)
Backport of #26782 to camel-4.18.x.
Co-authored-by: Claude Opus 4.8 <[email protected]>
Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
Co-authored-by: Guillaume Nodet - AI Bot <[email protected]>
---
.../lifecycle/FileBasedKeyLifecycleManager.java | 36 ++++++++--
.../FileBasedKeyLifecycleManagerPathTest.java | 77 ++++++++++++++++++++++
2 files changed, 108 insertions(+), 5 deletions(-)
diff --git
a/components/camel-pqc/src/main/java/org/apache/camel/component/pqc/lifecycle/FileBasedKeyLifecycleManager.java
b/components/camel-pqc/src/main/java/org/apache/camel/component/pqc/lifecycle/FileBasedKeyLifecycleManager.java
index b82de7b880fb..aadcbce396d0 100644
---
a/components/camel-pqc/src/main/java/org/apache/camel/component/pqc/lifecycle/FileBasedKeyLifecycleManager.java
+++
b/components/camel-pqc/src/main/java/org/apache/camel/component/pqc/lifecycle/FileBasedKeyLifecycleManager.java
@@ -75,7 +75,7 @@ public class FileBasedKeyLifecycleManager implements
KeyLifecycleManager {
private final ConcurrentHashMap<String, KeyMetadata> metadataCache = new
ConcurrentHashMap<>();
public FileBasedKeyLifecycleManager(String keyDirectoryPath) throws
IOException {
- this.keyDirectory = Paths.get(keyDirectoryPath);
+ this.keyDirectory =
Paths.get(keyDirectoryPath).toAbsolutePath().normalize();
this.objectMapper = new ObjectMapper();
this.objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
Files.createDirectories(keyDirectory);
@@ -474,19 +474,45 @@ public class FileBasedKeyLifecycleManager implements
KeyLifecycleManager {
}
private Path getPrivateKeyFile(String keyId) {
- return keyDirectory.resolve(keyId + ".private.json");
+ return resolveKeyFile(keyId, ".private.json");
}
private Path getPublicKeyFile(String keyId) {
- return keyDirectory.resolve(keyId + ".public.json");
+ return resolveKeyFile(keyId, ".public.json");
}
private Path getMetadataFile(String keyId) {
- return keyDirectory.resolve(keyId + ".metadata");
+ return resolveKeyFile(keyId, ".metadata");
}
private Path getLegacyKeyFile(String keyId) {
- return keyDirectory.resolve(keyId + ".key");
+ return resolveKeyFile(keyId, ".key");
+ }
+
+ /**
+ * Resolves a key file inside the configured key directory, confining it
to that directory. keyId values arrive from
+ * message headers (CamelPQCKeyId / CamelPQCNewKeyId), so a value
containing path separators, parent references or
+ * an absolute path could otherwise resolve to a location outside the key
directory. keyId is therefore constrained
+ * to a single flat file-name segment, and the resolved path is checked to
lie textually under the key directory
+ * (which is stored absolute and normalized in the constructor).
+ * <p/>
+ * The {@code startsWith} check is textual and does not follow symbolic
links, so a symlink placed inside the key
+ * directory could still point elsewhere. Creating such a link requires
write access to the operator-controlled key
+ * directory, which is outside the header-supplied keyId threat this
guards against.
+ */
+ Path resolveKeyFile(String keyId, String suffix) {
+ if (keyId == null || keyId.isBlank()) {
+ throw new IllegalArgumentException("keyId must not be null or
empty");
+ }
+ if (keyId.indexOf('/') >= 0 || keyId.indexOf('\\') >= 0 ||
keyId.indexOf('\0') >= 0) {
+ throw new IllegalArgumentException("keyId must not contain path
separators (length: " + keyId.length() + ")");
+ }
+ Path resolved = keyDirectory.resolve(keyId + suffix).normalize();
+ if (!resolved.startsWith(keyDirectory)) {
+ throw new IllegalArgumentException(
+ "keyId must resolve inside the key directory (length: " +
keyId.length() + ")");
+ }
+ return resolved;
}
/**
diff --git
a/components/camel-pqc/src/test/java/org/apache/camel/component/pqc/lifecycle/FileBasedKeyLifecycleManagerPathTest.java
b/components/camel-pqc/src/test/java/org/apache/camel/component/pqc/lifecycle/FileBasedKeyLifecycleManagerPathTest.java
new file mode 100644
index 000000000000..6d54536b5877
--- /dev/null
+++
b/components/camel-pqc/src/test/java/org/apache/camel/component/pqc/lifecycle/FileBasedKeyLifecycleManagerPathTest.java
@@ -0,0 +1,77 @@
+/*
+ * 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.assertEquals;
+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(""));
+ // 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
+ assertNull(manager.getKeyMetadata("tenant-a-signing-key"));
+ // and its key file resolves directly inside the key directory
+ Path resolved = manager.resolveKeyFile("tenant-a-signing-key",
".private.json");
+ assertEquals(keyDir.toAbsolutePath().normalize(),
resolved.getParent());
+ assertEquals("tenant-a-signing-key.private.json",
resolved.getFileName().toString());
+ }
+}