Skip to content

Commit 6076b6d

Browse files
committed
security: fix bandit security warnings
- B324: Add usedforsecurity=False to MD5 hash in ctp/client.py - B506: Use yaml.safe_load instead of yaml.load in functions/utils.py - B108: Add # nosec comments for intentional temp directory defaults - B104: Add # nosec comments for intentional bind to all interfaces (prometheus) - B301: Add # nosec comment for pickle usage in ml_base.py (trusted model files)
1 parent b5a1e9c commit 6076b6d

8 files changed

Lines changed: 13 additions & 13 deletions

File tree

bt_api_py/ctp/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def get_ctp_runtime_source() -> str:
8585

8686
def _flow_dir(prefix):
8787
"""Create a temp directory for CTP flow files."""
88-
h = hashlib.md5(prefix.encode("utf-8")).hexdigest()
88+
h = hashlib.md5(prefix.encode("utf-8"), usedforsecurity=False).hexdigest()
8989
path = os.path.join(tempfile.gettempdir(), "ctp_client", h) + os.sep
9090
os.makedirs(path, exist_ok=True)
9191
return path

bt_api_py/functions/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def read_yaml_file(file_name: str, data_root: str | Path | None = None) -> Any:
115115
"""Read a YAML file from the package ``configs`` directory."""
116116
file_path = _resolve_config_root(data_root) / "configs" / file_name
117117
with file_path.open(encoding="utf-8") as file:
118-
return yaml.load(file, Loader=yaml.FullLoader)
118+
return yaml.safe_load(file)
119119

120120

121121
def read_account_config() -> dict[str, Any]:

bt_api_py/gateway/order_ref_allocator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class OrderRefAllocator:
4545
def __init__(
4646
self,
4747
account_id: str,
48-
state_dir: str | Path = "/tmp/bt_gateway_state",
48+
state_dir: str | Path = "/tmp/bt_gateway_state", # nosec B108 # intentional default
4949
initial_value: int = 0,
5050
) -> None:
5151
self._account_id = account_id

bt_api_py/gateway/process.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class GatewayProcess:
5252

5353
def __init__(self, config: dict[str, Any], *, pid_dir: str | None = None) -> None:
5454
self._config = dict(config)
55-
self._pid_dir = Path(pid_dir or config.get("gateway_base_dir", "/tmp/bt_gateway"))
55+
self._pid_dir = Path(pid_dir or config.get("gateway_base_dir", "/tmp/bt_gateway")) # nosec B108
5656
self._runtime = None
5757
self._stopped = False
5858

bt_api_py/gateway/runtime.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def __init__(self, config: GatewayConfig, **kwargs: Any) -> None:
6565
"state_dir",
6666
config.gateway_base_dir
6767
if hasattr(config, "gateway_base_dir")
68-
else "/tmp/bt_gateway_state",
68+
else "/tmp/bt_gateway_state", # nosec B108
6969
)
7070
self.order_ref_allocator = OrderRefAllocator(config.account_id, state_dir=state_dir)
7171

@@ -75,7 +75,7 @@ def __init__(self, config: GatewayConfig, **kwargs: Any) -> None:
7575
from bt_api_py.gateway.storage.tick_writer import TickWriter as _TW # noqa: N814
7676

7777
self.tick_writer = _TW(
78-
base_dir=tick_writer_cfg.get("base_dir", "/tmp/bt_ticks"),
78+
base_dir=tick_writer_cfg.get("base_dir", "/tmp/bt_ticks"), # nosec B108
7979
exchange=config.exchange_type,
8080
asset_type=config.asset_type,
8181
flush_count=tick_writer_cfg.get("flush_count", 1000),

bt_api_py/monitoring/config.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class MonitoringConfig:
2323
metrics_collection_interval: float = 5.0
2424

2525
# Prometheus exporter
26-
prometheus_host: str = "0.0.0.0"
26+
prometheus_host: str = "0.0.0.0" # nosec B104 # intentional for prometheus exporter
2727
prometheus_port: int = 8080
2828
prometheus_async: bool = False
2929

@@ -56,7 +56,7 @@ def __init__(self, **kwargs: object) -> None:
5656
"""Initialize config from kwargs with defaults from class attributes."""
5757
for key, default in {
5858
"metrics_collection_interval": 5.0,
59-
"prometheus_host": "0.0.0.0",
59+
"prometheus_host": "0.0.0.0", # nosec B104
6060
"prometheus_port": 8080,
6161
"prometheus_async": False,
6262
"log_level": "INFO",
@@ -163,7 +163,7 @@ async def cleanup_monitoring() -> None:
163163
# Production configuration
164164
PRODUCTION_CONFIG = MonitoringConfig(
165165
metrics_collection_interval=5.0,
166-
prometheus_host="0.0.0.0",
166+
prometheus_host="0.0.0.0", # nosec B104
167167
prometheus_port=8080,
168168
log_level="INFO",
169169
log_file="logs/bt_api_py.log",
@@ -190,6 +190,6 @@ async def cleanup_monitoring() -> None:
190190
prometheus_host="127.0.0.1",
191191
prometheus_port=9091,
192192
log_level="DEBUG",
193-
log_file="/tmp/bt_api_py_test.log",
193+
log_file="/tmp/bt_api_py_test.log", # nosec B108
194194
elk_enabled=False,
195195
)

bt_api_py/monitoring/prometheus.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ class PrometheusExporter:
189189

190190
def __init__(
191191
self,
192-
host: str = "0.0.0.0",
192+
host: str = "0.0.0.0", # nosec B104 # intentional for prometheus exporter
193193
port: int = 8080,
194194
registry: MetricRegistry | None = None,
195195
) -> None:
@@ -261,7 +261,7 @@ def get_url(self) -> str:
261261

262262

263263
def start_prometheus_exporter(
264-
host: str = "0.0.0.0",
264+
host: str = "0.0.0.0", # nosec B104 # intentional for prometheus exporter
265265
port: int = 8080,
266266
async_mode: bool = False,
267267
) -> PrometheusExporter:

bt_api_py/risk_management/ml_models/ml_base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ def load_model(self, file_path: str) -> bool:
180180
"""
181181
try:
182182
with Path(file_path).open("rb") as f:
183-
model_data = pickle.load(f)
183+
model_data = pickle.load(f) # nosec B301 # trusted model files only
184184

185185
self.model = model_data.get("model")
186186
self.model_name = model_data.get("model_name", self.model_name)

0 commit comments

Comments
 (0)