Skip to content

fix: 配置模块可靠性加固 - #88

Merged
JusterZhu merged 3 commits into
mainfrom
fix/config-harden
Jun 2, 2026
Merged

fix: 配置模块可靠性加固#88
JusterZhu merged 3 commits into
mainfrom
fix/config-harden

Conversation

@JusterZhu

Copy link
Copy Markdown
Collaborator

Summary

Closes#87

对配置管理模块进行完整审查并修复 6 个可靠性风险。

修复详情

#风险严重度修复
1手动编辑 config.json 输入非法 AuthScheme 枚举值HIGHAppConfig.Sanitize() + AuthCredential.Sanitize() 自动校验修复
2Load() 仅 catch JsonException,磁盘满/权限拒绝导致启动崩溃HIGH扩展为 catch IOException + UnauthorizedAccessException
312 处 fire-and-forget SaveAsync 调用,I/O 失败静默丢失HIGHSafeFireAndForgetSave() 包裹 Task.Run + try-catch + Trace 日志
4窗口关闭用 Task.Run 异步写盘,进程退出前可能未完成HIGH改为同步 Save(),<1ms 完成
5PatchViewModel 构造函数触发不必要的 SaveAsyncMEDIUM添加 _initialized 守卫
6LoadAsync() 两条路径漏调 Sanitize()MEDIUM补全调用

文件变更

  • 12 files changed, +108 -37
  • dotnet build: 0 errors

关键新增

// 配置加载后自动修复(AppConfig.cs)internalvoidSanitize(){UploadAuth??=newAuthCredential();SimulationAuth??=newAuthCredential();if(UploadTimeoutSeconds<10)UploadTimeoutSeconds=300;if(UploadRetryCountis<0 or >10)UploadRetryCount=3;UploadAuth.Sanitize();SimulationAuth.Sanitize();}// 安全的 fire-and-forget 保存(ConfigService.cs)publicstaticvoidSafeFireAndForgetSave(ConfigServiceservice){_=Task.Run(async()=>{try{awaitservice.SaveAsync();}catch(Exceptionex){Trace.WriteLine($"Config save failed: {ex.Message}");}});}

🤖 Generated with Claude Code

JusterZhuand others added 3 commits June 2, 2026 16:11
…t data loss
Risks discovered in security review and their mitigations:
1. [HIGH] Invalid enum deserialization from manual JSON edits
- Add AppConfig.Sanitize() + AuthCredential.Sanitize() to repair invalid
AuthScheme values and null nested objects on load
2. [HIGH] Narrow exception catch in Load() — only caught JsonException
- Expanded to catch IOException + UnauthorizedAccessException to handle
disk-full and permission-denied scenarios gracefully
3. [HIGH] Fire-and-forget SaveAsync exceptions silently lost
- Added ConfigService.SafeFireAndForgetSave() that logs failures to Trace
- Updated all 12 fire-and-forget call sites in App + 4 ViewModels
4. [MEDIUM] Unnecessary disk write on every startup
- OnAutoUploadEnabledChanged in PatchViewModel constructor triggered
SaveAsync during init. Added _initialized guard to skip during construction
5. [LOW] Corrupted config silently overwrites backup before recovery attempt
- Sanitize() is called on recovered configs before re-saving to disk
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Window close handler used SafeFireAndForgetSave (Task.Run), which creates
a race: the process may exit before the thread-pool write completes,
silently losing window size/position/maximized state on next launch.
Changed to synchronous ConfigService.Save() which completes in <1ms for
the tiny (<2KB) config file. Also promoted Save() from private to public
and added it to IConfigService for this exact shutdown scenario.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
- Add missing Sanitize() calls in LoadAsync() backup-recovery and normal paths
- Remove unused System.Threading.Tasks import from App.axaml.cs
- Remove unused BuildUrl dead code from HttpUploadService
- Remove unnecessary 'new' keyword on AppLanguageManager.Instance
- Fix LoadAsync() to catch IOException/UnauthorizedAccessException
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
CopilotAI review requested due to automatic review settings June 2, 2026 08:12
@JusterZhu
JusterZhu removed the request for review from CopilotJune 2, 2026 08:12
@JusterZhu
JusterZhu merged commit 851406d into mainJun 2, 2026
1 check passed
@JusterZhu
JusterZhu deleted the fix/config-harden branch June 2, 2026 08:13
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: 配置模块可靠性加固 — 防止损坏、静默丢失和边界崩溃

1 participant

@JusterZhu