# GHSA-j5g3-5c8r-7qfx: Prevent logging invalid header values

> Apollo Server may log sensitive Studio API keys if they contain invalid characters, exposing secrets to logs when using specific fetchers and reporting features.

- **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:** 30 Aug 2023

## What is GHSA-j5g3-5c8r-7qfx?

GHSA-j5g3-5c8r-7qfx is a vulnerability in @apollo/server, published on 30 Aug 2023. An attacker can exploit it over the internet without logging in, with no action from the victim.

### Official description

## Impact
### What kind of vulnerability is it?
Apollo Server can log sensitive information (Studio API keys) if they are passed incorrectly (with leading/trailing whitespace) or if they have any characters that are invalid as part of a header value.

### Who is impacted?
Users who (all of the below):
* use either the schema reporting or usage reporting feature
* use an Apollo Studio API key which has invalid header values
* use the default fetcher (`node-fetch`) or configured their own `node-fetch` fetcher

The following node snippet can test whether your API key has invalid header values. This code is taken directly from `node-fetch@2`'s header value validation code.
```js
const invalidHeaderCharRegex = /[^\t\x20-\x7e\x80-\xff]/;
if (invalidHeaderCharRegex.test('<YOUR_API_KEY>')) {
  console.log('potentially affected');
}
console.log('unaffected');
```

If the provided API key is not a valid header value, whenever Apollo Server uses that API key in a request (to Studio, for example), `node-fetch` will throw an error _which contains the header value_. This error is logged in various ways depending on the user's configuration, but most likely the console or some configured logging service.

### Patches
This problem is patched in the latest version of Apollo Server as soon as this advisory is published.

### Workarounds
* Try retrieving a new API key from Studio. Note: this may not work if the invalid character is not part of the secret (it may be derived from identifiers like graph name, user name).
* Override the `fetcher` 
* Disable schema reporting and/or usage reporting

### Solution
* Apollo Server will now call `.trim()` on incoming API keys in order to eliminate leading/trailing whitespace and log a warning when it does so.
* Apollo Server will now perform the same validation of API keys as `node-fetch@2` performs on header values on startup. Apollo Server will throw an error on startup (i.e., fail to start completely) and notify the user their API key is invalid along with the offending characters.

## Which versions are affected by GHSA-j5g3-5c8r-7qfx?

6 open source packages ship an affected version, including @apollo/server and apollo-server-core.


| Package | Ecosystem | Affected | Fixed in |
| --- | --- | --- | --- |
| `@apollo/server` | npm | All versions | 4.9.3 |
| `@apollo/server` | npm | All versions | 4.9.3 |
| `apollo-server-core` | npm | All versions | 2.26.1 |
| `apollo-server-core` | npm | All versions | 2.26.1 |
| `apollo-server-core` | npm | From 3.0.0 | 3.12.1 |
| `apollo-server-core` | npm | From 3.0.0 | 3.12.1 |

## How do I fix GHSA-j5g3-5c8r-7qfx?

Upgrade @apollo/server to 4.9.3 or later.

## Is GHSA-j5g3-5c8r-7qfx being exploited?

There is no known exploitation of GHSA-j5g3-5c8r-7qfx in the wild and no public exploit code.

## Fix prompt for AI coding agents

```text
Check this codebase for GHSA-j5g3-5c8r-7qfx and fix it if it is affected.

What it is: GHSA-j5g3-5c8r-7qfx is a vulnerability in @apollo/server, published on 30 Aug 2023. 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-j5g3-5c8r-7qfx in the wild and no public exploit code.

Affected packages and fixed versions:
- @apollo/server (npm): affected all versions; fixed in 4.9.3
- @apollo/server (npm): affected all versions; fixed in 4.9.3
- apollo-server-core (npm): affected all versions; fixed in 2.26.1
- apollo-server-core (npm): affected all versions; fixed in 2.26.1
- apollo-server-core (npm): affected from 3.0.0; fixed in 3.12.1
- apollo-server-core (npm): affected from 3.0.0; fixed in 3.12.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-j5g3-5c8r-7qfx/ (Markdown: https://insights.sbomify.com/GHSA-j5g3-5c8r-7qfx/index.md)
```

## Sources

- OSV: https://osv.dev/vulnerability/GHSA-j5g3-5c8r-7qfx

---
From sbomify Insights: https://insights.sbomify.com/GHSA-j5g3-5c8r-7qfx/
Summaries are generated and may contain mistakes; the official description above is the record.
