fix: prevent scientific notation parsing in YAML (#957)

This commit is contained in:
Memory 2025-08-15 13:24:44 +08:00 committed by GitHub
parent fd708ec8bd
commit 73068f6544
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 14 additions and 2 deletions

View File

@ -205,7 +205,10 @@ export async function setProfileStr(id: string, content: string): Promise<void>
export async function getProfile(id: string | undefined): Promise<IMihomoConfig> {
const profile = await getProfileStr(id)
let result = yaml.parse(profile, { merge: true }) || {}
// 替换 防止错误使用科学记数法解析
const patchedProfile = profile.replace(/(\w+:\s*)(\d+E\d+)(\s|$)/gi, '$1"$2"$3')
let result = yaml.parse(patchedProfile, { merge: true }) || {}
if (typeof result !== 'object') result = {}
return result
}

View File

@ -50,7 +50,16 @@ export async function generateProfile(): Promise<void> {
profile['log-level'] = 'info'
}
runtimeConfig = profile
runtimeConfigStr = yaml.stringify(profile)
// 先正常生成 YAML 字符串
let yamlStr = yaml.stringify(profile)
// 还原科学记数法的引号
yamlStr = yamlStr.replace(
/(\w+:\s*)"(\d+E\d+)"(\s|$)/gi,
'$1$2$3'
)
runtimeConfigStr = yamlStr
if (diffWorkDir) {
await prepareProfileWorkDir(current)
}