File tree 5 files changed +50
-3
lines changed
src/main/resources/applications/knoxauth/app
main/java/org/apache/knox/gateway/util
test/java/org/apache/knox/gateway/util
5 files changed +50
-3
lines changed Original file line number Diff line number Diff line change 58
58
<groupId >org.apache.knox</groupId >
59
59
<artifactId >knox-webshell-ui</artifactId >
60
60
</dependency >
61
+ <dependency >
62
+ <groupId >org.apache.commons</groupId >
63
+ <artifactId >commons-text</artifactId >
64
+ </dependency >
61
65
</dependencies >
62
66
63
67
<build >
Original file line number Diff line number Diff line change 21
21
<%@ page import =" org.apache.knox.gateway.config.GatewayConfig" %>
22
22
<%@ page import =" java.net.MalformedURLException" %>
23
23
<%@ page import =" org.apache.knox.gateway.util.Urls" %>
24
+ <%@ page import =" org.apache.commons.text.StringEscapeUtils" %>
24
25
25
26
<!DOCTYPE html>
26
27
<!-- [if lt IE 7]><html class="no-js lt-ie9 lt-ie8 lt-ie7"><![endif]-->
93
94
if (origUrl != null ) {
94
95
validRedirect = RegExUtils . checkWhitelist(whitelist, origUrl);
95
96
}
97
+ if (validRedirect) {
98
+ validRedirect = Urls . isValidURL(originalUrl);
99
+ }
96
100
if ((" 1" . equals(request. getParameter(" returnToApp" )))) {
97
101
if (validRedirect) {
98
102
response. setStatus(HttpServletResponse . SC_MOVED_PERMANENTLY );
178
182
the application. If your previously established SSO session is still valid then
179
183
you will likely be automatically logged into your application. Otherwise, you
180
184
will be required to login again.
181
- <a href =" ?returnToApp=1&originalUrl=<%= originalUrl % >" >Return to Application</a >
185
+ <a href =" ?returnToApp=1&originalUrl=<%= StringEscapeUtils . escapeHtml4( originalUrl) % >" >Return to Application</a >
182
186
</p >
183
187
<%
184
188
if (globalLogoutPageURL != null && ! globalLogoutPageURL. isEmpty()) {
Original file line number Diff line number Diff line change 20
20
<%@ page import =" org.apache.knox.gateway.util.RegExUtils" %>
21
21
<%@ page import =" org.apache.knox.gateway.util.Urls" %>
22
22
<%@ page import =" org.apache.knox.gateway.util.WhitelistUtils" %>
23
+ <%@ page import =" org.apache.commons.text.StringEscapeUtils" %>
23
24
24
25
<!DOCTYPE html>
25
26
<!-- [if lt IE 7]><html class="no-js lt-ie9 lt-ie8 lt-ie7"><![endif]-->
57
58
// if not a well formed URL then not a valid redirect
58
59
validRedirect = false ;
59
60
}
61
+ if (validRedirect) {
62
+ validRedirect = Urls . isValidURL(originalUrl);
63
+ }
60
64
if (validRedirect) {
61
65
Topology topology = (Topology )request. getSession(). getServletContext(). getAttribute(" org.apache.knox.gateway.topology" );
62
66
String whitelist = null ;
82
86
document .addEventListener (" load" , redirectOnLoad ());
83
87
84
88
function redirectOnLoad () {
85
- var originalUrl = " <%= originalUrl % >" ;
89
+ var originalUrl = " <%= StringEscapeUtils . escapeEcmaScript( originalUrl) % >" ;
86
90
if (originalUrl != null ) {
87
91
redirect (originalUrl);
88
92
}
103
107
<div style =" background : white ;" class =" l-logo" >
104
108
<img src =" images/loading.gif" alt =" Knox logo" style =" text-align :center ;width : 2% ; height : 2% " >
105
109
</div >
106
- <p style =" color : white ;display : block " >Loading should complete in few a seconds. If not, click <a href =" <%= originalUrl % >" >here</a ></p >
110
+ <p style =" color : white ;display : block " >Loading should complete in few a seconds. If not, click <a href =" <%= StringEscapeUtils . escapeHtml4( originalUrl) % >" >here</a ></p >
107
111
<%
108
112
} else {
109
113
% >
Original file line number Diff line number Diff line change 19
19
20
20
import java .io .UnsupportedEncodingException ;
21
21
import java .net .MalformedURLException ;
22
+ import java .net .URISyntaxException ;
22
23
import java .net .URL ;
23
24
import java .nio .charset .StandardCharsets ;
24
25
import java .util .regex .Matcher ;
@@ -81,6 +82,20 @@ public static boolean containsUserInfo(String url) throws MalformedURLException
81
82
return (new URL (url ).getUserInfo () != null );
82
83
}
83
84
85
+ /**
86
+ * Checks if a given URL is valid according to RFC2396.
87
+ * @param url a URL
88
+ * @return true if URL conforms to RFC2396, false otherwise.
89
+ */
90
+ public static boolean isValidURL (String url ) {
91
+ try {
92
+ new URL (url ).toURI ();
93
+ return true ;
94
+ } catch (MalformedURLException | URISyntaxException e ) {
95
+ return false ;
96
+ }
97
+ }
98
+
84
99
/**
85
100
* Compute the domain name from an URL.
86
101
*
Original file line number Diff line number Diff line change 26
26
27
27
public class UrlsTest {
28
28
29
+ private static final String INVALID_ORIGINAL_URL = "https://localhost:8443/gateway/homepage/homeisyr\" >" ;
30
+ private static final String VALID_LOCAL_ORIGINAL_URL = "https://localhost:8443/gateway/homepage/home" ;
31
+ private static final String VALID_ORIGINAL_URL = "https://knoxhost.site:8443/gateway/homepage/home" ;
32
+ private static final String VALID_ORIGINAL_URL_SOLR = "https://knoxhost.site:8443/gateway/cdp-proxy/solr/" ;
33
+ private static final String LOGOUT_LOCAL_ORIGINAL_URL = "https://localhost:8443/gateway/homepage/home?profile=token" ;
34
+ private static final String LOGOUT_ORIGINAL_URL_WITH_PARAMETERS = "https://knoxhost.site:8443/gateway/homepage/session/api/v1/sessioninfo?logoutPageProfile=token&logoutPageTopologies=cdp-proxy-token" ;
35
+ private static final String LOGOUT_ORIGINAL_URL_WITH_PARAMETERS_ENCODED = "https://knoxhost.site:8443/gateway/homepage/session/api/v1/sessioninfo%3FlogoutPageProfile=token%26logoutPageTopologies=cdp-proxy-token" ;
36
+
29
37
/*
30
38
* Domain name creation follows the following algorithm:
31
39
* 1. if the incoming request hostname endsWith a configured domain suffix return the suffix - with prefixed dot
@@ -101,4 +109,16 @@ public void testContainsUserInfo() throws Exception {
101
109
assertTrue (
Urls .
containsUserInfo (
"https://www.local.com:[email protected] " ));
102
110
assertFalse (Urls .containsUserInfo ( "https://www.local.com:8443/google.com" ));
103
111
}
112
+
113
+ @ Test
114
+ public void testValidUrl () throws Exception {
115
+ assertFalse (Urls .isValidURL (INVALID_ORIGINAL_URL ));
116
+ assertTrue (Urls .isValidURL (VALID_LOCAL_ORIGINAL_URL ));
117
+ assertTrue (Urls .isValidURL (VALID_ORIGINAL_URL ));
118
+ assertTrue (Urls .isValidURL (VALID_ORIGINAL_URL_SOLR ));
119
+ assertTrue (Urls .isValidURL (LOGOUT_LOCAL_ORIGINAL_URL ));
120
+ assertTrue (Urls .isValidURL (LOGOUT_ORIGINAL_URL_WITH_PARAMETERS ));
121
+ assertTrue (Urls .isValidURL (LOGOUT_ORIGINAL_URL_WITH_PARAMETERS_ENCODED ));
122
+ }
123
+
104
124
}
You can’t perform that action at this time.
0 commit comments