Skip to content
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