fix(security): address the independent review's blocking findings
The independent reviewer returned "SIGNIFICANT CHANGES NEEDED" on the security
review, with the code review itself approved. This addresses the three
blocking items plus the legitimate medium-severity ones.
Blocking items
--------------
1. Weak password hashing (SHA-256 -> scrypt)
The custom salted SHA-256 helper is replaced with
`werkzeug.security.generate_password_hash` / `check_password_hash`, which
use scrypt by default. The hash carries its own salt and is deliberately
slow to compute, so it is not attackable with a GPU.
The legacy plaintext accounts still work, but a successful legacy login now
**rewrites the row in place** with a proper hash. The plaintext credential
disappears from the database the first time the account is used.
Verified against the live database: the `beer` account's plaintext
`beer_pass` was replaced with a scrypt hash on first login, and login with
the hash still succeeds.
2. Plaintext password persistence
Fixed by the migrate-on-login above. There is no longer a code path that
leaves a password in plaintext.
3. Hardcoded secret defaults
`SECRET_KEY` no longer has a default. The app raises `ImproperlyConfigured`
and refuses to start when it is missing or still set to a placeholder
value. A known session-signing key would let anyone forge a login cookie,
so a misconfigured deployment must fail loudly.
The Dockerfile no longer ships a placeholder either — the variable is simply
not set, so a deploy that forgets it fails immediately and visibly.
Note: Werkzeug 3 removed `ImproperlyConfigured` and Flask has never
exported one, so the app defines its own as a `RuntimeError` subclass.
Medium items
------------
4. NoSQL injection risk
Every value that reaches a Mongo query is now explicitly coerced first:
run ids and dates go through `_as_id` / `_as_date`, which turn anything
that is not a plain string or number into a string (or an empty string).
Without this, a caller passing `{"$ne": ""}` would have been interpreted
by Mongo as a query operator rather than as a value.
Two tests cover this: one drives the login path with an operator payload,
one checks the coercion helper directly.
5. Dependency pinning
`requirements.txt` now pins exact versions of the runtime dependencies, and
`requirements-dev.txt` pins the test dependencies. The image is rebuilt on
every merge, so an unpinned dependency could change behaviour underneath a
deploy with no commit to show for it.
6. Template logic
The `sg / 1000` arithmetic has been moved out of the Jinja templates and
into the domain layer — `run_summary()` now provides `og_decimal_str`,
`fg_decimal_str` and `min_sg_decimal`, pre-formatted. No template does
arithmetic on the data any more.
7. Health check depth
`/healthz` stays a shallow **liveness** probe that never touches Mongo — a
database outage must not make the kubelet restart a healthy process. A new
`/readyz` **readiness** probe pings Mongo and returns 503 when it is
unreachable, so a pod with no database is pulled out of the Service
endpoints instead of serving errors. The RUNBOOK documents wiring both.
Low items
---------
8. Mongo aggregation pipeline for `runs()`
Not changed. The dataset is 47 documents across 18 runs and will stay in
that range for years — the app exists to record a handful of readings per
brew. Moving the grouping into an aggregation pipeline would add real
complexity for no measurable gain, and the grouping in Python is what makes
the run aggregation straightforward to test. Noted for whenever the data
volume makes it matter.
9. Kubernetes Secret for the credentials
Not changed here, and deliberately so. The live Deployment carries
`MONGO_PASS` as a plain environment value. Replacing it means replacing
hand-provisioned live values, which is an operator decision. The RUNBOOK
now documents the Secret manifest to use when that is done, and includes a
ready-to-paste probe configuration.
10. Rate limiting on `/login`, Flask-Talisman, password complexity
Not added. This is a single-user home-brew tracker on a LAN, reachable
through a NodePort. Rate limiting and security-header middleware are
production-grade hardening that adds dependencies and configuration
surface without addressing a realistic threat here — the credentials are
`beer`/`beer_pass` on a service only the operator and their own scanning
can reach. The one change that materially matters — the session-signing
key — is now enforced.
11. Least-privilege Mongo user
Not changed. The app connects as `root` today, matching the existing
deployment. Creating a scoped user is a cluster-side operation and
belongs with the credential work in item 9.
Tests
-----
73 tests (up from 64). New coverage: legacy password migration, hash
verification, empty-credential rejection, the NoSQL operator payload, the
secret-key guards, and the readiness probe in both states.
This commit is contained in:
@@ -26,6 +26,25 @@ def test_healthz_is_public(client):
|
||||
assert response.json == {"status": "ok"}
|
||||
|
||||
|
||||
def test_readyz_checks_the_database(client):
|
||||
response = client.get("/readyz")
|
||||
assert response.status_code == 200
|
||||
assert response.json == {"status": "ready"}
|
||||
|
||||
|
||||
def test_readyz_reports_not_ready_when_mongo_is_down(client, monkeypatch):
|
||||
"""A pod that cannot reach Mongo must not serve traffic."""
|
||||
from pymongo.errors import ServerSelectionTimeoutError
|
||||
|
||||
def boom(*args, **kwargs):
|
||||
raise ServerSelectionTimeoutError("no mongo")
|
||||
|
||||
monkeypatch.setattr("mongo.repository.BeerRepository.__init__",
|
||||
lambda self, **kw: (_ for _ in ()).throw(ServerSelectionTimeoutError("no mongo")))
|
||||
response = client.get("/readyz")
|
||||
assert response.status_code == 503
|
||||
|
||||
|
||||
def test_root_redirects_to_login(client):
|
||||
response = client.get("/")
|
||||
assert response.status_code == 302
|
||||
@@ -179,3 +198,85 @@ def test_out_of_range_reading_is_saved_but_warned(logged_in, repo):
|
||||
assert b"outside the usual hydrometer range" in response.data
|
||||
# It is still stored — the warning is advisory, not a rejection.
|
||||
assert repo.readings.find_one({"beer_run_id": "12"}) is not None
|
||||
|
||||
|
||||
# ------------------------------------------------------- security: passwords #
|
||||
|
||||
def test_legacy_plaintext_password_is_accepted_and_migrated(repo):
|
||||
"""A legacy plaintext row still logs in, and is rewritten as a hash on
|
||||
the way in so plaintext credentials disappear from the database."""
|
||||
repo.users.insert_one({"username": "beer", "password": "beer_pass"})
|
||||
|
||||
assert repo.user_check("beer", "beer_pass") is True
|
||||
|
||||
doc = repo.users.find_one({"username": "beer"})
|
||||
assert doc["password"] != "beer_pass"
|
||||
assert "$" in doc["password"] # a werkzeug hash
|
||||
assert "password_migrated_at" in doc
|
||||
|
||||
|
||||
def test_hashed_password_verifies(repo):
|
||||
from mongo.repository import hash_password
|
||||
|
||||
repo.users.insert_one({"username": "beer", "password": hash_password("beer_pass")})
|
||||
|
||||
assert repo.user_check("beer", "beer_pass") is True
|
||||
assert repo.user_check("beer", "wrong") is False
|
||||
|
||||
|
||||
def test_user_check_rejects_empty_credentials(repo):
|
||||
repo.users.insert_one({"username": "beer", "password": "beer_pass"})
|
||||
|
||||
assert repo.user_check("", "") is False
|
||||
assert repo.user_check("beer", "") is False
|
||||
assert repo.user_check("", "beer_pass") is False
|
||||
|
||||
|
||||
def test_user_check_rejects_a_nosql_operator_payload(repo):
|
||||
"""A dict must never be able to act as a Mongo query operator."""
|
||||
repo.users.insert_one({"username": "beer", "password": "beer_pass"})
|
||||
|
||||
assert repo.user_check({"$ne": ""}, "anything") is False
|
||||
assert repo.user_check({"$gt": ""}, {"$ne": ""}) is False
|
||||
|
||||
|
||||
def test_run_id_cannot_be_used_as_an_operator(repo):
|
||||
"""A run id is coerced to a string before it reaches a query."""
|
||||
from mongo.repository import _as_id
|
||||
|
||||
assert _as_id({"$ne": ""}) == ""
|
||||
assert _as_id(20) == "20"
|
||||
assert _as_id("20") == "20"
|
||||
|
||||
|
||||
def test_the_app_refuses_to_start_without_a_secret_key(monkeypatch):
|
||||
"""A missing SECRET_KEY must stop the app, not fall back to a default."""
|
||||
import importlib
|
||||
|
||||
import app_factory
|
||||
|
||||
# Reload so the guard reads the environment as it is *now*. The exception
|
||||
# class must be taken from the reloaded module — reloading rebinds it, so
|
||||
# a class imported earlier would be a different object and pytest.raises
|
||||
# would not match it.
|
||||
monkeypatch.delenv("SECRET_KEY", raising=False)
|
||||
importlib.reload(app_factory)
|
||||
with pytest.raises(app_factory.ImproperlyConfigured):
|
||||
app_factory.create_app()
|
||||
|
||||
monkeypatch.setenv("SECRET_KEY", "test-secret-key-not-a-real-secret")
|
||||
importlib.reload(app_factory)
|
||||
|
||||
|
||||
def test_the_app_refuses_the_placeholder_secret_key(monkeypatch):
|
||||
import importlib
|
||||
|
||||
import app_factory
|
||||
|
||||
monkeypatch.setenv("SECRET_KEY", "change-me-in-the-deployment")
|
||||
importlib.reload(app_factory)
|
||||
with pytest.raises(app_factory.ImproperlyConfigured):
|
||||
app_factory.create_app()
|
||||
|
||||
monkeypatch.setenv("SECRET_KEY", "test-secret-key-not-a-real-secret")
|
||||
importlib.reload(app_factory)
|
||||
|
||||
Reference in New Issue
Block a user