davsclaus commented on code in PR #26782: URL: https://github.com/apache/camel/pull/26782#discussion_r4112510670
########## 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")); Review Comment: Lines 59-62 are exact duplicates of 55-58 (leftover from applying the suggestion alongside the existing lines). Please drop them. ########## 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 + // A normal flat keyId is accepted: metadata for an absent key returns null rather than being rejected Review Comment: Duplicate of the comment on line 73. ########## 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 + // A normal flat keyId is accepted: metadata for an absent key returns null rather than being rejected + assertNull(manager.getKeyMetadata("tenant-a-signing-key")); + // Verify the resolved private-key path (via getKey which calls resolveKeyFile) stays inside keyDir + Path keyDirPath = keyDir.toAbsolutePath().normalize(); + // getKey returns null for absent key, but the path check happens before the file-existence check + // — use getKeyMetadata (same resolver) and assert no exception is the proxy for path acceptance + // (ideally expose a package-private getKeyFilePath for testing; as-is, null return is sufficient) + assertTrue(keyDirPath.toString().startsWith(keyDirPath.getParent().toString()), Review Comment: This can never fail: every path starts with its own parent, and it never touches `manager`. The comments above it say as much. I'd remove lines 76-82. If you want a real positive-path assertion, make `resolveKeyFile` package-private and assert `resolveKeyFile("tenant-a-signing-key", ".private.json").startsWith(keyDir.toAbsolutePath().normalize())`. Otherwise, the `assertNull` on line 75 is enough. ########## docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_23.adoc: ########## @@ -2805,6 +2805,17 @@ are unaffected. Routes that set the header by its literal string name, or that u `allowTemplateFromHeader=true` with the old header names, must switch to the new `Camel`-prefixed names. +=== camel-pqc - FileBasedKeyLifecycleManager restricts keyId to a flat file name + +`FileBasedKeyLifecycleManager` now confines every key file to its configured key directory. A `keyId` +(supplied through the `CamelPQCKeyId` / `CamelPQCNewKeyId` headers) that contains a forward slash, a +backslash, a null character, or that would otherwise resolve outside the key directory is now rejected +with an `IllegalArgumentException`. + +Previously a `keyId` such as `tenant-a/signing` resolved into a subdirectory of the key directory. +Deployments that organised keys that way must switch to a flat `keyId` (for example `tenant-a-signing`). +Only the file-based manager is affected; the in-memory and cloud-backed managers were never file-path +based. Review Comment: A blank line is needed after this line. Otherwise the following `=== camel-debezium …` isn't recognised as a section heading. ```suggestion based. ``` ########## components/camel-pqc/src/main/java/org/apache/camel/component/pqc/lifecycle/FileBasedKeyLifecycleManager.java: ########## @@ -442,19 +442,44 @@ private static void restrictToOwner(Path path) { } 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. + */ + private 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: '" + keyId + "'"); Review Comment: The separator guard at line 476 no longer echoes the raw `keyId`, but this one still does. For consistency: ```suggestion throw new IllegalArgumentException("keyId must resolve inside the key directory (length: " + keyId.length() + ")"); ``` -- 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]
