Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions docs/patches/picomatch@2.3.2.patch
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
diff --git a/lib/parse.js b/lib/parse.js
index caa2b86aa5a567df33cef4bda3c7a97794f439f8..b5e0a33480897aef82fe9c6dfb6e8ee2b56afd63 100644
index caa2b86aa5a567df33cef4bda3c7a97794f439f8..a34abd76c4ef079277c93e33917ceeda634b3ec6 100644
--- a/lib/parse.js
+++ b/lib/parse.js
@@ -280,7 +280,36 @@ const repeatedExtglobRecursion = pattern => {
@@ -280,7 +280,51 @@ const repeatedExtglobRecursion = pattern => {
return depth;
};

Expand Down Expand Up @@ -36,11 +36,26 @@ index caa2b86aa5a567df33cef4bda3c7a97794f439f8..b5e0a33480897aef82fe9c6dfb6e8ee2
+ });
+};
+
+// A repeated body with a slash, such as `+(src/*/)`, backtracks only when one
+// path segment holds two `*`s. The last segment of a repeat and the first
+// segment of the next share one path segment, so they count together. A
+// globstar crosses `/`, so `+(**/a)` never qualifies, and neither does `+(*/*)`.
+const hasUnambiguousSegments = bodyTokens => {
+ const stars = [0];
+ for (const tok of bodyTokens) {
+ if (tok.type === 'globstar') return false;
+ if (tok.type === 'slash') stars.push(0);
+ if (tok.type === 'star' && tok.value !== '') stars[stars.length - 1]++;
+ }
+ if (stars.length < 2) return false;
+ return stars[0] + stars[stars.length - 1] <= 1 && stars.slice(1, -1).every(n => n <= 1);
+};
+
+const analyzeRepeatedExtglob = (body, options, bodyTokens = []) => {
if (options.maxExtglobRecursion === false) {
return { risky: false };
}
@@ -292,6 +321,24 @@ const analyzeRepeatedExtglob = (body, options) => {
@@ -292,6 +336,23 @@ const analyzeRepeatedExtglob = (body, options) => {

const branches = splitTopLevel(body).map(branch => branch.trim());

Expand All @@ -55,17 +70,16 @@ index caa2b86aa5a567df33cef4bda3c7a97794f439f8..b5e0a33480897aef82fe9c6dfb6e8ee2
+ }
+
+ // A literal slash fixes where each repeat starts, since `*` cannot cross
+ // it outside bash mode, so the repeat cannot backtrack.
+ const hasSlash = bodyTokens.some(tok => tok.type === 'slash');
+ if (options.bash === true || !hasSlash) {
+ // it outside bash mode, unless the slash-separated parts split ambiguously.
+ if (options.bash === true || !hasUnambiguousSegments(bodyTokens)) {
+ return { risky: true };
+ }
+ }
+
if (branches.length > 1) {
if (
branches.some(branch => branch === '') ||
@@ -510,7 +557,7 @@ const parse = (input, options) => {
@@ -510,7 +571,7 @@ const parse = (input, options) => {
const extglobClose = token => {
const literal = input.slice(token.startIndex, state.index + 1);
const body = input.slice(token.startIndex + 2, state.index);
Expand All @@ -74,7 +88,7 @@ index caa2b86aa5a567df33cef4bda3c7a97794f439f8..b5e0a33480897aef82fe9c6dfb6e8ee2

if ((token.type === 'plus' || token.type === 'star') && analysis.risky) {
const safeOutput = analysis.safeOutput
@@ -536,6 +583,10 @@ const parse = (input, options) => {
@@ -536,6 +597,10 @@ const parse = (input, options) => {
return;
}

Expand Down
30 changes: 22 additions & 8 deletions docs/patches/picomatch@4.0.7.patch
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
diff --git a/lib/parse.js b/lib/parse.js
index 29207512c60183b4bb9bf5112be3b9b5f4a9a9fd..296afb16b857e181315a800630978fea9f139929 100644
index 29207512c60183b4bb9bf5112be3b9b5f4a9a9fd..57a2333dfccc5005786b4805a749300a2de98f6f 100644
--- a/lib/parse.js
+++ b/lib/parse.js
@@ -284,7 +284,36 @@ const repeatedExtglobRecursion = pattern => {
@@ -284,7 +284,51 @@ const repeatedExtglobRecursion = pattern => {
return depth;
};

Expand Down Expand Up @@ -36,11 +36,26 @@ index 29207512c60183b4bb9bf5112be3b9b5f4a9a9fd..296afb16b857e181315a800630978fea
+ });
+};
+
+// A repeated body with a slash, such as `+(src/*/)`, backtracks only when one
+// path segment holds two `*`s. The last segment of a repeat and the first
+// segment of the next share one path segment, so they count together. A
+// globstar crosses `/`, so `+(**/a)` never qualifies, and neither does `+(*/*)`.
+const hasUnambiguousSegments = bodyTokens => {
+ const stars = [0];
+ for (const tok of bodyTokens) {
+ if (tok.type === 'globstar') return false;
+ if (tok.type === 'slash') stars.push(0);
+ if (tok.type === 'star' && tok.value !== '') stars[stars.length - 1]++;
+ }
+ if (stars.length < 2) return false;
+ return stars[0] + stars[stars.length - 1] <= 1 && stars.slice(1, -1).every(n => n <= 1);
+};
+
+const analyzeRepeatedExtglob = (body, options, bodyTokens = []) => {
if (options.maxExtglobRecursion === false) {
return { risky: false };
}
@@ -296,6 +325,24 @@ const analyzeRepeatedExtglob = (body, options) => {
@@ -296,6 +340,23 @@ const analyzeRepeatedExtglob = (body, options) => {

const branches = splitTopLevel(body).map(branch => branch.trim());

Expand All @@ -55,17 +70,16 @@ index 29207512c60183b4bb9bf5112be3b9b5f4a9a9fd..296afb16b857e181315a800630978fea
+ }
+
+ // A literal slash fixes where each repeat starts, since `*` cannot cross
+ // it outside bash mode, so the repeat cannot backtrack.
+ const hasSlash = bodyTokens.some(tok => tok.type === 'slash');
+ if (options.bash === true || !hasSlash) {
+ // it outside bash mode, unless the slash-separated parts split ambiguously.
+ if (options.bash === true || !hasUnambiguousSegments(bodyTokens)) {
+ return { risky: true };
+ }
+ }
+
if (branches.length > 1) {
if (
branches.some(branch => branch === '') ||
@@ -539,7 +586,7 @@ const parse = (input, options) => {
@@ -539,7 +600,7 @@ const parse = (input, options) => {
const extglobClose = token => {
const literal = input.slice(token.startIndex, state.index + 1);
const body = input.slice(token.startIndex + 2, state.index);
Expand All @@ -74,7 +88,7 @@ index 29207512c60183b4bb9bf5112be3b9b5f4a9a9fd..296afb16b857e181315a800630978fea

if ((token.type === 'plus' || token.type === 'star') && analysis.risky) {
const safeOutput = analysis.safeOutput
@@ -565,6 +612,10 @@ const parse = (input, options) => {
@@ -565,6 +626,10 @@ const parse = (input, options) => {
return;
}

Expand Down
26 changes: 13 additions & 13 deletions docs/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 16 additions & 2 deletions docs/tests/fixtures/picomatch-redos.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,22 @@
// quantifier. Runs in a child process because a backtracking regex blocks
// its own event loop. Usage: node picomatch-redos.cjs <path to picomatch>
const picomatch = require(process.argv[2]);
const globs = ['+(*)', '+(*a)', '*(*a)', '+(a*)b', '+(*[a])', '+(@(*a))'];
const inputs = ['a'.repeat(50000) + '!', 'a'.repeat(50000) + '/'];
const globs = [
'+(*)',
'+(*a)',
'*(*a)',
'+(a*)b',
'+(*[a])',
'+(@(*a))',
'+(*/*)',
'+(**/a)',
];
const inputs = [
'a'.repeat(50000) + '!',
'a'.repeat(50000) + '/',
'aa/'.repeat(3000) + '.x',
'a/'.repeat(5000) + '!',
];
for (const glob of globs) {
for (const input of inputs) {
picomatch(glob)(input);
Expand Down
5 changes: 4 additions & 1 deletion patches/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ repetition, so `+(P)` compiles as `(P)` and `*(P)` as `(P)?`, with the same
matches. Unlike upstream, the patch checks each compiled bracket class, because
a range such as `[+-0]` includes `/`. Other single-branch wildcard bodies are
literalized, like the existing risky-extglob guard. A branch with a literal `/`
keeps its repetition, because the slash fixes where each repeat starts. In root they
keeps its repetition only when no path segment can hold two `*`s, counting the
last segment of one repeat with the first segment of the next. So `+(src/*/)`
keeps working, while `+(*/*)` and `+(**/a)` (a globstar crosses `/`) are
literalized. In root they
cover the CLI globbing (`micromatch`), `typescript-eslint`, and build-time
watcher paths. Docs keeps identical copies under `../docs/patches/` for Astro. Run
`pnpm test src/__tests__/dependency-security.test.ts` and `pnpm --dir docs test`.
Expand Down
30 changes: 22 additions & 8 deletions patches/picomatch@2.3.2.patch
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
diff --git a/lib/parse.js b/lib/parse.js
index caa2b86aa5a567df33cef4bda3c7a97794f439f8..b5e0a33480897aef82fe9c6dfb6e8ee2b56afd63 100644
index caa2b86aa5a567df33cef4bda3c7a97794f439f8..a34abd76c4ef079277c93e33917ceeda634b3ec6 100644
--- a/lib/parse.js
+++ b/lib/parse.js
@@ -280,7 +280,36 @@ const repeatedExtglobRecursion = pattern => {
@@ -280,7 +280,51 @@ const repeatedExtglobRecursion = pattern => {
return depth;
};

Expand Down Expand Up @@ -36,11 +36,26 @@ index caa2b86aa5a567df33cef4bda3c7a97794f439f8..b5e0a33480897aef82fe9c6dfb6e8ee2
+ });
+};
+
+// A repeated body with a slash, such as `+(src/*/)`, backtracks only when one
+// path segment holds two `*`s. The last segment of a repeat and the first
+// segment of the next share one path segment, so they count together. A
+// globstar crosses `/`, so `+(**/a)` never qualifies, and neither does `+(*/*)`.
+const hasUnambiguousSegments = bodyTokens => {
+ const stars = [0];
+ for (const tok of bodyTokens) {
+ if (tok.type === 'globstar') return false;
+ if (tok.type === 'slash') stars.push(0);
+ if (tok.type === 'star' && tok.value !== '') stars[stars.length - 1]++;
+ }
+ if (stars.length < 2) return false;
+ return stars[0] + stars[stars.length - 1] <= 1 && stars.slice(1, -1).every(n => n <= 1);
+};
+
+const analyzeRepeatedExtglob = (body, options, bodyTokens = []) => {
if (options.maxExtglobRecursion === false) {
return { risky: false };
}
@@ -292,6 +321,24 @@ const analyzeRepeatedExtglob = (body, options) => {
@@ -292,6 +336,23 @@ const analyzeRepeatedExtglob = (body, options) => {

const branches = splitTopLevel(body).map(branch => branch.trim());

Expand All @@ -55,17 +70,16 @@ index caa2b86aa5a567df33cef4bda3c7a97794f439f8..b5e0a33480897aef82fe9c6dfb6e8ee2
+ }
+
+ // A literal slash fixes where each repeat starts, since `*` cannot cross
+ // it outside bash mode, so the repeat cannot backtrack.
+ const hasSlash = bodyTokens.some(tok => tok.type === 'slash');
+ if (options.bash === true || !hasSlash) {
+ // it outside bash mode, unless the slash-separated parts split ambiguously.
+ if (options.bash === true || !hasUnambiguousSegments(bodyTokens)) {
+ return { risky: true };
+ }
+ }
+
if (branches.length > 1) {
if (
branches.some(branch => branch === '') ||
@@ -510,7 +557,7 @@ const parse = (input, options) => {
@@ -510,7 +571,7 @@ const parse = (input, options) => {
const extglobClose = token => {
const literal = input.slice(token.startIndex, state.index + 1);
const body = input.slice(token.startIndex + 2, state.index);
Expand All @@ -74,7 +88,7 @@ index caa2b86aa5a567df33cef4bda3c7a97794f439f8..b5e0a33480897aef82fe9c6dfb6e8ee2

if ((token.type === 'plus' || token.type === 'star') && analysis.risky) {
const safeOutput = analysis.safeOutput
@@ -536,6 +583,10 @@ const parse = (input, options) => {
@@ -536,6 +597,10 @@ const parse = (input, options) => {
return;
}

Expand Down
Loading
Loading