diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index a978ce7..c466f41 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -2,7 +2,7 @@ name: Tests on: push: - branches: ["main", "fix/regex-regression-hardening"] + branches: ["main", "fix/regex-regression-hardening", "fix/regex-engine-regressions"] pull_request: workflow_dispatch: diff --git a/CHANGELOG.MD b/CHANGELOG.MD index b20ce32..dd69af1 100644 --- a/CHANGELOG.MD +++ b/CHANGELOG.MD @@ -7,6 +7,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 --- +## [1.1.1] - 2026-10-22 + +### Fixed +- Validate flags even when the regex pattern is empty. +- Report split output truncation only when segments actually exceed the configured cap. +- Bound highlighted preview rendering for large inputs while preserving full-input match counts. + +### Added +- Regression coverage for empty-pattern flag validation, exact split limits, and bounded HTML-safe previews. + ## [1.1.0] - 2026-10-22 ### Fixed diff --git a/README.MD b/README.MD index f853b44..37657a5 100644 --- a/README.MD +++ b/README.MD @@ -8,7 +8,7 @@ Built with pure HTML, CSS & JavaScript — zero dependencies, zero backend, zero [**Live Demo**](#) · [**Report Bug**](../../issues) · [**Request Feature**](../../issues) - +    @@ -28,7 +28,7 @@ Built with pure HTML, CSS & JavaScript — zero dependencies, zero backend, zero - 📊 **Detailed match info** — positions, lengths, captured & named groups - ❌ **Friendly error messages** for invalid regex patterns - 🛡️ **Worker-isolated execution** with a 2.5-second timeout and a manual Stop control -- 🧯 **Resource limits** for pattern length, text size, matches, split segments, and replacement output +- 🧯 **Resource limits** for pattern length, text size, matches, split segments, replacement output, and highlighted preview rendering - 🧩 **Correct capture offsets** and Unicode-safe zero-width match advancement - 📱 **100% responsive** — works beautifully on mobile, tablet & desktop @@ -142,6 +142,7 @@ The regression suite covers capture offsets, named and unmatched groups, zero-wi ### Safety limits - Pattern: 10,000 characters maximum - Test text: 5,000,000 characters maximum +- Highlighted preview: 250,000 characters maximum (full matching remains in the worker) - Matches and split segments: 50,000 each - Replacement input: 10,000 characters maximum - Worker evaluation timeout: 2.5 seconds for UI operations diff --git a/docs/REGRESSION_CHECKLIST.md b/docs/REGRESSION_CHECKLIST.md index 4e70ede..91f5289 100644 --- a/docs/REGRESSION_CHECKLIST.md +++ b/docs/REGRESSION_CHECKLIST.md @@ -18,3 +18,12 @@ ## Verification Run `npm test` with Node.js 18 or later. GitHub Actions runs the same suite on pushes and pull requests. Treat the latest workflow result as the test pass/fail status; checkmarks above track coverage, not successful execution. + + +## Additional hardening assertions + +- [x] Empty patterns still validate duplicate and incompatible flags. +- [x] Split output at exactly the configured cap is not incorrectly marked as truncated. +- [x] Large highlighted previews are bounded and user-controlled HTML remains escaped. + +These additional assertions complement the ten original checklist scenarios. diff --git a/package.json b/package.json index 27072fc..8290e8d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "regex-pro", - "version": "1.1.0", + "version": "1.1.1", "private": true, "description": "A zero-runtime-dependency browser regex tester with worker-isolated evaluation", "scripts": { diff --git a/src/Script/regex-core.js b/src/Script/regex-core.js index 794162a..02a594a 100644 --- a/src/Script/regex-core.js +++ b/src/Script/regex-core.js @@ -6,6 +6,7 @@ 'use strict'; const MAX_MATCHES = 50000, MAX_SEGMENTS = 50000, MAX_TEXT_LENGTH = 5000000; const MAX_PATTERN_LENGTH = 10000, MAX_REPLACEMENT_LENGTH = 10000, MAX_RESULT_LENGTH = 10000000; + const MAX_PREVIEW_LENGTH = 250000; const ALLOWED_FLAGS = /^[dgimsuvy]*$/; function validateFlags(flags) { @@ -19,9 +20,9 @@ function compile(pattern, flags) { if (typeof pattern !== 'string') return { regex: null, error: 'Regex pattern must be a string.' }; if (pattern.length > MAX_PATTERN_LENGTH) return { regex: null, error: 'Regex pattern is too long (maximum ' + MAX_PATTERN_LENGTH + ' characters).' }; - if (pattern === '') return { regex: null, error: null }; const flagError = validateFlags(flags); if (flagError) return { regex: null, error: flagError }; + if (pattern === '') return { regex: null, error: null }; try { return { regex: new RegExp(pattern, flags), error: null }; } catch (error) { return { regex: null, error: error && error.message ? error.message : 'Invalid regular expression.' }; } } @@ -207,12 +208,35 @@ const flags = regex.flags.includes('g') ? regex.flags : regex.flags + 'g'; return text.replace(new RegExp(regex.source, flags), replacement); } - function split(regex, text, maxSegments) { + function splitSummary(regex, text, maxSegments) { if (typeof text !== 'string') text = String(text == null ? '' : text); if (text.length > MAX_TEXT_LENGTH) throw new Error('Input text is too large (maximum ' + MAX_TEXT_LENGTH.toLocaleString() + ' characters).'); - if (!(regex instanceof RegExp)) return [text]; - const limit = Number.isInteger(maxSegments) && maxSegments > 0 ? Math.min(maxSegments, MAX_SEGMENTS) : MAX_SEGMENTS; - return text.split(new RegExp(regex.source, regex.flags), limit); + const requestedLimit = Number.isInteger(maxSegments) && maxSegments > 0 ? maxSegments : MAX_SEGMENTS; + const limit = Math.min(requestedLimit, MAX_SEGMENTS); + if (!(regex instanceof RegExp)) return { segments: [text], truncated: false }; + // Probe one extra segment so hitting the exact cap is not mistaken for truncation. + const result = text.split(new RegExp(regex.source, regex.flags), limit + 1); + const truncated = result.length > limit; + return { segments: truncated ? result.slice(0, limit) : result, truncated }; + } + + function split(regex, text, maxSegments) { + return splitSummary(regex, text, maxSegments).segments; + } + + function createPreview(text, matches, maxCharacters) { + if (typeof text !== 'string') text = String(text == null ? '' : text); + const requestedLimit = Number.isInteger(maxCharacters) && maxCharacters > 0 ? maxCharacters : MAX_PREVIEW_LENGTH; + const limit = Math.min(requestedLimit, MAX_PREVIEW_LENGTH); + const previewText = text.slice(0, limit); + const previewMatches = Array.isArray(matches) + ? matches.filter(match => match && Number.isFinite(match.index) && match.index < limit) + : []; + return { + html: highlight(previewText, previewMatches), + truncated: text.length > previewText.length, + visibleLength: previewText.length + }; } function normalizeStoredList(value, limit) { @@ -242,16 +266,16 @@ if (data.op === 'replace') return { replaceResult: replace(compiled.regex, text, data.replacement == null ? '' : data.replacement, data.all !== false) }; const found = findAll(compiled.regex, text, data.maxMatches); const replaceResult = replace(compiled.regex, text, data.replacement == null ? '' : data.replacement, true); - const segments = split(compiled.regex, text, MAX_SEGMENTS); + const splitResult = splitSummary(compiled.regex, text, MAX_SEGMENTS); return { - matches: found.matches, truncated: found.truncated, replaceResult, segments, - segmentsTruncated: segments.length >= MAX_SEGMENTS + matches: found.matches, truncated: found.truncated, replaceResult, segments: splitResult.segments, + segmentsTruncated: splitResult.truncated }; } return { - MAX_MATCHES, MAX_SEGMENTS, MAX_TEXT_LENGTH, MAX_PATTERN_LENGTH, MAX_REPLACEMENT_LENGTH, - compile, validateFlags, advanceStringIndex, findAll, escapeHtml, highlight, replace, split, + MAX_MATCHES, MAX_SEGMENTS, MAX_TEXT_LENGTH, MAX_PATTERN_LENGTH, MAX_REPLACEMENT_LENGTH, MAX_PREVIEW_LENGTH, + compile, validateFlags, advanceStringIndex, findAll, escapeHtml, highlight, createPreview, replace, split, splitSummary, normalizeStoredList, run }; }); diff --git a/src/Script/script.js b/src/Script/script.js index 693266f..1109a8f 100644 --- a/src/Script/script.js +++ b/src/Script/script.js @@ -651,21 +651,6 @@ state.text = dom.testString.value; updateTextMeta(); - if (!state.pattern) { - RegexWorker.cancel('evaluate', 'Pattern cleared.'); - RegexWorker.cancel('replace', 'Pattern cleared.'); - state.matches = []; - state.matchesTruncated = false; - clearResults(); - hideError(); - setStatus('', 'Ready'); - dom.explanation.innerHTML = '
Preview limited to the first ' + + formatNumber(preview.visibleLength) + + ' characters to keep the interface responsive. The original input remains unchanged.