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
40 changes: 38 additions & 2 deletions backend/app/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ class InvalidCredentialsError(AuthError):
"""Raised for an unknown username or an incorrect password."""


class UnknownUserError(AuthError):
"""Raised when a session is requested for a username that is not registered."""


class InvalidUsernameError(AuthError):
"""Raised when a username cannot be represented safely."""

Expand Down Expand Up @@ -323,6 +327,16 @@ def verify_credentials(self, username: str, password: str) -> str:

def create_session(self, username: str) -> tuple[str, int]:
normalized = normalize_username(username)
with self._connect() as connection:
row = connection.execute(
"SELECT 1 FROM users WHERE username = ?",
(normalized,),
).fetchone()
if row is None:
# Check first so a missing user is reported as such, instead of
# surfacing as a foreign-key IntegrityError retried as a
# "could not allocate a unique authentication token" failure.
raise UnknownUserError("user does not exist")
now = int(self._clock())
expires_at = now + self.session_ttl_seconds
for _ in range(3):
Expand All @@ -338,7 +352,13 @@ def create_session(self, username: str) -> tuple[str, int]:
(self._token_hash(token), normalized, now, expires_at),
)
return token, expires_at
except sqlite3.IntegrityError:
except sqlite3.IntegrityError as exc:
# Only a token-hash collision is worth retrying; any other
# constraint failure (e.g. a foreign key, if the user was
# deleted between the check above and the insert) must
# surface immediately instead of being misreported.
if not _is_unique_constraint_error(exc):
raise
continue
raise RuntimeError("could not allocate a unique authentication token")

Expand Down Expand Up @@ -593,6 +613,16 @@ def create_session(self, username: str) -> tuple[str, int]:
from sqlalchemy.exc import IntegrityError

normalized = normalize_username(username)
with self.engine.connect() as connection:
row = connection.execute(
text("SELECT 1 FROM medguide_users WHERE username = :username"),
{"username": normalized},
).first()
if row is None:
# Check first so a missing user is reported as such, instead of
# surfacing as a foreign-key IntegrityError retried as a
# "could not allocate a unique authentication token" failure.
raise UnknownUserError("user does not exist")
now = int(self._clock())
expires_at = now + self.session_ttl_seconds
for _ in range(3):
Expand All @@ -617,7 +647,13 @@ def create_session(self, username: str) -> tuple[str, int]:
},
)
return token, expires_at
except IntegrityError:
except IntegrityError as exc:
# Only a token-hash collision is worth retrying; any other
# constraint failure (e.g. a foreign key, if the user was
# deleted between the check above and the insert) must
# surface immediately instead of being misreported.
if not _is_unique_constraint_error(exc):
raise
continue
raise RuntimeError("could not allocate a unique authentication token")

Expand Down
14 changes: 11 additions & 3 deletions backend/app/embeddings.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ def _embedding_timeout() -> float:
return 15.0


_EMBEDDING_ENDPOINT_SUFFIXES = ("/embeddings", "/chat/completions", "/chaat/completions")
_EMBEDDING_ENDPOINT_SUFFIXES = ("/embeddings", "/chat/completions")


def _normalize_embedding_base_url(value: str | None) -> str | None:
Expand All @@ -299,11 +299,19 @@ def _normalize_embedding_base_url(value: str | None) -> str | None:
if not raw:
return None
parsed = urlsplit(raw)
if not parsed.scheme or not parsed.netloc:
if parsed.scheme.lower() not in {"http", "https"} or not parsed.netloc:
raise ValueError("Embedding base URL must include an HTTP(S) host")
path = parsed.path.rstrip("/")
for suffix in _EMBEDDING_ENDPOINT_SUFFIXES:
if path.lower().endswith(suffix):
path = path[: -len(suffix)].rstrip("/")
break
return urlunsplit((parsed.scheme, parsed.netloc, path, parsed.query, parsed.fragment)).rstrip("/")
else:
# See _normalize_base_url: a mistyped endpoint must fail closed
# instead of being silently truncated to a wrong base URL.
if path.lower().endswith("/completions"):
raise ValueError("Embedding base URL looks like a mistyped completion endpoint")
# Query strings and fragments are never part of a base URL; drop them
# instead of forwarding them (and any credentials they carry) with
# every request.
return urlunsplit((parsed.scheme, parsed.netloc, path, "", "")).rstrip("/")
21 changes: 17 additions & 4 deletions backend/app/infra.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,19 +139,25 @@ class RedisSessionAdapter:

