Fix JWT auth error logging consuming entire multipart bodies#5409
Draft
Fix JWT auth error logging consuming entire multipart bodies#5409
Conversation
Co-authored-by: kevwan <1918356+kevwan@users.noreply.github.com>
Co-authored-by: kevwan <1918356+kevwan@users.noreply.github.com>
…quests Co-authored-by: kevwan <1918356+kevwan@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix performance issues in JWT middleware for file uploads
Fix JWT auth error logging consuming entire multipart bodies
Feb 7, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
JWT authentication failure logging calls
httputil.DumpRequest(r, true), which reads and dumps the entire request body. For multipart/form-data file uploads, this causes 5-30 second delays and memory spikes equal to file size.Changes
detailAuthLog: Skip body dump for multipart/form-data requestsisMultipartFormData: Helper to detect multipart Content-TypeBehavior: Headers-only logging for multipart, full dump for JSON/form-urlencoded (preserves debugging for non-file requests).
Original prompt
This section details on the original issue you should resolve
<issue_title>Bug Report: JWT Middleware reads entire multipart/form-data causing performance issues with file uploads</issue_title>
<issue_description>## Describe the bug
When using the built-in JWT authentication middleware (
jwt: Auth) with file upload endpoints, the middleware attempts to read the entiremultipart/form-datarequest body (including large file contents) to search for the JWT token. This causes severe performance issues: the entire file content is printed to console, validation takes 5-30 seconds, and memory usage spikes to the file size.To Reproduce
Steps to reproduce the behavior:
The code is
The error/issue is
Expected behavior
Authorization,X-Token) and query parameters (?token=xxx)multipart/form-dataScreenshots
Before (with built-in JWT middleware):
After (with custom middleware that skips form parsing):
Environments
More description
Root Cause Analysis
The JWT middleware in go-zero searches for tokens in multiple locations, including form parameters:
The Problem:
r.FormValue("token")internally callsr.ParseMultipartForm(), which parses the entiremultipart/form-datarequest body, including all file uploads (potentially hundreds of MBs), just to check if there's atokenfield in the form.Impact
This bug makes it impractical to use go-zero's JWT middleware with file upload endpoints in production:
Suggested Solutions
Option 1: Skip Form Parsing for multipart/form-data (Recommended)
...
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.