Skip to content

Commit 8f240d9

Browse files
authored
chore: Prevent stack trace exposure in the tck error error response (hiero-ledger#2437)
Signed-off-by: Manish Dait <daitmanish88@gmail.com>
1 parent c880f35 commit 8f240d9

2 files changed

Lines changed: 22 additions & 10 deletions

File tree

tck/errors.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,19 +105,19 @@ def wrapper(*args, **kwargs):
105105
except JsonRpcError:
106106
raise
107107
except PrecheckError as e:
108-
logger.error(f"PrecheckError (status: {ResponseCode(e.status).name}, method: {func.__name__})")
108+
logger.error(f"PrecheckError (method: {func.__name__}, status: {ResponseCode(e.status).name})")
109109
raise JsonRpcError.hiero_error({"status": ResponseCode(e.status).name}) from e
110110

111111
except ReceiptStatusError as e:
112-
logger.error(f"ReceiptStatusError (status: {ResponseCode(e.status).name}, method: {func.__name__})")
112+
logger.error(f"ReceiptStatusError (method: {func.__name__}, status: {ResponseCode(e.status).name})")
113113
raise JsonRpcError.hiero_error({"status": ResponseCode(e.status).name}) from e
114114

115115
except MaxAttemptsError as e:
116-
logger.error(f"MaxAttemptsError (method: {func.__name__})")
117-
raise JsonRpcError.hiero_error(message=str(e)) from e
116+
logger.error(f"MaxAttemptsError (method: {func.__name__}, error: {str(e)})")
117+
raise JsonRpcError.hiero_error() from e
118118

119119
except Exception as e:
120-
logger.exception("Unhandled error in RPC handler")
121-
raise JsonRpcError.internal_error(message="Internal error") from e
120+
logger.error(f"InternalError (method: {func.__name__}) error: {str(e)}")
121+
raise JsonRpcError.internal_error() from e
122122

123123
return wrapper

tck/handlers/registry.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from __future__ import annotations
66

77
import inspect
8+
import logging
89
from collections.abc import Callable
910
from dataclasses import asdict, fields as dc_fields
1011
from typing import Any, get_type_hints
@@ -13,6 +14,9 @@
1314
from tck.protocol import build_json_rpc_error_response
1415

1516

17+
logger = logging.getLogger(__name__)
18+
19+
1620
# A global _HANDLERS dict to store method name -> handler function mappings
1721
_HANDLERS: dict[str, Callable] = {}
1822

@@ -43,7 +47,10 @@ def dispatch(method_name: str, params: Any) -> Any:
4347
handler = get_handler(method_name)
4448

4549
if handler is None:
46-
raise JsonRpcError.method_not_found_error(message=f"Method not found: {method_name}")
50+
logger.warning(
51+
f"MethodNotFoundError (method: {repr(method_name)}) error: The requested RPC method is not registered."
52+
)
53+
raise JsonRpcError.method_not_found_error()
4754

4855
try:
4956
target_func = getattr(handler, "__wrapped__", handler)
@@ -58,26 +65,31 @@ def dispatch(method_name: str, params: Any) -> Any:
5865
param_type = hints.get(param_name, parameters[0].annotation)
5966
params = param_type.parse_json_params(params)
6067
except (TypeError, ValueError) as e:
68+
logger.error(f"InvalidParamsError (method: {repr(method_name)}) error: {str(e)}")
6169
raise JsonRpcError.invalid_params_error(data=str(e)) from e
6270

6371
result = handler(params)
6472

6573
return parse_result(result)
6674

67-
except JsonRpcError:
75+
except JsonRpcError as e:
76+
logger.error(f"JsonRpcError (method: {repr(method_name)}) error: {str(e)}")
6877
raise
6978
except Exception as e:
70-
raise JsonRpcError.internal_error(data=str(e)) from e
79+
logger.error(f"InternalError (method: {repr(method_name)}) error: {str(e)}")
80+
raise JsonRpcError.internal_error() from e
7181

7282

7383
def safe_dispatch(method_name: str, params: Any, request_id: str | int | None) -> Any | dict[str, Any]:
7484
"""Safely dispatch the request and handle exceptions."""
7585
try:
7686
return dispatch(method_name, params)
7787
except JsonRpcError as e:
88+
logger.error(f"JsonRpcError (method: {repr(method_name)}) error: {str(e)}")
7889
return build_json_rpc_error_response(e, request_id)
7990
except Exception as e:
80-
error = JsonRpcError.internal_error(data=str(e))
91+
logger.error(f"InternalError (method: {repr(method_name)}) error: {str(e)}")
92+
error = JsonRpcError.internal_error()
8193
return build_json_rpc_error_response(error, request_id)
8294

8395

0 commit comments

Comments
 (0)