From dd644945eaa5567e1684516c1dbbfc4ce08e4d5b Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Tue, 30 Jun 2020 16:33:37 -0500 Subject: [PATCH] Fix encoding for env files in serverless mode (#14750) This makes sure to base64 encode the `.env*` file contents before passing them in the URL for the serverless-loader since `!` is a special character in this case which can cause webpack to fail to build Closes: https://github.com/vercel/next.js/issues/14749 --- packages/next/build/entries.ts | 5 ++++- .../next/build/webpack/loaders/next-serverless-loader.ts | 2 +- test/integration/env-config/app/.env | 5 +++-- test/integration/env-config/app/pages/api/all.js | 1 + test/integration/env-config/app/pages/index.js | 1 + test/integration/env-config/app/pages/some-ssg.js | 1 + test/integration/env-config/app/pages/some-ssp.js | 1 + test/integration/env-config/test/index.test.js | 1 + 8 files changed, 13 insertions(+), 4 deletions(-) diff --git a/packages/next/build/entries.ts b/packages/next/build/entries.ts index dc87eb2d9a..4d6c942751 100644 --- a/packages/next/build/entries.ts +++ b/packages/next/build/entries.ts @@ -92,7 +92,10 @@ export function createEntrypoints( }) : '', previewProps: JSON.stringify(previewMode), - loadedEnvFiles: JSON.stringify(loadedEnvFiles), + // base64 encode to make sure contents don't break webpack URL loading + loadedEnvFiles: Buffer.from(JSON.stringify(loadedEnvFiles)).toString( + 'base64' + ), } Object.keys(pages).forEach((page) => { diff --git a/packages/next/build/webpack/loaders/next-serverless-loader.ts b/packages/next/build/webpack/loaders/next-serverless-loader.ts index b788256419..3cc4d959dc 100644 --- a/packages/next/build/webpack/loaders/next-serverless-loader.ts +++ b/packages/next/build/webpack/loaders/next-serverless-loader.ts @@ -90,7 +90,7 @@ const nextServerlessLoader: loader.Loader = function () { : '' const envLoading = ` const { processEnv } = require('next/dist/lib/load-env-config') - processEnv(${loadedEnvFiles}) + processEnv(${Buffer.from(loadedEnvFiles, 'base64').toString()}) ` const runtimeConfigImports = runtimeConfig diff --git a/test/integration/env-config/app/.env b/test/integration/env-config/app/.env index ea9c02f8cc..d50cde6183 100644 --- a/test/integration/env-config/app/.env +++ b/test/integration/env-config/app/.env @@ -1,4 +1,4 @@ -PROCESS_ENV_KEY=env +PROCESS_ENV_KEY="env" ENV_FILE_KEY=env ENV_FILE_LOCAL_OVERRIDE_TEST=env ENV_FILE_DEVELOPMENT_OVERRIDE_TEST=env @@ -10,4 +10,5 @@ ENV_FILE_TEST_LOCAL_OVERRIDEOVERRIDE_TEST=env NEXT_PUBLIC_TEST_DEST=another ENV_FILE_EXPANDED=$ENV_FILE_KEY ENV_FILE_EXPANDED_CONCAT=hello-${ENV_FILE_KEY} -ENV_FILE_EXPANDED_ESCAPED=\$ENV_FILE_KEY \ No newline at end of file +ENV_FILE_EXPANDED_ESCAPED=\$ENV_FILE_KEY +ENV_FILE_KEY_EXCLAMATION="hello!" \ No newline at end of file diff --git a/test/integration/env-config/app/pages/api/all.js b/test/integration/env-config/app/pages/api/all.js index 61d66efb0e..71c4968c8c 100644 --- a/test/integration/env-config/app/pages/api/all.js +++ b/test/integration/env-config/app/pages/api/all.js @@ -18,6 +18,7 @@ const variables = [ 'ENV_FILE_EXPANDED', 'ENV_FILE_EXPANDED_CONCAT', 'ENV_FILE_EXPANDED_ESCAPED', + 'ENV_FILE_KEY_EXCLAMATION', ] const items = {} diff --git a/test/integration/env-config/app/pages/index.js b/test/integration/env-config/app/pages/index.js index c3e6932677..da92cea371 100644 --- a/test/integration/env-config/app/pages/index.js +++ b/test/integration/env-config/app/pages/index.js @@ -18,6 +18,7 @@ const variables = [ 'ENV_FILE_EXPANDED', 'ENV_FILE_EXPANDED_CONCAT', 'ENV_FILE_EXPANDED_ESCAPED', + 'ENV_FILE_KEY_EXCLAMATION', ] export async function getStaticProps() { diff --git a/test/integration/env-config/app/pages/some-ssg.js b/test/integration/env-config/app/pages/some-ssg.js index c3e6932677..da92cea371 100644 --- a/test/integration/env-config/app/pages/some-ssg.js +++ b/test/integration/env-config/app/pages/some-ssg.js @@ -18,6 +18,7 @@ const variables = [ 'ENV_FILE_EXPANDED', 'ENV_FILE_EXPANDED_CONCAT', 'ENV_FILE_EXPANDED_ESCAPED', + 'ENV_FILE_KEY_EXCLAMATION', ] export async function getStaticProps() { diff --git a/test/integration/env-config/app/pages/some-ssp.js b/test/integration/env-config/app/pages/some-ssp.js index 10cfebf896..62db07bb42 100644 --- a/test/integration/env-config/app/pages/some-ssp.js +++ b/test/integration/env-config/app/pages/some-ssp.js @@ -18,6 +18,7 @@ const variables = [ 'ENV_FILE_EXPANDED', 'ENV_FILE_EXPANDED_CONCAT', 'ENV_FILE_EXPANDED_ESCAPED', + 'ENV_FILE_KEY_EXCLAMATION', ] export async function getServerSideProps() { diff --git a/test/integration/env-config/test/index.test.js b/test/integration/env-config/test/index.test.js index 2ae15211a8..3c3d7222fc 100644 --- a/test/integration/env-config/test/index.test.js +++ b/test/integration/env-config/test/index.test.js @@ -50,6 +50,7 @@ const runTests = (mode = 'dev') => { expect(data.ENV_FILE_EXPANDED).toBe('env') expect(data.ENV_FILE_EXPANDED_CONCAT).toBe('hello-env') expect(data.ENV_FILE_EXPANDED_ESCAPED).toBe('$ENV_FILE_KEY') + expect(data.ENV_FILE_KEY_EXCLAMATION).toBe('hello!') } it('should have process environment override .env', async () => { -- GitLab