Skip to content

Commit e0788ab

Browse files
authored
Fix #7615 encode relative URIs (#7765)
* Fix #7615 encode relative URIs cherry-picked from 9c30caf Signed-off-by: Greg Wilkins <[email protected]> * Fix #7615 encode relative URIs fixed checkstyle Signed-off-by: Greg Wilkins <[email protected]>
1 parent ae5c8e3 commit e0788ab

File tree

2 files changed

+19
-16
lines changed

2 files changed

+19
-16
lines changed

jetty-server/src/main/java/org/eclipse/jetty/server/Response.java

+17-15
Original file line numberDiff line numberDiff line change
@@ -342,14 +342,18 @@ public boolean containsHeader(String name)
342342
@Override
343343
public String encodeURL(String url)
344344
{
345+
if (url == null)
346+
return null;
347+
345348
final Request request = _channel.getRequest();
346349
SessionHandler sessionManager = request.getSessionHandler();
347350

348351
if (sessionManager == null)
349352
return url;
350353

351354
HttpURI uri = null;
352-
if (sessionManager.isCheckingRemoteSessionIdEncoding() && URIUtil.hasScheme(url))
355+
boolean hasScheme = URIUtil.hasScheme(url);
356+
if (sessionManager.isCheckingRemoteSessionIdEncoding() && hasScheme)
353357
{
354358
uri = HttpURI.from(url);
355359
String path = uri.getPath();
@@ -371,9 +375,6 @@ public String encodeURL(String url)
371375
if (sessionURLPrefix == null)
372376
return url;
373377

374-
if (url == null)
375-
return null;
376-
377378
// should not encode if cookies in evidence
378379
if ((sessionManager.isUsingCookies() && request.isRequestedSessionIdFromCookie()) || !sessionManager.isUsingURLs())
379380
{
@@ -404,9 +405,6 @@ public String encodeURL(String url)
404405

405406
String id = sessionManager.getExtendedId(session);
406407

407-
if (uri == null)
408-
uri = HttpURI.from(url);
409-
410408
// Already encoded
411409
int prefix = url.indexOf(sessionURLPrefix);
412410
if (prefix != -1)
@@ -421,20 +419,24 @@ public String encodeURL(String url)
421419
url.substring(suffix);
422420
}
423421

422+
// check for a null path
423+
String nonNullPath = "";
424+
if (hasScheme)
425+
{
426+
if (uri == null)
427+
uri = HttpURI.from(url);
428+
if (uri.getPath() == null)
429+
nonNullPath = "/";
430+
}
431+
424432
// edit the session
425433
int suffix = url.indexOf('?');
426434
if (suffix < 0)
427435
suffix = url.indexOf('#');
428436
if (suffix < 0)
429-
{
430-
return url +
431-
((HttpScheme.HTTPS.is(uri.getScheme()) || HttpScheme.HTTP.is(uri.getScheme())) && uri.getPath() == null ? "/" : "") + //if no path, insert the root path
432-
sessionURLPrefix + id;
433-
}
437+
return url + nonNullPath + sessionURLPrefix + id;
434438

435-
return url.substring(0, suffix) +
436-
((HttpScheme.HTTPS.is(uri.getScheme()) || HttpScheme.HTTP.is(uri.getScheme())) && uri.getPath() == null ? "/" : "") + //if no path so insert the root path
437-
sessionURLPrefix + id + url.substring(suffix);
439+
return url.substring(0, suffix) + nonNullPath + sessionURLPrefix + id + url.substring(suffix);
438440
}
439441

440442
@Override

jetty-server/src/test/java/org/eclipse/jetty/server/ResponseTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1642,7 +1642,7 @@ public void testWriteCheckError() throws Exception
16421642
}
16431643

16441644
@Test
1645-
public void testEncodeRedirect()
1645+
public void testEncodeURLs()
16461646
{
16471647
ContextHandler context = new ContextHandler("/path");
16481648
Response response = getResponse();
@@ -1708,6 +1708,7 @@ public void testEncodeRedirect()
17081708
assertEquals("/;jsessionid=12345", response.encodeURL("/"));
17091709
assertEquals("/foo.html;jsessionid=12345#target", response.encodeURL("/foo.html#target"));
17101710
assertEquals(";jsessionid=12345", response.encodeURL(""));
1711+
assertEquals("../foo/bar.jsp;jsessionid=12345", response.encodeURL("../foo/bar.jsp"));
17111712
}
17121713

17131714
@Test

0 commit comments

Comments
 (0)