mirror of
https://github.com/LukeHagar/vercel.git
synced 2025-12-10 12:57:47 +00:00
This PR fixes a bug that was causing too many env vars to be exposed to the frontend by adding the framework `envPrefix`. Some frameworks, such as CRA, will include all of these in the frontend even if not explicitly used so we must only prefix known [System Environment Variables](https://vercel.com/docs/concepts/projects/environment-variables/system-environment-variables). This PR adds an allowlist to ensure this is handle properly. - Fixes https://linear.app/vercel/issue/VCCLI-639
91 lines
2.3 KiB
TypeScript
Vendored
91 lines
2.3 KiB
TypeScript
Vendored
import { getPrefixedEnvVars } from '../src';
|
|
|
|
describe('Test `getPrefixedEnvVars()`', () => {
|
|
const cases: Array<{
|
|
name: string;
|
|
args: Parameters<typeof getPrefixedEnvVars>[0];
|
|
want: ReturnType<typeof getPrefixedEnvVars>;
|
|
}> = [
|
|
{
|
|
name: 'should work with NEXT_PUBLIC_',
|
|
args: {
|
|
envPrefix: 'NEXT_PUBLIC_',
|
|
envs: {
|
|
VERCEL: '1',
|
|
VERCEL_URL: 'example.vercel.sh',
|
|
USER_ENV_VAR_NOT_VERCEL: 'example.com',
|
|
VERCEL_ARTIFACTS_TOKEN: 'abc123',
|
|
FOO: 'bar',
|
|
},
|
|
},
|
|
want: {
|
|
NEXT_PUBLIC_VERCEL_URL: 'example.vercel.sh',
|
|
TURBO_CI_VENDOR_ENV_KEY: 'NEXT_PUBLIC_VERCEL_',
|
|
},
|
|
},
|
|
{
|
|
name: 'should work with GATSBY_',
|
|
args: {
|
|
envPrefix: 'GATSBY_',
|
|
envs: {
|
|
USER_ENV_VAR_NOT_VERCEL: 'example.com',
|
|
VERCEL_ARTIFACTS_TOKEN: 'abc123',
|
|
FOO: 'bar',
|
|
VERCEL_URL: 'example.vercel.sh',
|
|
VERCEL_ENV: 'production',
|
|
VERCEL_REGION: 'iad1',
|
|
VERCEL_GIT_COMMIT_AUTHOR_LOGIN: 'rauchg',
|
|
},
|
|
},
|
|
want: {
|
|
GATSBY_VERCEL_URL: 'example.vercel.sh',
|
|
GATSBY_VERCEL_ENV: 'production',
|
|
GATSBY_VERCEL_REGION: 'iad1',
|
|
GATSBY_VERCEL_GIT_COMMIT_AUTHOR_LOGIN: 'rauchg',
|
|
TURBO_CI_VENDOR_ENV_KEY: 'GATSBY_VERCEL_',
|
|
},
|
|
},
|
|
{
|
|
name: 'should not return anything if no system env vars detected',
|
|
args: {
|
|
envPrefix: 'GATSBY_',
|
|
envs: {
|
|
USER_ENV_VAR_NOT_VERCEL: 'example.com',
|
|
FOO: 'bar',
|
|
BLARG_VERCEL_THING: 'fake',
|
|
VERCEL_ARTIFACTS_TOKEN: 'abc123',
|
|
},
|
|
},
|
|
want: {},
|
|
},
|
|
{
|
|
name: 'should not return anything if envPrefix is empty string',
|
|
args: {
|
|
envPrefix: '',
|
|
envs: {
|
|
VERCEL: '1',
|
|
VERCEL_URL: 'example.vercel.sh',
|
|
},
|
|
},
|
|
want: {},
|
|
},
|
|
{
|
|
name: 'should not return anything if envPrefix is undefined',
|
|
args: {
|
|
envPrefix: undefined,
|
|
envs: {
|
|
VERCEL: '1',
|
|
VERCEL_URL: 'example.vercel.sh',
|
|
},
|
|
},
|
|
want: {},
|
|
},
|
|
];
|
|
|
|
for (const { name, args, want } of cases) {
|
|
it(name, () => {
|
|
expect(getPrefixedEnvVars(args)).toEqual(want);
|
|
});
|
|
}
|
|
});
|