-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
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
fix(importAnalysis): strip url base before passing as safeModulePaths #13712
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tried it adding a /base/
to the fs-serve
playground and it is working as expected. We could expand the tests, but I prefer to keep the current fs-serve using the default base.
}, | ||
} | ||
|
||
const rewriteTestRootOptions = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You cannot directly use the configuration from vite.config-base.js here. In vite.config-base.js, the variable __dirname is used, which returns a value based on "playground" instead of "playground-temp" that is used for testing. I guess this may be a bug. That's why I am using "serve" to start the testing server instead of relying on vite.config-base.js.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The config file read in __tests__
from vite.config.js
is different from the one read in playground-temp
, especially when it involves expressions like __dirname
.
like:
{
build: {
rollupOptions: {
input: {
main: path.resolve(__dirname, 'src/index.html'),
},
},
},
server: {
fs: {
strict: true,
allow: [path.resolve(__dirname, 'src')],
},
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is strange, we are using __dirname
in the worker and assets playgrounds too in the configs. Maybe there is an issue here because the root
is set?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You were correct, I sent a PR to fix the config variants handling here:
This comment was marked as off-topic.
This comment was marked as off-topic.
@@ -556,8 +556,10 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin { | |||
} | |||
} | |||
|
|||
// record as safe modules | |||
server?.moduleGraph.safeModulesPath.add(fsPathFromUrl(url)) | |||
// record as safe modules # stripBase: https://github.com/vitejs/vite/issues/9438#issuecomment-1486662486 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it we're going to link to a comment it should probably be #9438 (comment), but it'd be nicer to just explain why it's necessary here rather than making someone copy paste a URL into their browser to read the explanation (same below)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried to fix it. Thanks for guiding me on this PR.
// When the base path is set, the safeModulesPath does not include the base prefix internally. | ||
// Therefore, when retrieving a file from safeModulesPath, the base path should be stripped. | ||
// See https://github.com/vitejs/vite/issues/9438#issuecomment-1465270409 | ||
const file = fsPathFromUrl(stripBase(url, server.config.rawBase)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function is called from serveRawFsMiddleware
/serveStaticMiddleware
/transformMiddleware
that runs after baseMiddleware
(this middleware strips the base). So I think we shouldn't run stripBase
here.
vite/packages/vite/src/node/server/index.ts
Lines 614 to 646 in 1292ad0
// base | |
if (config.base !== '/') { | |
middlewares.use(baseMiddleware(server)) | |
} | |
// open in editor support | |
middlewares.use('/__open-in-editor', launchEditorMiddleware()) | |
// ping request handler | |
// Keep the named function. The name is visible in debug logs via `DEBUG=connect:dispatcher ...` | |
middlewares.use(function viteHMRPingMiddleware(req, res, next) { | |
if (req.headers['accept'] === 'text/x-vite-ping') { | |
res.writeHead(204).end() | |
} else { | |
next() | |
} | |
}) | |
// serve static files under /public | |
// this applies before the transform middleware so that these files are served | |
// as-is without transforms. | |
if (config.publicDir) { | |
middlewares.use( | |
servePublicMiddleware(config.publicDir, config.server.headers), | |
) | |
} | |
// main transform middleware | |
middlewares.use(transformMiddleware(server)) | |
// serve static files | |
middlewares.use(serveRawFsMiddleware(server)) | |
middlewares.use(serveStaticMiddleware(root, server)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So that's how it is. I remove stripBase here
// inside allowed dir, safe fetch | ||
fetch('/src/safe.txt') | ||
fetch(base + '/src/safe.txt') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if we've got /base/
then we'll end up with a //
. This should probably use joinUrlSegments
or path.posix.join
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i use joinUrlSegments
to fix that
Co-authored-by: Ben McCann <[email protected]>
The config will be loaded automatically as vitejs#13725 is merged
IIUC, this issue can't be exploited, no? In that case, let's merge it for Vite 5 (we'll start the beta soon). @sapphi-red let me know if you think it is important to get this one in a patch though. |
I'm not sure if we need to hold off this change. I think it'd be nice to fix as patch though. |
I think this issue can't be exploited. But I think it'd be good to merge this in a patch. |
update call stripBase in utils.ts before passing to fsPathFromUrl (fix #9438)
Description
Additional context
What is the purpose of this pull request?
Before submitting the PR, please make sure you do the following
fixes #123
).