Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""add used_email_token table

Revision ID: a9e5d21c7b48
Revises: f2a91c7e4d08
Create Date: 2026-07-06

One-time-use ledger for email link tokens (verify / reset / join-org): the
token's jti is recorded on first successful use so replays are refused.
"""

import sqlalchemy as sa

from alembic import op

# revision identifiers, used by Alembic.
revision = "a9e5d21c7b48"
down_revision = "f2a91c7e4d08"
branch_labels = None
depends_on = None


def upgrade() -> None:
op.create_table(
"used_email_token",
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
sa.Column("jti", sa.String(length=36), nullable=False),
sa.Column("expires_at", sa.DateTime(), nullable=False),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("jti"),
)
op.create_index("ix_used_email_token_expires_at", "used_email_token", ["expires_at"])


def downgrade() -> None:
op.drop_index("ix_used_email_token_expires_at", table_name="used_email_token")
op.drop_table("used_email_token")
18 changes: 13 additions & 5 deletions back-end/controllers/database_controller/vt_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from controllers.database_controller.kml_ops import get_kml_data
from database.models import mbtiles, vector_tiles
from database.sessions import Session
from utils.logger_config import logger
from utils.settings import DATABASE_URL

from .file_ops import (
Expand Down Expand Up @@ -203,11 +204,18 @@ def add_values_to_VT(geojson_file_path, mbtiles_file_paths, folderid):


def run_tippecanoe(command):
result = subprocess.run(command, shell=True, check=True, stderr=subprocess.PIPE)

if result.stderr:
print("Tippecanoe stderr:", result.stderr.decode())

"""Run a tippecanoe/tile-join command, surfacing stderr on BOTH paths.
On failure the raised message must carry the real error: it is what gets
stored as the task result, and a bare "returned non-zero exit status 1"
leaves tile failures undiagnosable without shell access to the worker."""
result = subprocess.run(command, shell=True, stderr=subprocess.PIPE)
stderr = (result.stderr or b"").decode(errors="replace").strip()
if result.returncode != 0:
logger.error(f"tippecanoe failed (exit {result.returncode}): {stderr}\ncmd: {command}")
raise RuntimeError(f"tippecanoe exit {result.returncode}: {stderr[-2000:]}")
if stderr:
# tippecanoe reports progress/feature counts on stderr even on success.
logger.info(f"tippecanoe stderr: {stderr}")
return result.returncode


Expand Down
14 changes: 14 additions & 0 deletions back-end/database/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,20 @@ class audit_log(Base):
ip = Column(String, nullable=True)


class used_email_token(Base):
"""One-time-use ledger for email link tokens (verify / reset / join-org):
a token's jti is recorded on its first successful use and any replay is
refused. Rows are dead weight once expires_at passes (the JWT itself has
expired by then) and are pruned opportunistically on insert."""

__tablename__ = "used_email_token"
__table_args__ = (Index("ix_used_email_token_expires_at", "expires_at"),)

id = Column(Integer, primary_key=True, autoincrement=True)
jti = Column(String(36), unique=True, nullable=False)
expires_at = Column(DateTime, nullable=False)


class site_setting(Base):
"""Site-wide key/value settings the platform operator controls from the
admin panel (e.g. site_theme). Not per-org, not user-visible to edit."""
Expand Down
47 changes: 46 additions & 1 deletion back-end/routes/_email.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import uuid
from datetime import UTC, datetime, timedelta

import jwt
from flask_mail import Message
from sqlalchemy.exc import IntegrityError

from utils.flask_app import app, mail

Expand All @@ -12,22 +14,65 @@
EMAIL_TOKEN_AUDIENCE = "bdk-email"


# How long a verify/reset/join link stays valid. An hour, not minutes: users
# routinely open these emails well after they arrive, and an expired link
# reads as a broken product. Tokens are not single-use, so this is also the
# replay window for an already-used link — keep it to hours, not days.
EMAIL_TOKEN_LIFETIME = timedelta(minutes=60)


def create_email_token(userid, email, operation, org_id=-1):
# Expiry must be timezone-aware UTC: PyJWT treats a naive datetime as UTC,
# so naive local time on a non-UTC host mints already-expired tokens.
expiration = datetime.now(UTC) + timedelta(minutes=15)
expiration = datetime.now(UTC) + EMAIL_TOKEN_LIFETIME
email_token = jwt.encode(
{
"sub": {"id": userid, "email": email, "operation": operation, "org_id": org_id},
"exp": expiration,
"aud": EMAIL_TOKEN_AUDIENCE,
# One-time-use handle: consume_email_token records it on first
# successful use, so a leaked/reused link can't be replayed.
"jti": uuid.uuid4().hex,
},
app.config["JWT_SECRET_KEY"],
algorithm="HS256",
)
return email_token


def consume_email_token(decoded):
"""One-time-use gate. True the first time a decoded token is consumed;
False on replay or when the token has no jti (minted before stamping —
those age out within EMAIL_TOKEN_LIFETIME of the deploy).

