Skip to content

fix: Potential ReDoS Vulnerability or Inefficient Regular Expression in Project: Need for Assessment and Mitigation #2057

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions packages/glob/__tests__/internal-pattern.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as path from 'path'
import {MatchKind} from '../src/internal-match-kind'
import {promises as fs} from 'fs'
import {Pattern} from '../src/internal-pattern'
import { performance } from 'perf_hooks'

const IS_WINDOWS = process.platform === 'win32'

Expand Down Expand Up @@ -347,6 +348,17 @@ describe('pattern', () => {
})
})

describe('globEscape ReDos', () => {
it('done in 1s', () => {
const attackString = `${'['.repeat(100000)}\u0000`
const startTime = performance.now()
Pattern.globEscape(attackString)
const endTime = performance.now()
const timeTaken = endTime - startTime
expect(timeTaken).toBeLessThan(1000)
})
})

function getTestTemp(): string {
return path.join(__dirname, '_temp', 'internal-pattern')
}
2 changes: 1 addition & 1 deletion packages/glob/src/internal-pattern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ export class Pattern {
*/
static globEscape(s: string): string {
return (IS_WINDOWS ? s : s.replace(/\\/g, '\\\\')) // escape '\' on Linux/macOS
.replace(/(\[)(?=[^/]+\])/g, '[[]') // escape '[' when ']' follows within the path segment
.replace(/(\[)(?!\[{500})([^/]+\])/g, '[[]$2') // escape '[' when ']' follows within the path segment
.replace(/\?/g, '[?]') // escape '?'
.replace(/\*/g, '[*]') // escape '*'
}
Expand Down