Skip to content

Commit d63f265

Browse files
committed
feat: Do not serialize NaN, Infinity, or -Infinity
BREAKING CHANGE: Any attempt to serialize NaN, Infinity, or -Infinity will now throw an UnserializableParamError.
1 parent 8a6e719 commit d63f265

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ which encodes most non-alphanumeric characters.
6262
is not supported and will throw an `UnserializableParamError`.
6363
- Serialization of functions or other objects is
6464
is not supported and will throw an `UnserializableParamError`.
65+
- Serialization of `NaN`, `Infinity`, and `-Infinity`
66+
is not supported and will throw an `UnserializableParamError`.
6567

6668
### Compatible parsing strategy
6769

src/lib/serialize.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,19 @@ const serialize = (k: string, v: unknown): string => {
8383
}
8484
return v.toString()
8585
}
86-
if (typeof v === 'number') return v.toString()
86+
if (typeof v === 'number') {
87+
if (
88+
isNaN(v) ||
89+
v === Infinity ||
90+
v === -Infinity ||
91+
v.toString() === 'NaN' ||
92+
v.toString() === 'Infinity' ||
93+
v.toString() === '-Infinity'
94+
) {
95+
throw new UnserializableParamError(k, `is ${v}`)
96+
}
97+
return v.toString()
98+
}
8799
if (typeof v === 'bigint') return v.toString()
88100
if (typeof v === 'boolean') return v.toString()
89101
if (isDateLike(v)) return v.toISOString()

test/serialization.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,18 @@ test('cannot serialize functions', (t) => {
125125
})
126126
})
127127

128+
test('cannot serialize number pointers', (t) => {
129+
t.throws(() => serializeUrlSearchParams({ foo: Infinity }), {
130+
instanceOf: UnserializableParamError,
131+
})
132+
t.throws(() => serializeUrlSearchParams({ foo: -Infinity }), {
133+
instanceOf: UnserializableParamError,
134+
})
135+
t.throws(() => serializeUrlSearchParams({ foo: -NaN }), {
136+
instanceOf: UnserializableParamError,
137+
})
138+
})
139+
128140
test('cannot serialize non-plain objects', (t) => {
129141
class Foo {
130142
bar: string

0 commit comments

Comments
 (0)