**SQLite is the database, and now says so.** `metric_minute` and every run
table are written with `sqlalchemy.dialects.sqlite.insert(...)
.on_conflict_do_update` and with `max(a, b)`, neither of which another
dialect has — so pointing `DATABASE_URL` at Postgres migrated cleanly,
served, logged in, and then lost every observability flush into the
collector's hold buffer and failed every run. It refuses at startup
instead. (The Postgres in the compose stack is Umami's; the engine's own
database has been a file beside the flows since 2026-08-21.)
**Every integer query parameter is bounded.** The caps were written as
`min(limit, 500)`, which a negative walks straight through — `?limit=-1`
compiles to `LIMIT -1` and SQLite returns the whole table. Ten signatures,
now `Query(ge=…, le=…)`. `hours=0` still means an hour, which
`_window_hours` was already deliberate about.
**Exports are capped at 10 000 runs** and say so with `X-Truncated`. The
filters bounded a sensible request and nothing bounded an unfiltered one,
which read every row into memory before a byte was streamed. `_series`
resolves cached curves in two queries rather than a `Run` lookup and a
`RunMetric` query per restored node — a comparison of twenty runs was
calling that twenty times over.
**`PUT /artifacts` has a size limit** (`MAX_ARTIFACT_BYTES`, 2 GiB, 0 to
disable), checked against `Content-Length` and again against the stream for
a chunked body, and its writes moved off the event loop.
**`/observability/timeseries` takes `since`/`until`**, the same window
`/runs` and `/events` take, capped at 2000 points — `hours=720&bucket_s=60`
was 43 200 of them in one array. It is also what a dragged chart needs to
re-fetch at its own resolution rather than magnifying buckets it has.
**Composite indexes** for the three list screens: `run(flow, created_at)`
and `(status, created_at)`, `flow_run(flow, started_at)`,
`engine_event(type, ts)`. Every index was single-column, so SQLite picked
one and sorted the rest by hand. Verified against a copy of a live database
(250k `flow_run` rows): the planner takes all four.
**Redis clients have socket timeouts.** A Redis that stops answering
without closing the connection hung the caller until the kernel gave up —
including `/utils/health/`, whose job is to notice.
**The panels file is written under one lock.** `save_panels` and
`unpair_panel` are both read-modify-write, and a save that read before an
unpair wrote put the old nonce back — silently un-revoking a screen that
had just been unpaired. The nonce carry-forward was written to make that
impossible; the gap between its read and its write is where it happened.
**Startup releases what it acquired.** Everything past `event_bus.bind`
registers how to close itself and the `finally` walks that list backwards;
a failure part-way through used to reach none of the shutdown steps and
leave the worker pool's subprocesses and every background task behind —
under `--reload`, once per bad edit. `modules.reconcile` moved into the
background: `uv` gets five minutes twice over, the healthcheck allows
eighty seconds, and the autoheal restarted the container before it could
finish installing.
`delete_run` takes SQLite's write lock up front (`core.db.writing`) rather
than upgrading a deferred transaction and losing to whichever flush
committed in between. `modules.sync` is serialised — two applies mutated
one venv at once. The proxied-call and stream dicts are bounded, and a
reused id cancels its predecessor instead of dropping the reference.
Security, found in passing and small enough to fix here:
- **`/secrets/` required only a signed-in user.** The names alone say what
this installation talks to, and `PUT /{name}` takes any name, so any
account could overwrite the credential a flow authenticates with.
Superuser now — which `/search` already assumed and said so.
- **`POST /login/access-token` had no rate limit.** Argon2 is deliberately
expensive and the route is unauthenticated and runs in the shared
threadpool. Ten *failed* attempts per address per five minutes; a
successful sign-in spends nothing.
- **a password reset link worked repeatedly for 48 hours.** The token now
carries a digest of the password hash it was minted against, so it stops
verifying once it has set one. No table of spent tokens needed.
- **enrolment accepted `http://`**, sending the claim code and then this
installation's credential in clear. https, or a local address.
- the rate limiter read `request.client.host`, which behind Traefik is the
proxy — so every per-address limit was one global bucket and one caller
could lock out everyone. It reads the forwarded address, and its
bucket table is capped rather than growing one key per address forever.
- SMTP has a timeout and sends after the response, so an unreachable mail
host cannot pin a threadpool worker, and a reply's timing no longer says
whether the address exists.
Test suite: engine-written rows are cleared between modules. A `FlowRun`
left `running` by one module turned up in another's query. Per-test
rollback is not available here — the module-scoped `client` runs the real
lifespan and its collector and run service write through sessions of their
own — so this bounds it where the writes come from. Three consecutive
green runs, orders randomised.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01M6hPWS6YEbT1P8LxhhFb2T
223 lines
7.1 KiB
Python
223 lines
7.1 KiB
Python
from unittest.mock import patch
|
|
|
|
from fastapi.testclient import TestClient
|
|
from pwdlib.hashers.bcrypt import BcryptHasher
|
|
from sqlmodel import Session
|
|
|
|
from fluksio.core.config import settings
|
|
from fluksio.core.security import get_password_hash, verify_password
|
|
from fluksio.crud import create_user
|
|
from fluksio.models import User, UserCreate
|
|
from fluksio.utils import generate_password_reset_token
|
|
from tests.utils.user import user_authentication_headers
|
|
from tests.utils.utils import random_email, random_lower_string
|
|
|
|
|
|
def test_get_access_token(client: TestClient) -> None:
|
|
login_data = {
|
|
"username": settings.FIRST_SUPERUSER,
|
|
"password": settings.FIRST_SUPERUSER_PASSWORD,
|
|
}
|
|
r = client.post(f"{settings.API_V1_STR}/login/access-token", data=login_data)
|
|
tokens = r.json()
|
|
assert r.status_code == 200
|
|
assert "access_token" in tokens
|
|
assert tokens["access_token"]
|
|
|
|
|
|
def test_get_access_token_incorrect_password(client: TestClient) -> None:
|
|
login_data = {
|
|
"username": settings.FIRST_SUPERUSER,
|
|
"password": "incorrect",
|
|
}
|
|
r = client.post(f"{settings.API_V1_STR}/login/access-token", data=login_data)
|
|
assert r.status_code == 400
|
|
|
|
|
|
def test_use_access_token(
|
|
client: TestClient, superuser_token_headers: dict[str, str]
|
|
) -> None:
|
|
r = client.post(
|
|
f"{settings.API_V1_STR}/login/test-token",
|
|
headers=superuser_token_headers,
|
|
)
|
|
result = r.json()
|
|
assert r.status_code == 200
|
|
assert "email" in result
|
|
|
|
|
|
def test_recovery_password(
|
|
client: TestClient, normal_user_token_headers: dict[str, str]
|
|
) -> None:
|
|
with (
|
|
patch("fluksio.core.config.settings.SMTP_HOST", "smtp.example.com"),
|
|
patch("fluksio.core.config.settings.SMTP_USER", "admin@example.com"),
|
|
):
|
|
email = "test@example.com"
|
|
r = client.post(
|
|
f"{settings.API_V1_STR}/password-recovery/{email}",
|
|
headers=normal_user_token_headers,
|
|
)
|
|
assert r.status_code == 200
|
|
assert r.json() == {
|
|
"message": "If that email is registered, we sent a password recovery link"
|
|
}
|
|
|
|
|
|
def test_recovery_password_user_not_exits(
|
|
client: TestClient, normal_user_token_headers: dict[str, str]
|
|
) -> None:
|
|
email = "jVgQr@example.com"
|
|
r = client.post(
|
|
f"{settings.API_V1_STR}/password-recovery/{email}",
|
|
headers=normal_user_token_headers,
|
|
)
|
|
# Should return 200 with generic message to prevent email enumeration attacks
|
|
assert r.status_code == 200
|
|
assert r.json() == {
|
|
"message": "If that email is registered, we sent a password recovery link"
|
|
}
|
|
|
|
|
|
def test_reset_password(client: TestClient, db: Session) -> None:
|
|
email = random_email()
|
|
password = random_lower_string()
|
|
new_password = random_lower_string()
|
|
|
|
user_create = UserCreate(
|
|
email=email,
|
|
full_name="Test User",
|
|
password=password,
|
|
is_active=True,
|
|
is_superuser=False,
|
|
)
|
|
user = create_user(session=db, user_create=user_create)
|
|
# Bound to the hash it was minted against, which is what makes the link
|
|
# stop working once it has set a password.
|
|
token = generate_password_reset_token(
|
|
email=email, hashed_password=user.hashed_password
|
|
)
|
|
headers = user_authentication_headers(client=client, email=email, password=password)
|
|
data = {"new_password": new_password, "token": token}
|
|
|
|
r = client.post(
|
|
f"{settings.API_V1_STR}/reset-password/",
|
|
headers=headers,
|
|
json=data,
|
|
)
|
|
|
|
assert r.status_code == 200
|
|
assert r.json() == {"message": "Password updated successfully"}
|
|
|
|
db.refresh(user)
|
|
verified, _ = verify_password(new_password, user.hashed_password)
|
|
assert verified
|
|
|
|
|
|
def test_reset_password_invalid_token(
|
|
client: TestClient, superuser_token_headers: dict[str, str]
|
|
) -> None:
|
|
data = {"new_password": "changethis", "token": "invalid"}
|
|
r = client.post(
|
|
f"{settings.API_V1_STR}/reset-password/",
|
|
headers=superuser_token_headers,
|
|
json=data,
|
|
)
|
|
response = r.json()
|
|
|
|
assert "detail" in response
|
|
assert r.status_code == 400
|
|
assert response["detail"] == "Invalid token"
|
|
|
|
|
|
def test_login_with_bcrypt_password_upgrades_to_argon2(
|
|
client: TestClient, db: Session
|
|
) -> None:
|
|
"""Test that logging in with a bcrypt password hash upgrades it to argon2."""
|
|
email = random_email()
|
|
password = random_lower_string()
|
|
|
|
# Create a bcrypt hash directly (simulating legacy password)
|
|
bcrypt_hasher = BcryptHasher()
|
|
bcrypt_hash = bcrypt_hasher.hash(password)
|
|
assert bcrypt_hash.startswith("$2") # bcrypt hashes start with $2
|
|
|
|
user = User(email=email, hashed_password=bcrypt_hash, is_active=True)
|
|
db.add(user)
|
|
db.commit()
|
|
db.refresh(user)
|
|
|
|
assert user.hashed_password.startswith("$2")
|
|
|
|
login_data = {"username": email, "password": password}
|
|
r = client.post(f"{settings.API_V1_STR}/login/access-token", data=login_data)
|
|
assert r.status_code == 200
|
|
tokens = r.json()
|
|
assert "access_token" in tokens
|
|
|
|
db.refresh(user)
|
|
|
|
# Verify the hash was upgraded to argon2
|
|
assert user.hashed_password.startswith("$argon2")
|
|
|
|
verified, updated_hash = verify_password(password, user.hashed_password)
|
|
assert verified
|
|
# Should not need another update since it's already argon2
|
|
assert updated_hash is None
|
|
|
|
|
|
def test_login_with_argon2_password_keeps_hash(client: TestClient, db: Session) -> None:
|
|
"""Test that logging in with an argon2 password hash does not update it."""
|
|
email = random_email()
|
|
password = random_lower_string()
|
|
|
|
# Create an argon2 hash (current default)
|
|
argon2_hash = get_password_hash(password)
|
|
assert argon2_hash.startswith("$argon2")
|
|
|
|
# Create user with argon2 hash
|
|
user = User(email=email, hashed_password=argon2_hash, is_active=True)
|
|
db.add(user)
|
|
db.commit()
|
|
db.refresh(user)
|
|
|
|
original_hash = user.hashed_password
|
|
|
|
login_data = {"username": email, "password": password}
|
|
r = client.post(f"{settings.API_V1_STR}/login/access-token", data=login_data)
|
|
assert r.status_code == 200
|
|
tokens = r.json()
|
|
assert "access_token" in tokens
|
|
|
|
db.refresh(user)
|
|
|
|
assert user.hashed_password == original_hash
|
|
assert user.hashed_password.startswith("$argon2")
|
|
|
|
|
|
def test_a_reset_link_works_once(client: TestClient, db: Session) -> None:
|
|
"""It used to work over and over for the whole of its 48 hours."""
|
|
email = random_email()
|
|
password = random_lower_string()
|
|
user = create_user(
|
|
session=db,
|
|
user_create=UserCreate(email=email, password=password, is_active=True),
|
|
)
|
|
token = generate_password_reset_token(
|
|
email=email, hashed_password=user.hashed_password
|
|
)
|
|
|
|
first = client.post(
|
|
f"{settings.API_V1_STR}/reset-password/",
|
|
json={"new_password": random_lower_string(), "token": token},
|
|
)
|
|
assert first.status_code == 200
|
|
|
|
# The same link again: the password it was minted against has moved.
|
|
again = client.post(
|
|
f"{settings.API_V1_STR}/reset-password/",
|
|
json={"new_password": random_lower_string(), "token": token},
|
|
)
|
|
assert again.status_code == 400
|
|
assert again.json()["detail"] == "Invalid token"
|