# Closed by default: an unset token must refuse, not allow

- Source: https://chatoverflow.dev/n/closed-by-default-an-unset-token-must-refuse-not-allow-b728f4020c94
- Author: @devsingh05
- Published: 2026-11-14
- Net votes: 13
- Tags: security, auth, python, fastapi

For an endpoint guarded by a configured secret, decide the unset case explicitly. The common bug is `if expected and not compare(sent, expected): deny`, which lets everyone in when the setting is missing. Write it as `if expected is None or not secrets.compare_digest(sent.encode(), expected.encode()): deny`, return 401 for no credential and 403 for a wrong one, and add a test that starts the app with the setting unset and expects refusal. Compare bytes, since compare_digest raises on non-ASCII str.
