[GH-ISSUE #213] API key: an empty value silently disables authentication, and the key is compared with == #111

Closed
opened 2026-11-04 01:40:02 +07:00 by bsakaguchi · 2 comments
Owner

Originally created by @alphastorm on GitHub (Sep 30, 2026).
Original GitHub issue: https://github.com/Niko1221/Strata/issues/213

Two small hardening points in the API key check (serve/server.py#L1243-L1251):

  1. An empty key turns authentication off, silently on 127.0.0.1. --api-key defaults to $STRATA_API_KEY (#L1669-L1670), and an empty value falls back to the config's api_key (#L1727); when both are empty, _authorized() lets every request through. The "no API key" warning is printed only for other hosts (#L1752-L1761); on 127.0.0.1 the ready line just leaves out ", API key required". A launcher that meant to set a key but exported an empty STRATA_API_KEY gets an open server and no sign of it.
  2. The key is compared with == (given == svc.api_key), which is not a constant-time comparison. hmac.compare_digest(given.encode(), svc.api_key.encode()) is the usual replacement.

Suggested: refuse to start, or at least warn on every host, when --api-key or STRATA_API_KEY is given but empty, and compare with hmac.compare_digest.

Originally created by @alphastorm on GitHub (Sep 30, 2026). Original GitHub issue: https://github.com/Niko1221/Strata/issues/213 Two small hardening points in the API key check ([serve/server.py#L1243-L1251](https://github.com/Niko1221/Strata/blob/a79080535d1b2a71a3419a0d97d8e7dca194b0f1/serve/server.py#L1243-L1251)): 1. **An empty key turns authentication off, silently on 127.0.0.1.** `--api-key` defaults to `$STRATA_API_KEY` ([#L1669-L1670](https://github.com/Niko1221/Strata/blob/a79080535d1b2a71a3419a0d97d8e7dca194b0f1/serve/server.py#L1669-L1670)), and an empty value falls back to the config's `api_key` ([#L1727](https://github.com/Niko1221/Strata/blob/a79080535d1b2a71a3419a0d97d8e7dca194b0f1/serve/server.py#L1727)); when both are empty, `_authorized()` lets every request through. The "no API key" warning is printed only for other hosts ([#L1752-L1761](https://github.com/Niko1221/Strata/blob/a79080535d1b2a71a3419a0d97d8e7dca194b0f1/serve/server.py#L1752-L1761)); on 127.0.0.1 the ready line just leaves out ", API key required". A launcher that meant to set a key but exported an empty `STRATA_API_KEY` gets an open server and no sign of it. 2. **The key is compared with `==`** (`given == svc.api_key`), which is not a constant-time comparison. `hmac.compare_digest(given.encode(), svc.api_key.encode())` is the usual replacement. Suggested: refuse to start, or at least warn on every host, when `--api-key` or `STRATA_API_KEY` is given but empty, and compare with `hmac.compare_digest`.
Author
Owner

@Niko1221 commented on GitHub (Sep 30, 2026):

Good points. 0.1.28 (in testing) compares the key in constant time (hmac.compare_digest), and an explicitly empty key (--api-key "" or an empty STRATA_API_KEY) stops the server with a message instead of switching authentication off.

<!-- gh-comment-id:5908107667 --> @Niko1221 commented on GitHub (Sep 30, 2026): Good points. 0.1.28 (in testing) compares the key in constant time (`hmac.compare_digest`), and an explicitly empty key (`--api-key ""` or an empty `STRATA_API_KEY`) stops the server with a message instead of switching authentication off.
Author
Owner

@Niko1221 commented on GitHub (Sep 30, 2026):

Fixed in 0.1.28: constant-time comparison (hmac.compare_digest), and an explicitly empty key (--api-key "" or an empty STRATA_API_KEY) stops the server with a message.

<!-- gh-comment-id:5908631910 --> @Niko1221 commented on GitHub (Sep 30, 2026): Fixed in [0.1.28](https://github.com/Niko1221/Strata/releases/tag/v0.1.28): constant-time comparison (`hmac.compare_digest`), and an explicitly empty key (`--api-key ""` or an empty `STRATA_API_KEY`) stops the server with a message.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Niko1221/Strata#111
No description provided.