Skip to content

Commit 7953af1

Browse files
committed
do not allow .. to consume drive letter on Windows
1 parent 1caf918 commit 7953af1

2 files changed

Lines changed: 36 additions & 1 deletion

File tree

src/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,7 @@ export class Minimatch {
624624
parts = this.slashSplit(parts)
625625
}
626626
let didSomething: boolean = false
627+
627628
do {
628629
didSomething = false
629630
// <pre>/<e>/<rest> -> <pre>/<rest>
@@ -652,7 +653,13 @@ export class Minimatch {
652653
let dd: number = 0
653654
while (-1 !== (dd = parts.indexOf('..', dd + 1))) {
654655
const p = parts[dd - 1]
655-
if (p && p !== '.' && p !== '..' && p !== '**') {
656+
if (
657+
p &&
658+
p !== '.' &&
659+
p !== '..' &&
660+
p !== '**' &&
661+
!(this.isWindows && /^[a-z]:$/i.test(p))
662+
) {
656663
didSomething = true
657664
parts.splice(dd - 1, 2)
658665
dd -= 2

test/optimization-level-2.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ process.env._MINIMATCH_TEST_OPTIMIZATION_LEVEL = String(optimizationLevel)
99

1010
// run all the basic tests with this setting
1111
import './basic.js'
12+
import { Minimatch } from '../src/index.js'
1213

1314
t.test('explicit pattern coalescing and optimization', t => {
1415
t.plan(2)
@@ -122,3 +123,30 @@ t.test('optimize the file as well', t => {
122123
}
123124
t.end()
124125
})
126+
127+
t.test('do not eat the drive letter on Windows', t => {
128+
const mw1 = new Minimatch('public/**', {
129+
optimizationLevel: 1,
130+
platform: 'win32',
131+
})
132+
const mw2 = new Minimatch('public/**', {
133+
optimizationLevel: 2,
134+
platform: 'win32',
135+
})
136+
const mu1 = new Minimatch('public/**', {
137+
optimizationLevel: 1,
138+
platform: 'darwin',
139+
})
140+
const mu2 = new Minimatch('public/**', {
141+
optimizationLevel: 2,
142+
platform: 'darwin',
143+
})
144+
// drive letters are a root on Windows
145+
t.equal(mw1.match('C:/../public/secret'), false)
146+
t.equal(mw2.match('C:/../public/secret'), false)
147+
// level 1 doesn't optimize out ..
148+
t.equal(mu1.match('C:/../public/secret'), false)
149+
// not special on unix systems, so .. eats the drive letter
150+
t.equal(mu2.match('C:/../public/secret'), true)
151+
t.end()
152+
})

0 commit comments

Comments
 (0)