From dbbabc8a1547a6f6cc778f070bb013315575a256 Mon Sep 17 00:00:00 2001 From: 4ayo Date: Wed, 17 Sep 2025 19:38:11 +0200 Subject: [PATCH 1/2] Fixed .env.example file --- .env.example | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.env.example b/.env.example index 4c2c113..4bd4982 100644 --- a/.env.example +++ b/.env.example @@ -132,4 +132,4 @@ BEATMAP_PROXY_PREFIX=b-ppy SAVE_REPLAYS=0 REDIS_HOST=localhost SHARED_INTEROP_DOMAIN=http://localhost:8000 -SERVER_PORT=8006 +SERVER_PORT=80 From 6330e9b6e1aaacabd8bdf899932fcdf98bb4e5bc Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 17 Sep 2025 17:39:21 +0000 Subject: [PATCH 2/2] chore(linter): auto fix by pre-commit hooks --- app/database/events.py | 6 ++-- app/exceptions/userpage.py | 5 +++ app/models/userpage.py | 7 ++--- app/service/bbcode_service.py | 58 +++++++++++++++++++++++++++-------- 4 files changed, 55 insertions(+), 21 deletions(-) diff --git a/app/database/events.py b/app/database/events.py index 890a021..2df37b4 100644 --- a/app/database/events.py +++ b/app/database/events.py @@ -61,11 +61,11 @@ class Event(UTCBaseModel, SQLModel, table=True): # 临时修复:统一成就事件格式 (TODO: 可在数据迁移完成后移除) if self.type == EventType.ACHIEVEMENT and "achievement" in self.event_payload: achievement_data = self.event_payload["achievement"] - if ( - "achievement_id" in achievement_data - and ("name" not in achievement_data or "slug" not in achievement_data) + if "achievement_id" in achievement_data and ( + "name" not in achievement_data or "slug" not in achievement_data ): from app.models.achievement import MEDALS + achievement_id = achievement_data["achievement_id"] for medal in MEDALS: if medal.id == achievement_id: diff --git a/app/exceptions/userpage.py b/app/exceptions/userpage.py index a0a8e60..f7ee16d 100644 --- a/app/exceptions/userpage.py +++ b/app/exceptions/userpage.py @@ -7,6 +7,7 @@ from __future__ import annotations class UserpageError(Exception): """用户页面处理错误基类""" + def __init__(self, message: str, code: str = "userpage_error"): self.message = message self.code = code @@ -15,6 +16,7 @@ class UserpageError(Exception): class ContentTooLongError(UserpageError): """内容过长错误""" + def __init__(self, current_length: int, max_length: int): message = f"Content too long. Maximum {max_length} characters allowed, got {current_length}." super().__init__(message, "content_too_long") @@ -24,12 +26,14 @@ class ContentTooLongError(UserpageError): class ContentEmptyError(UserpageError): """内容为空错误""" + def __init__(self): super().__init__("Content cannot be empty.", "content_empty") class BBCodeValidationError(UserpageError): """BBCode验证错误""" + def __init__(self, errors: list[str]): message = f"BBCode validation failed: {'; '.join(errors)}" super().__init__(message, "bbcode_validation_error") @@ -38,6 +42,7 @@ class BBCodeValidationError(UserpageError): class ForbiddenTagError(UserpageError): """禁止标签错误""" + def __init__(self, tag: str): message = f"Forbidden tag '{tag}' is not allowed." super().__init__(message, "forbidden_tag") diff --git a/app/models/userpage.py b/app/models/userpage.py index 1981105..d7815a1 100644 --- a/app/models/userpage.py +++ b/app/models/userpage.py @@ -13,7 +13,7 @@ class UpdateUserpageRequest(BaseModel): body: str = Field( description="用户页面的BBCode原始内容", max_length=60000, - examples=["[b]Hello![/b] This is my profile page.\n[color=blue]Blue text[/color]"] + examples=["[b]Hello![/b] This is my profile page.\n[color=blue]Blue text[/color]"], ) @field_validator("body") @@ -47,10 +47,7 @@ class UserpageResponse(BaseModel): class ValidateBBCodeRequest(BaseModel): """验证BBCode请求模型""" - content: str = Field( - description="要验证的BBCode内容", - max_length=60000 - ) + content: str = Field(description="要验证的BBCode内容", max_length=60000) class ValidateBBCodeResponse(BaseModel): diff --git a/app/service/bbcode_service.py b/app/service/bbcode_service.py index 0e6d783..4e6a9dc 100644 --- a/app/service/bbcode_service.py +++ b/app/service/bbcode_service.py @@ -25,12 +25,34 @@ class BBCodeService: # 允许的HTML标签和属性 - 基于官方实现 ALLOWED_TAGS: ClassVar[list[str]] = [ - "a", "audio", "blockquote", "br", "button", "center", "code", "del", "div", "em", "h2", "h4", - "iframe", "img", "li", "ol", "p", "pre", "span", "strong", "u", "ul", + "a", + "audio", + "blockquote", + "br", + "button", + "center", + "code", + "del", + "div", + "em", + "h2", + "h4", + "iframe", + "img", + "li", + "ol", + "p", + "pre", + "span", + "strong", + "u", + "ul", # imagemap 相关 - "map", "area", + "map", + "area", # 自定义容器 - "details", "summary", + "details", + "summary", ] ALLOWED_ATTRIBUTES: ClassVar[dict[str, list[str]]] = { @@ -57,8 +79,22 @@ class BBCodeService: # 危险的BBCode标签(不允许) FORBIDDEN_TAGS: ClassVar[list[str]] = [ - "script", "iframe", "object", "embed", "form", "input", "textarea", - "select", "option", "meta", "link", "style", "title", "head", "html", "body", + "script", + "iframe", + "object", + "embed", + "form", + "input", + "textarea", + "select", + "option", + "meta", + "link", + "style", + "title", + "head", + "html", + "body", ] @classmethod @@ -256,14 +292,11 @@ class BBCodeService: if href == "#": # 无链接区域 - links.append( - f'' - ) + links.append(f'') else: # 有链接区域 links.append( - f'' + f'' ) except (ValueError, IndexError): continue @@ -340,8 +373,7 @@ class BBCodeService: """解析 [quote] 标签""" # [quote="author"]content[/quote] pattern1 = r'\[quote="([^"]+)"\]\s*(.*?)\s*\[/quote\]' - text = re.sub(pattern1, r"

\1 wrote:

\2
", text, - flags=re.DOTALL | re.IGNORECASE) + text = re.sub(pattern1, r"

\1 wrote:

\2
", text, flags=re.DOTALL | re.IGNORECASE) # [quote]content[/quote] pattern2 = r"\[quote\]\s*(.*?)\s*\[/quote\]"