34
34
import org .eclipse .jetty .ee8 .servlet .ServletContextHandler ;
35
35
import org .eclipse .jetty .ee8 .servlet .ServletHandler ;
36
36
import org .eclipse .jetty .ee8 .servlet .ServletMapping ;
37
+ import org .eclipse .jetty .server .AliasCheck ;
38
+ import org .eclipse .jetty .server .AllowedResourceAliasChecker ;
37
39
import org .eclipse .jetty .util .StringUtil ;
38
40
import org .eclipse .jetty .util .URIUtil ;
39
41
import org .eclipse .jetty .util .resource .Resource ;
@@ -59,6 +61,7 @@ public class ResourceFileServlet extends HttpServlet {
59
61
private Resource resourceBase ;
60
62
private String [] welcomeFiles ;
61
63
private FileSender fSender ;
64
+ private AliasCheck aliasCheck ;
62
65
ServletContextHandler chandler ;
63
66
ServletContext context ;
64
67
String defaultServletName ;
@@ -90,6 +93,12 @@ public void init() throws ServletException {
90
93
try {
91
94
URL resourceBaseUrl = context .getResource ("/" + appVersion .getPublicRoot ());
92
95
resourceBase = (resourceBaseUrl == null ) ? null : ResourceFactory .of (chandler ).newResource (resourceBaseUrl );
96
+ if (resourceBase != null ) {
97
+ ContextHandler contextHandler = ContextHandler .getContextHandler (context );
98
+ contextHandler .addAliasCheck (
99
+ new AllowedResourceAliasChecker (contextHandler .getCoreContextHandler (), resourceBase ));
100
+ aliasCheck = contextHandler .getCoreContextHandler ();
101
+ }
93
102
} catch (Exception ex ) {
94
103
throw new ServletException (ex );
95
104
}
@@ -162,41 +171,32 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response)
162
171
}
163
172
164
173
// Find the resource
165
- Resource resource = null ;
166
- try {
167
- resource = getResource (pathInContext );
174
+ Resource resource = getResource (pathInContext );
175
+ if (resource == null ) {
176
+ response .sendError (HttpServletResponse .SC_NOT_FOUND );
177
+ return ;
178
+ }
168
179
169
- if (resource == null ) {
170
- response .sendError (HttpServletResponse .SC_NOT_FOUND );
171
- return ;
172
- }
180
+ if (StringUtil .endsWithIgnoreCase (resource .getName (), ".jsp" )) {
181
+ // General paranoia: don't ever serve raw .jsp files.
182
+ response .sendError (HttpServletResponse .SC_NOT_FOUND );
183
+ return ;
184
+ }
173
185
174
- if ( StringUtil . endsWithIgnoreCase ( resource . getName (), ".jsp" )) {
175
- // General paranoia: don't ever serve raw .jsp files.
176
- response . sendError ( HttpServletResponse . SC_NOT_FOUND );
177
- return ;
186
+ // Handle resource
187
+ if ( resource . isDirectory ()) {
188
+ if ( included || ! fSender . checkIfUnmodified ( request , response , resource )) {
189
+ response . sendError ( HttpServletResponse . SC_FORBIDDEN ) ;
178
190
}
179
-
180
- // Handle resource
181
- if (resource .isDirectory ()) {
182
- if (included || !fSender .checkIfUnmodified (request , response , resource )) {
183
- response .sendError (HttpServletResponse .SC_FORBIDDEN );
184
- }
191
+ } else {
192
+ if (!resource .exists () || !aliasCheck .checkAlias (pathInContext , resource )) {
193
+ logger .atWarning ().log ("Non existent resource: %s = %s" , pathInContext , resource );
194
+ response .sendError (HttpServletResponse .SC_NOT_FOUND );
185
195
} else {
186
- if (resource == null || !resource .exists ()) {
187
- logger .atWarning ().log ("Non existent resource: %s = %s" , pathInContext , resource );
188
- response .sendError (HttpServletResponse .SC_NOT_FOUND );
189
- } else {
190
- if (included || !fSender .checkIfUnmodified (request , response , resource )) {
191
- fSender .sendData (context , response , included , resource , request .getRequestURI ());
192
- }
196
+ if (included || !fSender .checkIfUnmodified (request , response , resource )) {
197
+ fSender .sendData (context , response , included , resource , request .getRequestURI ());
193
198
}
194
199
}
195
- } finally {
196
- if (resource != null ) {
197
- // TODO: do we need to release.
198
- // resource.release();
199
- }
200
200
}
201
201
}
202
202
@@ -226,6 +226,7 @@ protected boolean isProtectedPath(String target) {
226
226
private Resource getResource (String pathInContext ) {
227
227
try {
228
228
if (resourceBase != null ) {
229
+ pathInContext = URIUtil .encodePath (pathInContext );
229
230
return resourceBase .resolve (pathInContext );
230
231
}
231
232
} catch (Exception ex ) {
0 commit comments