flask icon indicating copy to clipboard operation
flask copied to clipboard

[rejected AI] Cannot disable request limits with None

Open Anil123-eng opened this issue 4 weeks ago • 2 comments

Description

When an application configures a request limit globally, assigning None to the corresponding request property does not disable that limit for an individual request.

For example, with MAX_CONTENT_LENGTH = 10, setting request.max_content_length = None still enforces the 10-byte limit.

Steps to reproduce

from flask import Flask, request

app = Flask(__name__)
app.config["MAX_CONTENT_LENGTH"] = 10

@app.post("/unlimited")
def unlimited():
    request.max_content_length = None
    return str(len(request.get_data()))

client = app.test_client()
response = client.post("/unlimited", data="A" * 20)

print(response.status_code)

Actual behavior

413 Request Entity Too Large

The explicit None assignment does not disable the configured limit.

A positive integer per-request override works, but an explicit None does not.

The same getter/setter pattern appears in max_form_memory_size and max_form_parts. Their effective values were also checked with None assignments, although the HTTP reproduction above specifically demonstrates max_content_length.

Expected behavior

Setting request.max_content_length = None should disable the Flask application-level limit for that request, allowing the 20-byte request body to be read.

Possible root cause

In src/flask/wrappers.py, the getter checks:

if self._max_content_length is not None:
    return self._max_content_length

If the stored value is None, it falls back to the application configuration.

The setter stores the supplied value directly:

self._max_content_length = value

This makes an unset override indistinguishable from an explicitly assigned None.

The same pattern appears in the other two request-limit properties.

Existing test coverage

tests/test_request.py checks application-level configuration and positive integer per-request overrides in test_limit_config, but does not test explicitly setting these properties to None after configuring finite application-level limits.

Environment

  • Flask: 3.2.0.dev0
  • Python: 3.14.7
  • OS: Windows 11
  • Branch: main
  • Commit: 49943b5a

Anil123-eng avatar Nov 06 '26 09:11 Anil123-eng

Here is the cause and a fix for this:

Cause

In src/flask/wrappers.py, Request._max_content_length, _max_form_memory_size, and _max_form_parts default to None. In their property getters, the check is:

if self._max_content_length is not None:
    return self._max_content_length

Setting request.max_content_length = None therefore causes the getter to treat it as unset and fall back to current_app.config["MAX_CONTENT_LENGTH"].

Fix

Using a private singleton sentinel (_sentinel = object(), matching the convention used elsewhere in Flask like ctx.py and sansio/scaffold.py) for the initial unset state allows explicit None to be recognized as an override that disables limits.

I implemented this along with regression tests and a changelog entry on branch: https://github.com/X6M-Work/flask/tree/fix-request-limits-none

Full diff comparison against main: https://github.com/pallets/flask/compare/main...X6M-Work:flask:fix-request-limits-none

(Since PR creation is restricted to collaborators on this repository, feel free to cherry-pick from the branch or let me know if you'd like an invite to submit the PR directly!)

X6M-Work avatar Nov 06 '26 13:11 X6M-Work

https://palletsprojects.com/contributing/llm-ai

davidism avatar Nov 06 '26 17:11 davidism