Skip to content

Commit 5f0015d

Browse files
committed
fix: replace bare except Exception with specific exception types (bandit B110/B112)
1 parent 35c4b5f commit 5f0015d

2 files changed

Lines changed: 12 additions & 8 deletions

File tree

bases/renku_data_services/mcp_api/main.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,11 @@ def _load_rnk_token() -> str | None:
9191
continue # token is for a different deployment
9292
if payload.get("exp") and time.time() > int(payload["exp"]) - 60:
9393
continue # token is expired or expires in < 60 s
94-
except Exception:
95-
pass # accept token anyway if JWT decode fails
94+
except (ValueError, KeyError, IndexError):
95+
# JWT decode failed — accept the token anyway and let Keycloak validate it.
96+
pass
9697
return access
97-
except Exception:
98+
except (OSError, json.JSONDecodeError, KeyError):
9899
continue
99100
return None
100101

@@ -115,8 +116,8 @@ def _resolve_token() -> str:
115116
entry = json.loads(f.read_text()).get(_base_url(), {})
116117
if t := entry.get("access_token") or entry.get("token"):
117118
return t
118-
except Exception:
119-
pass
119+
except (OSError, json.JSONDecodeError, KeyError, AttributeError):
120+
pass # malformed or unreadable credential file — try next source
120121

121122
# 3. Official rnk CLI token file
122123
if t := _load_rnk_token():
@@ -254,7 +255,7 @@ async def _run_stdio() -> None:
254255
def _run_http() -> None:
255256
import uvicorn
256257

257-
host = os.environ.get("MCP_HOST", "0.0.0.0")
258+
host = os.environ.get("MCP_HOST", "0.0.0.0") # nosec B104 — intentional for server deployment
258259
port = int(os.environ.get("MCP_PORT", "9000"))
259260
app = _build_http_app(_deps)
260261
logger.info("Starting Renku MCP server (HTTP) on %s:%s", host, port)

bases/renku_data_services/mcp_api/server.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ def _token(ctx: Context) -> str:
4040
if t:
4141
set_current_token(t) # cache for this session once found
4242
return t
43-
except Exception:
43+
except RuntimeError:
44+
# _resolve_token raises RuntimeError when no token is available;
45+
# return empty string so the tool call proceeds and the API returns 401.
4446
pass
4547
return ""
4648

@@ -830,7 +832,8 @@ async def job_run(
830832
if started_at:
831833
age = time.time() - datetime.datetime.fromisoformat(started_at.replace("Z", "+00:00")).timestamp()
832834
created = age < 60
833-
except Exception:
835+
except (ValueError, TypeError, AttributeError):
836+
# Malformed or missing started_at — assume created to be safe.
834837
pass
835838
data["_created"] = created
836839
return data

0 commit comments

Comments
 (0)