Skip to content
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: prevent loading env outside of root #6995

Merged
merged 3 commits into from Mar 3, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/playground/.env
@@ -0,0 +1 @@
VITE_PARENT_ENV=dont_load_me
1 change: 1 addition & 0 deletions packages/playground/env-nested/.env
@@ -0,0 +1 @@
VITE_PARENT_ENV=dont_load_me
15 changes: 15 additions & 0 deletions packages/playground/env-nested/__tests__/env-nested.spec.ts
@@ -0,0 +1,15 @@
import { isBuild } from 'testUtils'

const mode = isBuild ? `production` : `development`

test('mode', async () => {
expect(await page.textContent('.mode')).toBe(mode)
})

test('mode file override', async () => {
expect(await page.textContent('.mode-file')).toBe(`.env.${mode}`)
})

test('should not load parent .env file', async () => {
expect(await page.textContent('.parent-env')).not.toBe('dont_load_me')
})
1 change: 1 addition & 0 deletions packages/playground/env-nested/envs/.env.development
@@ -0,0 +1 @@
VITE_EFFECTIVE_MODE_FILE_NAME=.env.development
1 change: 1 addition & 0 deletions packages/playground/env-nested/envs/.env.production
@@ -0,0 +1 @@
VITE_EFFECTIVE_MODE_FILE_NAME=.env.production
16 changes: 16 additions & 0 deletions packages/playground/env-nested/index.html
@@ -0,0 +1,16 @@
<h1>Nested Environment Variables</h1>
<p>import.meta.env.MODE: <code class="mode"></code></p>
<p>
import.meta.env.VITE_EFFECTIVE_MODE_FILE_NAME: <code class="mode-file"></code>
</p>
<p>Empty import.meta.env.VITE_PARENT_ENV: <code class="parent-env"></code></p>

<script type="module">
text('.mode', import.meta.env.MODE)
text('.mode-file', import.meta.env.VITE_EFFECTIVE_MODE_FILE_NAME)
text('.parent-env', import.meta.env.VITE_PARENT_ENV)

function text(el, text) {
document.querySelector(el).textContent = text
}
</script>
11 changes: 11 additions & 0 deletions packages/playground/env-nested/package.json
@@ -0,0 +1,11 @@
{
"name": "test-env-nested",
"private": true,
"version": "0.0.0",
"scripts": {
"dev": "vite",
"build": "vite build",
"debug": "node --inspect-brk ../../vite/bin/vite",
"preview": "vite preview"
}
}
5 changes: 5 additions & 0 deletions packages/playground/env-nested/vite.config.js
@@ -0,0 +1,5 @@
const { defineConfig } = require('vite')

module.exports = defineConfig({
envDir: './envs'
})
16 changes: 8 additions & 8 deletions packages/vite/src/node/config.ts
Expand Up @@ -385,7 +385,7 @@ export async function resolveConfig(
: resolvedRoot
const userEnv =
inlineConfig.envFile !== false &&
loadEnv(mode, envDir, resolveEnvPrefix(config))
loadEnv(mode, envDir, resolveEnvPrefix(config), false)

// Note it is possible for user to have a custom mode, e.g. `staging` where
// production-like behavior is expected. This is indicated by NODE_ENV=production
Expand All @@ -401,11 +401,7 @@ export async function resolveConfig(
const resolvedBuildOptions = resolveBuildOptions(config.build)

// resolve cache directory
const pkgPath = lookupFile(
resolvedRoot,
[`package.json`],
true /* pathOnly */
)
const pkgPath = lookupFile(resolvedRoot, [`package.json`], { pathOnly: true })
const cacheDir = config.cacheDir
? path.resolve(resolvedRoot, config.cacheDir)
: pkgPath
Expand Down Expand Up @@ -1054,7 +1050,8 @@ async function loadConfigFromBundledFile(
export function loadEnv(
mode: string,
envDir: string,
prefixes: string | string[] = 'VITE_'
prefixes: string | string[] = 'VITE_',
searchAboveEnvDir = true
): Record<string, string> {
if (mode === 'local') {
throw new Error(
Expand Down Expand Up @@ -1083,7 +1080,10 @@ export function loadEnv(
}

for (const file of envFiles) {
const path = lookupFile(envDir, [file], true)
const path = lookupFile(envDir, [file], {
pathOnly: true,
rootDir: searchAboveEnvDir ? undefined : envDir
})
if (path) {
const parsed = dotenv.parse(fs.readFileSync(path), {
debug: process.env.DEBUG?.includes('vite:dotenv') || undefined
Expand Down
16 changes: 12 additions & 4 deletions packages/vite/src/node/utils.ts
Expand Up @@ -292,20 +292,28 @@ export function isDefined<T>(value: T | undefined | null): value is T {
return value != null
}

interface LookupFileOptions {
pathOnly?: boolean
rootDir?: string
}

export function lookupFile(
dir: string,
formats: string[],
pathOnly = false
options?: LookupFileOptions
): string | undefined {
for (const format of formats) {
const fullPath = path.join(dir, format)
if (fs.existsSync(fullPath) && fs.statSync(fullPath).isFile()) {
return pathOnly ? fullPath : fs.readFileSync(fullPath, 'utf-8')
return options?.pathOnly ? fullPath : fs.readFileSync(fullPath, 'utf-8')
}
}
const parentDir = path.dirname(dir)
if (parentDir !== dir) {
return lookupFile(parentDir, formats, pathOnly)
if (
parentDir !== dir &&
(!options?.rootDir || parentDir.startsWith(options?.rootDir))
) {
return lookupFile(parentDir, formats, options)
}
}

Expand Down