Skip to content

Commit 3379872

Browse files
committed
server: Add validation checks for SSL certificate and key files
Add input validation to verify that SSL certificate and key files exist and are regular files before attempting to create the SSL context. This provides clearer error messages to users when certificate files are missing or invalid, following the same validation pattern used for config files.
1 parent c38172c commit 3379872

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

integrations/mock_server/src/main.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,4 @@
8080
parser.add_argument("--key", type=str, default="server.key", help="SSL Private Key file")
8181

8282
args = parser.parse_args()
83-
run_server(args.port, Path(args.config), Path(args.routing_config_dir), args.cert, args.key)
83+
run_server(args.port, Path(args.config), Path(args.routing_config_dir), Path(args.cert), Path(args.key))

integrations/mock_server/src/server.py

+17-6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import http.server
1616
import logging
17+
import socketserver
1718
import ssl
1819
from pathlib import Path
1920

@@ -65,17 +66,27 @@ def run_server(port: int, config_path: Path, routing_config_dir: Path, cert_path
6566

6667
logging.basicConfig(level=logging.DEBUG, format="[%(levelname)s] %(message)s")
6768

68-
config: Configuration = load_configurations(config_path, routing_config_dir)
69+
if not config_path.is_file():
70+
raise ValueError(f"'{config_path}' is not a file")
6971

70-
logging.info("Server starting on port %s", port)
71-
server_address = ("", port)
72+
if not routing_config_dir.is_dir():
73+
raise ValueError(f"'{routing_config_dir}' is not a directory")
7274

73-
theMockServerHandler = createMockServerHandler(config)
75+
if not cert_path.is_file():
76+
raise ValueError(f"'{cert_path}' is not a file")
7477

75-
httpd = http.server.ThreadingHTTPServer(server_address, theMockServerHandler)
76-
context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
78+
if not key_path.is_file():
79+
raise ValueError(f"'{key_path}' is not a file")
80+
81+
config: Configuration = load_configurations(config_path, routing_config_dir)
82+
server_address: socketserver._AfInetAddress = ("", port)
83+
context: ssl.SSLContext = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
7784
context.load_cert_chain(certfile=cert_path, keyfile=key_path)
7885

86+
theMockServerHandler = createMockServerHandler(config)
87+
httpd = http.server.ThreadingHTTPServer(server_address, theMockServerHandler)
88+
89+
logging.info("Server starting on port %s", port)
7990
with context.wrap_socket(httpd.socket, server_side=True) as httpd.socket:
8091
logging.info("Server started on port %s", port)
8192
logging.info("HTTPS enabled with cert: %s and key: %s", cert_path, key_path)

0 commit comments

Comments
 (0)