Skip to content

Mitigate Vercel 413 upload failures with strict payload limits, validation, and user-facing error handling#2

Merged
gzhang33 merged 5 commits into
masterfrom
copilot/fix-file-upload-413-error
May 9, 2026
Merged

Mitigate Vercel 413 upload failures with strict payload limits, validation, and user-facing error handling#2
gzhang33 merged 5 commits into
masterfrom
copilot/fix-file-upload-413-error

Conversation

Copilot AI commented May 9, 2026

Copy link
Copy Markdown
Contributor

This PR addresses 413 FUNCTION_PAYLOAD_TOO_LARGE on Vercel by hardening the current server-upload path to block oversize payloads early, enforce file safety constraints, and return clear UI feedback instead of opaque failures.
Given the existing Flask/Vercel deployment shape, the changes focus on minimal-risk production mitigation while preserving current conversion flow.

  • Upload-path hard limits (server-side)

    • Set request body cap below Vercel’s 4.5MB boundary (MAX_CONTENT_LENGTH = 4MB safety margin).
    • Enforced aggregate upload-size guard during /upload processing.
    • Added one-shot session error channel for upload-form feedback.
  • Validation and safety policy

    • Introduced centralized payload validation (validate_upload_payload) for:
      • file extension whitelist (.jpg/.jpeg/.png)
      • MIME whitelist (image/jpeg, image/png)
      • per-file size limit
      • decodable raster-image check
    • Kept existing path safety controls (safe_subdir, path-under-root checks) intact.
  • 413 + payload-too-large UX fallback

    • Added RequestEntityTooLarge handler that redirects to upload page with concise user-facing message.
    • Surfaced form-level error banner on the upload page for 413 and client-precheck failures.
  • Client-side preflight and state recovery

    • Added reusable front-end validation helpers for type/size/total-size checks before submit.
    • Added bounded multi-error display and total-size short-circuiting.
    • Added browser-compat fallback when DataTransfer assignment is unavailable, with explicit recovery guidance.
  • Critical regression coverage

    • Added focused tests for invalid type rejection, oversize file rejection, and 413 redirect/error visibility path.
# src/imgtowebp/web/app.py
app.config["MAX_CONTENT_LENGTH"] = 4 * 1024 * 1024

@app.errorhandler(RequestEntityTooLarge)
def handle_request_too_large(_error: RequestEntityTooLarge):
    session[SESSION_FORM_ERROR_ONCE_KEY] = (
        f"Upload is too large. Keep total upload size under {limit_label(UPLOAD_TOTAL_LIMIT_BYTES)}."
    )
    return redirect(url_for("index"), code=303)

@vercel

vercel Bot commented May 9, 2026

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
imgtowebp Ready Ready Preview, Comment May 9, 2026 4:24pm

@gzhang33 gzhang33 marked this pull request as ready for review May 9, 2026 16:44
Copilot AI review requested due to automatic review settings May 9, 2026 16:44
@gzhang33 gzhang33 merged commit a77fde7 into master May 9, 2026
5 checks passed
@gzhang33 gzhang33 deleted the copilot/fix-file-upload-413-error branch May 9, 2026 16:44
Copilot AI review requested due to automatic review settings May 9, 2026 17:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants