# GHSA-7gcf-g7xr-8hxj: serde_with: KeyValueMap serialization panics on empty sequence or map entries

> An attacker can crash applications using serde_with KeyValueMap serialization by providing empty sequence or map entries, causing a panic.

- **Severity:** Not scored
- **Exploited in the wild:** Not known
- **Login needed:** Not needed
- **Attackable:** Over the internet
- **Victim action needed:** Not needed
- **Fix available:** Yes
- **Published:** 15 Jul 2026

## What is GHSA-7gcf-g7xr-8hxj?

GHSA-7gcf-g7xr-8hxj is a denial of service vulnerability in serde_with, published on 15 Jul 2026. An attacker can exploit it over the internet without logging in, with no action from the victim.

### Official description

### Summary

The public `KeyValueMap` serializer assumes that each mapped element has at least one field or item to use as the map key, but it subtracts `1` from the caller-visible length before validating that assumption. An application that serializes attacker-controlled data through `#[serde_as(as = "KeyValueMap<_>")]` can be crashed by an empty inner sequence or map entry.

### Details

The affected public surface includes:

- Serialization of `#[serde_as(as = "KeyValueMap<_>")]` values through `serde_json::to_string` or any other Serde serializer`
- Public `KeyValueMap` conversions for sequence and map-backed entries`

The root cause is: The `KeyValueMap` serializer preallocating `Vec::with_capacity(len - 1)` or `Vec::with_capacity(len.unwrap_or(17) - 1)` before checking that the element actually contains the required first key field or item.

The vulnerable data/control flow is: attacker-controlled empty entry -> `serde_json::to_string` -> `KeyValueMap<TAs>::serialize_as` -> `SeqAsMapSerializer::{serialize_seq,serialize_map}` -> `Vec::with_capacity(len - 1)` or `Vec::with_capacity(len.unwrap_or(17) - 1)` -> panic

Relevant source locations:

- `serde_with/src/key_value_map.rs:590`
- `serde_with/src/key_value_map.rs:599`
- `serde_with/src/key_value_map.rs:613`
- `serde_with/src/key_value_map.rs:632`
- `serde_with/src/key_value_map.rs:648`

### PoC

```rust
/*
[dependencies]
serde = {version = "*", features = ["derive"]}
serde_with = "*"
serde_json = "*"
*/

use serde::Serialize;
use serde_with::{serde_as, KeyValueMap};

#[derive(Serialize)]
#[serde(transparent)]
struct Seq(Vec<String>);

#[serde_as]
#[derive(Serialize)]
#[serde(transparent)]
struct KVMap {
    #[serde_as(as = "KeyValueMap<_>")]
    foo: Vec<Seq>,
}

fn main() {
    let value = KVMap {
        foo: vec![Seq(Vec::new())],
    };
    let _ = serde_json::to_string(&value).unwrap();
}
```

### Impact

A local attacker who can trigger serialization of attacker-controlled data through `KeyValueMap` can terminate the process, causing a denial of service.

## Which versions are affected by GHSA-7gcf-g7xr-8hxj?

2 open source packages ship an affected version, including serde_with.


| Package | Ecosystem | Affected | Fixed in |
| --- | --- | --- | --- |
| `serde_with` | crates.io | From 2.3.0 | 3.21.0 |
| `serde_with` | crates.io | From 2.3.0 | 3.21.0 |

## How do I fix GHSA-7gcf-g7xr-8hxj?

Upgrade serde_with to 3.21.0 or later.

## Is GHSA-7gcf-g7xr-8hxj being exploited?

There is no known exploitation of GHSA-7gcf-g7xr-8hxj in the wild and no public exploit code.

## Fix prompt for AI coding agents

```text
Check this codebase for GHSA-7gcf-g7xr-8hxj and fix it if it is affected.

What it is: GHSA-7gcf-g7xr-8hxj is a denial of service vulnerability in serde_with, published on 15 Jul 2026. An attacker can exploit it over the internet without logging in, with no action from the victim.
Exploitation: There is no known exploitation of GHSA-7gcf-g7xr-8hxj in the wild and no public exploit code.

Affected packages and fixed versions:
- serde_with (crates.io): affected from 2.3.0; fixed in 3.21.0
- serde_with (crates.io): affected from 2.3.0; fixed in 3.21.0

Task:
1. Search the dependency manifests and lockfiles (for example package.json, package-lock.json, requirements.txt, poetry.lock, uv.lock, pom.xml, build.gradle, go.mod, Cargo.lock, Gemfile.lock) and any Dockerfiles for the affected packages, including transitive dependencies.
2. If an affected version is in use, upgrade it to the fixed version for its release line with the project's package manager, so the lockfile is regenerated. Prefer the smallest upgrade that fixes it.
3. If there is no fix, or the upgrade would break the build, do not guess: explain the risk and the safest workaround instead.
4. Run the tests and summarise what changed and why.
5. If nothing in the project is affected, say so and change nothing.

Reference: https://insights.sbomify.com/GHSA-7gcf-g7xr-8hxj/ (Markdown: https://insights.sbomify.com/GHSA-7gcf-g7xr-8hxj/index.md)
```

## Sources

- OSV: https://osv.dev/vulnerability/GHSA-7gcf-g7xr-8hxj

---
From sbomify Insights: https://insights.sbomify.com/GHSA-7gcf-g7xr-8hxj/
Summaries are generated and may contain mistakes; the official description above is the record.
