forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
middlewares: limit
process.env
to inferred usage (vercel#33186)
Production middlewares will only expose env vars that are statically analyzable, as mentioned here: https://nextjs.org/docs/api-reference/next/server#how-do-i-access-environment-variables This creates some incompatibility with `next dev` and `next start`, where all `process.env` data is shared and can lead to unexpected behavior in runtime. This PR fixes it by limiting the data in `process.env` with the inferred env vars from the code usage. I believe the test speaks for itself 🕺 <!-- ## Bug - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Errors have helpful link attached, see `contributing.md` ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have helpful link attached, see `contributing.md` ## Documentation / Examples - [ ] Make sure the linting passes by running `yarn lint` -->
- Loading branch information
Showing
6 changed files
with
77 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
...middleware-environment-variables-in-node-server-reflect-the-usage-inference/index.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { createNext } from 'e2e-utils' | ||
import { NextInstance } from 'test/lib/next-modes/base' | ||
import { renderViaHTTP } from 'next-test-utils' | ||
|
||
describe('middleware environment variables in node server reflect the usage inference', () => { | ||
let next: NextInstance | ||
|
||
beforeAll(() => { | ||
process.env.CAN_BE_INFERRED = 'can-be-inferred' | ||
process.env.X_CUSTOM_HEADER = 'x-custom-header' | ||
process.env.IGNORED_ENV_VAR = 'ignored-env-var' | ||
}) | ||
|
||
beforeAll(async () => { | ||
next = await createNext({ | ||
files: { | ||
'pages/_middleware.js': ` | ||
export default function middleware() { | ||
return new Response(JSON.stringify({ | ||
canBeInferred: process.env.CAN_BE_INFERRED, | ||
rest: process.env | ||
}), { | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'X-Custom-Header': process.env.X_CUSTOM_HEADER, | ||
} | ||
}) | ||
} | ||
`, | ||
}, | ||
dependencies: {}, | ||
}) | ||
}) | ||
afterAll(() => next.destroy()) | ||
|
||
it('limits process.env to only contain env vars that are inferred from usage', async () => { | ||
const html = await renderViaHTTP(next.url, '/test') | ||
let parsed: any | ||
expect(() => { | ||
parsed = JSON.parse(html) | ||
}).not.toThrow() | ||
expect(parsed).toEqual({ | ||
canBeInferred: 'can-be-inferred', | ||
rest: { | ||
CAN_BE_INFERRED: 'can-be-inferred', | ||
X_CUSTOM_HEADER: 'x-custom-header', | ||
}, | ||
}) | ||
}) | ||
}) |