Files
ServerKit/backend/app/bootstrap/configuration.py
T
6e096fa286 Add real preview deployments and strengthen core boundaries (#184)
* fix(security): honour the failed-login time window (#180)

check_failed_logins computed a cutoff from since_hours and never used
it, so the Security -> Events "Failed login attempts (24h)" card and
its alert counted every failure still in /var/log/auth.log or
/var/log/secure: up to a week of SSH attempts under the usual weekly
log rotation.

Each matching line is now dated from its stamp, either RFC 3339
(rsyslog's high-precision format) or the traditional BSD syslog stamp
(no year, so a December line read in January is placed in the previous
year), and lines older than the cutoff are skipped. A line whose stamp
is not recognised is still counted rather than hidden. The log paths
move to SecurityService.AUTH_LOGS so the tests can point at a temp
file.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

* refactor: separate backend startup phases and guard ordering

* refactor: route file operations through capability-checked adapters

* chore: bump version to 1.11.25 [skip ci]

* refactor: split File Manager state and views with regression guards

* test: enforce core boundaries and document extension rules

* test: cover File Manager tree races and listing failures

* fix: preserve File Manager deep links on initial mount

* refactor: keep File Manager download operations in the action hook

* fix: start agent maintenance only in the guarded runtime phase

* test: capture nginx rendering contract before extraction

* refactor: separate nginx configuration rendering from host operations

* test: enforce pure nginx configuration boundaries

* fix: require provisioned runtime before advertising previews

* feat: expose value-free effective environment provenance through app access boundary

* feat: add effective environment inspector with browser regression checks

* feat: provision isolated generic previews with retryable resource cleanup

* feat: queue preview automation and preserve cleanup ownership through app deletion

* feat: add pinned preview deployment controls and queued lifecycle feedback

* fix: preserve container port semantics for previews of slot deployments

* fix: preserve search focus and ignore stale list responses

* feat: run approved assistant deployments through persistent recipe operations

* test: run search and assistant deployment regressions in CI

* fix: refresh preview API and controller boundary inventories

* docs: refresh controller migration count after preview extraction

* fix: bind file editor previews to their storage target

* chore: bump version to 1.11.26 [skip ci]

---------

Co-authored-by: Max Freedom Pollard <272618364+MaxFreedomPollard@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2026-10-24 15:31:39 -04:00

115 lines
5.5 KiB
Python

"""Flask extensions, database connections, and request middleware."""
from flask_cors import CORS
def configure_application(app, db, migrate, jwt, limiter):
# Trust the reverse proxy's forwarding headers to derive the real client IP
# (config-gated; default off). ProxyFix rewrites request.remote_addr from the
# rightmost TRUSTED_PROXY_HOPS entries of X-Forwarded-For — the hops our own
# proxies appended — so a client-forged leftmost value is ignored. Applied
# before the limiter and request handlers so every remote_addr consumer
# (flask-limiter's get_remote_address, get_client_ip(), audit logs) benefits.
if app.config.get('TRUST_PROXY_HEADERS'):
from werkzeug.middleware.proxy_fix import ProxyFix
hops = app.config.get('TRUSTED_PROXY_HOPS', 1)
app.wsgi_app = ProxyFix(app.wsgi_app, x_for=hops, x_proto=1, x_host=0, x_port=0)
# Initialize extensions
db.init_app(app)
from app.middleware.request_profiling import register_request_profiling
register_request_profiling(app, db)
migrate.init_app(app, db)
jwt.init_app(app)
# Storage backend comes from app.config's RATELIMIT_STORAGE_URI when set
# (see config.py); unset = in-memory, correct for the single-worker design.
limiter.init_app(app)
# SQLite concurrency tuning: the queue-bus consumers, job system and
# metrics collector share one database file with request handling, and in
# the default journal mode any writer locks the WHOLE file — readers
# included — producing sporadic "database is locked" errors under load.
# WAL lets readers coexist with a writer, a generous busy timeout absorbs
# the remaining writer-writer contention, and synchronous=NORMAL is the
# safe pairing for WAL. (journal_mode persists in the database file; the
# per-connection pragmas re-assert it for fresh files.)
if app.config['SQLALCHEMY_DATABASE_URI'].startswith('sqlite') and not app.config.get('TESTING'):
# (Skipped under TESTING: tests/conftest.py owns connection pragmas
# for the test process — journal_mode=MEMORY et al. — and two
# listeners fighting over journal_mode on the same file deadlocks.)
from sqlalchemy import event
with app.app_context():
@event.listens_for(db.engine, 'connect')
def _sqlite_tune(dbapi_connection, _record):
cursor = dbapi_connection.cursor()
cursor.execute('PRAGMA journal_mode=WAL')
cursor.execute('PRAGMA busy_timeout=30000')
cursor.execute('PRAGMA synchronous=NORMAL')
cursor.close()
# Build CORS origins. Start with static config/env, then append the
# persisted canonical domain from system settings so pointing an A record
# at the panel works without restarting to edit .env.
cors_origins = list(app.config['CORS_ORIGINS'])
try:
with app.app_context():
from app.services.settings_service import SettingsService
from app.utils.domain import canonical_origin
canonical_domain = SettingsService.get('canonical_domain', '') or ''
if canonical_domain:
https_enabled = SettingsService.get('canonical_https_enabled', False) or False
origin = canonical_origin(canonical_domain, https_enabled)
if origin not in cors_origins:
cors_origins.append(origin)
except Exception:
# Database may not exist yet during first install / migrations.
pass
CORS(
app,
origins=cors_origins,
supports_credentials=True,
allow_headers=[
'Content-Type', 'Authorization', 'X-Requested-With', 'X-API-Key',
'X-Request-ID',
],
expose_headers=['X-Request-ID'],
methods=['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS', 'PATCH'],
# Every panel request carries an Authorization header, which makes it a
# non-simple cross-origin request: without Access-Control-Max-Age the
# browser re-runs a preflight OPTIONS every few seconds (Chrome's
# default cache is 5s), so a cross-origin panel pays two round trips per
# call forever. One hour is the practical ceiling — Chrome caps the
# value at 2h, Firefox at 24h, and both take the smaller of the two.
max_age=3600,
)
# Assign one diagnostic ID at the outer API boundary. Downstream handlers,
# logs and jobs can reuse g.request_id without inventing independent IDs.
from app.middleware.request_id import register_request_id
register_request_id(app)
# Register security headers middleware
from app.middleware.security import register_security_headers
register_security_headers(app)
# Demo mode guard — config-gated (default off), blocks mutating API calls
from app.middleware.demo import init_demo_mode
init_demo_mode(app)
# Register API key authentication middleware
from app.middleware.api_key_auth import register_api_key_auth
register_api_key_auth(app)
# Register API analytics middleware
from app.middleware.api_analytics import register_api_analytics
register_api_analytics(app)
# Register fallback audit logging for authenticated mutating API requests
from app.middleware.audit import register_audit_fallback
register_audit_fallback(app)
# Update rate limiter with custom key function
from app.middleware.rate_limit import get_rate_limit_key, register_rate_limit_headers
limiter._key_func = get_rate_limit_key
register_rate_limit_headers(app)