[PR #8055] [MERGED] fix(regex): keep imported pattern safety analysis static #8078

Closed
opened 2026-10-24 04:10:11 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/8055
Author: @SpicyMarinara
Created: 10/22/2026
Status: ✅ Merged
Merged: 10/22/2026
Merged by: @SpicyMarinara

Base: staging ← Head: fix/regex-scanning-659-660


📝 Commits (5)

  • 8db7c03 test: reproduce regex safety checker scanning findings
  • 95ffbea fix(regex): analyze character sets without evaluating patterns
  • 0fb298d fix(regex): keep Unicode surrogate analysis conservative
  • 7f8367b fix(regex): compare only guaranteed alternative prefixes
  • 3bd90a4 fix(regex): preserve optional alternative suffixes

📊 Changes

2 files changed (+317 additions, -23 deletions)

View changed files

📝 packages/shared/src/utils/regex-safety.ts (+159 -23)
➕ scripts/regressions/regex-safety-security.regression.ts (+158 -0)

📄 Description

Linked issue

Closes #8053

Addresses code-scanning alerts #659 and #660.

Why this change

Imported card regex scripts pass through a shared safety checker before import and display processing. The checker executed an interpolated pattern fragment while inspecting a character class, and it compared repeated alternatives by spelling rather than by the characters they match. Bounded probes reproduced a server-side checker stall and a synchronous client-processing stall on main and staging.

What changed

  • Inspect character classes statically; never construct or execute a regex from an unvalidated source fragment. Class closure follows JavaScript semantics, including empty [] and [^] classes.
  • Compare the characters that repeated alternatives can match, including literals, classes, ranges, complements, escapes and case variants. Unsupported atoms end positional proof conservatively; earlier known differences can still establish disjointness.
  • Preserve optional atoms and their full required suffixes. A memoized walk of token-position pairs checks both consuming and skipping optional atoms without expanding every combination. This retains disjoint optional/emoji patterns while rejecting the reproduced overlap and width aliases.
  • Keep the 1,000-character source ceiling, bounded class enumeration and per-analysis character-set cache. Add runnable, bounded regressions for both reported failures, review repairs and compatibility controls.

Validation

  • pnpm check passes locally
  • Container (Docker / Podman) built and ran without issue
  • Ran the app, clicked through the changes manually
  • Checked edge cases (light + dark mode, mobile viewport, empty states, error paths)
  • Above manual verification completed (describe below)
  • Read and followed CONTRIBUTING.md

Final implementation: 3bd90a4edeb9cc14f1b9d46fef7e70d18d76eb26.

  • pnpm check passed on this source, including formatting, localization, TypeScript, ESLint and production builds. Only the pre-existing HomeProfessorMariChat.tsx hook warning remains.
  • Retained regex-safety-security regression passed, with bounded child processes and a proxy proving the checker never dynamically compiles source fragments. The exact regression against immutable 7f8367b7 fails as expected on leading-optional compatibility.
  • Independent bounded matrix passed 59/59: 29 safe cases accepted and 30 unsafe cases rejected, including native tiny semantic controls. Immutable 7f8367b7 accepted only 20/29 safe controls. No repeated hostile regex was executed by these probes.
  • Existing prompt regression passed all 199 named cases; existing regex-script-timeout regression passed.
  • pnpm smoke:ui passed 7/7 desktop Chromium tests on isolated ports 7870/7871.
  • Actual extracted production client parse/apply/replacement functions ran in Node 24 and blank Chromium 153: 11/11 safe stored-script cases applied; four unsafe sources were skipped with zero RegExp constructor attempts. This is isolated function execution, not a React-hook, full card-import UI, persisted-database or physical-device test.
  • Independent final source review found no blocking defect. The final fresh local CodeRabbit review covered both changed files with zero findings. Its earlier valid suffix-preservation finding was reproduced, fixed and retained in the regression; the valid Copilot/Dottore Unicode and optional-prefix findings were also reproduced and covered.
  • A 998-character shared-prefix/optional cost control completed in about 6 ms locally. The position walk is bounded by the source ceiling; this is a measured control, not an exhaustive performance claim.
  • git diff --check passed. Fresh hosted CI and final-head Dottore Review remain merge gates and are tracked on GitHub.

Earlier hosted CI on 7f8367b7 completed all four workflows successfully, including native platform/container checks and both CodeQL analyses with zero results. Those earlier results are not substituted for checks on the final head.

Review dispositions and claim boundary

The previous body-wide surrogate refusal and leading-optional compatibility regression are repaired. Full suffixes now remain present during optional alignment; unsupported-width atoms still stop proof. Null/surrogate and complement handling retain their conservative semantics. The default source-length guard runs before analysis; production callers use its default options. ST conversion calls isPatternSafe before retaining imported scripts.

The checker remains a static heuristic. Some safe complex patterns can be refused when their syntax cannot be modeled, and this change does not claim exhaustive safety for every JavaScript regex or add a browser worker timeout. Existing server execution timeouts remain in place. The prior naming/allocation nitpicks were not correctness defects and were left scoped to the existing discriminator and bounded lookup.

Docs and release impact

No user-facing copy, installation/update flow, docs, changelog or release metadata changes.

UI evidence (if applicable)

No visual change.


🔄 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/8055 **Author:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Created:** 10/22/2026 **Status:** ✅ Merged **Merged:** 10/22/2026 **Merged by:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Base:** `staging` ← **Head:** `fix/regex-scanning-659-660` --- ### 📝 Commits (5) - [`8db7c03`](https://github.com/Pasta-Devs/Marinara-Engine/commit/8db7c03d33f986ce6f938334512b2053e4a89498) test: reproduce regex safety checker scanning findings - [`95ffbea`](https://github.com/Pasta-Devs/Marinara-Engine/commit/95ffbea6e076d053b2cf9fd17c4e03e20ace1749) fix(regex): analyze character sets without evaluating patterns - [`0fb298d`](https://github.com/Pasta-Devs/Marinara-Engine/commit/0fb298da64ff06f47735007072f7d5ac65fa9c67) fix(regex): keep Unicode surrogate analysis conservative - [`7f8367b`](https://github.com/Pasta-Devs/Marinara-Engine/commit/7f8367b7053bbcb02d630cd320d87c5f0e6d1a50) fix(regex): compare only guaranteed alternative prefixes - [`3bd90a4`](https://github.com/Pasta-Devs/Marinara-Engine/commit/3bd90a4edeb9cc14f1b9d46fef7e70d18d76eb26) fix(regex): preserve optional alternative suffixes ### 📊 Changes **2 files changed** (+317 additions, -23 deletions) <details> <summary>View changed files</summary> 📝 `packages/shared/src/utils/regex-safety.ts` (+159 -23) ➕ `scripts/regressions/regex-safety-security.regression.ts` (+158 -0) </details> ### 📄 Description ## Linked issue Closes #8053 Addresses code-scanning alerts #659 and #660. ## Why this change Imported card regex scripts pass through a shared safety checker before import and display processing. The checker executed an interpolated pattern fragment while inspecting a character class, and it compared repeated alternatives by spelling rather than by the characters they match. Bounded probes reproduced a server-side checker stall and a synchronous client-processing stall on main and staging. ## What changed - Inspect character classes statically; never construct or execute a regex from an unvalidated source fragment. Class closure follows JavaScript semantics, including empty `[]` and `[^]` classes. - Compare the characters that repeated alternatives can match, including literals, classes, ranges, complements, escapes and case variants. Unsupported atoms end positional proof conservatively; earlier known differences can still establish disjointness. - Preserve optional atoms and their full required suffixes. A memoized walk of token-position pairs checks both consuming and skipping optional atoms without expanding every combination. This retains disjoint optional/emoji patterns while rejecting the reproduced overlap and width aliases. - Keep the 1,000-character source ceiling, bounded class enumeration and per-analysis character-set cache. Add runnable, bounded regressions for both reported failures, review repairs and compatibility controls. ## Validation - [ ] `pnpm check` passes locally - [ ] Container (Docker / Podman) built and ran without issue - [ ] Ran the app, clicked through the changes manually - [ ] Checked edge cases (light + dark mode, mobile viewport, empty states, error paths) - [ ] Above manual verification completed (describe below) - [ ] Read and followed `CONTRIBUTING.md` Final implementation: `3bd90a4edeb9cc14f1b9d46fef7e70d18d76eb26`. - `pnpm check` passed on this source, including formatting, localization, TypeScript, ESLint and production builds. Only the pre-existing `HomeProfessorMariChat.tsx` hook warning remains. - Retained `regex-safety-security` regression passed, with bounded child processes and a proxy proving the checker never dynamically compiles source fragments. The exact regression against immutable `7f8367b7` fails as expected on leading-optional compatibility. - Independent bounded matrix passed 59/59: 29 safe cases accepted and 30 unsafe cases rejected, including native tiny semantic controls. Immutable `7f8367b7` accepted only 20/29 safe controls. No repeated hostile regex was executed by these probes. - Existing prompt regression passed all 199 named cases; existing regex-script-timeout regression passed. - `pnpm smoke:ui` passed 7/7 desktop Chromium tests on isolated ports 7870/7871. - Actual extracted production client parse/apply/replacement functions ran in Node 24 and blank Chromium 153: 11/11 safe stored-script cases applied; four unsafe sources were skipped with zero RegExp constructor attempts. This is isolated function execution, not a React-hook, full card-import UI, persisted-database or physical-device test. - Independent final source review found no blocking defect. The final fresh local CodeRabbit review covered both changed files with zero findings. Its earlier valid suffix-preservation finding was reproduced, fixed and retained in the regression; the valid Copilot/Dottore Unicode and optional-prefix findings were also reproduced and covered. - A 998-character shared-prefix/optional cost control completed in about 6 ms locally. The position walk is bounded by the source ceiling; this is a measured control, not an exhaustive performance claim. - `git diff --check` passed. Fresh hosted CI and final-head Dottore Review remain merge gates and are tracked on GitHub. Earlier hosted CI on `7f8367b7` completed all four workflows successfully, including native platform/container checks and both CodeQL analyses with zero results. Those earlier results are not substituted for checks on the final head. ### Review dispositions and claim boundary The previous body-wide surrogate refusal and leading-optional compatibility regression are repaired. Full suffixes now remain present during optional alignment; unsupported-width atoms still stop proof. Null/surrogate and complement handling retain their conservative semantics. The default source-length guard runs before analysis; production callers use its default options. ST conversion calls `isPatternSafe` before retaining imported scripts. The checker remains a static heuristic. Some safe complex patterns can be refused when their syntax cannot be modeled, and this change does not claim exhaustive safety for every JavaScript regex or add a browser worker timeout. Existing server execution timeouts remain in place. The prior naming/allocation nitpicks were not correctness defects and were left scoped to the existing discriminator and bounded lookup. ## Docs and release impact No user-facing copy, installation/update flow, docs, changelog or release metadata changes. ## UI evidence (if applicable) No visual change. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-24 04:10:11 +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#8078
No description provided.