File tree Expand file tree Collapse file tree 2 files changed +24
-2
lines changed Expand file tree Collapse file tree 2 files changed +24
-2
lines changed Original file line number Diff line number Diff line change @@ -29,4 +29,18 @@ describe('sanitizeNext', () => {
2929 it ( 'should return an empty string for paths with colons' , ( ) => {
3030 expect ( sanitizeNext ( '/path:to/resource' ) ) . toBe ( '' ) ;
3131 } ) ;
32+
33+ it ( 'should return an empty string for the root path' , ( ) => {
34+ expect ( sanitizeNext ( '/' ) ) . toBe ( '' ) ;
35+ } ) ;
36+
37+ it ( 'should decode URL encoded characters' , ( ) => {
38+ expect ( sanitizeNext ( '/path%2Fwith%2Fslashes' ) ) . toBe ( '/path/with/slashes' ) ;
39+ } ) ;
40+
41+ it ( 'should return an empty string for a malformed URI component' , ( ) => {
42+ const input = '%E0%A4%A' ; // Malformed URI
43+ const expectedOutput = '' ;
44+ expect ( sanitizeNext ( input ) ) . toBe ( expectedOutput ) ;
45+ } ) ;
3246} ) ;
Original file line number Diff line number Diff line change 11export const sanitizeNext = ( next : string ) => {
2- let sanitizedNext = next ;
2+ // Safely decode URI component
3+ let sanitizedNext : string ;
4+ try {
5+ sanitizedNext = decodeURIComponent ( next ) ;
6+ } catch ( e ) {
7+ // Return empty string if decoding fails
8+ return '' ;
9+ }
10+
311 // Validate that next is an internal path
412 if (
513 sanitizedNext . startsWith ( '//' ) ||
@@ -21,5 +29,5 @@ export const sanitizeNext = (next: string) => {
2129 // Only allow alphanumeric, hyphens
2230 sanitizedNext = pathname . replace ( / [ ^ \w \- \/ ] / g, '' ) ;
2331
24- return sanitizedNext ;
32+ return sanitizedNext === '/' ? '' : sanitizedNext ;
2533} ;
You can’t perform that action at this time.
0 commit comments