Skip to content

Commit 4a06e66

Browse files
authoredJun 6, 2023
feat: support files for fs.allow (#12863)
1 parent b9a6ba0 commit 4a06e66

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed
 

‎docs/config/server-options.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,8 @@ Restrict serving files outside of workspace root.
266266

267267
Restrict files that could be served via `/@fs/`. When `server.fs.strict` is set to `true`, accessing files outside this directory list that aren't imported from an allowed file will result in a 403.
268268

269+
Both directories and files can be provided.
270+
269271
Vite will search for the root of the potential workspace and use it as default. A valid workspace met the following conditions, otherwise will fall back to the [project root](/guide/#index-html-and-project-root).
270272

271273
- contains `workspaces` field in `package.json`
@@ -298,7 +300,8 @@ export default defineConfig({
298300
// search up for workspace root
299301
searchForWorkspaceRoot(process.cwd()),
300302
// your custom rules
301-
'/path/to/custom/allow',
303+
'/path/to/custom/allow_directory',
304+
'/path/to/custom/allow_file.demo',
302305
],
303306
},
304307
},

‎packages/vite/src/node/server/middlewares/static.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
isImportRequest,
1515
isInternalRequest,
1616
isParentDirectory,
17+
isSameFileUri,
1718
isWindows,
1819
removeLeadingSlash,
1920
shouldServeFile,
@@ -199,7 +200,11 @@ export function isFileServingAllowed(
199200

200201
if (server.moduleGraph.safeModulesPath.has(file)) return true
201202

202-
if (server.config.server.fs.allow.some((dir) => isParentDirectory(dir, file)))
203+
if (
204+
server.config.server.fs.allow.some(
205+
(uri) => isSameFileUri(uri, file) || isParentDirectory(uri, file),
206+
)
207+
)
203208
return true
204209

205210
return false

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

+16
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,22 @@ export function isParentDirectory(dir: string, file: string): boolean {
240240
)
241241
}
242242

243+
/**
244+
* Check if 2 file name are identical
245+
*
246+
* Warning: parameters are not validated, only works with normalized absolute paths
247+
*
248+
* @param file1 - normalized absolute path
249+
* @param file2 - normalized absolute path
250+
* @returns true if both files url are identical
251+
*/
252+
export function isSameFileUri(file1: string, file2: string): boolean {
253+
return (
254+
file1 === file2 ||
255+
(isCaseInsensitiveFS && file1.toLowerCase() === file2.toLowerCase())
256+
)
257+
}
258+
243259
export const queryRE = /\?.*$/s
244260

245261
const postfixRE = /[?#].*$/s

0 commit comments

Comments
 (0)
Please sign in to comment.