Skip to content

fix: wrap malformed query scalar values in SerializationException#137

Open
OmarAlJarrah wants to merge 1 commit into
mainfrom
c1-scalar-decode-exceptions
Open

fix: wrap malformed query scalar values in SerializationException#137
OmarAlJarrah wants to merge 1 commit into
mainfrom
c1-scalar-decode-exceptions

Conversation

@OmarAlJarrah

Copy link
Copy Markdown
Member

Summary

  • QueryDecoder's scalar decoders (decodeInt/decodeLong/decodeShort/decodeByte/decodeDouble/decodeFloat/decodeChar) converted the raw query string directly with toInt()/toLong()/single() etc., so a malformed value let the raw JVM/stdlib exception (NumberFormatException, NoSuchElementException, IllegalArgumentException) escape decodeFromQueryParameters instead of the SerializationException the format's enum-decoding path and the Url/Uri serializers already guarantee.
  • A caller wrapping decode in try { ... } catch (e: SerializationException) — the documented pattern for validating untrusted query input — would not catch these.
  • Switched each conversion to the *OrNull stdlib variant and fail with a descriptive SerializationException when it returns null, mirroring how the existing enum path resolves an unknown value. Applied to both the top-level QueryDecoder and the list-element QueryListDecoder paths.

Test plan

  • Added regression tests in SerdeTest.kt covering malformed Int (page=abc), empty Int, malformed Long/Short/Byte/Double/Float, empty Char, multi-character Char (charValue=ab), and a malformed list element — confirmed each fails before the fix and passes after
  • ./gradlew :kuri-serde-kotlinx:jvmTest :kuri-serde-kotlinx:ktlintCheck :kuri-serde-kotlinx:detekt :kuri-serde-kotlinx:apiCheck
  • ./gradlew :kuri-serde-kotlinx:jsNodeTest (commonMain touched)
  • Not run: wasmJsNodeTest (known to transiently OOM the Kotlin daemon in this environment when bundled with other targets) — unverified on wasm
  • No public API shape changed (both decoders are internal), so apiDump was not needed; apiCheck confirms no diff

Closes #83

QueryDecoder's scalar decoders (Int/Long/Short/Byte/Double/Float/Char)
converted the raw query string directly with toInt()/toLong()/single()
etc., so a malformed value threw the raw JVM/stdlib exception
(NumberFormatException, NoSuchElementException, IllegalArgumentException)
straight out of decodeFromQueryParameters instead of the
SerializationException the format's own enum-decoding path and the
Url/Uri serializers already guarantee. A caller catching
SerializationException around decode, the documented failure boundary
for untrusted query input, would not catch these.

Switch each conversion to the *OrNull stdlib variant and fail with a
descriptive SerializationException when it returns null, mirroring how
the existing enum path resolves an unknown value. Applied to both the
top-level QueryDecoder and the list-element QueryListDecoder paths.

Closes #83
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

kuri-serde-kotlinx: malformed numeric/char query values leak raw JVM exceptions instead of SerializationException

1 participant