Skip to content

Commit b287c0a

Browse files
committed
Fix bandit issues
1 parent 348e8b8 commit b287c0a

File tree

7 files changed

+49
-26
lines changed

7 files changed

+49
-26
lines changed

.bandit

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[bandit]
2+
# Configuration file for Bandit security scanner
3+
# See: https://bandit.readthedocs.io/en/latest/configuration/index.html
4+
5+
exclude_dirs = ['/tests', '/test', '/.venv', '/venv', '.git']
6+
tests =
7+
skips =
8+
9+
[bandit-assert_used:B101]
10+
# Skip assert checks - we use asserts only for input validation in non-critical paths
11+
skips =
12+
13+
[bandit-hardcoded_bind_all_interfaces:B104]
14+
# 0.0.0.0 is intentional for server services (stratum proxy, dashboard)
15+
# These are server-side network services, not client connections
16+
# Binding to all interfaces is the correct behavior for public-facing services
17+
skips =

kcn_proxy/state/updater.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import time, os, base58
1+
import time, os, base58, logging
22
from aiohttp import ClientSession
33
from ..rpc import kcn as rpc_kcn
44
from ..consensus.merkle import merkle_root_from_txids_le, merkle_branch_for_index0
@@ -8,6 +8,8 @@
88
)
99
from ..consensus.auxpow import refresh_aux_job
1010

11+
logger = logging.getLogger(__name__)
12+
1113

1214
async def update_once(state, settings, http: ClientSession, force_update: bool = False):
1315
ROLL_SECONDS = getattr(settings, "ntime_roll", 30)
@@ -168,8 +170,8 @@ async def update_once(state, settings, http: ClientSession, force_update: bool =
168170
from ..stratum.session import hashratedict
169171

170172
hashratedict.pop(wid, None)
171-
except Exception:
172-
pass
173+
except Exception as e:
174+
logger.debug("Failed to remove worker %s from hashrate tracker: %s", wid, e)
173175
else:
174176
alive.add(sess)
175177
state.all_sessions = alive

kcn_proxy/stratum/server.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ async def start_server(state, settings):
5555
# Force an initial tick so the (empty) state file is created promptly
5656
try:
5757
await _vardiff_mod.vardiff_manager.tick()
58-
except Exception:
59-
pass
58+
except Exception as e:
59+
logger.debug("Initial vardiff tick failed: %s", e)
6060

6161
# Periodic tick task
6262
async def _vardiff_tick_loop():
@@ -66,8 +66,8 @@ async def _vardiff_tick_loop():
6666
try:
6767
await asyncio.sleep(30)
6868
await _vardiff_mod.vardiff_manager.tick()
69-
except Exception:
70-
pass
69+
except Exception as e:
70+
logger.debug("Periodic vardiff tick failed: %s", e)
7171

7272
import asyncio
7373

kcn_proxy/stratum/session.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -394,8 +394,8 @@ async def connection_lost(self):
394394
wid = getattr(self, "_worker_id", None)
395395
if wid:
396396
hashrate_tracker.remove_worker(wid)
397-
except Exception:
398-
pass
397+
except Exception as e:
398+
self.logger.debug("Failed to remove worker from hashrate tracker: %s", e)
399399

400400
self._state.new_sessions.discard(self)
401401
self._state.all_sessions.discard(self)
@@ -405,8 +405,8 @@ async def connection_lost(self):
405405
except TypeError:
406406
try:
407407
super().connection_lost()
408-
except Exception:
409-
pass
408+
except Exception as e:
409+
self.logger.debug("Error calling connection_lost: %s", e)
410410

411411
async def handle_subscribe(self, *args):
412412
if self not in self._state.all_sessions:
@@ -570,8 +570,8 @@ async def _keepalive_loop(self):
570570
if abs(vd - difficulty) / max(difficulty, 1e-9) >= 0.05:
571571
difficulty = vd
572572
self._share_difficulty = vd
573-
except Exception:
574-
pass
573+
except Exception as e:
574+
self.logger.debug("Vardiff adjustment failed: %s", e)
575575
await self.send_notification("mining.set_difficulty", (difficulty,))
576576
self._last_activity = loop.time()
577577
self.logger.debug(
@@ -719,8 +719,8 @@ async def handle_submit(self, *args, **kwargs):
719719
# Record rejected share (confidence accounting) using assigned diff
720720
try:
721721
hashrate_tracker.add_share(worker, sent_diff, accepted=False)
722-
except Exception:
723-
pass
722+
except Exception as e:
723+
self.logger.debug("Failed to record rejected share: %s", e)
724724
if self._debug_shares:
725725
self.logger.error(
726726
"Low difficulty share: shareDiff=%.18f minerDiff=%.18f",
@@ -744,8 +744,8 @@ async def handle_submit(self, *args, **kwargs):
744744
await _vardiff_mod.vardiff_manager.record_share(
745745
worker, share_difficulty=sent_diff
746746
)
747-
except Exception:
748-
pass
747+
except Exception as e:
748+
self.logger.debug("Failed to record share for vardiff: %s", e)
749749

750750
# Log share statistics asynchronously
751751
import time

kcn_proxy/stratum/vardiff.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import time
22
import asyncio
3+
import logging
34
from collections import deque
45
from dataclasses import dataclass
56
from typing import Deque, Dict, Optional
67

8+
logger = logging.getLogger(__name__)
9+
710

811
@dataclass
912
class MinerState:
@@ -156,8 +159,8 @@ def _maybe_retarget(self, st: MinerState):
156159
cap = chain_diff * headroom
157160
if new_diff > cap:
158161
new_diff = cap
159-
except Exception:
160-
pass
162+
except Exception as e:
163+
logger.debug("Error adjusting difficulty for chain headroom: %s", e)
161164

162165
# Apply only if material (>5%) change
163166
if abs(new_diff - st.difficulty) / max(st.difficulty, 1e-12) >= 0.05:
@@ -203,8 +206,8 @@ def _save_state(self):
203206
}
204207
with open(self.state_path, "w") as f:
205208
json.dump(data, f)
206-
except Exception:
207-
pass
209+
except Exception as e:
210+
logger.debug("Failed to save vardiff state: %s", e)
208211

209212
def _load_state(self):
210213
try:
@@ -223,8 +226,8 @@ def _load_state(self):
223226
last_retarget=vd.get("last_retarget", now),
224227
ema_interval=vd.get("ema_interval"),
225228
)
226-
except Exception:
227-
pass
229+
except Exception as e:
230+
logger.debug("Failed to load vardiff state: %s", e)
228231

229232
def export_state(self) -> dict:
230233
return {

kcn_proxy/utils/enc.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33

44
def var_int(i: int) -> bytes:
5-
assert i >= 0
5+
if i < 0:
6+
raise ValueError(f"var_int requires non-negative integer, got {i}")
67
if i < 0xFD:
78
return i.to_bytes(1, "little")
89
if i <= 0xFFFF:

kcn_proxy/web/api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -559,8 +559,8 @@ async def lcn_hash_fix_status():
559559
row = await cur.fetchone()
560560
if row:
561561
bad_hash_count = row[0]
562-
except Exception:
563-
pass # DB might not exist or table not created
562+
except Exception as e:
563+
logger.debug("Failed to query bad hash count: %s", e)
564564

565565
needs_fix = bad_hash_count > 0
566566
return JSONResponse(

0 commit comments

Comments
 (0)