Skip to content

Commit

Permalink
fix: prevent loading env outside of root (#6995)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Mar 3, 2022
1 parent 7215a03 commit e0a4d81
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 10 deletions.
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'
})
8 changes: 2 additions & 6 deletions packages/vite/src/node/config.ts
Expand Up @@ -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 @@ -1083,7 +1079,7 @@ export function loadEnv(
}

for (const file of envFiles) {
const path = lookupFile(envDir, [file], true)
const path = lookupFile(envDir, [file], { pathOnly: true, rootDir: 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 @@ -298,20 +298,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

0 comments on commit e0a4d81

Please sign in to comment.