Commit 3c8b8668 authored by Cursor Agent's avatar Cursor Agent

feat: add environment variable support for allowedOrigins configs

- Add NEXT_ALLOWED_DEV_ORIGINS env var to inject allowedDevOrigins
- Add NEXT_SERVER_ACTIONS_ALLOWED_ORIGINS env var to inject experimental.serverActions.allowedOrigins
- Both env vars accept comma-separated lists of origins
- Values from env vars are merged with config values
- Add comprehensive unit tests for the new functionality
Co-authored-by: 's avatarJimmy Lai <hello@jimmyl.ai>
parent 4385ed36
...@@ -379,6 +379,39 @@ function assignDefaultsAndValidate( ...@@ -379,6 +379,39 @@ function assignDefaultsAndValidate(
}, },
} }
// Parse and merge environment variables for allowed origins
if (process.env.NEXT_ALLOWED_DEV_ORIGINS) {
const envOrigins = process.env.NEXT_ALLOWED_DEV_ORIGINS.split(',')
.map((origin) => origin.trim())
.filter(Boolean)
if (envOrigins.length > 0) {
result.allowedDevOrigins = [
...(result.allowedDevOrigins || []),
...envOrigins,
]
}
}
// Parse and merge environment variables for server actions allowed origins
if (process.env.NEXT_SERVER_ACTIONS_ALLOWED_ORIGINS) {
const envOrigins = process.env.NEXT_SERVER_ACTIONS_ALLOWED_ORIGINS.split(
','
)
.map((origin) => origin.trim())
.filter(Boolean)
if (envOrigins.length > 0) {
if (!result.experimental.serverActions) {
result.experimental.serverActions = {}
}
result.experimental.serverActions.allowedOrigins = [
...(result.experimental.serverActions.allowedOrigins || []),
...envOrigins,
]
}
}
// ensure correct default is set for api-resolver revalidate handling // ensure correct default is set for api-resolver revalidate handling
if (!result.experimental.trustHostHeader && ciEnvironment.hasNextSupport) { if (!result.experimental.trustHostHeader && ciEnvironment.hasNextSupport) {
result.experimental.trustHostHeader = true result.experimental.trustHostHeader = true
......
...@@ -223,4 +223,136 @@ describe('config', () => { ...@@ -223,4 +223,136 @@ describe('config', () => {
expect(config.turbopack.root).toBe(config.outputFileTracingRoot) expect(config.turbopack.root).toBe(config.outputFileTracingRoot)
}) })
}) })
describe('Environment variable support for allowed origins', () => {
it('Should merge NEXT_ALLOWED_DEV_ORIGINS with config allowedDevOrigins', async () => {
const originalEnv = process.env.NEXT_ALLOWED_DEV_ORIGINS
process.env.NEXT_ALLOWED_DEV_ORIGINS = 'example.com,*.test.com'
try {
const config = await loadConfig(PHASE_DEVELOPMENT_SERVER, '<rootDir>', {
customConfig: {
allowedDevOrigins: ['localhost'],
},
})
expect(config.allowedDevOrigins).toContain('localhost')
expect(config.allowedDevOrigins).toContain('example.com')
expect(config.allowedDevOrigins).toContain('*.test.com')
} finally {
if (originalEnv === undefined) {
delete process.env.NEXT_ALLOWED_DEV_ORIGINS
} else {
process.env.NEXT_ALLOWED_DEV_ORIGINS = originalEnv
}
}
})
it('Should parse NEXT_ALLOWED_DEV_ORIGINS when no config value exists', async () => {
const originalEnv = process.env.NEXT_ALLOWED_DEV_ORIGINS
process.env.NEXT_ALLOWED_DEV_ORIGINS = 'example.com,another.com'
try {
const config = await loadConfig(PHASE_DEVELOPMENT_SERVER, '<rootDir>', {
customConfig: {},
})
expect(config.allowedDevOrigins).toContain('example.com')
expect(config.allowedDevOrigins).toContain('another.com')
} finally {
if (originalEnv === undefined) {
delete process.env.NEXT_ALLOWED_DEV_ORIGINS
} else {
process.env.NEXT_ALLOWED_DEV_ORIGINS = originalEnv
}
}
})
it('Should trim whitespace from NEXT_ALLOWED_DEV_ORIGINS values', async () => {
const originalEnv = process.env.NEXT_ALLOWED_DEV_ORIGINS
process.env.NEXT_ALLOWED_DEV_ORIGINS = ' example.com , another.com '
try {
const config = await loadConfig(PHASE_DEVELOPMENT_SERVER, '<rootDir>', {
customConfig: {},
})
expect(config.allowedDevOrigins).toContain('example.com')
expect(config.allowedDevOrigins).toContain('another.com')
expect(config.allowedDevOrigins).not.toContain(' example.com ')
} finally {
if (originalEnv === undefined) {
delete process.env.NEXT_ALLOWED_DEV_ORIGINS
} else {
process.env.NEXT_ALLOWED_DEV_ORIGINS = originalEnv
}
}
})
it('Should merge NEXT_SERVER_ACTIONS_ALLOWED_ORIGINS with config', async () => {
const originalEnv = process.env.NEXT_SERVER_ACTIONS_ALLOWED_ORIGINS
process.env.NEXT_SERVER_ACTIONS_ALLOWED_ORIGINS = 'api.example.com'
try {
const config = await loadConfig(PHASE_DEVELOPMENT_SERVER, '<rootDir>', {
customConfig: {
experimental: {
serverActions: {
allowedOrigins: ['localhost'],
},
},
},
})
expect(config.experimental.serverActions?.allowedOrigins).toContain(
'localhost'
)
expect(config.experimental.serverActions?.allowedOrigins).toContain(
'api.example.com'
)
} finally {
if (originalEnv === undefined) {
delete process.env.NEXT_SERVER_ACTIONS_ALLOWED_ORIGINS
} else {
process.env.NEXT_SERVER_ACTIONS_ALLOWED_ORIGINS = originalEnv
}
}
})
it('Should create serverActions config from NEXT_SERVER_ACTIONS_ALLOWED_ORIGINS when not set', async () => {
const originalEnv = process.env.NEXT_SERVER_ACTIONS_ALLOWED_ORIGINS
process.env.NEXT_SERVER_ACTIONS_ALLOWED_ORIGINS = 'api.example.com'
try {
const config = await loadConfig(PHASE_DEVELOPMENT_SERVER, '<rootDir>', {
customConfig: {},
})
expect(config.experimental.serverActions?.allowedOrigins).toContain(
'api.example.com'
)
} finally {
if (originalEnv === undefined) {
delete process.env.NEXT_SERVER_ACTIONS_ALLOWED_ORIGINS
} else {
process.env.NEXT_SERVER_ACTIONS_ALLOWED_ORIGINS = originalEnv
}
}
})
it('Should handle empty NEXT_ALLOWED_DEV_ORIGINS', async () => {
const originalEnv = process.env.NEXT_ALLOWED_DEV_ORIGINS
process.env.NEXT_ALLOWED_DEV_ORIGINS = ''
try {
const config = await loadConfig(PHASE_DEVELOPMENT_SERVER, '<rootDir>', {
customConfig: {
allowedDevOrigins: ['localhost'],
},
})
expect(config.allowedDevOrigins).toEqual(['localhost'])
} finally {
if (originalEnv === undefined) {
delete process.env.NEXT_ALLOWED_DEV_ORIGINS
} else {
process.env.NEXT_ALLOWED_DEV_ORIGINS = originalEnv
}
}
})
})
}) })
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment