Skip to content

Commit

Permalink
refactor: isFileReadable
Browse files Browse the repository at this point in the history
  • Loading branch information
patak-dev committed Oct 26, 2021
1 parent 45a4c42 commit 978e9ab
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 17 deletions.
16 changes: 6 additions & 10 deletions packages/vite/src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ import {
isDataUrl,
cleanUrl,
slash,
nestedResolveFrom
nestedResolveFrom,
isFileReadable
} from '../utils'
import { ViteDevServer, SSROptions } from '..'
import { createFilter } from '@rollup/pluginutils'
Expand Down Expand Up @@ -372,15 +373,10 @@ function tryResolveFile(
tryPrefix?: string,
skipPackageJson?: boolean
): string | undefined {
let isReadable = false
try {
// #2051 if we don't have read permission on a directory, existsSync() still
// works and will result in massively slow subsequent checks (which are
// unnecessary in the first place)
fs.accessSync(file, fs.constants.R_OK)
isReadable = true
} catch (e) {}
if (isReadable) {
// #2051 if we don't have read permission on a directory, existsSync() still
// works and will result in massively slow subsequent checks (which are
// unnecessary in the first place)
if (isFileReadable(file)) {
if (!fs.statSync(file).isDirectory()) {
return getRealPath(file, preserveSymlinks) + postfix
} else if (tryIndex) {
Expand Down
8 changes: 4 additions & 4 deletions packages/vite/src/node/server/middlewares/static.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import path from 'path'
import fs from 'fs'
import { ServerResponse } from 'http'
import sirv, { Options } from 'sirv'
import { Connect } from 'types/connect'
Expand All @@ -12,7 +11,8 @@ import {
isImportRequest,
isInternalRequest,
isWindows,
slash
slash,
isFileReadable
} from '../../utils'

const sirvOptions: Options = {
Expand Down Expand Up @@ -144,7 +144,7 @@ export function isFileServingAllowed(
return true

if (!server.config.server.fs.strict) {
if (fs.existsSync(file)) {
if (isFileReadable(file)) {
server.config.logger.warnOnce(`Unrestricted file system access to "${url}"`)
server.config.logger.warnOnce(
`For security concerns, accessing files outside of serving allow list will ` +
Expand All @@ -167,7 +167,7 @@ function ensureServingAccess(
if (isFileServingAllowed(url, server)) {
return true
}
if (fs.existsSync(url)) {
if (isFileReadable(cleanUrl(url))) {
const message = `The request url "${url}" is outside of Vite serving allow list:
${server.config.server.fs.allow.map((i) => `- ${i}`).join('\n')}
Expand Down
5 changes: 2 additions & 3 deletions packages/vite/src/node/server/searchRoot.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from 'fs'
import { dirname } from 'path'
import { join } from 'path'
import { isFileReadable } from '../utils'

// https://github.com/vitejs/vite/issues/2820#issuecomment-812495079
const ROOT_FILES = [
Expand All @@ -21,9 +22,7 @@ const ROOT_FILES = [
// yarn: https://classic.yarnpkg.com/en/docs/workspaces/#toc-how-to-use-it
function hasWorkspacePackageJSON(root: string): boolean {
const path = join(root, 'package.json')
try {
fs.accessSync(path, fs.constants.R_OK)
} catch {
if (!isFileReadable(path)) {
return false
}
const content = JSON.parse(fs.readFileSync(path, 'utf-8')) || {}
Expand Down
14 changes: 14 additions & 0 deletions packages/vite/src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,20 @@ export function writeFile(
fs.writeFileSync(filename, content)
}

/**
* Use instead of fs.existsSync(filename)
* #2051 if we don't have read permission on a directory, existsSync() still
* works and will result in massively slow subsequent checks (which are
* unnecessary in the first place)
*/
export function isFileReadable(filename: string,) {
try {
fs.accessSync(filename, fs.constants.R_OK)
return true
} catch (e) {}
return false
}

/**
* Delete every file and subdirectory. **The given directory must exist.**
* Pass an optional `skip` array to preserve files in the root directory.
Expand Down

0 comments on commit 978e9ab

Please sign in to comment.