[PR #2422] [MERGED] Guard profile-import regex scripts against ReDoS patterns #5598

Closed
opened 2026-10-22 04:17:42 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/2422
Author: @Romuromylus
Created: 6/6/2026
Status: ✅ Merged
Merged: 6/6/2026
Merged by: @Romuromylus

Base: refactor ← Head: fix/regex-redos-guard


📝 Commits (1)

  • ffe866d Guard profile-import regex scripts against ReDoS patterns

📊 Changes

2 files changed (+265 additions, -0 deletions)

View changed files

📝 src-tauri/src/commands/storage/profile.rs (+264 -0)
📝 src-tauri/src/commands/storage/profile/legacy.rs (+1 -0)

📄 Description

Linked issue

Closes #2364

Why this change

  • Regex scripts run a content-supplied findRegex against the full transcript on every generation turn. Only the ST character-card import path was guarded by isPatternSafe; the profile/preset bundle import wrote regex-scripts rows verbatim with no ReDoS check (the issue's repro: import a .marinara profile whose regex-scripts contains (a+)+$, then send a turn with a long run of a → the single-threaded renderer freezes on catastrophic backtracking).

What changed

  • src-tauri/src/commands/storage/profile.rs: added drop_unsafe_regex_scripts plus a faithful Rust port of the TypeScript isPatternSafe heuristic (src/engine/shared/regex/regex-safety.ts — star-height, {n,m} bounds, length cap). The modern profile import (profile_collections_import_plan) now drops regex-scripts rows whose findRegex fails the heuristic, mirroring the existing ST-card import behavior of skipping unsafe patterns at the storage boundary.
  • src-tauri/src/commands/storage/profile/legacy.rs: the legacy SQLite profile import calls the same super::drop_unsafe_regex_scripts, so both import paths share one source of truth.

Scope note (deliberate)

This PR guards the import boundary — the issue's demonstrated vector. I intentionally did not add the isPatternSafe heuristic to the in-app editor schema or the three runtime script executors, because the existing heuristic rejects common safe patterns (\n{3,}, \s{2,}) and both shipped default regex scripts (the HTML-stripper and the blank-line collapser seeded in seed_defaults.rs). A schema refine would block users from saving those patterns, and a runtime skip would silently break the seeded defaults. The issue's preferred runtime hardening is the lorebook scanner's interruptible-worker timeout (vmRegexExecutor, regex-timeout.ts); routing the script executors through that timeout (so a catastrophic pattern is interrupted without dropping safe-but-heuristic-failing patterns) is a larger follow-up worth its own PR.

Refactor impact

Primary owner: Rust storage (imports)

Impact areas reviewed:

  • src-tauri/src/commands/storage/profile.rs (profile_collections_import_plan + new helper)
  • src-tauri/src/commands/storage/profile/legacy.rs (legacy import per-collection match)

Boundary notes:

  • None. Confined to the Rust import normalization layer; the safety helper lives in profile.rs and is shared with the legacy submodule via super::. No TS, engine, or runtime path touched, so seeded defaults and the editor are unaffected.

Pressure points touched:

  • Import modules.

Validation

  • Matching validation command passes locally
  • Full pnpm check passes before PR push/handoff
  • Human/manual validation completed by contributor or reviewer

Manual verification notes

  • cargo test --manifest-path src-tauri/Cargo.toml -p marinara-engine (profile import) — a local-only test confirmed a profile importing findRegex: "(a+)+$" drops that script while a safe a+ script imports. The Rust heuristic was cross-checked against the TS isPatternSafe for parity.
  • cargo clippy --manifest-path src-tauri/Cargo.toml --workspace --all-targets -- -D warnings — clean.

Feature Discoverability

  • Updated src/features/shell/discovery/
  • N/A because this PR is a security hardening fix at the import boundary.

Reason:

  • No new discoverable surface.

Docs and release impact

  • No docs changes needed

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Pasta-Devs/Marinara-Engine/pull/2422 **Author:** [@Romuromylus](https://github.com/Romuromylus) **Created:** 6/6/2026 **Status:** ✅ Merged **Merged:** 6/6/2026 **Merged by:** [@Romuromylus](https://github.com/Romuromylus) **Base:** `refactor` ← **Head:** `fix/regex-redos-guard` --- ### 📝 Commits (1) - [`ffe866d`](https://github.com/Pasta-Devs/Marinara-Engine/commit/ffe866d241d098a45c89af07be2235147f59f49a) Guard profile-import regex scripts against ReDoS patterns ### 📊 Changes **2 files changed** (+265 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `src-tauri/src/commands/storage/profile.rs` (+264 -0) 📝 `src-tauri/src/commands/storage/profile/legacy.rs` (+1 -0) </details> ### 📄 Description ## Linked issue Closes #2364 ## Why this change - Regex scripts run a content-supplied `findRegex` against the full transcript on every generation turn. Only the ST character-card import path was guarded by `isPatternSafe`; the profile/preset bundle import wrote `regex-scripts` rows verbatim with no ReDoS check (the issue's repro: import a `.marinara` profile whose `regex-scripts` contains `(a+)+$`, then send a turn with a long run of `a` → the single-threaded renderer freezes on catastrophic backtracking). ## What changed - `src-tauri/src/commands/storage/profile.rs`: added `drop_unsafe_regex_scripts` plus a faithful Rust port of the TypeScript `isPatternSafe` heuristic (`src/engine/shared/regex/regex-safety.ts` — star-height, `{n,m}` bounds, length cap). The modern profile import (`profile_collections_import_plan`) now drops `regex-scripts` rows whose `findRegex` fails the heuristic, mirroring the existing ST-card import behavior of skipping unsafe patterns at the storage boundary. - `src-tauri/src/commands/storage/profile/legacy.rs`: the legacy SQLite profile import calls the same `super::drop_unsafe_regex_scripts`, so both import paths share one source of truth. ## Scope note (deliberate) This PR guards the **import boundary** — the issue's demonstrated vector. I intentionally did **not** add the `isPatternSafe` heuristic to the in-app editor schema or the three runtime script executors, because the existing heuristic rejects common safe patterns (`\n{3,}`, `\s{2,}`) and both shipped default regex scripts (the HTML-stripper and the blank-line collapser seeded in `seed_defaults.rs`). A schema refine would block users from saving those patterns, and a runtime skip would silently break the seeded defaults. The issue's preferred runtime hardening is the lorebook scanner's interruptible-worker timeout (`vmRegexExecutor`, `regex-timeout.ts`); routing the script executors through that timeout (so a catastrophic pattern is interrupted without dropping safe-but-heuristic-failing patterns) is a larger follow-up worth its own PR. ## Refactor impact Primary owner: Rust storage (imports) Impact areas reviewed: - `src-tauri/src/commands/storage/profile.rs` (`profile_collections_import_plan` + new helper) - `src-tauri/src/commands/storage/profile/legacy.rs` (legacy import per-collection match) Boundary notes: - None. Confined to the Rust import normalization layer; the safety helper lives in `profile.rs` and is shared with the `legacy` submodule via `super::`. No TS, engine, or runtime path touched, so seeded defaults and the editor are unaffected. Pressure points touched: - Import modules. ## Validation - [x] Matching validation command passes locally - [ ] Full `pnpm check` passes before PR push/handoff - [ ] Human/manual validation completed by contributor or reviewer ### Manual verification notes - `cargo test --manifest-path src-tauri/Cargo.toml -p marinara-engine` (profile import) — a local-only test confirmed a profile importing `findRegex: "(a+)+$"` drops that script while a safe `a+` script imports. The Rust heuristic was cross-checked against the TS `isPatternSafe` for parity. - `cargo clippy --manifest-path src-tauri/Cargo.toml --workspace --all-targets -- -D warnings` — clean. ## Feature Discoverability - [ ] Updated `src/features/shell/discovery/` - [x] N/A because this PR is a security hardening fix at the import boundary. Reason: - No new discoverable surface. ## Docs and release impact - [x] No docs changes needed --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-22 04:17:42 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#5598
No description provided.