|
19 | 19 | import java.net.URISyntaxException; |
20 | 20 | import java.nio.file.Files; |
21 | 21 | import java.nio.file.Paths; |
| 22 | +import java.nio.file.Path; |
22 | 23 | import java.security.Security; |
23 | 24 | import java.util.Base64; |
24 | 25 | import java.util.Properties; |
@@ -546,15 +547,41 @@ public final ManagedChannelBuilder<?> newChannelBuilder() throws IOException { |
546 | 547 | } |
547 | 548 |
|
548 | 549 | 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); |
551 | 554 |
|
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()) |
553 | 560 | .keyManager(new ByteArrayInputStream(Base64.getDecoder().decode(ccb)), |
554 | 561 | new ByteArrayInputStream(Base64.getDecoder().decode(ckb))) |
555 | 562 | .build(); |
556 | 563 | } |
557 | 564 |
|
| 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 | + |
558 | 585 | @Deprecated |
559 | 586 | protected static Response newSuccessResponse(final String message, final byte[] payload) { |
560 | 587 | return ResponseUtils.newSuccessResponse(message, payload); |
|
0 commit comments