From f833ef9bda5217888ca0ce6d6512f3b030285c41 Mon Sep 17 00:00:00 2001 From: Asaf Shemesh Date: Fri, 6 Dec 2024 12:17:42 +0200 Subject: [PATCH] feat: import --- .gitignore | 14 ++++ README.md | 38 +++++++++ apache24-django.conf | 61 +++++++++++++++ app/__init__.py | 0 app/admin.py | 3 + app/apps.py | 6 ++ app/migrations/__init__.py | 0 app/models.py | 3 + app/tests.py | 3 + app/views.py | 42 ++++++++++ db.sqlite3 | 0 manage.py | 22 ++++++ pydantic_mod_wsgi_bug/__init__.py | 0 pydantic_mod_wsgi_bug/asgi.py | 16 ++++ pydantic_mod_wsgi_bug/settings.py | 123 ++++++++++++++++++++++++++++++ pydantic_mod_wsgi_bug/urls.py | 24 ++++++ pydantic_mod_wsgi_bug/wsgi.py | 18 +++++ requirements.txt | Bin 0 -> 154 bytes 18 files changed, 373 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 apache24-django.conf create mode 100644 app/__init__.py create mode 100644 app/admin.py create mode 100644 app/apps.py create mode 100644 app/migrations/__init__.py create mode 100644 app/models.py create mode 100644 app/tests.py create mode 100644 app/views.py create mode 100644 db.sqlite3 create mode 100644 manage.py create mode 100644 pydantic_mod_wsgi_bug/__init__.py create mode 100644 pydantic_mod_wsgi_bug/asgi.py create mode 100644 pydantic_mod_wsgi_bug/settings.py create mode 100644 pydantic_mod_wsgi_bug/urls.py create mode 100644 pydantic_mod_wsgi_bug/wsgi.py create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..423af85 --- /dev/null +++ b/.gitignore @@ -0,0 +1,14 @@ + +*.log + +# virtualenv +.venv +venv/ +ENV/ +.vscode +.idea/ + +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..6ed325f --- /dev/null +++ b/README.md @@ -0,0 +1,38 @@ +# pydantic mod-wsgi bug + +This repository is a minimal reproduction of a bug that occurs when using some pydantic features with mod-wsgi. + +This was tested on a Windows machine, with `mod-wsgi==4.9.2`, Python 3.9.9, apache 2.4.57.0 + +Also tested with Python 3.11 and bug still exists. Tried also `mod_wsgi==5.0.2`. + +## Installation +- Get a Windows apache release from https://www.apachelounge.com/download/ +- Place your installation at C:/Apache24 + +Then, install mod-wsgi (Requires VS build tools): +```bash +pip install mod-wsgi==4.9.2 +``` + +```bash +pip install -r requirements.txt +``` + +### Configure apache +- Open `apache24-django.conf` and replace APP_ROOT to the absolute path of the wsgi.py folder, e.g: +`DEFINE WSGI_ROOT "C:/your_workspace/pydantic_mod_wsgi_bug/pydantic_mod_wsgi_bug"` +- If you don't use python 3.9 run `mod_wsgi-express module-config` and replace the three lines in `apache24-django.conf` with the output of the command. + +- Start apache server + +## Run +Try the endpoints with django dev server, where they will all work: +`python manage.py runserver` +- Browse to `http://localhost:8000/docs` + + +Then, try the endpoints with apache server, where some of the endpoints will fail: +- Browser to `http://localhost/docs` +The endpoints with string strip and string constraints, will hang, and will actually cause apache to crash. +- (Check apache error.log, you will see the server has crashed and restarted) \ No newline at end of file diff --git a/apache24-django.conf b/apache24-django.conf new file mode 100644 index 0000000..a353e81 --- /dev/null +++ b/apache24-django.conf @@ -0,0 +1,61 @@ +############################### +# For Apache 2.4.x +############################### + + +## Output from mod_wsgi-express module-config: +# If you don't use python3.9, replace with the output you got from running 'mod_wsgi-express module-config' +LoadFile "C:/Python39/python39.dll" +LoadModule wsgi_module "C:/Python39/lib/site-packages/mod_wsgi/server/mod_wsgi.cp39-win_amd64.pyd" +WSGIPythonHome "C:/Python39" +## end of output from mod_wsgi-express module-config + + +WSGIApplicationGroup %{GLOBAL} +WSGIPassAuthorization On + + +# This reduce the default 'Server' header sent by apache, to only send Server: Apache +ServerName localhost +ServerTokens ProductOnly +ServerSignature Off + + +# Replace here: Set this variable to the folder where wsgi.py is located +DEFINE WSGI_ROOT "C:/Asaf/pydantic_mod_wsgi_bug/pydantic_mod_wsgi_bug" + + + + + Require all granted + + + + WSGIScriptAlias / "${WSGI_ROOT}/wsgi.py" + + + +# Compression of file types, for faster download. requires mod_deflate + + AddOutputFilterByType DEFLATE text/plain text/html text/css application/javascript application/x-javascript + + + + +LoadModule evasive_module modules/mod_evasive.so + + DOSEnabled true + DOSHashTableSize 3097 + DOSPageCount 4 + DOSSiteCount 50 + DOSPageInterval 1 + DOSSiteInterval 1 + DOSBlockingPeriod 10 + + + + + RewriteEngine off + RewriteCond %{HTTPS} off + RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} + diff --git a/app/__init__.py b/app/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/admin.py b/app/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/app/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/app/apps.py b/app/apps.py new file mode 100644 index 0000000..bcfe39b --- /dev/null +++ b/app/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class AppConfig(AppConfig): + default_auto_field = "django.db.models.BigAutoField" + name = "app" diff --git a/app/migrations/__init__.py b/app/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/models.py b/app/models.py new file mode 100644 index 0000000..71a8362 --- /dev/null +++ b/app/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/app/tests.py b/app/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/app/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/app/views.py b/app/views.py new file mode 100644 index 0000000..ac10201 --- /dev/null +++ b/app/views.py @@ -0,0 +1,42 @@ +from typing import Annotated + +from ninja import NinjaAPI, Form +from pydantic import ConfigDict, BaseModel, StringConstraints + +router = NinjaAPI() + + +class PayloadString(BaseModel): + # This works with apache + string: str + + +class PayloadStringStrip(BaseModel): + # This will fail with apache + string: str + + model_config = ConfigDict(str_strip_whitespace=True) + + +class PayLoadStringConstraints(BaseModel): + number: Annotated[str, StringConstraints(pattern=r"^0\d\d$", strip_whitespace=True)] + + + +@router.post("/payload_string") +def payload_string(request, payload: Form[PayloadString]): + # Works with no issues + return {"status": "OK", "payload": payload.model_dump()} + + +@router.post("/payload_string_strip") +def payload_string_strip(request, payload: Form[PayloadStringStrip]): + # Won't work in apache + return {"status": "OK", "payload": payload.model_dump()} + + + +@router.post("/payload_string_constraints") +def payload_string_constraints(request, payload: Form[PayLoadStringConstraints]): + # This works only if you input a non-valid number, e.g '11'. For a valid input like '123', apache will freeze + return {"status": "OK", "payload": payload.model_dump()} diff --git a/db.sqlite3 b/db.sqlite3 new file mode 100644 index 0000000..e69de29 diff --git a/manage.py b/manage.py new file mode 100644 index 0000000..cf5bc44 --- /dev/null +++ b/manage.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python +"""Django's command-line utility for administrative tasks.""" +import os +import sys + + +def main(): + """Run administrative tasks.""" + os.environ.setdefault("DJANGO_SETTINGS_MODULE", "pydantic_mod_wsgi_bug.settings") + try: + from django.core.management import execute_from_command_line + except ImportError as exc: + raise ImportError( + "Couldn't import Django. Are you sure it's installed and " + "available on your PYTHONPATH environment variable? Did you " + "forget to activate a virtual environment?" + ) from exc + execute_from_command_line(sys.argv) + + +if __name__ == "__main__": + main() diff --git a/pydantic_mod_wsgi_bug/__init__.py b/pydantic_mod_wsgi_bug/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pydantic_mod_wsgi_bug/asgi.py b/pydantic_mod_wsgi_bug/asgi.py new file mode 100644 index 0000000..e661a5d --- /dev/null +++ b/pydantic_mod_wsgi_bug/asgi.py @@ -0,0 +1,16 @@ +""" +ASGI config for pydantic_mod_wsgi_bug project. + +It exposes the ASGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/4.1/howto/deployment/asgi/ +""" + +import os + +from django.core.asgi import get_asgi_application + +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "pydantic_mod_wsgi_bug.settings") + +application = get_asgi_application() diff --git a/pydantic_mod_wsgi_bug/settings.py b/pydantic_mod_wsgi_bug/settings.py new file mode 100644 index 0000000..d90b979 --- /dev/null +++ b/pydantic_mod_wsgi_bug/settings.py @@ -0,0 +1,123 @@ +""" +Django settings for pydantic_mod_wsgi_bug project. + +Generated by 'django-admin startproject' using Django 4.1.12. + +For more information on this file, see +https://docs.djangoproject.com/en/4.1/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/4.1/ref/settings/ +""" + +from pathlib import Path + +# Build paths inside the project like this: BASE_DIR / 'subdir'. +BASE_DIR = Path(__file__).resolve().parent.parent + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/4.1/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = "django-insecure-*yu4s3b=@y^&2$hlm#(xmt4h4+k3kvx$lb(4w&&gdp7gt^1=dj" + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ALLOWED_HOSTS = [] + + +# Application definition + +INSTALLED_APPS = [ + "django.contrib.admin", + "django.contrib.auth", + "django.contrib.contenttypes", + "django.contrib.sessions", + "django.contrib.messages", + "django.contrib.staticfiles", +] + +MIDDLEWARE = [ + "django.middleware.security.SecurityMiddleware", + "django.contrib.sessions.middleware.SessionMiddleware", + "django.middleware.common.CommonMiddleware", + "django.middleware.csrf.CsrfViewMiddleware", + "django.contrib.auth.middleware.AuthenticationMiddleware", + "django.contrib.messages.middleware.MessageMiddleware", + "django.middleware.clickjacking.XFrameOptionsMiddleware", +] + +ROOT_URLCONF = "pydantic_mod_wsgi_bug.urls" + +TEMPLATES = [ + { + "BACKEND": "django.template.backends.django.DjangoTemplates", + "DIRS": [], + "APP_DIRS": True, + "OPTIONS": { + "context_processors": [ + "django.template.context_processors.debug", + "django.template.context_processors.request", + "django.contrib.auth.context_processors.auth", + "django.contrib.messages.context_processors.messages", + ], + }, + }, +] + +WSGI_APPLICATION = "pydantic_mod_wsgi_bug.wsgi.application" + + +# Database +# https://docs.djangoproject.com/en/4.1/ref/settings/#databases + +DATABASES = { + "default": { + "ENGINE": "django.db.backends.sqlite3", + "NAME": BASE_DIR / "db.sqlite3", + } +} + + +# Password validation +# https://docs.djangoproject.com/en/4.1/ref/settings/#auth-password-validators + +AUTH_PASSWORD_VALIDATORS = [ + { + "NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator", + }, + { + "NAME": "django.contrib.auth.password_validation.MinimumLengthValidator", + }, + { + "NAME": "django.contrib.auth.password_validation.CommonPasswordValidator", + }, + { + "NAME": "django.contrib.auth.password_validation.NumericPasswordValidator", + }, +] + + +# Internationalization +# https://docs.djangoproject.com/en/4.1/topics/i18n/ + +LANGUAGE_CODE = "en-us" + +TIME_ZONE = "UTC" + +USE_I18N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/4.1/howto/static-files/ + +STATIC_URL = "static/" + +# Default primary key field type +# https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field + +DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField" diff --git a/pydantic_mod_wsgi_bug/urls.py b/pydantic_mod_wsgi_bug/urls.py new file mode 100644 index 0000000..2cf8a2c --- /dev/null +++ b/pydantic_mod_wsgi_bug/urls.py @@ -0,0 +1,24 @@ +"""pydantic_mod_wsgi_bug URL Configuration + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/4.1/topics/http/urls/ +Examples: +Function views + 1. Add an import: from my_app import views + 2. Add a URL to urlpatterns: path('', views.home, name='home') +Class-based views + 1. Add an import: from other_app.views import Home + 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') +Including another URLconf + 1. Import the include() function: from django.urls import include, path + 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) +""" + +from django.contrib import admin +from django.urls import path + +from app import views + +urlpatterns = [ + path("", views.router.urls), +] diff --git a/pydantic_mod_wsgi_bug/wsgi.py b/pydantic_mod_wsgi_bug/wsgi.py new file mode 100644 index 0000000..e8ce333 --- /dev/null +++ b/pydantic_mod_wsgi_bug/wsgi.py @@ -0,0 +1,18 @@ +""" +WSGI config for pydantic_mod_wsgi_bug project. + +It exposes the WSGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/4.1/howto/deployment/wsgi/ +""" + +import os +import sys + +from django.core.wsgi import get_wsgi_application + +sys.path.append(os.path.dirname(os.path.dirname(__file__))) +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "pydantic_mod_wsgi_bug.settings") + +application = get_wsgi_application() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000000000000000000000000000000000000..a947891e9a55b698caf502126ff90f841b88d7da GIT binary patch literal 154 zcmZ|I!3sc76olb_tvm`L#m3HiNJuPP$;QiL&Q-F}X;$+!=Q?XX n`cE54O^SJwrV_7Bt{l>~v`ckfWNM3F(MUNqhS#Q)($D7`|C<;E literal 0 HcmV?d00001