Fix fragment parsing for relative URI in RFC URI parser#36762
Open
daguimu wants to merge 1 commit intospring-projects:mainfrom
Open
Fix fragment parsing for relative URI in RFC URI parser#36762daguimu wants to merge 1 commit intospring-projects:mainfrom
daguimu wants to merge 1 commit intospring-projects:mainfrom
Conversation
When parsing a relative URI such as `path2#foo`, RfcUriParser's SCHEME_OR_PATH state advanced to FRAGMENT without moving the component index past the `#` character, so the captured fragment included the entire input (`path2#foo`) instead of just `foo`. As a result, `toUriString()` produced `path2#path2#foo`. Update the SCHEME_OR_PATH `#` transition to advance the component index to `i + 1`, matching the sibling `?` -> QUERY transition in the same state and every other `-> FRAGMENT` transition in the parser. URIs with a `/` in the path are unaffected because they leave SCHEME_OR_PATH for PATH on the first slash; the WhatWG parser already handled this case correctly. Closes spring-projectsgh-36759 Signed-off-by: daguimu <daguimu.geek@gmail.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.
Problem
UriComponentsBuilder.fromUriString("path2#foo")parses a relative URI with a fragment incorrectly:As a result,
toUriString()producespath2#path2#fooinstead ofpath2#foo.Root Cause
In
RfcUriParser, every state that transitions intoFRAGMENTadvances the component index toi + 1so the fragment starts after the#character — exceptSCHEME_OR_PATH, which callsadvanceTo(FRAGMENT)without the index argument. For a schemeless single-segment input the parser entersSCHEME_OR_PATHat index 0, the component index stays at 0, andcaptureFragmentIfNotEmpty()ends up returninguri.substring(0, length)— the entire input.URIs with
/in the path don't hit this code path: they leaveSCHEME_OR_PATHforPATHon the first/, andPATH's#case correctly passesi + 1. TheWhatWGparser is also unaffected (it produces the expected output for this input today).Fix
In
RfcUriParser.SCHEME_OR_PATH, changeadvanceTo(FRAGMENT)toadvanceTo(FRAGMENT, i + 1), mirroring the sibling?→QUERYtransition two lines above and every other→ FRAGMENTtransition in the parser.Tests Added
SCHEME_OR_PATH#advances component index past#fromUriStringRelativeUriWithFragment— verifiespath2#foosplits aspath = "path2",fragment = "foo"and round-trips viatoUriString()fromUriStringRelativeUriWithEmptyFragment— boundary: trailing#with no fragment yieldspath = "path2",fragment = nullBoth tests are parameterized over
ParserType(RFCandWHAT_WG). Without the fix they fail forRFCand pass forWHAT_WG(which already handled this case correctly); with the fix both parsers agree.The pre-existing assertion for
docs/guide/collections/designfaq.html#28infromUriString(ParserType)(which goes through thePATHstate, notSCHEME_OR_PATH) continues to pass, providing regression coverage for multi-segment relative URIs.Impact
spring-webonly — one-line behavioral change in a private parser, no public API surface added.ParserType.RFCparser./in the path (different parser states);ParserType.WHAT_WG.Closes gh-36759