3434import org .eclipse .jetty .ee8 .servlet .ServletContextHandler ;
3535import org .eclipse .jetty .ee8 .servlet .ServletHandler ;
3636import org .eclipse .jetty .ee8 .servlet .ServletMapping ;
37+ import org .eclipse .jetty .server .AliasCheck ;
38+ import org .eclipse .jetty .server .AllowedResourceAliasChecker ;
3739import org .eclipse .jetty .util .StringUtil ;
3840import org .eclipse .jetty .util .URIUtil ;
3941import org .eclipse .jetty .util .resource .Resource ;
@@ -59,6 +61,7 @@ public class ResourceFileServlet extends HttpServlet {
5961 private Resource resourceBase ;
6062 private String [] welcomeFiles ;
6163 private FileSender fSender ;
64+ private AliasCheck aliasCheck ;
6265 ServletContextHandler chandler ;
6366 ServletContext context ;
6467 String defaultServletName ;
@@ -90,6 +93,12 @@ public void init() throws ServletException {
9093 try {
9194 URL resourceBaseUrl = context .getResource ("/" + appVersion .getPublicRoot ());
9295 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+ }
93102 } catch (Exception ex ) {
94103 throw new ServletException (ex );
95104 }
@@ -162,41 +171,32 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response)
162171 }
163172
164173 // 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+ }
168179
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+ }
173185
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 ) ;
178190 }
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 );
185195 } 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 ());
193198 }
194199 }
195- } finally {
196- if (resource != null ) {
197- // TODO: do we need to release.
198- // resource.release();
199- }
200200 }
201201 }
202202
@@ -226,6 +226,7 @@ protected boolean isProtectedPath(String target) {
226226 private Resource getResource (String pathInContext ) {
227227 try {
228228 if (resourceBase != null ) {
229+ pathInContext = URIUtil .encodePath (pathInContext );
229230 return resourceBase .resolve (pathInContext );
230231 }
231232 } catch (Exception ex ) {
0 commit comments