-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Java: add CSRF query #18288
Java: add CSRF query #18288
Conversation
java/ql/lib/semmle/code/java/security/CsrfUnprotectedRequestTypeQuery.qll
Fixed
Show fixed
Hide fixed
java/ql/lib/semmle/code/java/security/CsrfUnprotectedRequestTypeQuery.qll
Fixed
Show fixed
Hide fixed
java/ql/lib/semmle/code/java/security/CsrfUnprotectedRequestTypeQuery.qll
Fixed
Show fixed
Hide fixed
java/ql/src/experimental/Security/CWE/CWE-089/MyBatisMapperXmlSqlInjection.ql
Fixed
Show fixed
Hide fixed
java/ql/test/query-tests/security/CWE-352/CsrfUnprotectedRequestTypeTest.ql
Fixed
Show fixed
Hide fixed
java/ql/test/query-tests/security/CWE-352/CsrfUnprotectedRequestTypeTest.ql
Fixed
Show fixed
Hide fixed
java/ql/test/query-tests/security/CWE-352/CsrfUnprotectedRequestTypeTest.ql
Fixed
Show fixed
Hide fixed
java/ql/test/query-tests/security/CWE-352/CsrfUnprotectedRequestTypeTest.ql
Fixed
Show fixed
Hide fixed
java/ql/test/query-tests/security/CWE-352/CsrfUnprotectedRequestTypeTest.ql
Fixed
Show fixed
Hide fixed
ff0477c
to
13753dd
Compare
QHelp previews: java/ql/src/Security/CWE/CWE-352/CsrfUnprotectedRequestType.qhelpHTTP request type unprotected from CSRFCross-site request forgery (CSRF) is a type of vulnerability in which an attacker is able to force a user to carry out an action that the user did not intend. The attacker tricks an authenticated user into submitting a request to the web application. Typically, this request will result in a state change on the server, such as changing the user's password. The request can be initiated when the user visits a site controlled by the attacker. If the web application relies only on cookies for authentication, or on other credentials that are automatically included in the request, then this request will appear as legitimate to the server. RecommendationMake sure any requests that change application state are protected from CSRF. Some application frameworks provide default CSRF protection for unsafe HTTP request methods (such as This query currently supports the Spring and Stapler web frameworks. Spring provides default CSRF protection for all unsafe HTTP methods whereas Stapler provides default CSRF protection for the ExampleThe following examples show Spring request handlers allowing safe HTTP request methods for state-changing actions. Since safe HTTP request methods do not have default CSRF protection in Spring, they should not be used when modifying application state. Instead, use one of the unsafe HTTP methods which Spring default-protects from CSRF. import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
// BAD - a safe HTTP request like GET should not be used for a state-changing action
@RequestMapping(value="/transfer", method=RequestMethod.GET)
public boolean doTransfer(HttpServletRequest request, HttpServletResponse response){
return transfer(request, response);
}
// BAD - no HTTP request type is specified, so safe HTTP requests are allowed
@RequestMapping(value="/delete")
public boolean doDelete(HttpServletRequest request, HttpServletResponse response){
return delete(request, response);
} import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.DeleteMapping;
// GOOD - use an unsafe HTTP request like POST
@RequestMapping(value="/transfer", method=RequestMethod.POST)
public boolean doTransfer(HttpServletRequest request, HttpServletResponse response){
return transfer(request, response);
}
// GOOD - use an unsafe HTTP request like DELETE
@DeleteMapping(value="/delete")
public boolean doDelete(HttpServletRequest request, HttpServletResponse response){
return delete(request, response);
} The following examples show Stapler web methods allowing safe HTTP request methods for state-changing actions. Since safe HTTP request methods do not have default CSRF protection in Stapler, they should not be used when modifying application state. Instead, use the import org.kohsuke.stapler.verb.GET;
// BAD - a safe HTTP request like GET should not be used for a state-changing action
@GET
public HttpRedirect doTransfer() {
return transfer();
}
// BAD - no HTTP request type is specified, so safe HTTP requests are allowed
public HttpRedirect doPost() {
return post();
} import org.kohsuke.stapler.verb.POST;
// GOOD - use POST
@POST
public HttpRedirect doTransfer() {
return transfer();
}
// GOOD - use POST
@POST
public HttpRedirect doPost() {
return post();
} References
|
c217d7f
to
3d14e16
Compare
2472aad
to
695eca6
Compare
695eca6
to
b56a8d8
Compare
java/ql/lib/semmle/code/java/security/CsrfUnprotectedRequestTypeQuery.qll
Outdated
Show resolved
Hide resolved
java/ql/lib/semmle/code/java/security/CsrfUnprotectedRequestTypeQuery.qll
Outdated
Show resolved
Hide resolved
java/ql/lib/semmle/code/java/frameworks/spring/SpringController.qll
Outdated
Show resolved
Hide resolved
java/ql/lib/semmle/code/java/security/CsrfUnprotectedRequestTypeQuery.qll
Outdated
Show resolved
Hide resolved
java/ql/lib/semmle/code/java/security/CsrfUnprotectedRequestTypeQuery.qll
Outdated
Show resolved
Hide resolved
java/ql/lib/semmle/code/java/security/CsrfUnprotectedRequestTypeQuery.qll
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
QL part LGTM now, but be sure to verify performance with an additional dca run.
Performance looks fine in the new DCA run. |
…lt-protected from CSRF
…lib so importable, and fix experimental files broken by the move
remove PUT and DELETE from StaplerCsrfUnprotectedMethod remove OPTIONS and TRACE from SpringCsrfUnprotectedMethod
remove import no longer needed since contents of MyBatisMapperXML.qll have been moved to MyBatis.qll
d2a6fb9
to
0071e1a
Compare
java/ql/src/Security/CWE/CWE-352/CsrfUnprotectedRequestType.qhelp
Outdated
Show resolved
Hide resolved
java/ql/src/Security/CWE/CWE-352/CsrfUnprotectedRequestType.qhelp
Outdated
Show resolved
Hide resolved
I'll review this on behalf of Docs. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jcogs33 👋🏻 - Approving this on behalf of Docs in order not to block you ⚡
I have left a few minor comments and suggestions. Hope these help!
java/ql/src/Security/CWE/CWE-352/CsrfUnprotectedRequestType.qhelp
Outdated
Show resolved
Hide resolved
java/ql/src/Security/CWE/CWE-352/CsrfUnprotectedRequestType.qhelp
Outdated
Show resolved
Hide resolved
java/ql/src/Security/CWE/CWE-352/CsrfUnprotectedRequestType.qhelp
Outdated
Show resolved
Hide resolved
java/ql/src/Security/CWE/CWE-352/CsrfUnprotectedRequestType.qhelp
Outdated
Show resolved
Hide resolved
java/ql/src/Security/CWE/CWE-352/CsrfUnprotectedRequestType.qhelp
Outdated
Show resolved
Hide resolved
java/ql/src/Security/CWE/CWE-352/CsrfUnprotectedRequestType.qhelp
Outdated
Show resolved
Hide resolved
java/ql/src/Security/CWE/CWE-352/CsrfUnprotectedRequestType.qhelp
Outdated
Show resolved
Hide resolved
…ed by Python and Ruby
discussing tokens is not directly relevant to this query's recommendation and examples
@mchammer01, Thanks for the review! Regarding the wording in the overview section, I've changed that section completely so that it aligns more closely with the QHelp overviews in some of our other CSRF queries (c.f. Python, Ruby). Would you mind re-reviewing the overview section and confirming that this re-write looks okay? 🙏 For context: I copied the original overview section from the Java SpringCSRFProtection.qhelp since I wanted to align with our other Java CSRF query, but I agree that the wording could be improved. If you think the re-write is okay, then I'll open a follow-up PR to update |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jcogs33 - reviewed as requested. Left a couple of minor suggestions and asked a question.
As usual, feel free to ignore anything you don't agree with 😄
java/ql/src/Security/CWE/CWE-352/CsrfUnprotectedRequestType.qhelp
Outdated
Show resolved
Hide resolved
java/ql/src/Security/CWE/CWE-352/CsrfUnprotectedRequestType.qhelp
Outdated
Show resolved
Hide resolved
Thanks for the review @mchammer01! I've addressed your comments. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Description
Adds a query for CSRF vulnerabilities caused by using HTTP request types that are not default-protected by a given framework for apparent state-changing actions. Supports Spring and Stapler frameworks for now. State-changing actions are determined heuristically based on whether the request handling method updates a database or whether its name suggests that it might change application state. Due to the heuristic nature of determining state changes, this is a low precision query.
Consideration
Pull Request checklist
All query authors
.qhelp
. See the documentation in this repository.Internal query authors only
.ql
,.qll
, or.qhelp
files. See the documentation (internal access required).