Skip to content
Merged
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
27 changes: 27 additions & 0 deletions astrbot/core/platform/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ def __init__(self, config: AstrBotConfig, event_queue: Queue):
约定整个项目中对 unique_session 的引用都从 default 的配置中获取"""
self.event_queue = event_queue

def _is_valid_platform_id(self, platform_id: str | None) -> bool:
if not platform_id:
return False
return ":" not in platform_id and "!" not in platform_id

def _sanitize_platform_id(self, platform_id: str | None) -> tuple[str | None, bool]:
if not platform_id:
return platform_id, False
sanitized = platform_id.replace(":", "_").replace("!", "_")
return sanitized, sanitized != platform_id

async def initialize(self):
"""初始化所有平台适配器"""
for platform in self.platforms_config:
Expand All @@ -53,6 +64,22 @@ async def load_platform(self, platform_config: dict):
try:
if not platform_config["enable"]:
return
platform_id = platform_config.get("id")
if not self._is_valid_platform_id(platform_id):
sanitized_id, changed = self._sanitize_platform_id(platform_id)
if sanitized_id and changed:
logger.warning(
"平台 ID %r 包含非法字符 ':' 或 '!',已替换为 %r。",
platform_id,
sanitized_id,
)
platform_config["id"] = sanitized_id
self.astrbot_config.save_config()
else:
logger.error(
f"平台 ID {platform_id!r} 不能为空,跳过加载该平台适配器。",
)
return

logger.info(
f"载入 {platform_config['type']}({platform_config['id']}) 平台适配器 ...",
Expand Down
25 changes: 24 additions & 1 deletion dashboard/src/components/platform/AddNewPlatform.vue
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,10 @@ export default {
return false;
}

if (!this.isPlatformIdValid(this.selectedPlatformConfig?.id)) {
return false;
}

// 如果是使用现有配置文件模式
if (this.aBConfigRadioVal === '0') {
return !!this.selectedAbConfId;
Expand Down Expand Up @@ -637,6 +641,12 @@ export default {
return;
}

if (!this.isPlatformIdValid(id)) {
this.loading = false;
this.showError(this.tm('dialog.invalidPlatformId'));
return;
}

try {
// 更新平台配置
let resp = await axios.post('/api/config/platform/update', {
Expand All @@ -662,6 +672,12 @@ export default {
}
},
async savePlatform() {
if (!this.isPlatformIdValid(this.selectedPlatformConfig?.id)) {
this.loading = false;
this.showError(this.tm('dialog.invalidPlatformId'));
return;
}

// 检查 ID 是否已存在
const existingPlatform = this.config_data.platform?.find(p => p.id === this.selectedPlatformConfig.id);
if (existingPlatform || this.selectedPlatformConfig.id === 'webchat') {
Expand Down Expand Up @@ -808,6 +824,13 @@ export default {
this.$emit('show-toast', { message: message, type: 'error' });
},

isPlatformIdValid(id) {
if (!id) {
return false;
}
return !/[!:]/.test(id);
},

// 获取该平台适配器使用的所有配置文件(新版本:直接操作路由表)
async getPlatformConfigs(platformId) {
if (!platformId) {
Expand Down Expand Up @@ -1032,4 +1055,4 @@ export default {
overflow-y: auto;
padding: 16px 16px 24px 16px;
}
</style>
</style>
5 changes: 3 additions & 2 deletions dashboard/src/i18n/locales/en-US/features/platform.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
"title": "Security Warning",
"aiocqhttpTokenMissing": "To enhance connection security, it is strongly recommended to set ws_reverse_token. Not setting a token may lead to security risks.",
"learnMore": "Learn More"
}
},
"invalidPlatformId": "Platform ID cannot contain ':' or '!'."
},
"messages": {
"updateSuccess": "Update successful!",
Expand Down Expand Up @@ -76,4 +77,4 @@
"traceback": "Traceback",
"close": "Close"
}
}
}
5 changes: 3 additions & 2 deletions dashboard/src/i18n/locales/zh-CN/features/platform.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
"title": "安全提醒",
"aiocqhttpTokenMissing": "为了增强连接安全性,强烈建议您设置 ws_reverse_token。未设置 Token 可能导致安全风险。",
"learnMore": "了解更多"
}
},
"invalidPlatformId": "平台 ID 不能包含 ':' 或 '!'。"
},
"messages": {
"updateSuccess": "更新成功!",
Expand Down Expand Up @@ -76,4 +77,4 @@
"traceback": "错误堆栈",
"close": "关闭"
}
}
}