Skip to content

Commit 69d4043

Browse files
author
Bhaskar Ram
committed
Fix potential path traversal vulnerability in SSL context creation
Signed-off-by: Bhaskar Ram <bhaskar.ram@linux.com>
1 parent 1917b78 commit 69d4043

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/ChaincodeBase.java

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.net.URISyntaxException;
2020
import java.nio.file.Files;
2121
import java.nio.file.Paths;
22+
import java.nio.file.Path;
2223
import java.security.Security;
2324
import java.util.Base64;
2425
import java.util.Properties;
@@ -546,15 +547,41 @@ public final ManagedChannelBuilder<?> newChannelBuilder() throws IOException {
546547
}
547548

548549
final SslContext createSSLContext() throws IOException {
549-
final byte[] ckb = Files.readAllBytes(Paths.get(this.tlsClientKeyPath));
550-
final byte[] ccb = Files.readAllBytes(Paths.get(this.tlsClientCertPath));
550+
// Validate and sanitize file paths
551+
Path clientKeyPath = validateAndNormalizePath(this.tlsClientKeyPath);
552+
Path clientCertPath = validateAndNormalizePath(this.tlsClientCertPath);
553+
Path clientRootCertPath = validateAndNormalizePath(this.tlsClientRootCertPath);
551554

552-
return GrpcSslContexts.forClient().trustManager(new File(this.tlsClientRootCertPath))
555+
// Read files using sanitized paths
556+
final byte[] ckb = Files.readAllBytes(clientKeyPath);
557+
final byte[] ccb = Files.readAllBytes(clientCertPath);
558+
559+
return GrpcSslContexts.forClient().trustManager(clientRootCertPath.toFile())
553560
.keyManager(new ByteArrayInputStream(Base64.getDecoder().decode(ccb)),
554561
new ByteArrayInputStream(Base64.getDecoder().decode(ckb)))
555562
.build();
556563
}
557564

565+
private Path validateAndNormalizePath(String pathStr) throws IOException {
566+
if (pathStr == null || pathStr.isEmpty()) {
567+
throw new IllegalArgumentException("File path cannot be null or empty");
568+
}
569+
570+
Path path = Paths.get(pathStr).normalize();
571+
572+
// Check if the path is absolute and exists
573+
if (!path.isAbsolute() || !Files.exists(path)) {
574+
throw new IOException("Invalid or non-existent file path: " + pathStr);
575+
}
576+
577+
// Additional security check: ensure the path doesn't contain any suspicious components
578+
if (path.toString().contains("..")) {
579+
throw new IOException("Potentially malicious file path: " + pathStr);
580+
}
581+
582+
return path;
583+
}
584+
558585
@Deprecated
559586
protected static Response newSuccessResponse(final String message, final byte[] payload) {
560587
return ResponseUtils.newSuccessResponse(message, payload);

0 commit comments

Comments
 (0)