# GHSA-pqhp-25j4-6hq9: smol-toml has a Denial of Service via malicious TOML document using deeply nested inline tables

> An attacker can cause a denial of service by sending a deeply nested TOML document, crashing the application via stack overflow.

- **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:** 22 Nov 2024

## What is GHSA-pqhp-25j4-6hq9?

GHSA-pqhp-25j4-6hq9 is a denial of service vulnerability in smol-toml, published on 22 Nov 2024. An attacker can exploit it over the internet without logging in, with no action from the victim.

### Official description

### Summary
An attacker can send a maliciously crafted TOML to cause the parser to crash because of a stack overflow caused by a deeply nested inline structure. A similar problem occurs when attempting to stringify deeply nested objects.

The library does not limit the maximum exploration depth while parsing or producing TOML documents, nor does it offer a way to do so.

### Proof of concept
```js
require("smol-toml").parse("e=" + "{e=".repeat(9999) + "{}" + "}".repeat(9999))
```

### Impact
Applications which parse arbitrary TOML documents may suffer availability issues if they receive malicious input. If uncaught, the crash may cause the application itself to crash. The impact is deemed minor, as the function is already likely to throw errors on invalid input and therefore to properly handle errors.

Due to the design of most JavaScript runtimes, the uncontrolled recursion does not lead to excessive memory usage and the execution is quickly aborted.

As a reminder, it is **strongly** advised when working with untrusted user input to expect errors to occur and to appropriately catch them.

### Patches
Version 1.3.1 offers a mechanism to limit the exploration depth before halting with a `TomlError` when parsing, with a default cap of 1000. A same mechanism has been implemented for stringifying objects.

**Please note that the parser will *still* throw an error upon such cases.** It is, however, a now-controlled and documented behavior of the library.

### Workarounds
Wrap all invocations of `parse` and `stringify` in a try/catch block.

## Which versions are affected by GHSA-pqhp-25j4-6hq9?

2 open source packages ship an affected version, including smol-toml.


| Package | Ecosystem | Affected | Fixed in |
| --- | --- | --- | --- |
| `smol-toml` | npm | All versions | 1.3.1 |
| `smol-toml` | npm | All versions | 1.3.1 |

## How do I fix GHSA-pqhp-25j4-6hq9?

Upgrade smol-toml to 1.3.1 or later.

## Is GHSA-pqhp-25j4-6hq9 being exploited?

There is no known exploitation of GHSA-pqhp-25j4-6hq9 in the wild and no public exploit code.

## Fix prompt for AI coding agents

```text
Check this codebase for GHSA-pqhp-25j4-6hq9 and fix it if it is affected.

What it is: GHSA-pqhp-25j4-6hq9 is a denial of service vulnerability in smol-toml, published on 22 Nov 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-pqhp-25j4-6hq9 in the wild and no public exploit code.

Affected packages and fixed versions:
- smol-toml (npm): affected all versions; fixed in 1.3.1
- smol-toml (npm): affected all versions; fixed in 1.3.1

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-pqhp-25j4-6hq9/ (Markdown: https://insights.sbomify.com/GHSA-pqhp-25j4-6hq9/index.md)
```

## Sources

- OSV: https://osv.dev/vulnerability/GHSA-pqhp-25j4-6hq9

---
From sbomify Insights: https://insights.sbomify.com/GHSA-pqhp-25j4-6hq9/
Summaries are generated and may contain mistakes; the official description above is the record.
