168.e16111b9.js 30.0 KB
Newer Older
茶陵後's avatar
茶陵後 已提交
1
(window.webpackJsonp=window.webpackJsonp||[]).push([[168],{592:function(e,t,o){"use strict";o.r(t);var a=o(56),r=Object(a.a)({},(function(){var e=this,t=e.$createElement,o=e._self._c||t;return o("ContentSlotsDistributor",{attrs:{"slot-key":e.$parent.slotKey}},[o("h1",{attrs:{id:"cross-site-request-forgery-csrf"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#cross-site-request-forgery-csrf"}},[e._v("#")]),e._v(" Cross Site Request Forgery (CSRF)")]),e._v(" "),o("p",[e._v("Spring provides comprehensive support for protecting against "),o("a",{attrs:{href:"https://en.wikipedia.org/wiki/Cross-site_request_forgery",target:"_blank",rel:"noopener noreferrer"}},[e._v("Cross Site Request Forgery (CSRF)"),o("OutboundLink")],1),e._v(" attacks.\nIn the following sections we will explore:")]),e._v(" "),o("ul",[o("li",[o("p",[o("a",{attrs:{href:"#csrf-explained"}},[e._v("What is a CSRF Attack?")])])]),e._v(" "),o("li",[o("p",[o("a",{attrs:{href:"#csrf-protection"}},[e._v("Protecting Against CSRF Attacks")])])]),e._v(" "),o("li",[o("p",[o("a",{attrs:{href:"#csrf-considerations"}},[e._v("CSRF Considerations")])])])]),e._v(" "),o("table",[o("thead",[o("tr",[o("th"),e._v(" "),o("th",[e._v("This portion of the documentation discusses the general topic of CSRF protection."),o("br"),e._v("Refer to the relevant sections for specific information on CSRF protection for "),o("RouterLink",{attrs:{to:"/servlet/exploits/csrf.html#servlet-csrf"}},[e._v("servlet")]),e._v(" and "),o("RouterLink",{attrs:{to:"/reactive/exploits/csrf.html#webflux-csrf"}},[e._v("WebFlux")]),e._v(" based applications.")],1)])]),e._v(" "),o("tbody")]),e._v(" "),o("h2",{attrs:{id:"what-is-a-csrf-attack"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#what-is-a-csrf-attack"}},[e._v("#")]),e._v(" What is a CSRF Attack?")]),e._v(" "),o("p",[e._v("The best way to understand a CSRF attack is by taking a look at a concrete example.")]),e._v(" "),o("p",[e._v("Assume that your bank’s website provides a form that allows transferring money from the currently logged in user to another bank account.\nFor example, the transfer form might look like:")]),e._v(" "),o("p",[e._v("Example 1. Transfer form")]),e._v(" "),o("div",{staticClass:"language- extra-class"},[o("pre",{pre:!0,attrs:{class:"language-text"}},[o("code",[e._v('<form method="post"\n\taction="/transfer">\n<input type="text"\n\tname="amount"/>\n<input type="text"\n\tname="routingNumber"/>\n<input type="text"\n\tname="account"/>\n<input type="submit"\n\tvalue="Transfer"/>\n</form>\n')])])]),o("p",[e._v("The corresponding HTTP request might look like:")]),e._v(" "),o("p",[e._v("Example 2. Transfer HTTP request")]),e._v(" "),o("div",{staticClass:"language- extra-class"},[o("pre",{pre:!0,attrs:{class:"language-text"}},[o("code",[e._v("POST /transfer HTTP/1.1\nHost: bank.example.com\nCookie: JSESSIONID=randomid\nContent-Type: application/x-www-form-urlencoded\n\namount=100.00&routingNumber=1234&account=9876\n")])])]),o("p",[e._v("Now pretend you authenticate to your bank’s website and then, without logging out, visit an evil website.\nThe evil website contains an HTML page with the following form:")]),e._v(" "),o("p",[e._v("Example 3. Evil transfer form")]),e._v(" "),o("div",{staticClass:"language- extra-class"},[o("pre",{pre:!0,attrs:{class:"language-text"}},[o("code",[e._v('<form method="post"\n\taction="https://bank.example.com/transfer">\n<input type="hidden"\n\tname="amount"\n\tvalue="100.00"/>\n<input type="hidden"\n\tname="routingNumber"\n\tvalue="evilsRoutingNumber"/>\n<input type="hidden"\n\tname="account"\n\tvalue="evilsAccountNumber"/>\n<input type="submit"\n\tvalue="Win Money!"/>\n</form>\n')])])]),o("p",[e._v("You like to win money, so you click on the submit button.\nIn the process, you have unintentionally transferred $100 to a malicious user.\nThis happens because, while the evil website cannot see your cookies, the cookies associated with your bank are still sent along with the request.")]),e._v(" "),o("p",[e._v("Worst yet, this whole process could have been automated using JavaScript.\nThis means you didn’t even need to click on the button.\nFurthermore, it could just as easily happen when visiting an honest site that is a victim of a "),o("a",{attrs:{href:"https://www.owasp.org/index.php/Cross-site_Scripting_(XSS)",target:"_blank",rel:"noopener noreferrer"}},[e._v("XSS attack"),o("OutboundLink")],1),e._v(".\nSo how do we protect our users from such attacks?")]),e._v(" "),o("h2",{attrs:{id:"protecting-against-csrf-attacks"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#protecting-against-csrf-attacks"}},[e._v("#")]),e._v(" Protecting Against CSRF Attacks")]),e._v(" "),o("p",[e._v("The reason that a CSRF attack is possible is that the HTTP request from the victim’s website and the request from the attacker’s website are exactly the same.\nThis means there is no way to reject requests coming from the evil website and allow requests coming from the bank’s website.\nTo protect against CSRF attacks we need to ensure there is something in the request that the evil site is unable to provide so we can differentiate the two requests.")]),e._v(" "),o("p",[e._v("Spring provides two mechanisms to protect against CSRF attacks:")]),e._v(" "),o("ul",[o("li",[o("p",[e._v("The "),o("a",{attrs:{href:"#csrf-protection-stp"}},[e._v("Synchronizer Token Pattern")])])]),e._v(" "),o("li",[o("p",[e._v("Specifying the "),o("a",{attrs:{href:"#csrf-protection-ssa"}},[e._v("SameSite Attribute")]),e._v(" on your session cookie")])])]),e._v(" "),o("table",[o("thead",[o("tr",[o("th"),e._v(" "),o("th",[e._v("Both protections require that "),o("a",{attrs:{href:"#csrf-protection-idempotent"}},[e._v("Safe Methods Must be Idempotent")])])])]),e._v(" "),o("tbody")]),e._v(" "),o("h3",{attrs:{id:"safe-methods-must-be-idempotent"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#safe-methods-must-be-idempotent"}},[e._v("#")]),e._v(" Safe Methods Must be Idempotent")]),e._v(" "),o("p",[e._v("In order for "),o("a",{attrs:{href:"#csrf-protection"}},[e._v("either protection")]),e._v(" against CSRF to work, the application must ensure that "),o("a",{attrs:{href:"https://tools.ietf.org/html/rfc7231#section-4.2.1",target:"_blank",rel:"noopener noreferrer"}},[e._v('"safe" HTTP methods are idempotent'),o("OutboundLink")],1),e._v(".\nThis means that requests with the HTTP method "),o("code",[e._v("GET")]),e._v(", "),o("code",[e._v("HEAD")]),e._v(", "),o("code",[e._v("OPTIONS")]),e._v(", and "),o("code",[e._v("TRACE")]),e._v(" should not change the state of the application.")]),e._v(" "),o("h3",{attrs:{id:"synchronizer-token-pattern"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#synchronizer-token-pattern"}},[e._v("#")]),e._v(" Synchronizer Token Pattern")]),e._v(" "),o("p",[e._v("The predominant and most comprehensive way to protect against CSRF attacks is to use the "),o("a",{attrs:{href:"https://cheatsheetseries.owasp.org/cheatsheets/Cross-Site_Request_Forgery_Prevention_Cheat_Sheet.html#synchronizer-token-pattern",target:"_blank",rel:"noopener noreferrer"}},[e._v("Synchronizer Token Pattern"),o("OutboundLink")],1),e._v(".\nThis solution is to ensure that each HTTP request requires, in addition to our session cookie, a secure random generated value called a CSRF token must be present in the HTTP request.")]),e._v(" "),o("p",[e._v("When an HTTP request is submitted, the server must look up the expected CSRF token and compare it against the actual CSRF token in the HTTP request.\nIf the values do not match, the HTTP request should be rejected.")]),e._v(" "),o("p",[e._v("The key to this working is that the actual CSRF token should be in a part of the HTTP request that is not automatically included by the browser.\nFor example, requiring the actual CSRF token in an HTTP parameter or an HTTP header will protect against CSRF attacks.\nRequiring the actual CSRF token in a cookie does not work because cookies are automatically included in the HTTP request by the browser.")]),e._v(" "),o("p",[e._v("We can relax the expectations to only require the actual CSRF token for each HTTP request that updates state of the application.\nFor that to work, our application must ensure that "),o("a",{attrs:{href:"#csrf-protection-idempotent"}},[e._v("safe HTTP methods are idempotent")]),e._v(".\nThis improves usability since we want to allow linking to our website using links from external sites.\nAdditionally, we do not want to include the random token in HTTP GET as this can cause the tokens to be leaked.")]),e._v(" "),o("p",[e._v("Let’s take a look at how "),o("a",{attrs:{href:"#csrf-explained"}},[e._v("our example")]),e._v(" would change when using the Synchronizer Token Pattern.\nAssume the actual CSRF token is required to be in an HTTP parameter named "),o("code",[e._v("_csrf")]),e._v(".\nOur application’s transfer form would look like:")]),e._v(" "),o("p",[e._v("Example 4. Synchronizer Token Form")]),e._v(" "),o("div",{staticClass:"language- extra-class"},[o("pre",{pre:!0,attrs:{class:"language-text"}},[o("code",[e._v('<form method="post"\n\taction="/transfer">\n<input type="hidden"\n\tname="_csrf"\n\tvalue="4bfd1575-3ad1-4d21-96c7-4ef2d9f86721"/>\n<input type="text"\n\tname="amount"/>\n<input type="text"\n\tname="routingNumber"/>\n<input type="hidden"\n\tname="account"/>\n<input type="submit"\n\tvalue="Transfer"/>\n</form>\n')])])]),o("p",[e._v("The form now contains a hidden input with the value of the CSRF token.\nExternal sites cannot read the CSRF token since the same origin policy ensures the evil site cannot read the response.")]),e._v(" "),o("p",[e._v("The corresponding HTTP request to transfer money would look like this:")]),e._v(" "),o("p",[e._v("Example 5. Synchronizer Token request")]),e._v(" "),o("div",{staticClass:"language- extra-class"},[o("pre",{pre:!0,attrs:{class:"language-text"}},[o("code",[e._v("POST /transfer HTTP/1.1\nHost: bank.example.com\nCookie: JSESSIONID=randomid\nContent-Type: application/x-www-form-urlencoded\n\namount=100.00&routingNumber=1234&account=9876&_csrf=4bfd1575-3ad1-4d21-96c7-4ef2d9f86721\n")])])]),o("p",[e._v("You will notice that the HTTP request now contains the "),o("code",[e._v("_csrf")]),e._v(" parameter with a secure random value.\nThe evil website will not be able to provide the correct value for the "),o("code",[e._v("_csrf")]),e._v(" parameter (which must be explicitly provided on the evil website) and the transfer will fail when the server compares the actual CSRF token to the expected CSRF token.")]),e._v(" "),o("h3",{attrs:{id:"samesite-attribute"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#samesite-attribute"}},[e._v("#")]),e._v(" SameSite Attribute")]),e._v(" "),o("p",[e._v("An emerging way to protect against "),o("a",{attrs:{href:"#csrf"}},[e._v("CSRF Attacks")]),e._v(" is to specify the "),o("a",{attrs:{href:"https://tools.ietf.org/html/draft-west-first-party-cookies",target:"_blank",rel:"noopener noreferrer"}},[e._v("SameSite Attribute"),o("OutboundLink")],1),e._v(" on cookies.\nA server can specify the "),o("code",[e._v("SameSite")]),e._v(" attribute when setting a cookie to indicate that the cookie should not be sent when coming from external sites.")]),e._v(" "),o("table",[o("thead",[o("tr",[o("th"),e._v(" "),o("th",[e._v("Spring Security does not directly control the creation of the session cookie, so it does not provide support for the SameSite attribute."),o("a",{attrs:{href:"https://spring.io/projects/spring-session",target:"_blank",rel:"noopener noreferrer"}},[e._v("Spring Session"),o("OutboundLink")],1),e._v(" provides support for the "),o("code",[e._v("SameSite")]),e._v(" attribute in servlet based applications."),o("br"),e._v("Spring Framework’s "),o("a",{attrs:{href:"https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/server/session/CookieWebSessionIdResolver.html",target:"_blank",rel:"noopener noreferrer"}},[e._v("CookieWebSessionIdResolver"),o("OutboundLink")],1),e._v(" provides out of the box support for the "),o("code",[e._v("SameSite")]),e._v(" attribute in WebFlux based applications.")])])]),e._v(" "),o("tbody")]),e._v(" "),o("p",[e._v("An example, HTTP response header with the "),o("code",[e._v("SameSite")]),e._v(" attribute might look like:")]),e._v(" "),o("p",[e._v("Example 6. SameSite HTTP response")]),e._v(" "),o("div",{staticClass:"language- extra-class"},[o("pre",{pre:!0,attrs:{class:"language-text"}},[o("code",[e._v("Set-Cookie: JSESSIONID=randomid; Domain=bank.example.com; Secure; HttpOnly; SameSite=Lax\n")])])]),o("p",[e._v("Valid values for the "),o("code",[e._v("SameSite")]),e._v(" attribute are:")]),e._v(" "),o("ul",[o("li",[o("p",[o("code",[e._v("Strict")]),e._v(" - when specified any request coming from the "),o("a",{attrs:{href:"https://tools.ietf.org/html/draft-west-first-party-cookies-07#section-2.1",target:"_blank",rel:"noopener noreferrer"}},[e._v("same-site"),o("OutboundLink")],1),e._v(" will include the cookie.\nOtherwise, the cookie will not be included in the HTTP request.")])]),e._v(" "),o("li",[o("p",[o("code",[e._v("Lax")]),e._v(" - when specified cookies will be sent when coming from the "),o("a",{attrs:{href:"https://tools.ietf.org/html/draft-west-first-party-cookies-07#section-2.1",target:"_blank",rel:"noopener noreferrer"}},[e._v("same-site"),o("OutboundLink")],1),e._v(" or when the request comes from top-level navigations and the "),o("a",{attrs:{href:"#csrf-protection-idempotent"}},[e._v("method is idempotent")]),e._v(".\nOtherwise, the cookie will not be included in the HTTP request.")])])]),e._v(" "),o("p",[e._v("Let’s take a look at how "),o("a",{attrs:{href:"#csrf-explained"}},[e._v("our example")]),e._v(" could be protected using the "),o("code",[e._v("SameSite")]),e._v(" attribute.\nThe bank application can protect against CSRF by specifying the "),o("code",[e._v("SameSite")]),e._v(" attribute on the session cookie.")]),e._v(" "),o("p",[e._v("With the "),o("code",[e._v("SameSite")]),e._v(" attribute set on our session cookie, the browser will continue to send the "),o("code",[e._v("JSESSIONID")]),e._v(" cookie with requests coming from the banking website.\nHowever, the browser will no longer send the "),o("code",[e._v("JSESSIONID")]),e._v(" cookie with a transfer request coming from the evil website.\nSince the session is no longer present in the transfer request coming from the evil website, the application is protected from the CSRF attack.")]),e._v(" "),o("p",[e._v("There are some important "),o("a",{attrs:{href:"https://tools.ietf.org/html/draft-west-first-party-cookies-07#section-5",target:"_blank",rel:"noopener noreferrer"}},[e._v("considerations"),o("OutboundLink")],1),e._v(" that one should be aware about when using "),o("code",[e._v("SameSite")]),e._v(" attribute to protect against CSRF attacks.")]),e._v(" "),o("p",[e._v("Setting the "),o("code",[e._v("SameSite")]),e._v(" attribute to "),o("code",[e._v("Strict")]),e._v(" provides a stronger defense but can confuse users.\nConsider a user that stays logged into a social media site hosted at "),o("a",{attrs:{href:"https://social.example.com",target:"_blank",rel:"noopener noreferrer"}},[e._v("https://social.example.com"),o("OutboundLink")],1),e._v(".\nThe user receives an email at "),o("a",{attrs:{href:"https://email.example.org",target:"_blank",rel:"noopener noreferrer"}},[e._v("https://email.example.org"),o("OutboundLink")],1),e._v(" that includes a link to the social media site.\nIf the user clicks on the link, they would rightfully expect to be authenticated to the social media site.\nHowever, if the "),o("code",[e._v("SameSite")]),e._v(" attribute is "),o("code",[e._v("Strict")]),e._v(" the cookie would not be sent and so the user would not be authenticated.")]),e._v(" "),o("table",[o("thead",[o("tr",[o("th"),e._v(" "),o("th",[e._v("We could improve the protection and usability of "),o("code",[e._v("SameSite")]),e._v(" protection against CSRF attacks by implementing "),o("a",{attrs:{href:"https://github.com/spring-projects/spring-security/issues/7537",target:"_blank",rel:"noopener noreferrer"}},[e._v("gh-7537"),o("OutboundLink")],1),e._v(".")])])]),e._v(" "),o("tbody")]),e._v(" "),o("p",[e._v("Another obvious consideration is that in order for the "),o("code",[e._v("SameSite")]),e._v(" attribute to protect users, the browser must support the "),o("code",[e._v("SameSite")]),e._v(" attribute.\nMost modern browsers do "),o("a",{attrs:{href:"https://developer.mozilla.org/en-US/docs/Web/HTTP/headers/Set-Cookie#Browser_compatibility",target:"_blank",rel:"noopener noreferrer"}},[e._v("support the SameSite attribute"),o("OutboundLink")],1),e._v(".\nHowever, older browsers that are still in use may not.")]),e._v(" "),o("p",[e._v("For this reason, it is generally recommended to use the "),o("code",[e._v("SameSite")]),e._v(" attribute as a defense in depth rather than the sole protection against CSRF attacks.")]),e._v(" "),o("h2",{attrs:{id:"when-to-use-csrf-protection"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#when-to-use-csrf-protection"}},[e._v("#")]),e._v(" When to use CSRF protection")]),e._v(" "),o("p",[e._v("When should you use CSRF protection?\nOur recommendation is to use CSRF protection for any request that could be processed by a browser by normal users.\nIf you are only creating a service that is used by non-browser clients, you will likely want to disable CSRF protection.")]),e._v(" "),o("h3",{attrs:{id:"csrf-protection-and-json"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#csrf-protection-and-json"}},[e._v("#")]),e._v(" CSRF protection and JSON")]),e._v(" "),o("p",[e._v('A common question is "do I need to protect JSON requests made by javascript?"\nThe short answer is, it depends.\nHowever, you must be very careful as there are CSRF exploits that can impact JSON requests.\nFor example, a malicious user can create a '),o("a",{attrs:{href:"http://blog.opensecurityresearch.com/2012/02/json-csrf-with-parameter-padding.html",target:"_blank",rel:"noopener noreferrer"}},[e._v("CSRF with JSON using the following form"),o("OutboundLink")],1),e._v(":")]),e._v(" "),o("p",[e._v("Example 7. CSRF with JSON form")]),e._v(" "),o("div",{staticClass:"language- extra-class"},[o("pre",{pre:!0,attrs:{class:"language-text"}},[o("code",[e._v('<form action="https://bank.example.com/transfer" method="post" enctype="text/plain">\n\t<input name=\'{"amount":100,"routingNumber":"evilsRoutingNumber","account":"evilsAccountNumber", "ignore_me":"\' value=\'test"}\' type=\'hidden\'>\n\t<input type="submit"\n\t\tvalue="Win Money!"/>\n</form>\n')])])]),o("p",[e._v("This will produce the following JSON structure")]),e._v(" "),o("p",[e._v("Example 8. CSRF with JSON request")]),e._v(" "),o("div",{staticClass:"language- extra-class"},[o("pre",{pre:!0,attrs:{class:"language-text"}},[o("code",[e._v('{ "amount": 100,\n"routingNumber": "evilsRoutingNumber",\n"account": "evilsAccountNumber",\n"ignore_me": "=test"\n}\n')])])]),o("p",[e._v("If an application were not validating the Content-Type, then it would be exposed to this exploit.\nDepending on the setup, a Spring MVC application that validates the Content-Type could still be exploited by updating the URL suffix to end with "),o("code",[e._v(".json")]),e._v(" as shown below:")]),e._v(" "),o("p",[e._v("Example 9. CSRF with JSON Spring MVC form")]),e._v(" "),o("div",{staticClass:"language- extra-class"},[o("pre",{pre:!0,attrs:{class:"language-text"}},[o("code",[e._v('<form action="https://bank.example.com/transfer.json" method="post" enctype="text/plain">\n\t<input name=\'{"amount":100,"routingNumber":"evilsRoutingNumber","account":"evilsAccountNumber", "ignore_me":"\' value=\'test"}\' type=\'hidden\'>\n\t<input type="submit"\n\t\tvalue="Win Money!"/>\n</form>\n')])])]),o("h3",{attrs:{id:"csrf-and-stateless-browser-applications"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#csrf-and-stateless-browser-applications"}},[e._v("#")]),e._v(" CSRF and Stateless Browser Applications")]),e._v(" "),o("p",[e._v("What if my application is stateless?\nThat doesn’t necessarily mean you are protected.\nIn fact, if a user does not need to perform any actions in the web browser for a given request, they are likely still vulnerable to CSRF attacks.")]),e._v(" "),o("p",[e._v("For example, consider an application that uses a custom cookie that contains all the state within it for authentication instead of the JSESSIONID.\nWhen the CSRF attack is made the custom cookie will be sent with the request in the same manner that the JSESSIONID cookie was sent in our previous example.\nThis application will be vulnerable to CSRF attacks.")]),e._v(" "),o("p",[e._v("Applications that use basic authentication are also vulnerable to CSRF attacks.\nThe application is vulnerable since the browser will automatically include the username and password in any requests in the same manner that the JSESSIONID cookie was sent in our previous example.")]),e._v(" "),o("h2",{attrs:{id:"csrf-considerations"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#csrf-considerations"}},[e._v("#")]),e._v(" CSRF Considerations")]),e._v(" "),o("p",[e._v("There are a few special considerations to consider when implementing protection against CSRF attacks.")]),e._v(" "),o("h3",{attrs:{id:"logging-in"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#logging-in"}},[e._v("#")]),e._v(" Logging In")]),e._v(" "),o("p",[e._v("In order to protect against "),o("a",{attrs:{href:"https://en.wikipedia.org/wiki/Cross-site_request_forgery#Forging_login_requests",target:"_blank",rel:"noopener noreferrer"}},[e._v("forging log in requests"),o("OutboundLink")],1),e._v(" the log in HTTP request should be protected against CSRF attacks.\nProtecting against forging log in requests is necessary so that a malicious user cannot read a victim’s sensitive information.\nThe attack is performed as follows:")]),e._v(" "),o("ul",[o("li",[o("p",[e._v("A malicious user performs a CSRF log in using the malicious user’s credentials.\nThe victim is now authenticated as the malicious user.")])]),e._v(" "),o("li",[o("p",[e._v("The malicious user then tricks the victim to visit the compromised website and enter sensitive information")])]),e._v(" "),o("li",[o("p",[e._v("The information is associated to the malicious user’s account so the malicious user can log in with their own credentials and view the vicitim’s sensitive information")])])]),e._v(" "),o("p",[e._v("A possible complication to ensuring log in HTTP requests are protected against CSRF attacks is that the user might experience a session timeout that causes the request to be rejected.\nA session timeout is surprising to users who do not expect to need to have a session in order to log in.\nFor more information refer to "),o("a",{attrs:{href:"#csrf-considerations-timeouts"}},[e._v("CSRF and Session Timeouts")]),e._v(".")]),e._v(" "),o("h3",{attrs:{id:"logging-out"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#logging-out"}},[e._v("#")]),e._v(" Logging Out")]),e._v(" "),o("p",[e._v("In order to protect against forging log out requests, the log out HTTP request should be protected against CSRF attacks.\nProtecting against forging log out requests is necessary so a malicious user cannot read a victim’s sensitive information.\nFor details on the attack refer to "),o("a",{attrs:{href:"https://labs.detectify.com/2017/03/15/loginlogout-csrf-time-to-reconsider/",target:"_blank",rel:"noopener noreferrer"}},[e._v("this blog post"),o("OutboundLink")],1),e._v(".")]),e._v(" "),o("p",[e._v("A possible complication to ensuring log out HTTP requests are protected against CSRF attacks is that the user might experience a session timeout that causes the request to be rejected.\nA session timeout is surprising to users who do not expect to need to have a session in order to log out.\nFor more information refer to "),o("a",{attrs:{href:"#csrf-considerations-timeouts"}},[e._v("CSRF and Session Timeouts")]),e._v(".")]),e._v(" "),o("h3",{attrs:{id:"csrf-and-session-timeouts"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#csrf-and-session-timeouts"}},[e._v("#")]),e._v(" CSRF and Session Timeouts")]),e._v(" "),o("p",[e._v("More often than not, the expected CSRF token is stored in the session.\nThis means that as soon as the session expires the server will not find an expected CSRF token and reject the HTTP request.\nThere are a number of options to solve timeouts each of which come with trade offs.")]),e._v(" "),o("ul",[o("li",[o("p",[e._v("The best way to mitigate the timeout is by using JavaScript to request a CSRF token on form submission.\nThe form is then updated with the CSRF token and submitted.")])]),e._v(" "),o("li",[o("p",[e._v("Another option is to have some JavaScript that lets the user know their session is about to expire.\nThe user can click a button to continue and refresh the session.")])]),e._v(" "),o("li",[o("p",[e._v("Finally, the expected CSRF token could be stored in a cookie.\nThis allows the expected CSRF token to outlive the session.")]),e._v(" "),o("p",[e._v("One might ask why the expected CSRF token isn’t stored in a cookie by default.\nThis is because there are known exploits in which headers (for example, to specify the cookies) can be set by another domain.\nThis is the same reason Ruby on Rails "),o("a",{attrs:{href:"https://weblog.rubyonrails.org/2011/2/8/csrf-protection-bypass-in-ruby-on-rails/",target:"_blank",rel:"noopener noreferrer"}},[e._v("no longer skips CSRF checks when the header X-Requested-With is present"),o("OutboundLink")],1),e._v(".\nSee "),o("a",{attrs:{href:"https://web.archive.org/web/20210221120355/https://lists.webappsec.org/pipermail/websecurity_lists.webappsec.org/2011-February/007533.html",target:"_blank",rel:"noopener noreferrer"}},[e._v("this webappsec.org thread"),o("OutboundLink")],1),e._v(" for details on how to perform the exploit.\nAnother disadvantage is that by removing the state (that is, the timeout), you lose the ability to forcibly invalidate the token if it is compromised.")])])]),e._v(" "),o("h3",{attrs:{id:""}},[o("a",{staticClass:"header-anchor",attrs:{href:"#"}},[e._v("#")])]),e._v(" "),o("p",[e._v("Protecting multipart requests (file uploads) from CSRF attacks causes a "),o("a",{attrs:{href:"https://en.wikipedia.org/wiki/Chicken_or_the_egg",target:"_blank",rel:"noopener noreferrer"}},[e._v("chicken and the egg"),o("OutboundLink")],1),e._v(" problem.\nIn order to prevent a CSRF attack from occurring, the body of the HTTP request must be read to obtain actual CSRF token.\nHowever, reading the body means that the file will be uploaded which means an external site can upload a file.")]),e._v(" "),o("p",[e._v("There are two options to using CSRF protection with multipart/form-data.\nEach option has its trade-offs.")]),e._v(" "),o("ul",[o("li",[o("p",[o("a",{attrs:{href:"#csrf-considerations-multipart-body"}},[e._v("Place CSRF Token in the Body")])])]),e._v(" "),o("li",[o("p",[o("a",{attrs:{href:"#csrf-considerations-multipart-url"}},[e._v("Place CSRF Token in the URL")])])])]),e._v(" "),o("table",[o("thead",[o("tr",[o("th"),e._v(" "),o("th",[e._v("Before you integrate Spring Security’s CSRF protection with multipart file upload, ensure that you can upload without the CSRF protection first."),o("br"),e._v("More information about using multipart forms with Spring can be found within the "),o("a",{attrs:{href:"https://docs.spring.io/spring/docs/5.2.x/spring-framework-reference/web.html#mvc-multipart",target:"_blank",rel:"noopener noreferrer"}},[e._v("1.1.11. Multipart Resolver"),o("OutboundLink")],1),e._v(" section of the Spring reference and the "),o("a",{attrs:{href:"https://docs.spring.io/spring/docs/5.2.x/javadoc-api/org/springframework/web/multipart/support/MultipartFilter.html",target:"_blank",rel:"noopener noreferrer"}},[e._v("MultipartFilter javadoc"),o("OutboundLink")],1),e._v(".")])])]),e._v(" "),o("tbody")]),e._v(" "),o("h4",{attrs:{id:"place-csrf-token-in-the-body"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#place-csrf-token-in-the-body"}},[e._v("#")]),e._v(" Place CSRF Token in the Body")]),e._v(" "),o("p",[e._v("The first option is to include the actual CSRF token in the body of the request.\nBy placing the CSRF token in the body, the body will be read before authorization is performed.\nThis means that anyone can place temporary files on your server.\nHowever, only authorized users will be able to submit a file that is processed by your application.\nIn general, this is the recommended approach because the temporary file upload should have a negligible impact on most servers.")]),e._v(" "),o("h4",{attrs:{id:"include-csrf-token-in-url"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#include-csrf-token-in-url"}},[e._v("#")]),e._v(" Include CSRF Token in URL")]),e._v(" "),o("p",[e._v("If allowing unauthorized users to upload temporary files is not acceptable, an alternative is to include the expected CSRF token as a query parameter in the action attribute of the form.\nThe disadvantage to this approach is that query parameters can be leaked.\nMore generally, it is considered best practice to place sensitive data within the body or headers to ensure it is not leaked.\nAdditional information can be found in "),o("a",{attrs:{href:"https://www.w3.org/Protocols/rfc2616/rfc2616-sec15.html#sec15.1.3",target:"_blank",rel:"noopener noreferrer"}},[e._v("RFC 2616 Section 15.1.3 Encoding Sensitive Information in URI’s"),o("OutboundLink")],1),e._v(".")]),e._v(" "),o("h4",{attrs:{id:"hiddenhttpmethodfilter"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#hiddenhttpmethodfilter"}},[e._v("#")]),e._v(" HiddenHttpMethodFilter")]),e._v(" "),o("p",[e._v("In some applications a form parameter can be used to override the HTTP method.\nFor example, the form below could be used to treat the HTTP method as a "),o("code",[e._v("delete")]),e._v(" rather than a "),o("code",[e._v("post")]),e._v(".")]),e._v(" "),o("p",[e._v("Example 10. CSRF Hidden HTTP Method Form")]),e._v(" "),o("div",{staticClass:"language- extra-class"},[o("pre",{pre:!0,attrs:{class:"language-text"}},[o("code",[e._v('<form action="/process"\n\tmethod="post">\n\t\x3c!-- ... --\x3e\n\t<input type="hidden"\n\t\tname="_method"\n\t\tvalue="delete"/>\n</form>\n')])])]),o("p",[e._v("Overriding the HTTP method occurs in a filter.\nThat filter must be placed before Spring Security’s support.\nNote that overriding only happens on a "),o("code",[e._v("post")]),e._v(", so this is actually unlikely to cause any real problems.\nHowever, it is still best practice to ensure it is placed before Spring Security’s filters.")])])}),[],!1,null,null,null);t.default=r.exports}}]);