-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
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
When referencing an environment variable that doesnt exist, vite dumps your whole environment into the bundle as an object #17710
Comments
Oh, lol, related: #17701 |
@innocenzi seems we both were frustrated with the same issue yesterday haha |
I ran into this as well, but more so from the side of being surprised that if Thinking about this, there's a combination of possible things that can be done in my eyes to improve the current behavior:
As to the issue description: I don't think it dumps the "whole environment" into the build, but rather just it's own values + any value with correct prefix. Building var e={VITE_FOO:"foo",BASE_URL:"/",MODE:"production",DEV:!1,PROD:!0,SSR:!1};console.info(e.VITE_BAR); Thus the potential security issue is a bit more limited/subtle here. |
FWIW The security risk is because I have a plugin I made that just places the whole contents of Basically |
#17648 should fix this, but still, you shouldn't put any sensitive information in |
I should be able to put anything I want in If I never use And |
Vite assumes anything in |
(just a FYI) Even if the above issue is completely remedied, if you expose too much data to |
Dumping an object of environment variables whether you explicitly defined them to be ABLE to be referenced or not is the security issue, full stop. Enough with the virtue signaling.
This applies to Vite approved env vars too. Many I'd really like if people just acknowledge that there's bugs and those bugs can have unintended side effects that could be a security issue and go "yeah we'll work on resolving that" instead of "ya'll big dum dums are not allowed to use env vars we don't approve of!" Every single one of these arguments can be applied to "the sanctioned way" and still be correct. Only difference is my report is a legitimate bug and security risk, yours are just placing blame on the victims. |
(Preface: I'm not here to deny the behavior is buggy/confusing in any way; just trying to clarify the behavior and the potential impact. As I mentioned above, I also think the behavior is potentially confusing and/or misleading and things could be done to improve it, it's just that any solution here will still have drawbacks and still requires users to be careful.) I think there might be some confusing regarding the "vite dumps your whole environment" part here, because it does not. By default, Vite will only look at environment variables prefixed with By default, Like I suggested above, altering the current behavior to not (only) expose variables with the See also the docs at https://vitejs.dev/guide/env-and-mode.html#env-files:
|
It's okay, I'm an adult. I know how to handle my environment variables. You don't need to tell me what I can and cannot use in my apps. Lets skip the bike shedding about the contents of the variables themselves, and focus on the fact that this is a bug and if an undefined variable is referenced it should be replaced with |
You act as if “secret env var” equates to keys to services and other sensitive stuff and not something that is ACTUALLY needed in the app, such as I don’t know… “secret pass code to unlock hidden chest in level 3 of game that needs to be configurable” and if a 3rd party library somehow manages to sneak a |
I'm very sorry but I just still fail to see how this is a bug nor how it is a security issue. I'm glad that #17648 will land and change the behavior, but I don't think the current behavior is unreasonable in any way, just possibly unexpected. Yet as stressed above (and just to repeat for anyone reading along), anything on Edit: just to clarify, I don't intend to keep this discussion going any further, as I think it's already gone too far off-topic for the scope of this issue. |
k, I've wasted enough time arguing about this. Clearly I'm not the only one who sees a problem with this. But I ain't a maintainer so do whatever ya'll want. Closing this. |
@jnoordsij there's a bit of context in this PR as to why this is a DX issue: #17701 And I want to answer to this:
Maybe it's by design, but that doesn't mean it's good. That design, if that was actually intended, was such a footgun that we had to have that I just think that code like |
@innocenzi I do agree that the current behavior can be confusing at points; I've created #17765 as follow-up to repeat the suggestions I listed above to improve it. Feel free to put forward any further suggestions you have on the topic there! Note there already are ways to expose non-prefixed environment variables to the client code (see https://vitejs.dev/config/shared-options#envprefix), but indeed there may be possible improvements on achieving such a thing more easily. |
I Found one simple solution to use this with the help of core javascript concept. To access env. variable you need to define with VITE_ prefix (for ex: VITE_APP_VERSION ) For accessing images in the assets folder, the traditional require() method is not supported in Vite. Instead, you should use the import.meta.url with the URL class. However, a more efficient approach is to create a utility file for managing images. This allows you to import images directly and use them throughout your project. import imageName from '@/assets/your_image_name'; and then use in any file like this import { ImageUtil } from './ImageUtil'; Hope it is helpful |
Describe the bug
When you reference an env var, the idea is that Vite will replace the reference with the value of that env var. So given the following
.env
And the following code
The resulting output should be
And this is true and works as expected. But now reference an env var that doesn't exist, you'd expect the replacement to become
undefined
, like so:What actually happens is the whole "env" object is dumped directly into the bundle, then references the var from that, which because its not defined, in the end gives you
undefined
I believe this is wrong and can lead to bad security leaks and instead it should output the following
Reproduction
https://github.com/joshmanders/vite-env-bug
Steps to reproduce
Run
npm install
Run
npm run build
Inspect output in
dist/assets/
System Info
System: OS: macOS 14.5 CPU: (10) arm64 Apple M1 Max Memory: 1.38 GB / 64.00 GB Shell: 3.7.1 - /opt/homebrew/bin/fish Binaries: Node: 20.15.0 - /opt/homebrew/bin/node npm: 10.7.0 - /opt/homebrew/bin/npm Watchman: 2024.06.24.00 - /opt/homebrew/bin/watchman Browsers: Brave Browser: 126.1.67.123 Chrome: 126.0.6478.127 Edge: 126.0.2592.102 Safari: 17.5 npmPackages: vite: ^5.3.4 => 5.3.4
Used Package Manager
npm
Logs
Click to expand!
Validations
The text was updated successfully, but these errors were encountered: