沙盒修复

This commit is contained in:
Developer
2026-06-26 08:20:54 +08:00
parent 0f3fab70a7
commit 0c9faf30b7
2 changed files with 234 additions and 28 deletions

View File

@@ -6,6 +6,75 @@
***
## [v6.136.0] - 2026-06-26
### 🚨 严重 Bug 修复(数据丢失 - macOS/Windows/Linux 非沙盒模式)
#### 背景
用户反馈macOS Debug 模式下,进入「更多设置 → 重置与清理 → 清空软件所有数据」输入"重置"确认后,**整个项目源代码文件全部丢失**。
#### 根因
`lib/features/settings/presentation/more_settings_page.dart``_executeFactoryReset` 方法第 6 步缓存清理逻辑存在严重缺陷:
```dart
// ❌ 原 dangerous 代码
final dirs = <Directory>[];
dirs.add(await getTemporaryDirectory());
dirs.add(await getApplicationDocumentsDirectory()); // macOS 非沙盒 → ~/Documents
dirs.add(await getApplicationSupportDirectory());
for (final dir in dirs) {
await for (final entity in dir.list()) {
if (entity is Directory) {
await entity.delete(recursive: true); // 递归删除子目录!
}
}
}
```
- macOS Debug 模式下 [DebugProfile.entitlements](file:///Users/wushu/Documents/trae_projects/project/xianyan/macos/Runner/DebugProfile.entitlements) 中 `com.apple.security.app-sandbox = false`(未启用沙盒)
- 非沙盒模式下 `getApplicationDocumentsDirectory()` 返回**用户级 `~/Documents`**(不是应用容器目录)
- 数据库连接 [native.dart#L27-L28](file:///Users/wushu/Documents/trae_projects/project/xianyan/lib/core/storage/database/database_connection/native.dart#L27-L28) 将 `xianyan.db` 放在 `~/Documents/xianyan.db`
- 代码遍历 `~/Documents` 下所有子项并递归删除 → `~/Documents/trae_projects` 被递归删除 → **整个项目源代码丢失**
- 同样的危险在 Windows/Linux 非沙盒下都存在
#### 修复方案
**`lib/features/settings/presentation/more_settings_page.dart`**
1. **新增 `_safeClearAppFilesystemData()` 方法**,遵循三大安全原则:
- 永不递归删除 `getApplicationDocumentsDirectory()` 等返回目录的根内容
- 仅删除已知应用专属路径(数据库文件、应用标识子目录)
- 路径校验:清理子目录前验证路径包含 `apps.xy.xianyan``xianyan`,否则跳过
2. **精确删除数据库文件**:仅删除 `xianyan.db``xianyan.db-wal``xianyan.db-shm``xianyan.db-journal`,不遍历父目录
3. **`_safeClearDirectoryContents()` 通用方法**
- `requireAppIdentifier` 参数:要求路径包含应用标识才清理
- 仅清空目录的直接子项,不删除目录本身
- 单个文件删除失败不影响整体清理
- 详细的日志记录
4. **分层清理策略**
- 数据库文件:精确文件删除(无条件)
- 临时目录:直接清空(应用专属,安全)
- 应用支持目录:带路径校验的清空
- 应用缓存目录:直接清空(应用专属,安全)
- Flutter ImageCache内存缓存清理
#### 影响范围
- ✅ macOS 非沙盒Debug 模式):修复项目源代码丢失问题
- ✅ Windows 非沙盒:修复 `Documents` 目录被清空问题
- ✅ Linux 非沙盒:修复用户主目录被清空问题
- ✅ iOS/Android 沙盒:原行为正常,现增加路径校验作为防御
- ✅ macOS Release 沙盒模式:原行为正常,现增加路径校验作为防御
#### 举一反三
- 数据库连接文件位置(`native.dart`)使用 `getApplicationDocumentsDirectory()` 在桌面非沙盒下不理想,建议未来迁移至 `getApplicationSupportDirectory()`(应用专属,更安全)
- 「数据管理页面」的 `_clearAllData` 方法仅清理数据库表,不涉及文件系统删除,安全
- 「缓存管理页面」的清理操作通过 `CacheService` 调用,使用应用专属路径,安全
---
## [v6.135.0] - 2026-06-26
### 🧹 仓库瘦身(历史大文件清理)