Skip to content

Commit 8688f98

Browse files
fix(config): correctly validate cacheHandlers names (#95358)
## What? The current `cacheHandlers` validation uses `/[a-z-]/`, which only checks whether a key contains at least one lowercase letter or hyphen. As a result, invalid keys such as `abc123`, `abc_123`, `abc.def`, and `handler!` are accepted even though they contain characters outside the documented allowed set. ## Why? The validation error states that handler names must only contain `a-z` and `-`, but the current implementation does not enforce that constraint because the regex is not anchored. ## How? - Replace `/[a-z-]/` with `/^[a-z-]+$/` so the entire handler name is validated. - Add regression tests covering both valid and invalid handler names. ## Testing - Added regression tests in `packages/next/src/server/config.test.ts`. <!-- NEXT_JS_LLM_PR -->
1 parent 24262a8 commit 8688f98

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

‎packages/next/src/server/config.test.ts‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,4 +192,45 @@ describe('loadConfig', () => {
192192
expect(result.experimental.externalProxyRewritesResolve).toBe(true)
193193
})
194194
})
195+
196+
describe('cacheHandlers validation', () => {
197+
it('should reject invalid keys', async () => {
198+
const invalidKeys = [
199+
'abc123',
200+
'abc_123',
201+
'abc.def',
202+
'handler!',
203+
'123handler',
204+
'handler123',
205+
]
206+
207+
for (const key of invalidKeys) {
208+
await expect(
209+
loadConfig(PHASE_PRODUCTION_BUILD, __dirname, {
210+
customConfig: {
211+
cacheHandlers: {
212+
[key]: __filename,
213+
},
214+
},
215+
})
216+
).rejects.toThrow(/key must only use characters a-z and -/)
217+
}
218+
})
219+
220+
it('should accept valid keys', async () => {
221+
const result = await loadConfig(PHASE_PRODUCTION_BUILD, __dirname, {
222+
customConfig: {
223+
cacheHandlers: {
224+
abc: __filename,
225+
'valid-handler': __filename,
226+
'abc-def': __filename,
227+
},
228+
},
229+
})
230+
expect(result.cacheHandlers).toBeDefined()
231+
expect(result.cacheHandlers?.['abc']).toBeDefined()
232+
expect(result.cacheHandlers?.['valid-handler']).toBeDefined()
233+
expect(result.cacheHandlers?.['abc-def']).toBeDefined()
234+
})
235+
})
195236
})

‎packages/next/src/server/config.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1408,7 +1408,7 @@ function assignDefaultsAndValidate(
14081408
}
14091409

14101410
if (result.cacheHandlers) {
1411-
const allowedHandlerNameRegex = /[a-z-]/
1411+
const allowedHandlerNameRegex = /^[a-z-]+$/
14121412

14131413
if (typeof result.cacheHandlers !== 'object') {
14141414
throw new Error(

0 commit comments

Comments
 (0)