_UPSERT_FEEDBACK_SCRIPT = """
local previous = redis.call('HGET', KEYS[1], ARGV[1])
local stored_comment = redis.call('HGET', KEYS[1], ARGV[1] .. ':commented')
local comment_recorded = 0
if ARGV[3] == '1' and stored_comment ~= ARGV[5] then
redis.call('HSET', KEYS[1], ARGV[1] .. ':commented', ARGV[5])
if not stored_comment then
redis.call('HINCRBY', KEYS[2], 'feedback_commented', 1)
end
comment_recorded = 1
end
if previous == ARGV[2] then
redis.call('EXPIRE', KEYS[1], ARGV[4])
redis.call('EXPIRE', KEYS[2], ARGV[4])
return 0
return comment_recorded
end
if previous == 'up' or previous == 'down' then
redis.call('HINCRBY', KEYS[2], 'feedback_' .. previous, -1)
end
redis.call('HSET', KEYS[1], ARGV[1], ARGV[2])
redis.call('HINCRBY', KEYS[2], 'feedback_' .. ARGV[2], 1)
if ARGV[3] == '1' and not previous then
redis.call('HINCRBY', KEYS[2], 'feedback_commented', 1)
end
redis.call('EXPIRE', KEYS[1], ARGV[4])
redis.call('EXPIRE', KEYS[2], ARGV[4])
return 1
Expand Down Expand Up @@ -542,6 +548,12 @@ def upsert_feedback(
if not callable(evaluator):
return None
key = hashlib.sha256(f"{session_id}\x00{message_id}".encode("utf-8")).hexdigest()
# Only a hash of the comment reaches the metrics store, so a
# resubmitted rating with a new comment still counts as a change
# without storing user text in the metrics hash.
comment_hash = (
hashlib.sha256(comment.encode("utf-8")).hexdigest()[:16] if comment else ""
)
changed = evaluator(
self._UPSERT_FEEDBACK_SCRIPT,
2,
Expand All @@ -551,6 +563,7 @@ def upsert_feedback(
rating,
"1" if comment else "0",
self.ttl_seconds,
comment_hash,
)
return bool(int(changed))
except Exception as exc:
Expand Down
8 changes: 7 additions & 1 deletion backend/app/ingestion.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,13 @@ def _contains_nested_credentials(cls, value: str) -> bool:
break
decoded = next_value
normalized = unicodedata.normalize("NFKC", decoded)
for match in re.finditer(r"(?:^|[?&;\s])([^=&?#\s]+)\s*=", normalized):
# A nested URL carrying userinfo (scheme://user:password@host)
# hides credentials that no key=value scan would ever see.
if re.search(r"://[^/\s]*@", normalized):
return True
# `#` and `/` are delimiters too: a credential smuggled in as
# `.../x%23token=secret` or `.../x/token=secret` must be caught.
for match in re.finditer(r"(?:^|[?&;#/\s])([^=&?#\s]+)\s*=", normalized):
if cls._sensitive_query_key(match.group(1)):
return True
return False
Expand Down
16 changes: 13 additions & 3 deletions backend/app/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ async def aclose(self) -> None:
self.close()


_COMPLETION_ENDPOINT_SUFFIXES = ("/chat/completions", "/chaat/completions")
_COMPLETION_ENDPOINT_SUFFIXES = ("/chat/completions",)


def _normalize_local_base_url(value: str) -> str:
Expand Down Expand Up @@ -332,11 +332,21 @@ def _normalize_base_url(value: str | None) -> str | None:
if not raw:
return None
parsed = urlsplit(raw)
if not parsed.scheme or not parsed.netloc:
if parsed.scheme.lower() not in {"http", "https"} or not parsed.netloc:
raise ValueError("OpenAI base URL must include an HTTP(S) host")
path = parsed.path.rstrip("/")
for suffix in _COMPLETION_ENDPOINT_SUFFIXES:
if path.lower().endswith(suffix):
path = path[: -len(suffix)].rstrip("/")
break
return urlunsplit((parsed.scheme, parsed.netloc, path, parsed.query, parsed.fragment)).rstrip("/")
else:
# A path ending in some other ".../completions" is almost certainly
# a mistyped endpoint (e.g. "/chaat/completions"). Fail closed
# instead of silently truncating it to a base URL that then hits
# the wrong endpoint.
if path.lower().endswith("/completions"):
raise ValueError("OpenAI base URL looks like a mistyped completion endpoint")
# Query strings and fragments are never part of a base URL; drop them
# instead of forwarding them (and any credentials they carry) with
# every request.
return urlunsplit((parsed.scheme, parsed.netloc, path, "", "")).rstrip("/")
64 changes: 54 additions & 10 deletions backend/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,9 +489,13 @@ def _cors_origins() -> list[str]:
origins = [item.strip().rstrip("/") for item in raw.split(",") if item.strip()]
# Credentials and wildcard origins are mutually exclusive. Treat a
# wildcard as an explicit configuration error rather than silently
# downgrading browser security.
# downgrading browser security: failing startup is the only honest
# signal, otherwise an operator would believe CORS is open.
if "*" in origins:
return []
raise ValueError(
"MEDGUIDE_CORS_ORIGINS must not contain '*': wildcard origins cannot "
"be combined with credentials; list explicit origins instead."
)
return origins


Expand Down Expand Up @@ -565,9 +569,13 @@ def _require_access(
# A proxy-validated cookie can expire or be revoked before this handler.
# Cookie-bearing offline requests must not fall back to anonymous access;
# production service-header priority remains unchanged.
if principal is None and (
getattr(current, "production", False) or getattr(current, "local", False) or session_token is not None
):
# Issue #6: authentication is required in every mode. Test/offline used
# to let requests without any credential through (while rejecting an
# invalid cookie), which exposed protected APIs and other users'
# sessions anonymously. There is intentionally no anonymous fallback
# switch: /api/health and /api/ready stay public for probes, and every
# other caller must present a valid session cookie or service token.
if principal is None:
raise HTTPException(status_code=401, detail="请先登录")
_require_runtime(current)
return principal
Expand All @@ -580,11 +588,19 @@ def _require_service_access(
api_key: str | None = None,
) -> str | None:
_require_secure_transport(current, request)
if not current.production:
return None
principal = _principal_from_headers(current, authorization, api_key)
_require_runtime(current)
return principal
if current.production:
principal = _principal_from_headers(current, authorization, api_key)
_require_runtime(current)
return principal
# Issue #6: the operator endpoint was wide open outside production
# (this function returned before any check). No service API tokens are
# configured in test/offline/local modes, so a logged-in user session is
# the credential here; anonymous access is never allowed. Production
# keeps the stricter service-token-only rule.
username = _username_from_cookie(current, _session_token_from_request(current, request))
if username is None:
raise HTTPException(status_code=401, detail="请先登录")
return f"user:{username}"


def _message_fingerprint(message: str) -> str:
Expand Down Expand Up @@ -1022,6 +1038,34 @@ def create_session(
return SessionCreateResponse(session_id=state["session_id"], state=public_state(state))


@app.get("/api/sessions")
def list_sessions(
http_request: Request,
authorization: str | None = Header(default=None),
api_key: str | None = Header(default=None, alias="X-API-Key"),
) -> dict[str, Any]:
# Registered before /api/sessions/{session_id} so "sessions" is never
# treated as a session id.
current = get_services()
owner_id = _require_access(
current,
http_request,
authorization,
api_key,
_session_token_from_request(current, http_request),
)
try:
sessions = current.store.list_sessions(owner_id)
except SessionBackendError as exc:
raise HTTPException(status_code=503, detail=str(exc)) from exc
return {
"sessions": sessions,
# False when the shared backend cannot enumerate sessions; the list
# is then empty on purpose rather than partial. See store.py.
"complete": current.store.listing_complete,
}


@app.get("/api/sessions/{session_id}")
def get_session(
session_id: str,
Expand Down
24 changes: 21 additions & 3 deletions backend/app/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class MetricsStore:
feedback_by_message: dict[str, Literal["up", "down"]] = field(default_factory=dict)
backend: Any | None = field(default=None, repr=False, compare=False)
_recorded_responses: set[str] = field(default_factory=set, repr=False)
_commented_keys: set[str] = field(default_factory=set, repr=False)
_feedback_comments: dict[str, str] = field(default_factory=dict, repr=False)
_lock: threading.Lock = field(default_factory=threading.Lock, repr=False)

def record(self, state: WorkflowState) -> None:
Expand Down Expand Up @@ -94,18 +96,34 @@ def add_feedback(
with self._lock:
previous: str | None = None
feedback_key = f"{session_id}:{message_id}" if session_id and message_id else message_id
comment_recorded = False
if comment:
if feedback_key:
# "commented" counts messages that carry a comment, not
# feedback submissions: a later comment on the same
# message is stored, but the message is counted once.
# The latest comment text is kept so a resubmitted
# rating with a new comment is a real change, not a
# silent drop.
if feedback_key not in self._commented_keys:
self._commented_keys.add(feedback_key)
self.feedback["commented"] += 1
if self._feedback_comments.get(feedback_key) != comment:
self._feedback_comments[feedback_key] = comment
comment_recorded = True
else:
self.feedback["commented"] += 1
comment_recorded = True
if feedback_key:
previous = self.feedback_by_message.get(feedback_key)
if previous == rating:
return False
return comment_recorded
if previous:
self.feedback[previous] -= 1
if self.feedback[previous] <= 0:
self.feedback.pop(previous, None)
self.feedback_by_message[feedback_key] = rating
self.feedback[rating] += 1
if comment and previous is None:
self.feedback["commented"] += 1
return True

def snapshot(self) -> dict:
Expand Down
15 changes: 14 additions & 1 deletion backend/app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class PatientProfile(TypedDict, total=False):
age: int
sex: str
duration: str
temperature: str
symptoms: list[str]
associated_symptoms: list[str]
history: str
Expand All @@ -24,6 +25,11 @@ class WorkflowState(TypedDict, total=False):
owner_id: str
user_message: str
normalized_message: str
# Full turn transcript for this session, oldest first. Each entry is
# plain text plus identifiers only: {"role": "user" | "assistant",
# "content": str, "response_id": str (assistant only), "timestamp": str}.
# No passwords, cookies, or tokens are ever stored here.
messages: list[dict[str, Any]]
profile: PatientProfile
intent: Intent
risk_level: RiskLevel
Expand Down Expand Up @@ -115,13 +121,20 @@ class SQLQueryResult:
rows: tuple[dict[str, Any], ...]
blocked: bool = False
reason: str | None = None
# True when the rows come from the built-in seeded sample database
# (SQLite test/local backend) rather than a connected production
# source, so callers can tell sample data apart from live data.
sample_data: bool = False

def as_dict(self) -> dict[str, Any]:
# The SQL text itself is an internal detail: it must never cross
# the API boundary, where it would expose table/column layout.
# Internal callers that need the statement read ``.sql`` directly.
return {
"intent": self.intent,
"sql": self.sql,
"columns": list(self.columns),
"rows": [dict(row) for row in self.rows],
"blocked": self.blocked,
"reason": self.reason,
"sample_data": self.sample_data,
}
Loading