Skip to content

Commit b581b8f

Browse files
committed
decode input
1 parent c63d7b8 commit b581b8f

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

app/platform-redirect/utils.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff 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
});

app/platform-redirect/utils.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
export 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
};

0 commit comments

Comments
 (0)