Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UriComponentsBuilder handles invalid port numbers correctly #26905

Conversation

glqdlt
Copy link
Contributor

@glqdlt glqdlt commented May 7, 2021

Hello.
Fixed a bug in the'UriComponentBuilder' class.
The bug occurs in'UriComponentBuilder#fromUriString()'.
A bug occurs when the port of the argument is not a number.

This issue occurs with the 'PORT_PATTERN' regular expression.
If port is not a number, the regular expression is not captured.
This is a good regex, but it causes problems.

You can check the issue in the test code below.

UriComponents stringPort = UriComponentsBuilder.fromUriString("http://localhost:port/path").build();
assertThat(stringPort.getScheme()).isEqualTo("http");
assertThat(stringPort.getHost()).isEqualTo("localhost");
assertThat(stringPort.getPath()).isEqualTo("/path"); 
// expected: "/path", but was "port/path"

'PORT' that is not captured is captured by'PATH_PATTERN'.
private static final String PATH_PATTERN = "([^?#]*)";

I have fixed this bug.
Thank you.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label May 7, 2021
@rstoyanchev rstoyanchev self-assigned this May 7, 2021
@rstoyanchev rstoyanchev added in: web Issues in web modules (web, webmvc, webflux, websocket) type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels May 7, 2021
@rstoyanchev rstoyanchev added this to the 5.3.7 milestone May 7, 2021
@glqdlt glqdlt changed the title Fix bug when 'UriComponentsBuilder#fromUriString()' port was not numer. Fix bug when 'UriComponentsBuilder#fromUriString()' port was not number. May 7, 2021
@glqdlt glqdlt force-pushed the fix/UriComponentsBuilderNonNumberPort branch from 4e11755 to fd6659e Compare May 7, 2021 15:34
@glqdlt
Copy link
Contributor Author

glqdlt commented May 7, 2021

Sorry. I made a mistake.
Corrected a typo in the PR title and commit message.

(before) Fix bug when 'UriComponentsBuilder#fromUriString()' port was not numer.
(after) Fix bug when 'UriComponentsBuilder#fromUriString()' port was not number.

@rstoyanchev rstoyanchev changed the title Fix bug when 'UriComponentsBuilder#fromUriString()' port was not number. UriComponentsBuilder handles invalid port numbers correctly May 7, 2021
@glqdlt
Copy link
Contributor Author

glqdlt commented May 10, 2021

Hello @rstoyanchev
Sorry, there is a serious bug in this pull request.
i fixed a bug (#26918), Could you please review it?
Thank you.

@rstoyanchev
Copy link
Contributor

rstoyanchev commented May 10, 2021

This has broken a Boot test case with a URL such as "http://localhost:52567?trace=false&message=false".

@rstoyanchev
Copy link
Contributor

@glqdlt my apologies, I saw your comment but missed the fact you had opened a separate PR. For future reference, simply push your changes to the same branch in order to update the PR.

@glqdlt
Copy link
Contributor Author

glqdlt commented May 11, 2021

@rstoyanchev thank you very much for the comments.

bclozel added a commit that referenced this pull request Jun 8, 2021
This commit revisits the recently updated `PORT_PATTERN` in
`UriComponentsBuilder`. The fix introduced in gh-26905 fails with
ambiguous URL patterns, especially when the port and path parts of the
pattern are hard to differentiate, for example
"https://localhost:{port}{path}".

This commit reinstates the previous behavior without undoing the actual
fix. The only limitation introduced here is the fact that only a single
pattern variable is allowed for the port pattern part.

Fixes gh-27039
Zoran0104 pushed a commit to Zoran0104/spring-framework that referenced this pull request Aug 20, 2021
Zoran0104 pushed a commit to Zoran0104/spring-framework that referenced this pull request Aug 20, 2021
Zoran0104 pushed a commit to Zoran0104/spring-framework that referenced this pull request Aug 20, 2021
The pattern was changed in 65797d0
to check for characters up until "/", instead of for digits, but now
also checks up until "?" and "#".

Closes spring-projectsgh-26905
Zoran0104 pushed a commit to Zoran0104/spring-framework that referenced this pull request Aug 20, 2021
This commit revisits the recently updated `PORT_PATTERN` in
`UriComponentsBuilder`. The fix introduced in spring-projectsgh-26905 fails with
ambiguous URL patterns, especially when the port and path parts of the
pattern are hard to differentiate, for example
"https://localhost:{port}{path}".

This commit reinstates the previous behavior without undoing the actual
fix. The only limitation introduced here is the fact that only a single
pattern variable is allowed for the port pattern part.

Fixes spring-projectsgh-27039
lxbzmy pushed a commit to lxbzmy/spring-framework that referenced this pull request Mar 26, 2022
lxbzmy pushed a commit to lxbzmy/spring-framework that referenced this pull request Mar 26, 2022
lxbzmy pushed a commit to lxbzmy/spring-framework that referenced this pull request Mar 26, 2022
The pattern was changed in 65797d0
to check for characters up until "/", instead of for digits, but now
also checks up until "?" and "#".

Closes spring-projectsgh-26905
lxbzmy pushed a commit to lxbzmy/spring-framework that referenced this pull request Mar 26, 2022
This commit revisits the recently updated `PORT_PATTERN` in
`UriComponentsBuilder`. The fix introduced in spring-projectsgh-26905 fails with
ambiguous URL patterns, especially when the port and path parts of the
pattern are hard to differentiate, for example
"https://localhost:{port}{path}".

This commit reinstates the previous behavior without undoing the actual
fix. The only limitation introduced here is the fact that only a single
pattern variable is allowed for the port pattern part.

Fixes spring-projectsgh-27039
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: bug A general bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants