@@ -85,7 +85,41 @@ public void bad7(String url, HttpServletRequest request, HttpServletResponse res
8585 @ GetMapping ("/good1" )
8686 public void good1 (String url , HttpServletRequest request , HttpServletResponse response ) {
8787 try {
88- request .getRequestDispatcher ("/index.jsp?token=" + url ).forward (request , response );
88+ request .getRequestDispatcher ("/index.jsp?token=" + url ).forward (request , response ); // $ SPURIOUS: hasUrlForward
89+ } catch (ServletException e ) {
90+ e .printStackTrace ();
91+ } catch (IOException e ) {
92+ e .printStackTrace ();
93+ }
94+ }
95+
96+ // BAD: appended to a prefix without path sanitization
97+ @ GetMapping ("/bad8" )
98+ public void bad8 (String urlPath , HttpServletRequest request , HttpServletResponse response ) {
99+ try {
100+ String url = "/pages" + urlPath ;
101+ request .getRequestDispatcher (url ).forward (request , response ); // $ hasUrlForward
102+ } catch (ServletException e ) {
103+ e .printStackTrace ();
104+ } catch (IOException e ) {
105+ e .printStackTrace ();
106+ }
107+ }
108+
109+ // GOOD: appended to a prefix with path sanitization
110+ @ GetMapping ("/good2" )
111+ public void good2 (String urlPath , HttpServletRequest request , HttpServletResponse response ) {
112+ try {
113+ while (urlPath .contains ("%" )) {
114+ urlPath = URLDecoder .decode (urlPath , "UTF-8" );
115+ }
116+
117+ if (!urlPath .contains (".." ) && !urlPath .startsWith ("/WEB-INF" )) {
118+ // Note: path injection sanitizer does not account for string concatenation instead of a `startswith` check
119+ String url = "/pages" + urlPath ;
120+ request .getRequestDispatcher (url ).forward (request , response );
121+ }
122+
89123 } catch (ServletException e ) {
90124 e .printStackTrace ();
91125 } catch (IOException e ) {
0 commit comments