Sanitize invalid platform IDs on load (#4432)
This commit is contained in:
@@ -27,6 +27,17 @@ class PlatformManager:
|
||||
约定整个项目中对 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:
|
||||
@@ -53,6 +64,22 @@ class PlatformManager:
|
||||
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']}) 平台适配器 ...",
|
||||
|
||||
@@ -421,6 +421,10 @@ export default {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!this.isPlatformIdValid(this.selectedPlatformConfig?.id)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 如果是使用现有配置文件模式
|
||||
if (this.aBConfigRadioVal === '0') {
|
||||
return !!this.selectedAbConfId;
|
||||
@@ -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', {
|
||||
@@ -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') {
|
||||
@@ -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) {
|
||||
@@ -1032,4 +1055,4 @@ export default {
|
||||
overflow-y: auto;
|
||||
padding: 16px 16px 24px 16px;
|
||||
}
|
||||
</style>
|
||||
</style>
|
||||
|
||||
@@ -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!",
|
||||
@@ -76,4 +77,4 @@
|
||||
"traceback": "Traceback",
|
||||
"close": "Close"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,7 +42,8 @@
|
||||
"title": "安全提醒",
|
||||
"aiocqhttpTokenMissing": "为了增强连接安全性,强烈建议您设置 ws_reverse_token。未设置 Token 可能导致安全风险。",
|
||||
"learnMore": "了解更多"
|
||||
}
|
||||
},
|
||||
"invalidPlatformId": "平台 ID 不能包含 ':' 或 '!'。"
|
||||
},
|
||||
"messages": {
|
||||
"updateSuccess": "更新成功!",
|
||||
@@ -76,4 +77,4 @@
|
||||
"traceback": "错误堆栈",
|
||||
"close": "关闭"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user