# GHSA-cx7h-h87r-jpgr: The kstring integration in gix-attributes is unsound

> Unsound UTF-8 handling in gix-attributes exposes invalid strings to dependent crates, potentially causing memory corruption or crashes.

- **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:** 24 Jul 2024
- **Also known as:** RUSTSEC-2024-0359

## What is GHSA-cx7h-h87r-jpgr?

GHSA-cx7h-h87r-jpgr is a memory corruption vulnerability in gix-attributes, published on 24 Jul 2024. An attacker can exploit it over the internet without logging in, with no action from the victim.

### Official description

`gix-attributes` (in [`state::ValueRef`](https://github.com/Byron/gitoxide/blob/gix-attributes-v0.22.2/gix-attributes/src/state.rs#L19-L27)) unsafely creates a `&str` from a `&[u8]` containing non-UTF8 data, with the justification that so long as nothing reads the `&str` and relies on it being UTF-8 in the `&str`, there is no UB:

```rust
// SAFETY: our API makes accessing that value as `str` impossible, so illformed UTF8 is never exposed as such.
```

The problem is that the non-UTF8 `str` **is** exposed to outside code: first to the `kstring` crate itself, which requires UTF-8 in its documentation and may have UB as a consequence of this, but also to `serde`, where it propagates to e.g. `serde_json`, `serde_yaml`, etc., where the same problems occur.

This is not sound, and it could cause further UB down the line in these places that can view the `&str`.

*Thanks to [Devin Jeanpierre](https://github.com/ssbr) for discovering and reporting this issue.*

## Which versions are affected by GHSA-cx7h-h87r-jpgr?

3 open source packages ship an affected version, including gix-attributes.


| Package | Ecosystem | Affected | Fixed in |
| --- | --- | --- | --- |
| `gix-attributes` | crates.io | All versions | 0.22.3 |
| `gix-attributes` | crates.io | All versions | 0.22.3 |
| `gix-attributes` | crates.io | From 0.0.0-0 | 0.22.3 |

## How do I fix GHSA-cx7h-h87r-jpgr?

Upgrade gix-attributes to 0.22.3 or later.

## Is GHSA-cx7h-h87r-jpgr being exploited?

There is no known exploitation of GHSA-cx7h-h87r-jpgr in the wild and no public exploit code.

## Fix prompt for AI coding agents

```text
Check this codebase for GHSA-cx7h-h87r-jpgr and fix it if it is affected.

What it is: GHSA-cx7h-h87r-jpgr is a memory corruption vulnerability in gix-attributes, published on 24 Jul 2024. 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-cx7h-h87r-jpgr in the wild and no public exploit code.

Affected packages and fixed versions:
- gix-attributes (crates.io): affected all versions; fixed in 0.22.3
- gix-attributes (crates.io): affected all versions; fixed in 0.22.3
- gix-attributes (crates.io): affected from 0.0.0-0; fixed in 0.22.3

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-cx7h-h87r-jpgr/ (Markdown: https://insights.sbomify.com/GHSA-cx7h-h87r-jpgr/index.md)
```

## Sources

- OSV: https://osv.dev/vulnerability/GHSA-cx7h-h87r-jpgr

---
From sbomify Insights: https://insights.sbomify.com/GHSA-cx7h-h87r-jpgr/
Summaries are generated and may contain mistakes; the official description above is the record.
