From 1f0094145afd627ee99ee58867cb7117108ee502 Mon Sep 17 00:00:00 2001 From: Simon van Lierde <90462640+simonvanlierde@users.noreply.github.com> Date: Sat, 14 Nov 2026 17:23:25 +0100 Subject: [PATCH] fix(seed): reject prod fixtures Fixes #502 Signed-off-by: Simon van Lierde <90462640+simonvanlierde@users.noreply.github.com> --- backend/scripts/seed/migrations_entrypoint.sh | 10 ++++ docs/src/content/docs/operations/configure.md | 3 + scripts/test_ops.sh | 57 +++++++++++++++++++ 3 files changed, 70 insertions(+) diff --git a/backend/scripts/seed/migrations_entrypoint.sh b/backend/scripts/seed/migrations_entrypoint.sh index 6496cae93..cd712eee9 100755 --- a/backend/scripts/seed/migrations_entrypoint.sh +++ b/backend/scripts/seed/migrations_entrypoint.sh @@ -16,6 +16,16 @@ SEED_DUMMY_DATA="${SEED_DUMMY_DATA:-false}" BULK_SEED_PRODUCTS="${BULK_SEED_PRODUCTS:-0}" DEBUG="${DEBUG:-false}" +if [ "${ENVIRONMENT:-}" = "prod" ] && [ "$(lc "$SEED_DUMMY_DATA")" = "true" ]; then + echo "error: SEED_DUMMY_DATA=true is not allowed when ENVIRONMENT=prod." >&2 + exit 1 +fi + +if [ "${ENVIRONMENT:-}" = "prod" ] && [ "$BULK_SEED_PRODUCTS" -gt 0 ] 2>/dev/null; then + echo "error: BULK_SEED_PRODUCTS > 0 is not allowed when ENVIRONMENT=prod." >&2 + exit 1 +fi + require_taxonomy_seed_deps() { if ! .venv/bin/python -c "import pandas, requests" >/dev/null 2>&1; then echo "Taxonomy seeding requires the optional seed-taxonomies dependency group." >&2 diff --git a/docs/src/content/docs/operations/configure.md b/docs/src/content/docs/operations/configure.md index 2f0e8ae63..2aae7338c 100644 --- a/docs/src/content/docs/operations/configure.md +++ b/docs/src/content/docs/operations/configure.md @@ -68,6 +68,9 @@ describes the topology these steps produce. - `MALWARE_SCAN_ENABLED`: `true` starts ClamAV with the stack, see step 2 of [Deploy](/operations/deploy/). + With `ENVIRONMENT=prod`, migrations reject `SEED_DUMMY_DATA=true` or enabled bulk fixture seeding + (`BULK_SEED_PRODUCTS` greater than zero) before running Alembic or Python. + Upload quotas: `MAX_UPLOAD_FILES_PER_USER` and `MAX_UPLOAD_BYTES_PER_USER_MB` cap `contributor` accounts. The `*_LAB_USER*` pair caps `lab` accounts and must not be lower. The quota counts existing rows, so on a host with existing data, raise the limits before the first start: an diff --git a/scripts/test_ops.sh b/scripts/test_ops.sh index 070b3d072..d91fed121 100755 --- a/scripts/test_ops.sh +++ b/scripts/test_ops.sh @@ -28,6 +28,63 @@ assert_eq() { printf 'FAIL: %s\n expected: %s\n actual: %s\n' "$label" "$expected" "$actual" } +# The migration entrypoint must reject production dummy seeding before touching +# the database, including DEBUG's Alembic status check. Both executables are stubs. +migration_seed_guard() { + local environment="$1" dummy="$2" debug="$3" expected_status="$4" dir status expected_calls expected_error="" + local bulk="${5-0}" bulk_enabled="${6:-false}" + dir="$(mktemp -d "${TMPDIR:-/tmp}/migration-seed.XXXXXX")" + mkdir -p "$dir/.venv/bin" + cat >"$dir/.venv/bin/alembic" <<'EOF' +#!/bin/sh +printf '%s %s\n' "${0##*/}" "$*" >> calls +EOF + cp "$dir/.venv/bin/alembic" "$dir/.venv/bin/python" + chmod +x "$dir/.venv/bin/alembic" "$dir/.venv/bin/python" + : >"$dir/calls" + ( + cd "$dir" || exit 1 + env -i PATH="$PATH" ENVIRONMENT="$environment" SEED_DUMMY_DATA="$dummy" DEBUG="$debug" BULK_SEED_PRODUCTS="$bulk" \ + sh "$OLDPWD/backend/scripts/seed/migrations_entrypoint.sh" + ) >"$dir/stdout" 2>"$dir/stderr" + status=$? + expected_calls="" + if [[ "$expected_status" == 0 ]]; then + [[ "$debug" != true ]] || expected_calls=$'alembic current\n' + expected_calls+=$'alembic upgrade head\npython -m scripts.maintenance.backfill_image_dimensions' + if [[ "$dummy" != false ]]; then + expected_calls+=$'\npython -m scripts.db.is_empty --quiet\npython -m scripts.seed.dummy_data' + fi + if [[ "$bulk_enabled" == true ]]; then + expected_calls+=$'\npython -m scripts.seed.bulk_seed --products '"$bulk" + fi + expected_calls+=$'\npython -m scripts.users.create_superuser' + else + expected_error="error: SEED_DUMMY_DATA=true is not allowed when ENVIRONMENT=prod." + if [[ "$dummy" == false ]]; then + expected_error="error: BULK_SEED_PRODUCTS > 0 is not allowed when ENVIRONMENT=prod." + fi + fi + assert_eq "migration seed ($environment/$dummy/$debug/bulk=$bulk): exit status" "$expected_status" "$status" + assert_eq "migration seed ($environment/$dummy/$debug/bulk=$bulk): executable calls" "$expected_calls" "$(cat "$dir/calls")" + assert_eq "migration seed ($environment/$dummy/$debug/bulk=$bulk): stderr" "$expected_error" "$(cat "$dir/stderr")" + rm -rf "$dir" +} + +migration_seed_guard prod true false 1 +migration_seed_guard prod TrUe true 1 +migration_seed_guard prod false true 0 +for seed_environment in dev staging testing ""; do + migration_seed_guard "$seed_environment" TrUe false 0 +done +for bulk_products in 1 0002 +3 " 4 "; do + migration_seed_guard prod false true 1 "$bulk_products" true + migration_seed_guard testing false true 0 "$bulk_products" true +done +for bulk_products in "" 0 000 +0 -1 false 1.5; do + migration_seed_guard prod false true 0 "$bulk_products" +done + # --------------------------------------------------------------------------- # `stack ENV up`: the scanning profile follows MALWARE_SCAN_ENABLED in .env, read # the way Compose reads it, and the recipe's environment must match the host's.