Skip to content

fix(security): block triggers/views and bound size and execution time in SQL import - #9832

Merged
farion1231 merged 1 commit into
farion1231:mainfrom
YUZHEthefool:ao/ccswitch-audit/fix-sql-import-triggers
Nov 23, 2026
Merged

farion1231 merged 1 commit into
farion1231:mainfrom
YUZHEthefool:ao/ccswitch-audit/fix-sql-import-triggers

Conversation

@YUZHEthefool

Copy link
Copy Markdown
Collaborator

Human Note / 本人说明

修复历史遗留的 mcp,远程下载侧的安全漏洞与问题

Summary / 概述

Hardens SQL backup and WebDAV/S3 sync import (src-tauri/src/database/backup.rs) against persistent SQLite trigger/view injection and resource-exhaustion DoS:

  • Block triggers and views in import_authorizer: Deny CreateTrigger, CreateTempTrigger, DropTrigger, DropTempTrigger, CreateView, CreateTempView, DropView, and DropTempView while executing untrusted SQL. CC Switch's production schema uses no triggers or views; previously, a malicious .sql backup or compromised sync snapshot could plant an AFTER INSERT/UPDATE trigger on providers/mcp_servers that survived staging and was copied permanently into cc-switch.db.
  • Defense-in-depth schema check (validate_imported_schema): Verify sqlite_master contains zero trigger or view objects before migrating or copying the staging database to the live DB.
  • Bound SQL import size (MAX_SQL_IMPORT_BYTES): Enforce a size cap aligned with sync_protocol::MAX_SYNC_ARTIFACT_BYTES (512 MiB) via read_sql_file_bounded (metadata pre-check + bounded Read::take) and in-memory length check before executing SQL.
  • Bound SQL execution time (progress_handler): Install a SQLite progress_handler deadline (30s, checked every 10,000 VM opcodes) around execute_batch and detach it immediately afterward, converting OperationInterrupted into a localized timeout error so a small WITH RECURSIVE payload cannot hang the app or hold sync_mutex indefinitely.

Related Issue / 关联 Issue

None

Validation / 验证

  • cargo fmt --manifest-path src-tauri/Cargo.toml --all -- --check — passed
  • cargo clippy --manifest-path src-tauri/Cargo.toml -- -D warnings — passed
  • cargo test --manifest-path src-tauri/Cargo.toml --lib "database::backup::tests" — 40 passed (includes reproduction tests for persistent trigger/temp-trigger/view rejection, defense-in-depth validate_imported_schema check, read_sql_file_bounded size boundary, and WITH RECURSIVE interruption)

Checklist / 检查清单

  • Reproduced persistent trigger injection via failing test before implementing the fix
  • Verified dump_sql tests and legitimate backup round-trips remain passing
  • Verified cargo clippy -- -D warnings, cargo fmt --check, and tests pass

@github-actions github-actions Bot added the backend Backend (Rust/Tauri) label Nov 21, 2026
@YUZHEthefool
YUZHEthefool force-pushed the ao/ccswitch-audit/fix-sql-import-triggers branch from b7cc39c to 773bd43 Compare November 22, 2026 03:28
@farion1231
farion1231 merged commit 3157590 into farion1231:main Nov 23, 2026
9 checks passed
@farion1231

Copy link
Copy Markdown
Owner

已合并,感谢 @YUZHEthefool!导入时拦截 trigger / view 这一项很有价值,叠在最新 main 上验证过,fmt / clippy / 全部测试都通过,会随下一版发布。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend Backend (Rust/Tauri)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants