Repository navigation
Java: CWE-552 Query to detect unsafe request dispatcher usage #7286
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
smowton
merged 17 commits into
github:main
from
luchua-bc:java/unsafe-url-forward-dispatch
Jan 18, 2022
Merged
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
8bcffc2
Query to detect unsafe request dispatcher usage
luchua-bc 29ce0e9
Add sanitizer for virtual method calls
luchua-bc 263dbd3
Optimize the query
luchua-bc cd9a485
Refactor NullOrEmptyCheckGuard
atorralba 81feaae
Refactor PathMatchGuard
atorralba b6886b8
Move code to qll file
atorralba 877c529
Remove the deprecated library keyword
luchua-bc 136fefb
Apply suggestions from code review
atorralba fb1287d
Use dominance instead of getParent
atorralba eb1806c
Split PathMatchGuard into three guards
atorralba a2c98ba
Reordering
atorralba 978ef15
Update method names
luchua-bc 4797fce
Update use cases and qldoc
luchua-bc a3d65a8
Update recommendation in qldoc and make examples more comprehendible
luchua-bc 748008a
Remove dangling reference to UnsafeRequestPath.java
smowton d744cf9
Clean up guard logic:
smowton 1e32514
Avoid using `this` for a non-extending supertype, and remove needless…
smowton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Query to detect unsafe request dispatcher usage
- Loading branch information
commit 8bcffc2886f3c17d6083287d4177f053683e36db
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
java/ql/src/experimental/Security/CWE/CWE-552/UnsafeRequestPath.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| public class UnsafeRequestPath implements Filter { | ||
| private static final String BASE_PATH = "/pages"; | ||
|
|
||
| @Override | ||
| public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) | ||
| throws IOException, ServletException { | ||
|
|
||
| { | ||
| // BAD: Request dispatcher from servlet path without check | ||
| String path = ((HttpServletRequest) request).getServletPath(); | ||
| // A sample payload "/%57EB-INF/web.xml" can bypass this `startsWith` check | ||
| if (path != null && !path.startsWith("/WEB-INF")) { | ||
| request.getRequestDispatcher(path).forward(request, response); | ||
| } else { | ||
| chain.doFilter(request, response); | ||
| } | ||
| } | ||
|
|
||
| { | ||
| // GOOD: Request dispatcher from servlet path with path traversal check | ||
| String path = ((HttpServletRequest) request).getServletPath(); | ||
|
|
||
| if (path.startsWith(BASE_PATH) && !path.contains("..")) { | ||
| request.getRequestDispatcher(path).forward(request, response); | ||
| } else { | ||
| chain.doFilter(request, response); | ||
| } | ||
| } | ||
| } | ||
| } |
82 changes: 82 additions & 0 deletions
82
java/ql/src/experimental/Security/CWE/CWE-552/UnsafeServletRequestDispatch.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| public class UnsafeServletRequestDispatch extends HttpServlet { | ||
| private static final String BASE_PATH = "/pages"; | ||
|
|
||
| @Override | ||
| protected void doGet(HttpServletRequest request, HttpServletResponse response) | ||
|
luchua-bc marked this conversation as resolved.
Outdated
|
||
| throws ServletException, IOException { | ||
| { | ||
| // GOOD: whitelisted URI | ||
|
luchua-bc marked this conversation as resolved.
Outdated
|
||
| if (action.equals("Login")) { | ||
| ServletContext sc = cfg.getServletContext(); | ||
| RequestDispatcher rd = sc.getRequestDispatcher("/Login.jsp"); | ||
| rd.forward(request, response); | ||
| } | ||
| } | ||
|
|
||
| { | ||
| // BAD: Request dispatcher constructed from `ServletContext` without input validation | ||
| String returnURL = request.getParameter("returnURL"); | ||
| ServletConfig cfg = getServletConfig(); | ||
|
|
||
| ServletContext sc = cfg.getServletContext(); | ||
| RequestDispatcher rd = sc.getRequestDispatcher(returnURL); | ||
| rd.forward(request, response); | ||
| } | ||
|
|
||
| { | ||
| // BAD: Request dispatcher without path traversal check | ||
| String path = request.getParameter("path"); | ||
|
|
||
| // A sample payload "/pages/welcome.jsp/../WEB-INF/web.xml" can bypass the `startsWith` check | ||
| // The payload "/pages/welcome.jsp/../../%57EB-INF/web.xml" can bypass the check as well since RequestDispatcher will decode `%57` as `W` | ||
| if (path.startsWith(BASE_PATH)) { | ||
| request.getServletContext().getRequestDispatcher(path).include(request, response); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| { | ||
| // GOOD: Request dispatcher with path traversal check | ||
| String path = request.getParameter("path"); | ||
|
|
||
| if (path.startsWith(BASE_PATH) && !path.contains("..")) { | ||
| request.getServletContext().getRequestDispatcher(path).include(request, response); | ||
| } | ||
| } | ||
|
|
||
| { | ||
| // GOOD: Request dispatcher with path normalization | ||
| String path = request.getParameter("path"); | ||
| Path requestedPath = Paths.get(BASE_PATH).resolve(path).normalize(); | ||
|
|
||
| // /pages/welcome.jsp/../../WEB-INF/web.xml becomes /WEB-INF/web.xml | ||
| // /pages/welcome.jsp/../../%57EB-INF/web.xml becomes /%57EB-INF/web.xml | ||
| if (requestedPath.startsWith(BASE_PATH)) { | ||
| request.getServletContext().getRequestDispatcher(requestedPath.toString()).forward(request, response); | ||
| } | ||
| } | ||
|
|
||
| { | ||
| // BAD: Request dispatcher with improper negation check and without url decoding | ||
| String path = request.getParameter("path"); | ||
| Path requestedPath = Paths.get(BASE_PATH).resolve(path).normalize(); | ||
|
|
||
| if (!requestedPath.startsWith("/WEB-INF") && !requestedPath.startsWith("/META-INF")) { | ||
| request.getServletContext().getRequestDispatcher(requestedPath.toString()).forward(request, response); | ||
| } | ||
| } | ||
|
|
||
| { | ||
| // GOOD: Request dispatcher with path traversal check and url decoding | ||
| String path = request.getParameter("path"); | ||
| boolean hasEncoding = path.contains("%"); | ||
| while (hasEncoding) { | ||
| path = URLDecoder.decode(path, "UTF-8"); | ||
| hasEncoding = path.contains("%"); | ||
| } | ||
|
|
||
| if (!path.startsWith("/WEB-INF/") && !path.contains("..")) { | ||
| request.getServletContext().getRequestDispatcher(path).include(request, response); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.