Skip to content

Commit f387eb7

Browse files
committed
fix: Encode invisible ASCII control characters
1 parent 56e15a0 commit f387eb7

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

packages/nuqs/src/url-encoding.test.ts

+6
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ describe('url-encoding/encodeQueryValue', () => {
2424
expect(encodeQueryValue('<')).toEqual(encodeURIComponent('<'))
2525
expect(encodeQueryValue('>')).toEqual(encodeURIComponent('>'))
2626
})
27+
test('hidden ASCII characters are encoded', () => {
28+
const chars = Array.from({ length: 32 }, (_, i) => String.fromCharCode(i))
29+
chars.forEach(char => {
30+
expect(encodeQueryValue(char)).toBe(encodeURIComponent(char))
31+
})
32+
})
2733
test('Alphanumericals are passed through', () => {
2834
const input = 'abcdefghijklmnopqrstuvwxyz0123456789'
2935
expect(encodeQueryValue(input)).toBe(input)

packages/nuqs/src/url-encoding.ts

+2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ export function encodeQueryValue(input: string) {
4242
.replace(/`/g, '%60')
4343
.replace(/</g, '%3C')
4444
.replace(/>/g, '%3E')
45+
// Encode invisible ASCII control characters
46+
.replace(/[\x00-\x1F]/g, char => encodeURIComponent(char))
4547
)
4648
}
4749

0 commit comments

Comments
 (0)