Skip to content

Commit 92637a2

Browse files
authoredNov 13, 2022
fix: gracefully handle forbidden filesystem access (#10793)
1 parent 3b770a5 commit 92637a2

File tree

4 files changed

+29
-11
lines changed

4 files changed

+29
-11
lines changed
 

‎packages/vite/src/node/__tests__/utils.spec.ts

+27
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
import fs from 'node:fs'
12
import { describe, expect, test } from 'vitest'
23
import {
34
asyncFlatten,
45
getHash,
56
getLocalhostAddressIfDiffersFromDNS,
67
getPotentialTsSrcPaths,
78
injectQuery,
9+
isFileReadable,
810
isWindows,
911
posToNumber,
1012
resolveHostname
@@ -236,3 +238,28 @@ describe('asyncFlatten', () => {
236238
expect(arr).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9])
237239
})
238240
})
241+
242+
describe('isFileReadable', () => {
243+
test("file doesn't exist", async () => {
244+
expect(isFileReadable('/does_not_exist')).toBe(false)
245+
})
246+
247+
const testFile = require.resolve(
248+
'./utils/isFileReadable/permission-test-file'
249+
)
250+
test('file with normal permission', async () => {
251+
expect(isFileReadable(testFile)).toBe(true)
252+
})
253+
254+
if (process.platform !== 'win32') {
255+
test('file with read-only permission', async () => {
256+
fs.chmodSync(testFile, '400')
257+
expect(isFileReadable(testFile)).toBe(true)
258+
})
259+
test('file without read permission', async () => {
260+
fs.chmodSync(testFile, '044')
261+
expect(isFileReadable(testFile)).toBe(false)
262+
fs.chmodSync(testFile, '644')
263+
})
264+
}
265+
})

‎packages/vite/src/node/__tests__/utils/isFileReadable/permission-test-file

Whitespace-only changes.

‎packages/vite/src/node/plugins/resolve.ts

-3
Original file line numberDiff line numberDiff line change
@@ -531,9 +531,6 @@ function tryResolveFile(
531531
tryPrefix?: string,
532532
skipPackageJson?: boolean
533533
): string | undefined {
534-
// #2051 if we don't have read permission on a directory, existsSync() still
535-
// works and will result in massively slow subsequent checks (which are
536-
// unnecessary in the first place)
537534
if (isFileReadable(file)) {
538535
if (!fs.statSync(file).isDirectory()) {
539536
return getRealPath(file, options.preserveSymlinks) + postfix

‎packages/vite/src/node/utils.ts

+2-8
Original file line numberDiff line numberDiff line change
@@ -535,16 +535,10 @@ export function writeFile(
535535
fs.writeFileSync(filename, content)
536536
}
537537

538-
/**
539-
* Use fs.statSync(filename) instead of fs.existsSync(filename)
540-
* #2051 if we don't have read permission on a directory, existsSync() still
541-
* works and will result in massively slow subsequent checks (which are
542-
* unnecessary in the first place)
543-
*/
544538
export function isFileReadable(filename: string): boolean {
545539
try {
546-
const stat = fs.statSync(filename, { throwIfNoEntry: false })
547-
return !!stat
540+
fs.accessSync(filename, fs.constants.R_OK)
541+
return true
548542
} catch {
549543
return false
550544
}

0 commit comments

Comments
 (0)
Please sign in to comment.