CCCLL opened a new issue, #16563:
URL: https://github.com/apache/dubbo/issues/16563

   ### Apache Dubbo Component
   
   Java SDK (apache/dubbo)
   
   ### Dubbo Version
   
   Dubbo Java 3.3.2 to 3.3.6 and the `3.3` branch (1afcdf7883), OpenJDK 21, 
macOS. `dubbo-triple-servlet` with `javax.servlet-api` 4.0.1.
   
   ### Steps to reproduce this issue
   
   A Triple REST service whose methods redirect the way servlet code usually 
does:
   
   ```java
   @Mapping("/")
   public interface Api {
       @Mapping("/redirect")
       void redirect(HttpServletResponse response) throws Exception;
   
       @Mapping("/context")
       void context(HttpServletRequest request, HttpServletResponse response) 
throws Exception;
   
       @Mapping("/security")
       void security(HttpServletRequest request, HttpServletResponse response) 
throws Exception;
   }
   
   public class Impl implements Api {
       public void redirect(HttpServletResponse response) throws Exception {
           
response.sendRedirect(response.encodeRedirectURL("/login?next=/orders"));
       }
   
       public void context(HttpServletRequest request, HttpServletResponse 
response) throws Exception {
           response.sendRedirect(request.getContextPath() + 
"/login?next=/orders");
       }
   
       public void security(HttpServletRequest request, HttpServletResponse 
response) throws Exception {
           // Spring Security 5.8: calls encodeRedirectURL(getContextPath() + 
url) and then sendRedirect
           new DefaultRedirectStrategy().sendRedirect(request, response, 
"/login?next=/orders");
       }
   }
   ```
   
   | Request | `Location` with Dubbo 3.3.6 | `Location` with the same code in 
Tomcat 9.0.121 |
   |---|---|---|
   | `/redirect` | `%2Flogin%3Fnext%3D%2Forders` | `/login?next=/orders` |
   | `/context` | `//login?next=/orders` | `/login?next=/orders` |
   | `/security` | `%2F%2Flogin%3Fnext%3D%2Forders` | `/login?next=/orders` |
   
   `response.encodeURL("/login?next=/a b")` returns `%2Flogin%3Fnext%3D%2Fa+b` 
(Tomcat: `/login?next=/a b`). In 3.3.0 and 3.3.1 `sendRedirect` did not set 
`Location` at all (#14772, fixed by #14811).
   
   ### What you expected to happen
   
   `Location: /login?next=/orders` in all three cases.
   
   There are two causes, both in `dubbo-triple-servlet` since 3.3.0 (#13607):
   
   1. `ServletHttpRequestAdapter.getContextPath()` and 
`DummyServletContext.getContextPath()` return `"/"`. The servlet API says: "The 
path starts with a "/" character but does not end with a "/" character. For 
servlets in the default (root) context, this method returns ""." With `"/"`, 
every URL built as `getContextPath() + "/..."` starts with `//`.
   2. `ServletHttpResponseAdapter` implements `encodeURL`, `encodeRedirectURL`, 
`encodeUrl` and `encodeRedirectUrl` as `RequestUtils.encodeURL(url)`, which is 
`URLEncoder.encode`, so the whole URL is form-encoded. These methods are about 
session tracking, not about escaping. The servlet API says: "Encodes the 
specified URL by including the session ID, or, if encoding is not needed, 
returns the URL unchanged." and "Encodes the specified URL for use in the 
`sendRedirect` method or, if encoding is not needed, returns the URL 
unchanged." Dubbo does not rewrite URLs for sessions, so they should return the 
URL unchanged.
   
   Fixing only the second one is not enough for `DefaultRedirectStrategy`: it 
would then send `//login?next=/orders`.
   
   ### Anything else
   
   It happens every time servlet code redirects through `encodeRedirectURL` or 
builds a URL from `getContextPath()`.
   
   The values above are for the root context, which is the default. When the 
protocol has a `contextpath`, or `TripleFilter` runs in a webapp that is not 
deployed at the root, the servlet API would expect that path from 
`getContextPath()` (and a servlet path without it). Today Dubbo returns `"/"` 
there as well.
   
   ### Are you willing to submit a pull request to fix on your own?
   
   - [x] Yes I am willing to submit a pull request on my own!
   


-- 
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