Handle MIME type parameter names case-insensitively - #37192
Open
lArtiquel wants to merge 1 commit into
Open
Conversation
MIME type parameter names are case-insensitive, and MimeType already stores them in a LinkedCaseInsensitiveMap. Several code paths, however, still compared them with case-sensitive String.equals(). As a result, MimeType.hashCode() disagreed with MimeType.equals() for parameter names that differ only in case, breaking the equals/hashCode contract: text/plain;FOO=bar and text/plain;foo=bar are equal but hash differently, so one is not found in a hash-based collection holding the other. MimeType.compareTo() had the same blind spot for the charset parameter. MediaType was affected in two further ways: an out-of-range quality value escaped validation when spelled Q=, and removeQualityValue() left a Q= parameter in place. Signed-off-by: Artyom Tsvirko <36863599+lArtiquel@users.noreply.github.com>
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.
MimeTypekeeps parameters in aLinkedCaseInsensitiveMap, soequals()matches names case-insensitively.parametersHashCode()hashes the raw key, so it doesn't — which breaks theequals/hashCodecontract:The charset branch in
equals,hashCodeandcompareTokeys offPARAM_CHARSET.equals(key), so a parameter spelledCharsetskips it and is compared as an opaque string, even though the constructor already resolved it through the same case-insensitive map:MediaTypehas the same assumption in two more places: an out-of-range quality value escapes validation when spelledQ=1.1, andremoveQualityValue()leaves aQ=parameter in place.Fix is six lines —
equalsIgnoreCasein the four comparisons, and a normalized key before hashing.Two behavior changes, both bringing the uppercase spelling in line with the lowercase one:
Charset=UTF-8now equalsCharset=utf-8, andQ=1.1is now rejected at parse time. Four tests added, each failing before the change;spring-core,-web,-webmvc,-webflux,-messagingand-testsuites pass.