Desktop Application Security Focus
- ✅ Local data protection, API key management, Tauri command security, SQL injection prevention
- ❌ Not applicable: CSRF, CSP (web application security measures)
Security Check Checklist
1. Key Management
- No hardcoded API keys, passwords, tokens
- All keys use environment variables
- Sensitive configuration stored encrypted
// ✓ 正确
let api_key = env::var("OPENAI_API_KEY")?;
// ✗ Wrong const API_KEY: &str = "sk-1234567890";
### 2. Tauri 命令安全
- [ ] 所有命令参数已验证
- [ ] 使用 validator crate
- [ ] 错误消息不暴露内部信息
```rust
#[derive(Deserialize, Validate, Type)]
pub struct CreateFormulaDto {
#[validate(length(min = 2, max = 50))]
pub name: String,
}
#[tauri::command]
#[specta::specta]
pub async fn create_formula(dto: CreateFormulaDto) -> ApiResponse<Formula> {
if let Err(e) = dto.validate() {
return api_err(format!("输入验证失败: {}", e));
}
// ...
}
3. SQL Injection Prevention
- Use SQLx parameterized queries
- String concatenation of SQL is prohibited
- Use QueryBuilder for dynamic queries
// ✓ 正确
sqlx::query_as!(Formula, "SELECT * FROM formulas WHERE name = ?", name)
// ✗ 错误
let sql = format!("SELECT * FROM formulas WHERE name = '{}'", name);
4. Input Validation
- Frontend validation (first line of defense)
- Backend validation (required)
- File path validation to prevent path traversal
pub fn validate_file_path(path: &str) -> Result<PathBuf> {
let path = Path::new(path);
if path.components().any(|c| c == std::path::Component::ParentDir) {
return Err(anyhow!("不允许使用 .. 路径"));
}
Ok(path.to_path_buf())
}
5. Sensitive Data Protection
- No keys or passwords in logs
- Error messages do not expose internal information
// ✓ 正确
info!(formula_id = formula.id, "配方创建成功");
// ✗ 错误
info!("API Key: {}", api_key);
6. Dependency Security
cargo audit # 检查安全漏洞
cargo outdated # 检查过时依赖
cargo update # 更新依赖
Security Review Trigger Conditions
- Adding a new Tauri command
- Modifying the database access layer
- Handling user file upload/import
- Integrating a third-party API
- Adding a new configuration item
Pre-commit Checks
-
cargo clippyNo security warnings -
cargo auditNo known vulnerabilities - No hardcoded keys
- All SQL queries use parameterization
- All Tauri command parameters validated
- No sensitive information in logs
Common Security Pitfalls
- Trusting frontend validation → Backend must validate again
- Logging sensitive information → Only log necessary information
- SQL string concatenation → Use parameterized queries
- Overly detailed error messages → Return generic error messages