Call it after the domain checks pass and before mutating anything. The
unique constraint on jti is the actual gate, so two concurrent uses of
the same token can't both win. Own short session on purpose: the
IntegrityError rollback must not discard request-scoped work."""
from database.models import used_email_token
from database.sessions import Session

jti = decoded.get("jti")
if not jti:
return False
# Column is a naive-UTC DateTime like the rest of the schema.
now = datetime.now(UTC).replace(tzinfo=None)
expires_at = datetime.fromtimestamp(decoded["exp"], UTC).replace(tzinfo=None)
session = Session()
try:
# Opportunistic prune: expired rows guard nothing (their JWTs are
# already dead), so the table stays bounded by the token lifetime.
session.query(used_email_token).filter(used_email_token.expires_at < now).delete()
session.add(used_email_token(jti=jti, expires_at=expires_at))
session.commit()
return True
except IntegrityError:
session.rollback()
return False
finally:
session.close()


def send_verification_email_with_token(
email, token, title, content, join_org=False, joining_email="", link_path=None
):
Expand Down
11 changes: 8 additions & 3 deletions back-end/routes/admin_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
"""

from flask import Blueprint, g, redirect, render_template, request
from werkzeug.security import check_password_hash, generate_password_hash
from werkzeug.security import generate_password_hash

from controllers.database_controller import user_ops
from database.sessions import get_session
from services import admin_service
from services.audit import log_action
from utils.admin_auth import ADMIN_COOKIE, mint_admin_session, require_platform_admin
from utils.flask_app import limiter
from utils.passwords import needs_rehash, verify_password
from utils.settings import IN_PRODUCTION

bp = Blueprint("admin_ui", __name__)
Expand Down Expand Up @@ -66,8 +67,12 @@ def login():
and getattr(user, "is_platform_admin", False)
and not getattr(user, "disabled", False)
and user.password
and check_password_hash(user.password, password)
and verify_password(user.password, password)
):
if needs_rehash(user.password):
# Legacy (pre-pbkdf2) hash: the user just proved the password, so
# upgrade the stored hash in place — no forced reset.
user_ops.reset_user_password(user.id, password)
token, _csrf = mint_admin_session(user.id)
response = redirect("/admin/")
_set_admin_cookie(response, token)
Expand All @@ -76,7 +81,7 @@ def login():

# Equalize work on the failure path (absent / non-admin email) so response
# time doesn't reveal which emails are platform admins.
check_password_hash(_DUMMY_PASSWORD_HASH, password)
verify_password(_DUMMY_PASSWORD_HASH, password)
log_action("admin_login_failed", details={"email": email})
return render_template(
"admin/login.html", error="Invalid credentials or not a platform admin."
Expand Down
14 changes: 12 additions & 2 deletions back-end/routes/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,21 @@
verify_jwt_in_request,
)
from jwt import ExpiredSignatureError
from werkzeug.security import check_password_hash

from controllers.database_controller import (
user_ops,
)
from database.sessions import get_session
from routes._email import (
EMAIL_TOKEN_AUDIENCE,
consume_email_token,
create_email_token,
send_verification_email_with_token,
)
from services.audit import log_action
from utils.flask_app import app, limiter
from utils.logger_config import logger
from utils.passwords import needs_rehash, verify_password
from utils.validation import is_valid_email

bp = Blueprint("auth", __name__)
Expand Down Expand Up @@ -69,6 +70,8 @@ def reset_password():
user_id = decoded_token["sub"]["id"]
email = decoded_token["sub"]["email"]
if user_ops.verify_user_email(user_id, email, session, False):
if not consume_email_token(decoded_token):
return jsonify({"status": "error", "message": "Invalid token."}), 400
user_ops.reset_user_password(user_id, new_password)
return jsonify({"status": "success", "message": "Password reset successfully."}), 200
else:
Expand Down Expand Up @@ -106,6 +109,9 @@ def verify_token():

logger.debug(operation)

if not consume_email_token(decoded_token):
return jsonify({"status": "error", "message": "Invalid token."}), 400

setVerified = True if operation == "email_address_verification" else False
if user_ops.verify_user_email(user_id, email, session, setVerified):
if operation == "join_organization":
Expand Down Expand Up @@ -189,7 +195,11 @@ def login():
# Needs a try catch to return the correct message
user = user_ops.get_user_with_email(email)

