﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
37555	Non UTF-8 form submissions crash CSRF error handling instead of returning a 400 response	Matt Tuchfarber	Matt Tuchfarber	"If a form request includes a valid CSRF token but passes a non UTF-8 charset for `application/x-www-form-urlencoded` content type, it will first attempt to return a BadRequest and return the 400 page, but the `@requires_csrf_token decorator` on the `bad_request` view (https://github.com/django/django/blob/5.2.12/django/views/defaults.py#L102-L103) causes another read of `request.POST` which then throws a 500 error instead. 

This only happens when the CSRF token is valid and checked, which the existing tests did not catch. 

Django only reproduction:


{{{
import django
from django.conf import settings

settings.configure(
    SECRET_KEY=""repro"",
    DEBUG=False,
    ROOT_URLCONF=__name__,
    ALLOWED_HOSTS=[""testserver""],
    MIDDLEWARE=[""django.middleware.csrf.CsrfViewMiddleware""],
)
django.setup()

from django.http import HttpResponse
from django.test import Client
from django.urls import path

urlpatterns = [path("""", lambda request: HttpResponse(""OK""))]

client = Client(enforce_csrf_checks=True, raise_request_exception=False)
client.cookies[settings.CSRF_COOKIE_NAME] = ""a"" * 32

response = client.post(
    ""/"",
    data=""a=b"",
    content_type=""application/x-www-form-urlencoded; charset=ascii"",
)
assert response.status_code == 400
}}}

This is a follow-up to https://code.djangoproject.com/ticket/34709 which introduced the change"	Uncategorized	assigned	Uncategorized	6.1	Normal		application/x-www-form-urlencoded,csrf	Matt Tuchfarber	Unreviewed	1	0	0	0	0	0
