Skip to content

Commit 02a52c5

Browse files
committed
新增webui jargon鉴权
jargon未进行鉴权,导致jargon可被任意请求修改。
1 parent 04a5bf3 commit 02a52c5

File tree

1 file changed

+69
-11
lines changed

1 file changed

+69
-11
lines changed

src/webui/routers/jargon.py

Lines changed: 69 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""黑话(俚语)管理路由"""
22

33
from typing import Annotated, Any, List, Optional
4-
from fastapi import APIRouter, HTTPException, Query
4+
from fastapi import APIRouter, HTTPException, Query, Cookie, Header
55
from pydantic import BaseModel, Field
66
from sqlalchemy import func as fn
77
from sqlmodel import Session, col, delete, select
@@ -11,6 +11,7 @@
1111
from src.common.database.database import get_db_session
1212
from src.common.database.database_model import ChatSession, Jargon
1313
from src.common.logger import get_logger
14+
from src.webui.core import verify_auth_token_from_cookie_or_header
1415

1516
logger = get_logger("webui.jargon")
1617

@@ -177,6 +178,14 @@ class ChatListResponse(BaseModel):
177178
data: List[ChatInfoResponse]
178179

179180

181+
def verify_auth_token(
182+
maibot_session: Optional[str] = None,
183+
authorization: Optional[str] = None,
184+
) -> bool:
185+
"""验证认证 Token,支持 Cookie 和 Header"""
186+
return verify_auth_token_from_cookie_or_header(maibot_session, authorization)
187+
188+
180189
# ==================== 工具函数 ====================
181190

182191

@@ -211,9 +220,14 @@ async def get_jargon_list(
211220
search: Optional[str] = Query(None, description="搜索关键词"),
212221
chat_id: Optional[str] = Query(None, description="按聊天ID筛选"),
213222
is_jargon: Optional[bool] = Query(None, description="按是否是黑话筛选"),
223+
is_global: Optional[bool] = Query(None, description="按是否全局筛选"),
224+
maibot_session: Optional[str] = Cookie(None),
225+
authorization: Optional[str] = Header(None),
214226
):
215227
"""获取黑话列表"""
216228
try:
229+
verify_auth_token(maibot_session, authorization)
230+
217231
statement = select(Jargon)
218232
count_statement = select(fn.count()).select_from(Jargon)
219233

@@ -261,9 +275,14 @@ async def get_jargon_list(
261275

262276

263277
@router.get("/chats", response_model=ChatListResponse)
264-
async def get_chat_list():
278+
async def get_chat_list(
279+
maibot_session: Optional[str] = Cookie(None),
280+
authorization: Optional[str] = Header(None),
281+
):
265282
"""获取所有有黑话记录的聊天列表"""
266283
try:
284+
verify_auth_token(maibot_session, authorization)
285+
267286
with get_db_session() as session:
268287
statement = select(Jargon.session_id).distinct().where(col(Jargon.session_id).is_not(None))
269288
chat_id_list = [chat_id for chat_id in session.exec(statement).all() if chat_id]
@@ -308,22 +327,27 @@ async def get_chat_list():
308327

309328

310329
@router.get("/stats/summary", response_model=JargonStatsResponse)
311-
async def get_jargon_stats():
330+
async def get_jargon_stats(
331+
maibot_session: Optional[str] = Cookie(None),
332+
authorization: Optional[str] = Header(None),
333+
):
312334
"""获取黑话统计数据"""
313335
try:
336+
verify_auth_token(maibot_session, authorization)
337+
314338
with get_db_session() as session:
315339
total = session.exec(select(fn.count()).select_from(Jargon)).one()
316340

317341
confirmed_jargon = session.exec(
318-
select(fn.count()).select_from(Jargon).where(col(Jargon.is_jargon))
342+
select(fn.count()).select_from(Jargon).where(col(Jargon.is_jargon) == True)
319343
).one()
320344
confirmed_not_jargon = session.exec(
321-
select(fn.count()).select_from(Jargon).where(col(Jargon.is_jargon).is_(False))
345+
select(fn.count()).select_from(Jargon).where(col(Jargon.is_jargon) == False)
322346
).one()
323347
pending = session.exec(select(fn.count()).select_from(Jargon).where(col(Jargon.is_jargon).is_(None))).one()
324348

325349
complete_count = session.exec(
326-
select(fn.count()).select_from(Jargon).where(col(Jargon.is_complete))
350+
select(fn.count()).select_from(Jargon).where(col(Jargon.is_complete) == True)
327351
).one()
328352

329353
chat_count = session.exec(
@@ -360,9 +384,15 @@ async def get_jargon_stats():
360384

361385

362386
@router.get("/{jargon_id}", response_model=JargonDetailResponse)
363-
async def get_jargon_detail(jargon_id: int):
387+
async def get_jargon_detail(
388+
jargon_id: int,
389+
maibot_session: Optional[str] = Cookie(None),
390+
authorization: Optional[str] = Header(None),
391+
):
364392
"""获取黑话详情"""
365393
try:
394+
verify_auth_token(maibot_session, authorization)
395+
366396
with get_db_session() as session:
367397
jargon = session.exec(select(Jargon).where(col(Jargon.id) == jargon_id)).first()
368398
if not jargon:
@@ -379,9 +409,15 @@ async def get_jargon_detail(jargon_id: int):
379409

380410

381411
@router.post("/", response_model=JargonCreateResponse)
382-
async def create_jargon(request: JargonCreateRequest):
412+
async def create_jargon(
413+
request: JargonCreateRequest,
414+
maibot_session: Optional[str] = Cookie(None),
415+
authorization: Optional[str] = Header(None),
416+
):
383417
"""创建黑话"""
384418
try:
419+
verify_auth_token(maibot_session, authorization)
420+
385421
with get_db_session() as session:
386422
existing = session.exec(
387423
select(Jargon).where(
@@ -416,9 +452,16 @@ async def create_jargon(request: JargonCreateRequest):
416452

417453

418454
@router.patch("/{jargon_id}", response_model=JargonUpdateResponse)
419-
async def update_jargon(jargon_id: int, request: JargonUpdateRequest):
455+
async def update_jargon(
456+
jargon_id: int,
457+
request: JargonUpdateRequest,
458+
maibot_session: Optional[str] = Cookie(None),
459+
authorization: Optional[str] = Header(None),
460+
):
420461
"""更新黑话(增量更新)"""
421462
try:
463+
verify_auth_token(maibot_session, authorization)
464+
422465
with get_db_session() as session:
423466
jargon = session.exec(select(Jargon).where(col(Jargon.id) == jargon_id)).first()
424467
if not jargon:
@@ -449,9 +492,14 @@ async def update_jargon(jargon_id: int, request: JargonUpdateRequest):
449492

450493

451494
@router.delete("/{jargon_id}", response_model=JargonDeleteResponse)
452-
async def delete_jargon(jargon_id: int):
495+
async def delete_jargon(jargon_id: int,
496+
maibot_session: Optional[str] = Cookie(None),
497+
authorization: Optional[str] = Header(None),
498+
):
453499
"""删除黑话"""
454500
try:
501+
verify_auth_token(maibot_session, authorization)
502+
455503
with get_db_session() as session:
456504
jargon = session.exec(select(Jargon).where(col(Jargon.id) == jargon_id)).first()
457505
if not jargon:
@@ -472,9 +520,15 @@ async def delete_jargon(jargon_id: int):
472520

473521

474522
@router.post("/batch/delete", response_model=JargonDeleteResponse)
475-
async def batch_delete_jargons(request: BatchDeleteRequest):
523+
async def batch_delete_jargons(
524+
request: BatchDeleteRequest,
525+
maibot_session: Optional[str] = Cookie(None),
526+
authorization: Optional[str] = Header(None),
527+
):
476528
"""批量删除黑话"""
477529
try:
530+
verify_auth_token(maibot_session, authorization)
531+
478532
if not request.ids:
479533
raise HTTPException(status_code=400, detail="ID列表不能为空")
480534

@@ -501,9 +555,13 @@ async def batch_delete_jargons(request: BatchDeleteRequest):
501555
async def batch_set_jargon_status(
502556
ids: Annotated[List[int], Query(description="黑话ID列表")],
503557
is_jargon: Annotated[bool, Query(description="是否是黑话")],
558+
maibot_session: Optional[str] = Cookie(None),
559+
authorization: Optional[str] = Header(None),
504560
):
505561
"""批量设置黑话状态"""
506562
try:
563+
verify_auth_token(maibot_session, authorization)
564+
507565
if not ids:
508566
raise HTTPException(status_code=400, detail="ID列表不能为空")
509567

0 commit comments

Comments
 (0)