feat: comprehensive code review fixes — security, performance, quality
Backend security: - Reject Gitea webhooks when webhook_secret is empty (was silently skipping) - Add slowapi rate limiting on login (5/min) and setup (3/min) endpoints - Add CORS middleware with configurable origins - Mask telegram_webhook_secret in settings API response - Protect system-owned command template configs from regular user modification - Increase minimum password length to 8 characters Backend performance: - Batch queries in _resolve_command_context (3 queries instead of 3N) - Concurrent album fetching with asyncio.gather in immich commands - Singleton Jinja2 SandboxedEnvironment (reuse instead of per-render creation) - TTLCache for rate limits (bounded memory, auto-eviction) - Optional aiohttp session reuse in send_reply/send_media_group Backend code quality: - Extract dispatch_helpers.py (shared link_data loading + event filtering) - Extract database/seeds.py from main.py (490 lines → dedicated module) - Split immich_handler.py (415 lines) into commands/immich/ subpackage - Replace bare except blocks with logged warnings - Add per-provider config validation (Pydantic models) - Truncate command input to 512 chars - Expose usage_* and desc_* slots in capabilities and variables API Frontend security: - CSS.escape() for user-controlled querySelector in highlight.ts - Client-side password min 8 chars validation on setup and password change Frontend code quality: - Replace any types with proper interfaces across top files - Decompose targets/+page.svelte into TargetForm + ReceiverSection - Fix $derived.by usage, $state mutation patterns - Add console.warn to empty catch blocks Frontend UX: - Auth redirect via goto() with "Redirecting..." state - Platform-aware Ctrl/Cmd K keyboard hint - Remove stat-card hover transform Frontend accessibility: - Modal: role=dialog, aria-modal, focus trap, restore focus - EntitySelect/IconGridSelect: listbox/option roles, aria-selected/disabled
This commit is contained in:
@@ -4,6 +4,10 @@ import logging
|
||||
from contextlib import asynccontextmanager
|
||||
|
||||
from fastapi import FastAPI
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from slowapi import _rate_limit_exceeded_handler
|
||||
from slowapi.errors import RateLimitExceeded
|
||||
from slowapi.middleware import SlowAPIMiddleware
|
||||
|
||||
# Ensure app-level loggers are visible
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
@@ -50,10 +54,8 @@ async def lifespan(app: FastAPI):
|
||||
await migrate_template_locale(engine)
|
||||
await migrate_receivers_from_config(engine)
|
||||
await migrate_command_slot_locale(engine)
|
||||
await _seed_default_templates()
|
||||
await _seed_default_command_templates()
|
||||
await _seed_default_tracking_configs()
|
||||
await _seed_default_command_configs()
|
||||
from .database.seeds import seed_all
|
||||
await seed_all()
|
||||
# Configure webhook secret from DB setting (falls back to env var)
|
||||
from sqlmodel.ext.asyncio.session import AsyncSession as _AS
|
||||
from .api.app_settings import get_setting as _get_setting
|
||||
@@ -71,6 +73,23 @@ async def lifespan(app: FastAPI):
|
||||
|
||||
app = FastAPI(title="Notify Bridge", version="0.1.0", lifespan=lifespan)
|
||||
|
||||
# --- Rate limiting ---
|
||||
from .auth.routes import limiter
|
||||
app.state.limiter = limiter
|
||||
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)
|
||||
app.add_middleware(SlowAPIMiddleware)
|
||||
|
||||
# --- CORS ---
|
||||
from .config import settings as _cfg
|
||||
_origins = [o.strip() for o in _cfg.cors_allowed_origins.split(",") if o.strip()]
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=_origins,
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
|
||||
# Register routes — static paths before parameterized
|
||||
app.include_router(auth_router)
|
||||
app.include_router(template_vars_router)
|
||||
@@ -99,496 +118,6 @@ async def health():
|
||||
return {"status": "ok"}
|
||||
|
||||
|
||||
async def _seed_default_templates():
|
||||
"""Seed or update default (system-owned) templates on startup.
|
||||
|
||||
Uses TemplateSlot child rows for template content.
|
||||
"""
|
||||
from sqlalchemy import text
|
||||
from sqlmodel import func, select
|
||||
from sqlmodel.ext.asyncio.session import AsyncSession
|
||||
from .database.engine import get_engine
|
||||
from .database.models import TemplateConfig, TemplateSlot
|
||||
from notify_bridge_core.templates.defaults import load_default_templates
|
||||
|
||||
engine = get_engine()
|
||||
async with AsyncSession(engine) as session:
|
||||
# Find existing system-owned templates
|
||||
result = await session.exec(
|
||||
select(TemplateConfig).where(TemplateConfig.user_id == 0)
|
||||
)
|
||||
system_configs = result.all()
|
||||
existing_locales = {
|
||||
(c.locale if c.locale else ("ru" if "(RU)" in c.name else "en")): c
|
||||
for c in system_configs
|
||||
}
|
||||
|
||||
for locale in ("en", "ru"):
|
||||
slots = load_default_templates(locale, provider_type="immich")
|
||||
if not slots:
|
||||
continue
|
||||
|
||||
if locale not in existing_locales:
|
||||
# Create missing system template via raw SQL
|
||||
# (legacy NOT NULL columns may still exist in the DB)
|
||||
name = f"Default ({locale.upper()})"
|
||||
desc = f"Default Immich templates ({locale.upper()})"
|
||||
# Get column names to build INSERT with defaults for legacy cols
|
||||
col_info = (await session.execute(
|
||||
text("PRAGMA table_info(template_config)")
|
||||
)).fetchall()
|
||||
col_names = [c[1] for c in col_info if c[1] != "id"]
|
||||
values = {}
|
||||
from datetime import datetime, timezone
|
||||
now = datetime.now(timezone.utc).isoformat()
|
||||
for col in col_names:
|
||||
if col == "user_id":
|
||||
values[col] = 0
|
||||
elif col == "provider_type":
|
||||
values[col] = "immich"
|
||||
elif col == "name":
|
||||
values[col] = name
|
||||
elif col == "description":
|
||||
values[col] = desc
|
||||
elif col == "created_at":
|
||||
values[col] = now
|
||||
elif col == "date_format":
|
||||
values[col] = "%d.%m.%Y, %H:%M UTC"
|
||||
elif col == "date_only_format":
|
||||
values[col] = "%d.%m.%Y"
|
||||
elif col == "locale":
|
||||
values[col] = locale
|
||||
else:
|
||||
values[col] = "" # empty string for legacy columns
|
||||
cols_str = ", ".join(values.keys())
|
||||
placeholders = ", ".join(f":{k}" for k in values.keys())
|
||||
await session.execute(
|
||||
text(f"INSERT INTO template_config ({cols_str}) VALUES ({placeholders})"),
|
||||
values,
|
||||
)
|
||||
# Get the inserted ID
|
||||
row = (await session.execute(text("SELECT last_insert_rowid()"))).scalar()
|
||||
config_id = row
|
||||
|
||||
for slot_name, template_text in slots.items():
|
||||
session.add(TemplateSlot(
|
||||
config_id=config_id,
|
||||
slot_name=slot_name,
|
||||
template=template_text,
|
||||
))
|
||||
else:
|
||||
# Update existing system template slots
|
||||
config = existing_locales[locale]
|
||||
for slot_name, template_text in slots.items():
|
||||
slot_result = await session.exec(
|
||||
select(TemplateSlot).where(
|
||||
TemplateSlot.config_id == config.id,
|
||||
TemplateSlot.slot_name == slot_name,
|
||||
)
|
||||
)
|
||||
existing = slot_result.first()
|
||||
if existing:
|
||||
existing.template = template_text
|
||||
session.add(existing)
|
||||
else:
|
||||
session.add(TemplateSlot(
|
||||
config_id=config.id,
|
||||
slot_name=slot_name,
|
||||
template=template_text,
|
||||
))
|
||||
|
||||
# --- Seed Gitea default templates ---
|
||||
gitea_result = await session.exec(
|
||||
select(TemplateConfig).where(
|
||||
TemplateConfig.user_id == 0,
|
||||
TemplateConfig.provider_type == "gitea",
|
||||
)
|
||||
)
|
||||
gitea_configs = gitea_result.all()
|
||||
gitea_existing_locales = {
|
||||
(c.locale if c.locale else "en"): c for c in gitea_configs
|
||||
}
|
||||
|
||||
for locale in ("en", "ru"):
|
||||
gitea_slots = load_default_templates(locale, provider_type="gitea")
|
||||
if not gitea_slots:
|
||||
continue
|
||||
|
||||
if locale not in gitea_existing_locales:
|
||||
from datetime import datetime as _dt, timezone as _tz
|
||||
now = _dt.now(_tz.utc).isoformat()
|
||||
name = f"Default Gitea ({locale.upper()})"
|
||||
desc = f"Default Gitea templates ({locale.upper()})"
|
||||
col_info = (await session.execute(
|
||||
text("PRAGMA table_info(template_config)")
|
||||
)).fetchall()
|
||||
col_names = [c[1] for c in col_info if c[1] != "id"]
|
||||
values = {}
|
||||
for col in col_names:
|
||||
if col == "user_id":
|
||||
values[col] = 0
|
||||
elif col == "provider_type":
|
||||
values[col] = "gitea"
|
||||
elif col == "name":
|
||||
values[col] = name
|
||||
elif col == "description":
|
||||
values[col] = desc
|
||||
elif col == "created_at":
|
||||
values[col] = now
|
||||
elif col == "date_format":
|
||||
values[col] = "%d.%m.%Y, %H:%M UTC"
|
||||
elif col == "date_only_format":
|
||||
values[col] = "%d.%m.%Y"
|
||||
elif col == "locale":
|
||||
values[col] = locale
|
||||
else:
|
||||
values[col] = ""
|
||||
cols_str = ", ".join(values.keys())
|
||||
placeholders = ", ".join(f":{k}" for k in values.keys())
|
||||
await session.execute(
|
||||
text(f"INSERT INTO template_config ({cols_str}) VALUES ({placeholders})"),
|
||||
values,
|
||||
)
|
||||
row = (await session.execute(text("SELECT last_insert_rowid()"))).scalar()
|
||||
gitea_config_id = row
|
||||
for slot_name, template_text in gitea_slots.items():
|
||||
session.add(TemplateSlot(
|
||||
config_id=gitea_config_id,
|
||||
slot_name=slot_name,
|
||||
template=template_text,
|
||||
))
|
||||
else:
|
||||
config = gitea_existing_locales[locale]
|
||||
for slot_name, template_text in gitea_slots.items():
|
||||
slot_result = await session.exec(
|
||||
select(TemplateSlot).where(
|
||||
TemplateSlot.config_id == config.id,
|
||||
TemplateSlot.slot_name == slot_name,
|
||||
)
|
||||
)
|
||||
existing = slot_result.first()
|
||||
if existing:
|
||||
existing.template = template_text
|
||||
session.add(existing)
|
||||
else:
|
||||
session.add(TemplateSlot(
|
||||
config_id=config.id,
|
||||
slot_name=slot_name,
|
||||
template=template_text,
|
||||
))
|
||||
|
||||
# --- Seed Scheduler default templates ---
|
||||
sched_result = await session.exec(
|
||||
select(TemplateConfig).where(
|
||||
TemplateConfig.user_id == 0,
|
||||
TemplateConfig.provider_type == "scheduler",
|
||||
)
|
||||
)
|
||||
sched_configs = sched_result.all()
|
||||
sched_existing_locales = {
|
||||
(c.locale if c.locale else "en"): c for c in sched_configs
|
||||
}
|
||||
|
||||
for locale in ("en", "ru"):
|
||||
sched_slots = load_default_templates(locale, provider_type="scheduler")
|
||||
if not sched_slots:
|
||||
continue
|
||||
|
||||
if locale not in sched_existing_locales:
|
||||
from datetime import datetime as _dt2, timezone as _tz2
|
||||
now2 = _dt2.now(_tz2.utc).isoformat()
|
||||
name2 = f"Default Scheduler ({locale.upper()})"
|
||||
desc2 = f"Default Scheduler templates ({locale.upper()})"
|
||||
col_info2 = (await session.execute(
|
||||
text("PRAGMA table_info(template_config)")
|
||||
)).fetchall()
|
||||
col_names2 = [c[1] for c in col_info2 if c[1] != "id"]
|
||||
values2 = {}
|
||||
for col in col_names2:
|
||||
if col == "user_id":
|
||||
values2[col] = 0
|
||||
elif col == "provider_type":
|
||||
values2[col] = "scheduler"
|
||||
elif col == "name":
|
||||
values2[col] = name2
|
||||
elif col == "description":
|
||||
values2[col] = desc2
|
||||
elif col == "created_at":
|
||||
values2[col] = now2
|
||||
elif col == "date_format":
|
||||
values2[col] = "%d.%m.%Y, %H:%M UTC"
|
||||
elif col == "date_only_format":
|
||||
values2[col] = "%d.%m.%Y"
|
||||
elif col == "locale":
|
||||
values2[col] = locale
|
||||
else:
|
||||
values2[col] = ""
|
||||
cols_str2 = ", ".join(values2.keys())
|
||||
placeholders2 = ", ".join(f":{k}" for k in values2.keys())
|
||||
await session.execute(
|
||||
text(f"INSERT INTO template_config ({cols_str2}) VALUES ({placeholders2})"),
|
||||
values2,
|
||||
)
|
||||
row2 = (await session.execute(text("SELECT last_insert_rowid()"))).scalar()
|
||||
for slot_name, template_text in sched_slots.items():
|
||||
session.add(TemplateSlot(
|
||||
config_id=row2,
|
||||
slot_name=slot_name,
|
||||
template=template_text,
|
||||
))
|
||||
else:
|
||||
config = sched_existing_locales[locale]
|
||||
for slot_name, template_text in sched_slots.items():
|
||||
slot_result = await session.exec(
|
||||
select(TemplateSlot).where(
|
||||
TemplateSlot.config_id == config.id,
|
||||
TemplateSlot.slot_name == slot_name,
|
||||
)
|
||||
)
|
||||
existing = slot_result.first()
|
||||
if existing:
|
||||
existing.template = template_text
|
||||
session.add(existing)
|
||||
else:
|
||||
session.add(TemplateSlot(
|
||||
config_id=config.id,
|
||||
slot_name=slot_name,
|
||||
template=template_text,
|
||||
))
|
||||
|
||||
await session.commit()
|
||||
|
||||
|
||||
async def _seed_default_command_templates():
|
||||
"""Seed or update default command response templates on startup.
|
||||
|
||||
Creates a single 'Default Commands' config with locale-aware slots
|
||||
(each slot has an EN and RU version stored as separate rows).
|
||||
"""
|
||||
from sqlmodel import func, select
|
||||
from sqlmodel.ext.asyncio.session import AsyncSession
|
||||
from .database.engine import get_engine
|
||||
from .database.models import CommandTemplateConfig, CommandTemplateSlot
|
||||
from notify_bridge_core.templates.command_defaults import load_default_command_templates
|
||||
|
||||
engine = get_engine()
|
||||
async with AsyncSession(engine) as session:
|
||||
# Find or create the system-owned config
|
||||
result = await session.exec(
|
||||
select(CommandTemplateConfig).where(CommandTemplateConfig.user_id == 0)
|
||||
)
|
||||
system_configs = result.all()
|
||||
|
||||
if not system_configs:
|
||||
# First startup — create single merged config
|
||||
config = CommandTemplateConfig(
|
||||
user_id=0,
|
||||
provider_type="immich",
|
||||
name="Default Commands",
|
||||
description="Default Immich command templates",
|
||||
)
|
||||
session.add(config)
|
||||
await session.flush()
|
||||
else:
|
||||
config = system_configs[0]
|
||||
|
||||
# Upsert slots for each locale
|
||||
for locale in ("en", "ru"):
|
||||
slots = load_default_command_templates(locale, provider_type="immich")
|
||||
if not slots:
|
||||
continue
|
||||
for slot_name, template_text in slots.items():
|
||||
slot_result = await session.exec(
|
||||
select(CommandTemplateSlot).where(
|
||||
CommandTemplateSlot.config_id == config.id,
|
||||
CommandTemplateSlot.slot_name == slot_name,
|
||||
CommandTemplateSlot.locale == locale,
|
||||
)
|
||||
)
|
||||
existing = slot_result.first()
|
||||
if existing:
|
||||
existing.template = template_text
|
||||
session.add(existing)
|
||||
else:
|
||||
session.add(CommandTemplateSlot(
|
||||
config_id=config.id,
|
||||
slot_name=slot_name,
|
||||
locale=locale,
|
||||
template=template_text,
|
||||
))
|
||||
|
||||
# --- Seed Gitea default command templates ---
|
||||
gitea_cmd_result = await session.exec(
|
||||
select(CommandTemplateConfig).where(
|
||||
CommandTemplateConfig.user_id == 0,
|
||||
CommandTemplateConfig.provider_type == "gitea",
|
||||
)
|
||||
)
|
||||
gitea_cmd_configs = gitea_cmd_result.all()
|
||||
|
||||
if not gitea_cmd_configs:
|
||||
gitea_cmd_config = CommandTemplateConfig(
|
||||
user_id=0,
|
||||
provider_type="gitea",
|
||||
name="Default Gitea Commands",
|
||||
description="Default Gitea command templates",
|
||||
)
|
||||
session.add(gitea_cmd_config)
|
||||
await session.flush()
|
||||
else:
|
||||
gitea_cmd_config = gitea_cmd_configs[0]
|
||||
|
||||
for locale in ("en", "ru"):
|
||||
gitea_cmd_slots = load_default_command_templates(locale, provider_type="gitea")
|
||||
if not gitea_cmd_slots:
|
||||
continue
|
||||
for slot_name, template_text in gitea_cmd_slots.items():
|
||||
slot_result = await session.exec(
|
||||
select(CommandTemplateSlot).where(
|
||||
CommandTemplateSlot.config_id == gitea_cmd_config.id,
|
||||
CommandTemplateSlot.slot_name == slot_name,
|
||||
CommandTemplateSlot.locale == locale,
|
||||
)
|
||||
)
|
||||
existing = slot_result.first()
|
||||
if existing:
|
||||
existing.template = template_text
|
||||
session.add(existing)
|
||||
else:
|
||||
session.add(CommandTemplateSlot(
|
||||
config_id=gitea_cmd_config.id,
|
||||
slot_name=slot_name,
|
||||
locale=locale,
|
||||
template=template_text,
|
||||
))
|
||||
|
||||
await session.commit()
|
||||
|
||||
|
||||
async def _seed_default_tracking_configs():
|
||||
"""Seed system-owned default tracking configs for each provider type."""
|
||||
from sqlmodel import select
|
||||
from sqlmodel.ext.asyncio.session import AsyncSession
|
||||
from .database.engine import get_engine
|
||||
from .database.models import TrackingConfig
|
||||
|
||||
engine = get_engine()
|
||||
async with AsyncSession(engine) as session:
|
||||
# Find existing system-owned tracking configs
|
||||
result = await session.exec(
|
||||
select(TrackingConfig).where(TrackingConfig.user_id == 0)
|
||||
)
|
||||
existing = {c.provider_type: c for c in result.all()}
|
||||
|
||||
defaults = [
|
||||
{
|
||||
"provider_type": "gitea",
|
||||
"name": "Default Gitea",
|
||||
"track_push": True,
|
||||
"track_issue_opened": True,
|
||||
"track_issue_closed": True,
|
||||
"track_issue_commented": False,
|
||||
"track_pr_opened": True,
|
||||
"track_pr_closed": True,
|
||||
"track_pr_merged": True,
|
||||
"track_pr_commented": False,
|
||||
"track_release_published": True,
|
||||
},
|
||||
{
|
||||
"provider_type": "scheduler",
|
||||
"name": "Default Scheduler",
|
||||
"track_scheduled_message": True,
|
||||
},
|
||||
]
|
||||
|
||||
for cfg in defaults:
|
||||
ptype = cfg["provider_type"]
|
||||
if ptype in existing:
|
||||
continue
|
||||
session.add(TrackingConfig(user_id=0, **cfg))
|
||||
|
||||
await session.commit()
|
||||
|
||||
|
||||
async def _seed_default_command_configs():
|
||||
"""Seed system-owned default command configs for each provider type."""
|
||||
from sqlmodel import select
|
||||
from sqlmodel.ext.asyncio.session import AsyncSession
|
||||
from .database.engine import get_engine
|
||||
from .database.models import CommandConfig, CommandTemplateConfig
|
||||
|
||||
engine = get_engine()
|
||||
async with AsyncSession(engine) as session:
|
||||
# Find existing system-owned command configs
|
||||
result = await session.exec(
|
||||
select(CommandConfig).where(CommandConfig.user_id == 0)
|
||||
)
|
||||
existing = {c.provider_type: c for c in result.all()}
|
||||
|
||||
# Find system command template configs to link
|
||||
tmpl_result = await session.exec(
|
||||
select(CommandTemplateConfig).where(CommandTemplateConfig.user_id == 0)
|
||||
)
|
||||
tmpl_by_type = {t.provider_type: t.id for t in tmpl_result.all()}
|
||||
|
||||
defaults = [
|
||||
{
|
||||
"provider_type": "immich",
|
||||
"name": "Default Immich",
|
||||
"enabled_commands": [
|
||||
"help", "status", "albums", "events", "latest",
|
||||
"random", "favorites", "summary", "memory",
|
||||
],
|
||||
"response_mode": "media",
|
||||
"default_count": 5,
|
||||
"rate_limits": {"search": 30, "default": 10},
|
||||
},
|
||||
{
|
||||
"provider_type": "gitea",
|
||||
"name": "Default Gitea",
|
||||
"enabled_commands": [
|
||||
"help", "status", "repos", "issues", "prs", "commits",
|
||||
],
|
||||
"response_mode": "text",
|
||||
"default_count": 10,
|
||||
"rate_limits": {"api": 15, "default": 10},
|
||||
},
|
||||
]
|
||||
|
||||
for cfg in defaults:
|
||||
ptype = cfg["provider_type"]
|
||||
if ptype in existing:
|
||||
continue
|
||||
cmd_tmpl_id = tmpl_by_type.get(ptype)
|
||||
# Use raw SQL to handle legacy NOT NULL columns
|
||||
import json as _json2
|
||||
from sqlalchemy import text as _text2
|
||||
from datetime import datetime as _dt3, timezone as _tz3
|
||||
await session.execute(
|
||||
_text2(
|
||||
"INSERT INTO command_config "
|
||||
"(user_id, provider_type, name, icon, enabled_commands, locale, "
|
||||
"response_mode, default_count, rate_limits, command_template_config_id, created_at) "
|
||||
"VALUES (:uid, :pt, :name, :icon, :cmds, :locale, :rm, :dc, :rl, :ctid, :ca)"
|
||||
),
|
||||
{
|
||||
"uid": 0,
|
||||
"pt": ptype,
|
||||
"name": cfg["name"],
|
||||
"icon": "",
|
||||
"cmds": _json2.dumps(cfg["enabled_commands"]),
|
||||
"locale": "en",
|
||||
"rm": cfg["response_mode"],
|
||||
"dc": cfg["default_count"],
|
||||
"rl": _json2.dumps(cfg["rate_limits"]),
|
||||
"ctid": cmd_tmpl_id,
|
||||
"ca": _dt3.now(_tz3.utc).isoformat(),
|
||||
},
|
||||
)
|
||||
|
||||
await session.commit()
|
||||
|
||||
|
||||
def run():
|
||||
import uvicorn
|
||||
uvicorn.run(app, host="0.0.0.0", port=8420)
|
||||
|
||||
Reference in New Issue
Block a user