if user is not None and check_password_hash(user.password, pword):
if user is not None and verify_password(user.password, pword):
if needs_rehash(user.password):
# Legacy (pre-pbkdf2) hash: the user just proved the password, so
# upgrade the stored hash in place — no forced reset.
user_ops.reset_user_password(user.id, pword)
# A soft-disabled account cannot obtain a fresh session. Checked
# only after the password verifies, so we never reveal disabled status
# to someone who doesn't already hold the credentials.
Expand Down
22 changes: 20 additions & 2 deletions back-end/routes/auth_pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,18 @@
set_access_cookies,
unset_jwt_cookies,
)
from werkzeug.security import check_password_hash

from controllers.database_controller import setting_ops, user_ops
from database.sessions import get_session
from routes._email import (
EMAIL_TOKEN_AUDIENCE,
consume_email_token,
create_email_token,
send_verification_email_with_token,
)
from services.audit import log_action
from utils.flask_app import app, limiter
from utils.passwords import needs_rehash, verify_password
from utils.validation import is_valid_email

bp = Blueprint("auth_pages", __name__, template_folder="../templates")
Expand Down Expand Up @@ -53,10 +54,14 @@ def login_submit():
email = (request.form.get("email") or "").strip()
pword = request.form.get("password") or ""
user = user_ops.get_user_with_email(email)
if user is None or not check_password_hash(user.password, pword):
if user is None or not verify_password(user.password, pword):
# Don't leak whether the email exists; record the attempted email.
log_action("login_failed", details={"email": email})
return _page("app/auth_login.html", error="Invalid credentials")
if needs_rehash(user.password):
# Legacy (pre-pbkdf2) hash: the user just proved the password, so
# upgrade the stored hash in place — no forced reset.
user_ops.reset_user_password(user.id, pword)
if getattr(user, "disabled", False):
log_action("login_disabled", user_id=user.id, resource_type="user", resource_id=user.id)
return _page("app/auth_login.html", error="This account has been disabled.")
Expand Down Expand Up @@ -129,6 +134,13 @@ def verify_email(token):
)
if decoded["sub"]["operation"] != "email_address_verification":
raise ValueError
if not consume_email_token(decoded):
# Re-clicks and mail-scanner prefetches replay this GET link; once
# the account is verified that's a success, not a broken link.
userVal = user_ops.get_user_with_id(decoded["sub"]["id"], session=session)
if userVal is not None and userVal.verified:
return _page("app/auth_verified.html", ok=True, error=None)
raise ValueError
if not user_ops.verify_user_email(
decoded["sub"]["id"], decoded["sub"]["email"], session, True
):
Expand Down Expand Up @@ -208,6 +220,12 @@ def reset_token_submit(token):
email = decoded["sub"]["email"]
if not user_ops.verify_user_email(user_id, email, session, False):
raise ValueError
if not consume_email_token(decoded):
return _page(
"app/auth_reset_token.html",
token=token,
error="That link has already been used — request a new one.",
)
user_ops.reset_user_password(user_id, new_password)
except jwt.ExpiredSignatureError:
return _page(
Expand Down
44 changes: 44 additions & 0 deletions back-end/services/filing_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ def submission_debts(user_id, folderid, session):
submission, in plain words with a place to fix it. Empty list = ready.
Feeds the header badge ("N tasks to finish" / "Ready to submit") and the
generate sheet — the app's only hard gate."""
from database.models import celerytaskinfo, fabric_data
from database.models import file as file_model
from services import plan_service

Expand All @@ -161,6 +162,49 @@ def submission_debts(user_id, folderid, session):
"fix": "/files?tab=fabric",
}
)
else:
# The file row is committed synchronously at upload, before — and
# regardless of whether — the worker import succeeds. The gate must
# key on locations actually having landed, or a failed import could
# be submitted as a filing with no fabric-derived served locations.
fabric_imported = (
session.query(fabric_data.id)
.join(file_model, fabric_data.file_id == file_model.id)
.filter(file_model.folder_id == folderVal.id, file_model.type == "fabric")
.first()
is not None
)
if not fabric_imported:
# Task status only shapes the wording; the fabric_data rows above
# are the source of truth for readiness.
latest_task = (
session.query(celerytaskinfo)
.filter(
celerytaskinfo.organization_id == folderVal.organization_id,
celerytaskinfo.operation_type == "Fabric",
celerytaskinfo.folder_deadline == folderVal.deadline,
)
.order_by(celerytaskinfo.id.desc())
.first()
)
if latest_task is not None and latest_task.status in ("PENDING", "STARTED", "RETRY"):
debts.append(
{
"id": "fabric_import",
"label": "Fabric import in progress",
"sub": "locations are still loading — this clears when the import lands",
"fix": "/files?tab=fabric",
}
)
else:
debts.append(
{
"id": "fabric_import",
"label": "Fabric import failed",
"sub": "the upload didn't import any locations — retry or re-upload it",
"fix": "/files?tab=fabric",
}
)

coverage_files = (
session.query(file_model)
Expand Down
Loading
Loading