Skip to content

Fix/python apis length validation - #933

Open
onkar0127 wants to merge 4 commits into
Userunknown84:mainfrom
onkar0127:fix/python-apis-length-validation
Open

Fix/python apis length validation#933
onkar0127 wants to merge 4 commits into
Userunknown84:mainfrom
onkar0127:fix/python-apis-length-validation

Conversation

@onkar0127

@onkar0127 onkar0127 commented Jul 19, 2026

Copy link
Copy Markdown
Contributor

RESOLVES #839

PR Description:

Problem Summary

While the Node.js API gateway strictly enforces a maximum payload length of 5,000 characters on incoming prediction requests, the Python ML APIs (both the Flask and FastAPI servers) had no string length validation checks. This exposed the services to potential denial-of-service (DoS) and Out-of-Memory (OOM) attacks if clients hit the Python services directly.

Additionally, two pre-existing regressions blocked the Python backend test suite from executing cleanly:

  1. Flask IMAP Routes Regression: A previous merge regression had removed multiple /imap/... route handlers from backend/api.py, causing route-matching failures on incoming internal requests.
  2. Bulk Upload Stream Compatibility: The use of io.TextIOWrapper on the raw request file stream in bulk_predict.py triggered AttributeError: 'SpooledTemporaryFile' object has no attribute 'readable' crashes when uploading files in Python 3.10+ environments.

Key Changes

1. Payload Length Validation

  • Flask API (backend/api.py):
    • Set the default global MAX_MESSAGE_LENGTH to 5000 (down from 10000).
    • Removed a duplicate early-return block inside the /predict route handler which was bypassing the length validation and other processing pipeline components.
  • FastAPI API (fastapi_backend/main.py):
    • Integrated Pydantic Field constraints on the PredictIn schema to restrict the string length of the text field to a maximum of 5,000 characters:
      class PredictIn(BaseModel):
          text: str = Field(..., max_length=5000, description="Input query string")
          type: str

2. SpooledTemporaryFile Bulk Predict Fix

  • File: backend/bulk_predict.py
  • Fix: Replaced the incompatible io.TextIOWrapper stream wrapper with memory-buffered reading and standard io.StringIO wrapping.

3. Restored Missing IMAP Routes

  • File: backend/api.py
  • Fix: Rearmed and restored the missing IMAP route handlers (/imap/status, /imap/schedule, /imap/disconnect, /imap/scan-now, /imap/scan-results) with proper request decorators.

Verification and Testing

  • Ran the entire backend python test suite. All 129 tests now pass successfully:
    ====================== 129 passed, 4 warnings in 20.74s =======================
    

@vercel

vercel Bot commented Jul 19, 2026

Copy link
Copy Markdown

@onkar0127 is attempting to deploy a commit to the Aditya Sharma's projects Team on Vercel.

A member of the Team first needs to authorize it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

security: Missing input text length validation in Python ML APIs allows potential `memory exhaustion (DoS)

1 participant