-
-
Notifications
You must be signed in to change notification settings - Fork 112
feature: disable root path check when serve is false #467
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
8c39dca
387499c
64bc7f8
513b8c4
da2fb25
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,8 +18,13 @@ const supportedEncodings = ['br', 'gzip', 'deflate'] | |
| send.mime.default_type = 'application/octet-stream' | ||
|
|
||
| async function fastifyStatic (fastify, opts) { | ||
| if (opts.serve === false && opts.root === undefined) { | ||
| opts.root = process.cwd() | ||
| fastify.log.warn('No root path provided. Defaulting to current working directory. This may pose security risks if not intended.') | ||
| } | ||
|
|
||
| opts.root = normalizeRoot(opts.root) | ||
| checkRootPathForErrors(fastify, opts.root) | ||
| checkRootPathForErrors(fastify, opts.root, opts.serve === false) | ||
|
|
||
| const setHeaders = opts.setHeaders | ||
| if (setHeaders !== undefined && typeof setHeaders !== 'function') { | ||
|
|
@@ -408,7 +413,7 @@ function normalizeRoot (root) { | |
| return root | ||
| } | ||
|
|
||
| function checkRootPathForErrors (fastify, rootPath) { | ||
| function checkRootPathForErrors (fastify, rootPath, skipExistenceCheck) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am think is it necessary to skip the check of All of your served file should be contained within to the
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @climba03003 Thank you for your feedback on the security implications. Before implementing changes, I would like to confirm the approach that we remove the
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can skip for cwd, but you must provide a valid root. |
||
| if (rootPath === undefined) { | ||
| throw new Error('"root" option is required') | ||
| } | ||
|
|
@@ -425,40 +430,46 @@ function checkRootPathForErrors (fastify, rootPath) { | |
| } | ||
|
|
||
| // check each path and fail at first invalid | ||
| rootPath.map((path) => checkPath(fastify, path)) | ||
| rootPath.map((path) => checkPath(fastify, path, skipExistenceCheck)) | ||
| return | ||
| } | ||
|
|
||
| if (typeof rootPath === 'string') { | ||
| return checkPath(fastify, rootPath) | ||
| return checkPath(fastify, rootPath, skipExistenceCheck) | ||
| } | ||
|
|
||
| throw new Error('"root" option must be a string or array of strings') | ||
| } | ||
|
|
||
| function checkPath (fastify, rootPath) { | ||
| function checkPath (fastify, rootPath, skipExistenceCheck) { | ||
| // skip all checks if rootPath is the CWD | ||
| if (rootPath === process.cwd()) { | ||
| return | ||
| } | ||
| if (typeof rootPath !== 'string') { | ||
| throw new Error('"root" option must be a string') | ||
| } | ||
| if (path.isAbsolute(rootPath) === false) { | ||
| throw new Error('"root" option must be an absolute path') | ||
| } | ||
|
|
||
| let pathStat | ||
| if (!skipExistenceCheck) { | ||
| let pathStat | ||
|
|
||
| try { | ||
| pathStat = statSync(rootPath) | ||
| } catch (e) { | ||
| if (e.code === 'ENOENT') { | ||
| fastify.log.warn(`"root" path "${rootPath}" must exist`) | ||
| return | ||
| } | ||
| try { | ||
| pathStat = statSync(rootPath) | ||
| } catch (e) { | ||
| if (e.code === 'ENOENT') { | ||
| fastify.log.warn(`"root" path "${rootPath}" must exist`) | ||
| return | ||
| } | ||
|
|
||
| throw e | ||
| } | ||
| throw e | ||
| } | ||
|
|
||
| if (pathStat.isDirectory() === false) { | ||
| throw new Error('"root" option must point to a directory') | ||
| if (pathStat.isDirectory() === false) { | ||
| throw new Error('"root" option must point to a directory') | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.