mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-07-21 20:53:15 +08:00
fix(security): protect cookiecloud update uploads (#6053)
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
import gzip
|
||||
import hmac
|
||||
import json
|
||||
from typing import Annotated, Callable, Any, Dict, Optional
|
||||
|
||||
import aiofiles
|
||||
from anyio import Path as AsyncPath
|
||||
from fastapi import APIRouter, Body, Depends, HTTPException, Path, Request, Response
|
||||
from fastapi import APIRouter, Body, Depends, Header, HTTPException, Path, Request, Response
|
||||
from fastapi.responses import PlainTextResponse
|
||||
from fastapi.routing import APIRoute
|
||||
|
||||
@@ -44,6 +45,24 @@ async def verify_server_enabled():
|
||||
return True
|
||||
|
||||
|
||||
async def verify_update_auth(
|
||||
x_cookiecloud_auth: Annotated[
|
||||
Optional[str], Header(alias="X-CookieCloud-Auth")
|
||||
] = None,
|
||||
):
|
||||
"""
|
||||
校验CookieCloud上传接口的可选共享认证头。
|
||||
"""
|
||||
expected_header = (settings.COOKIECLOUD_AUTH_HEADER or "").strip()
|
||||
if not expected_header:
|
||||
return True
|
||||
|
||||
provided_header = (x_cookiecloud_auth or "").strip()
|
||||
if not hmac.compare_digest(provided_header, expected_header):
|
||||
raise HTTPException(status_code=403, detail="CookieCloud认证失败")
|
||||
return True
|
||||
|
||||
|
||||
cookie_router = APIRouter(
|
||||
route_class=GzipRoute,
|
||||
tags=["servcookie"],
|
||||
@@ -61,7 +80,7 @@ async def post_root():
|
||||
return "Hello MoviePilot! COOKIECLOUD API ROOT = /cookiecloud"
|
||||
|
||||
|
||||
@cookie_router.post("/update")
|
||||
@cookie_router.post("/update", dependencies=[Depends(verify_update_auth)])
|
||||
async def update_cookie(req: schemas.CookieData):
|
||||
"""
|
||||
上传Cookie数据
|
||||
|
||||
@@ -377,6 +377,8 @@ class ConfigModel(BaseModel):
|
||||
COOKIECLOUD_KEY: Optional[str] = None
|
||||
# CookieCloud端对端加密密码
|
||||
COOKIECLOUD_PASSWORD: Optional[str] = None
|
||||
# CookieCloud本地上传接口的X-CookieCloud-Auth期望值,留空表示不校验
|
||||
COOKIECLOUD_AUTH_HEADER: Optional[str] = None
|
||||
# CookieCloud同步间隔(分钟)
|
||||
COOKIECLOUD_INTERVAL: Optional[int] = 60 * 24
|
||||
# CookieCloud同步黑名单,多个域名,分割
|
||||
|
||||
Reference in New Issue
Block a user