[2.x] Implement content negotiation for forum error handling - #4677
Merged
Conversation
getPreferredContentType() runs inside the forum error handler, so it must never throw. Two ways it could: - Mimeparse::bestMatch() returns null when the Accept header matches none of the offered types (e.g. "application/xml"), violating the ": string" return type with a TypeError. - A malformed media range (e.g. one without a subtype) makes bestMatch() emit a warning and throw UnexpectedValueException. Both now fall back to an empty string (treated as "not an API request"), so a hostile or unusual Accept header can't turn an error response into a second, fatal error. Add coverage: - RequestUtilTest: isApiRequest/isHtmlRequest/getPreferredContentType across JSON:API, JSON, */*, empty, browser, unrelated and malformed headers. - ContentNegotiationFormatterTest: API requests format as JSON, browser requests as HTML. - ErrorNegotiationTest: forum errors are negotiated through the full stack (JSON for */* — the #3850 regression — and HTML for browser requests).
Member
|
Pushed some hardening and test coverage on top of this ( Hardening
|
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.
Fixes #3850
Changes proposed in this pull request:
text/htmlinAcceptsheader/loginroute not returning JSON from forumHighMaintenanceModeHandlernot returning JSON for requests initiated by forum (usesisApiRequest, and our requests do not set the header)Reviewers should focus on:
I added the
bitworking/mimeparsepackage (last updated end of 2025) to parse more complexAcceptsheaders (eg. with quality priority). It looks like browsers basically always (wasn't able to find concrete evidence of this) havetext/htmlin their Accepts. While our XHR requests haveAccepts: */*.However, this did make
isHtmlRequesta weaker check that could be true at the same time asisApiRequest(due to the quality priorities). I think properly parsing with this package makes sense, however, perhapsisHtmlRequestshould just be the negation of the strongerisApiRequestcheck (made this change)? Additionally,isHtmlRequestis only used inLogInControllerfor the maintenance login error validaiton.I also thought that adding this check to the forum-wide error handler made more sense than just the login route alone.
Plus, I didn't think just slapping an
Acceptsheader to our JS requests was the proper solution.Screenshot


Necessity
Confirmed
Frontend changes: tested on a local Flarum installation.composer test).