z1456651215-ctrl opened a new issue, #16547:
URL: https://github.com/apache/dubbo/issues/16547

   ### Pre-check
   
   - [x] I am sure that all the content I provide is in English.
   
   
   ### Search before asking
   
   - [x] I had searched in the 
[issues](https://github.com/apache/dubbo/issues?q=is%3Aissue) and found no 
similar issues.
   
   
   ### Apache Dubbo Component
   
   Java SDK (apache/dubbo)
   
   ### Dubbo Version
   
   Apache Dubbo Java SDK, Triple REST / servlet integration.
   
   The incorrect port-parsing expression is confirmed by source inspection in 
the official dubbo-3.3.5 tag and in the 3.3 branch at commit 
843b98b3a1c70134e965ab04efc2de668238e0b2.
   
   The exact Dubbo artifact version used by the reporting application, JDK 
version, operating system, and Spring version have not yet been provided. The 
source inspection above is not a claim that an unmodified upstream application 
was executed for those versions.
   
   ### Steps to reproduce this issue
   
   Calling serverHttpRequest.getURI() fails with 
java.lang.NumberFormatException when the underlying servlet request is backed 
by Dubbo's ServletHttpRequestAdapter and the Host header contains an explicit 
port.
   
   1. Use Dubbo Triple REST with the servlet request adapter.
   2. Send an HTTP/1.1 request to an exposed endpoint, for example GET 
/example, with Host: 127.0.0.1:8013. Do not supply X-Forwarded-Port, so the 
Host parsing branch is used.
   3. Wrap the adapted HttpServletRequest with Spring's 
ServletServerHttpRequest (or use the existing Spring integration) and call 
getURI():
   
   ```java
   ServerHttpRequest serverHttpRequest = new 
ServletServerHttpRequest(servletRequest);
   URI uri = serverHttpRequest.getURI();
   ```
   
   Actual result: java.lang.NumberFormatException.
   
   Debugger values at the failing port conversion:
   
   ```text
   host = "127.0.0.1:8013"
   index = host.lastIndexOf(':') = 9
   host.substring(0, index) = "127.0.0.1"
   Integer.parseInt("127.0.0.1") -> NumberFormatException
   ```
   
   The parsing error is also directly visible without Spring by invoking 
getServerPort() or getRequestURL() on the servlet adapter for the same request.
   
   A standalone reproduction project and a complete application stack trace are 
not attached; the values above are from debugging the reported failure.
   
   ### What you expected to happen
   
   getServerPort() should return 8013 for Host: 127.0.0.1:8013. getRequestURL() 
and ServerHttpRequest.getURI() should build a valid URL/URI, such as 
http://127.0.0.1:8013/example, without throwing NumberFormatException for a 
valid Host header.
   
   ### Anything else
   
   Root cause in upstream code
   
   DefaultHttpRequest.serverPort() currently uses:
   
   ```java
   int index = host.lastIndexOf(':');
   return index == -1 ? -1 : Integer.parseInt(host.substring(0, index));
   ```
   
   The substring selects the host portion instead of the port portion. For the 
valid authority 127.0.0.1:8013, this attempts to parse 127.0.0.1 as an integer. 
A hostname such as localhost:8013 has the same issue.
   
   Source in the dubbo-3.3.5 tag:
   
https://github.com/apache/dubbo/blob/dubbo-3.3.5/dubbo-remoting/dubbo-remoting-http12/src/main/java/org/apache/dubbo/remoting/http12/message/DefaultHttpRequest.java#L319-L335
   
   The same expression is present on the 3.3 branch at commit 
843b98b3a1c70134e965ab04efc2de668238e0b2:
   
https://github.com/apache/dubbo/blob/843b98b3a1c70134e965ab04efc2de668238e0b2/dubbo-remoting/dubbo-remoting-http12/src/main/java/org/apache/dubbo/remoting/http12/message/DefaultHttpRequest.java#L319-L335
   
   The upstream source explains the call path:
   Spring ServletServerHttpRequest.getURI() -> 
ServletHttpRequestAdapter.getRequestURL() -> 
ServletHttpRequestAdapter.getServerPort() -> DefaultHttpRequest.serverPort().
   This is a source-derived call path, not a copied runtime stack trace.
   
   Suggested fix: for a host with an explicit port, parse the substring after 
the separator (host.substring(index + 1)), with appropriate authority 
validation. Regression coverage should include IPv4 and hostname authorities 
with explicit ports, bracketed IPv6 authorities with and without an explicit 
port, and X-Forwarded-Port handling.
   
   ### Do you have a (mini) reproduction demo?
   
   - [ ] Yes, I have a minimal reproduction demo to help resolve this issue 
more effectively!
   
   ### Are you willing to submit a pull request to fix on your own?
   
   - [ ] Yes I am willing to submit a pull request on my own!
   
   ### Code of Conduct
   
   - [x] I agree to follow this project's [Code of 
Conduct](https://www.apache.org/foundation/policies/conduct)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to