Skip to content

Commit

Permalink
fix: Encode invisible ASCII control characters (#783)
Browse files Browse the repository at this point in the history
  • Loading branch information
franky47 authored Nov 23, 2024
1 parent 56e15a0 commit c768757
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
6 changes: 6 additions & 0 deletions packages/nuqs/src/url-encoding.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ describe('url-encoding/encodeQueryValue', () => {
expect(encodeQueryValue('<')).toEqual(encodeURIComponent('<'))
expect(encodeQueryValue('>')).toEqual(encodeURIComponent('>'))
})
test('hidden ASCII characters are encoded', () => {
const chars = Array.from({ length: 32 }, (_, i) => String.fromCharCode(i))
chars.forEach(char => {
expect(encodeQueryValue(char)).toBe(encodeURIComponent(char))
})
})
test('Alphanumericals are passed through', () => {
const input = 'abcdefghijklmnopqrstuvwxyz0123456789'
expect(encodeQueryValue(input)).toBe(input)
Expand Down
2 changes: 2 additions & 0 deletions packages/nuqs/src/url-encoding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ export function encodeQueryValue(input: string) {
.replace(/`/g, '%60')
.replace(/</g, '%3C')
.replace(/>/g, '%3E')
// Encode invisible ASCII control characters
.replace(/[\x00-\x1F]/g, char => encodeURIComponent(char))
)
}

Expand Down

0 comments on commit c768757

Please sign in to comment.