Skip to content

Conversation

xinxinhe1810
Copy link
Contributor

update call stripBase in utils.ts before passing to fsPathFromUrl (fix #9438)

Description

Additional context


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines and follow the PR Title Convention.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

patak-dev
patak-dev previously approved these changes Jul 4, 2023
Copy link
Member

@patak-dev patak-dev left a 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.

@patak-dev patak-dev added the p4-important Violate documented behavior or significantly improves performance (priority) label Jul 4, 2023
},
}

const rewriteTestRootOptions = {
Copy link
Contributor Author

@xinxinhe1810 xinxinhe1810 Jul 5, 2023

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image image

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')],
    },
}

Copy link
Member

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?

Copy link
Member

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:

@xinxinhe1810

This comment was marked as off-topic.

@patak-dev
Copy link
Member

CI tests always fail. What is the reason? I haven't encountered this failure locally. image

These are flaky tests. We're having issues with them since last week, it is unrelated to this PR.

@@ -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
Copy link
Collaborator

@benmccann benmccann Jul 7, 2023

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)

Copy link
Contributor Author

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))
Copy link
Member

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.

// 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))

Copy link
Contributor Author

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')
Copy link
Collaborator

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

Copy link
Contributor Author

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

@patak-dev
Copy link
Member

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.

@patak-dev patak-dev added this to the 5.0 milestone Jul 29, 2023
@bluwy bluwy changed the title fix(node): update call stripBase in utils.ts before passing to fsPath… fix(importAnalysis): strip url base before passing as safeModulePaths Jul 29, 2023
@bluwy
Copy link
Member

bluwy commented Jul 29, 2023

I'm not sure if we need to hold off this change. I think it'd be nice to fix as patch though.

@sapphi-red
Copy link
Member

I think this issue can't be exploited. But I think it'd be good to merge this in a patch.

@patak-dev patak-dev merged commit 1ab06a8 into vitejs:main Jul 29, 2023
@patak-dev patak-dev removed this from the 5.0 milestone Jul 29, 2023
@xinxinhe1810 xinxinhe1810 deleted the fix/base-module-path branch July 30, 2023 07:04
sapphi-red added a commit that referenced this pull request Sep 17, 2024
patak-dev pushed a commit that referenced this pull request Sep 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p4-important Violate documented behavior or significantly improves performance (priority)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

403 when loading a file from an external package if base is set
5 participants