Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .jules/sentinel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## 2025-02-23 - Prevent Exception Details Leakage in Auth Responses
**Vulnerability:** `auth/utils.py`'s `@login_required` decorator exposed internal exception strings (`details: str(e)`) to clients upon authentication failure, risking exposure of database/cache state or internal implementation details.
**Learning:** Returning `str(e)` in Flask error responses provides an easy path for information disclosure when third-party libraries or DB connections fail during auth verification.
**Prevention:** Always log exception details internally via standard logging (`logger.warning` / `logger.error`) and return sanitized generic error responses (e.g. `{'error': 'Unauthorized'}`) to clients.
13 changes: 9 additions & 4 deletions auth/utils.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import bcrypt
import jwt
import logging
import os
import secrets
from datetime import datetime, timedelta
from functools import wraps
from typing import Tuple, Optional
import bcrypt
import jwt
from flask import request, jsonify, g
from flask_jwt_extended import get_jwt_identity, verify_jwt_in_request
from cache_db.redis_client import redis_client
from cache_db.models import User, RefreshToken
from typing import Tuple, Optional

logger = logging.getLogger(__name__)


class PasswordUtils:
Expand Down Expand Up @@ -129,7 +132,9 @@ def decorated_function(*args, **kwargs):
g.user = user_data
return f(*args, **kwargs)
except Exception as e:
return jsonify({'error': 'Unauthorized', 'details': str(e)}), 401
# SECURITY: Do not leak internal exception details to client
logger.warning("Authentication failed: %s", e)
return jsonify({'error': 'Unauthorized'}), 401
return decorated_function


Expand Down
Empty file added src/handoff/__init__.py
Empty file.
49 changes: 49 additions & 0 deletions tests/test_auth_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import sys
from unittest.mock import MagicMock

# Mock external cache_db module before importing auth.utils
cache_db_mock = MagicMock()
sys.modules['cache_db'] = cache_db_mock
sys.modules['cache_db.redis_client'] = cache_db_mock
sys.modules['cache_db.models'] = cache_db_mock

from flask import Flask
from auth.utils import PasswordUtils, JWTUtils, login_required


def test_password_hashing():
pw = "secret_password123"
hashed = PasswordUtils.hash_password(pw)
assert PasswordUtils.verify_password(pw, hashed) is True
assert PasswordUtils.verify_password("wrong_password", hashed) is False


def test_jwt_utils():
access, refresh = JWTUtils.create_tokens("u123", "alice")
decoded_access = JWTUtils.decode_token(access)
assert decoded_access["user_id"] == "u123"
assert decoded_access["username"] == "alice"
assert decoded_access["type"] == "access"


def test_login_required_does_not_leak_details(monkeypatch):
app = Flask(__name__)

@app.route("/protected")
@login_required
def protected():
return "success", 200

def mock_verify():
raise RuntimeError("Sensitive DB exception with stack trace or internal connection details")

import flask_jwt_extended
monkeypatch.setattr(flask_jwt_extended, "verify_jwt_in_request", mock_verify)

client = app.test_client()
response = client.get("/protected")

assert response.status_code == 401
data = response.get_json()
assert data == {"error": "Unauthorized"}
assert "details" not in data