Bug 739040 followup: properly enforce the sniffing limit on uncompressed feeds, r=mano, a=desktop-only
authorGavin Sharp <gavin@gavinsharp.com>
Sat, 21 Apr 2012 02:22:52 -0400
changeset 93884 0dd02041e575b08a009b0909603bff9ad25951f2
parent 93883 e466bffc6a7bc931bd80de4c8a274f6630698fa1
child 93885 479edc4c0af425bb818507c6b40f4a613862ea4b
push idunknown
push userunknown
push dateunknown
reviewersmano, desktop-only
bugs739040
milestone14.0a1
Bug 739040 followup: properly enforce the sniffing limit on uncompressed feeds, r=mano, a=desktop-only
browser/components/feeds/src/nsFeedSniffer.cpp
browser/components/feeds/test/Makefile.in
browser/components/feeds/test/chrome/Makefile.in
browser/components/feeds/test/chrome/test_maxSniffing.html
browser/components/feeds/test/valid-feed.xml
browser/components/feeds/test/valid-unsniffable-feed.xml
--- a/browser/components/feeds/src/nsFeedSniffer.cpp
+++ b/browser/components/feeds/src/nsFeedSniffer.cpp
@@ -308,31 +308,33 @@ nsFeedSniffer::GetMIMETypeFromContent(ns
     return NS_OK;
   }
 
   // Now we need to potentially decompress data served with 
   // Content-Encoding: gzip
   nsresult rv = ConvertEncodedData(request, data, length);
   if (NS_FAILED(rv))
     return rv;
-  
-  const char* testData = 
-    mDecodedData.IsEmpty() ? (const char*)data : mDecodedData.get();
+
+  // We cap the number of bytes to scan at MAX_BYTES to prevent picking up 
+  // false positives by accidentally reading document content, e.g. a "how to
+  // make a feed" page.
+  const char* testData;
+  if (mDecodedData.IsEmpty()) {
+    testData = (const char*)data;
+    length = NS_MIN(length, MAX_BYTES);
+  } else {
+    testData = mDecodedData.get();
+    length = NS_MIN(mDecodedData.Length(), MAX_BYTES);
+  }
 
   // The strategy here is based on that described in:
   // http://blogs.msdn.com/rssteam/articles/PublishersGuide.aspx
   // for interoperarbility purposes.
 
-  // We cap the number of bytes to scan at MAX_BYTES to prevent picking up 
-  // false positives by accidentally reading document content, e.g. a "how to
-  // make a feed" page.
-  if (!mDecodedData.IsEmpty()) {
-    length = NS_MIN(mDecodedData.Length(), MAX_BYTES);
-  }
-
   // Thus begins the actual sniffing.
   nsDependentCSubstring dataString((const char*)testData, length);
 
   bool isFeed = false;
 
   // RSS 0.91/0.92/2.0
   isFeed = ContainsTopLevelSubstring(dataString, "<rss");
 
--- a/browser/components/feeds/test/Makefile.in
+++ b/browser/components/feeds/test/Makefile.in
@@ -53,12 +53,14 @@ include $(topsrcdir)/config/rules.mk
 
 _TEST_FILES =	bug408328-data.xml \
 		bug368464-data.xml \
 		test_bug494328.html \
 		bug494328-data.xml \
 		test_bug589543.html \
 		bug589543-data.xml \
 		test_registerHandler.html \
+		valid-feed.xml \
+		valid-unsniffable-feed.xml \
 		$(NULL)
 
 libs:: $(_TEST_FILES)
 	$(INSTALL) $(foreach f,$^,"$f") $(DEPTH)/_tests/testing/mochitest/tests/$(relativesrcdir)
--- a/browser/components/feeds/test/chrome/Makefile.in
+++ b/browser/components/feeds/test/chrome/Makefile.in
@@ -48,16 +48,17 @@ include $(topsrcdir)/config/rules.mk
 _HTTP_FILES	= \
 		sample_feed.atom \
 		$(NULL)
 
 _CHROME_FILES	= \
 		test_423060.xul \
 		test_bug368464.html \
 		test_bug408328.html \
+		test_maxSniffing.html \
 		$(NULL)
 
 libs:: $(_HTTP_FILES)
 	$(INSTALL) $(foreach f,$^,"$f") $(DEPTH)/_tests/testing/mochitest/tests/$(relativesrcdir)
 
 libs:: $(_CHROME_FILES)
 	$(INSTALL) $(foreach f,$^,"$f") $(DEPTH)/_tests/testing/mochitest/chrome/$(relativesrcdir)
 
copy from browser/components/feeds/test/chrome/test_bug368464.html
copy to browser/components/feeds/test/chrome/test_maxSniffing.html
--- a/browser/components/feeds/test/chrome/test_bug368464.html
+++ b/browser/components/feeds/test/chrome/test_maxSniffing.html
@@ -1,32 +1,37 @@
 <!DOCTYPE HTML>
 <html>
 <!--
-https://bugzilla.mozilla.org/show_bug.cgi?id=368464
+https://bugzilla.mozilla.org/show_bug.cgi?id=739040
 -->
 <head>
-  <title>Test that RSS 0.90 isn't sniffed</title>
+  <title>Test that we only sniff 512 bytes</title>
   <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
   <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"/>
 </head>
 <body>
-<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=368464">Mozilla Bug 368464</a>
-<p id="display"><iframe id="testFrame" src="http://mochi.test:8888/tests/browser/components/feeds/test/bug368464-data.xml"></iframe></p>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=739040">Mozilla Bug 739040</a>
+<p id="display">
+  <iframe id="validTestFrame" src="http://mochi.test:8888/tests/browser/components/feeds/test/valid-feed.xml"></iframe>
+  <iframe id="unsniffableTestFrame" src="http://mochi.test:8888/tests/browser/components/feeds/test/valid-unsniffable-feed.xml"></iframe>
+</p>
 <div id="content" style="display: none">
 
 </div>
 <pre id="test">
 <script class="testbody" type="text/javascript">
 
-/** Test for Bug 368464 **/
+/** Test for Bug 739040 **/
 SimpleTest.waitForExplicitFinish();
 
 addLoadEvent(function() {
-  ok($("testFrame").contentDocument.documentElement.id != "feedHandler",
-     "RSS 0.90 shouldn't be sniffed as a feed");
+  is($("validTestFrame").contentDocument.documentElement.id, "feedHandler",
+     "valid feed should be sniffed");
+  isnot($("unsniffableTestFrame").contentDocument.documentElement.id, "feedHandler",
+        "unsniffable feed should not be sniffed");
 });
 addLoadEvent(SimpleTest.finish);
 
 </script>
 </pre>
 </body>
 </html>
copy from browser/components/feeds/test/bug589543-data.xml
copy to browser/components/feeds/test/valid-feed.xml
copy from browser/components/feeds/test/bug589543-data.xml
copy to browser/components/feeds/test/valid-unsniffable-feed.xml
--- a/browser/components/feeds/test/bug589543-data.xml
+++ b/browser/components/feeds/test/valid-unsniffable-feed.xml
@@ -1,9 +1,18 @@
 <?xml version="1.0" encoding="utf-8"?>
+<!-- 512 bytes!
+xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
+xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
+xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
+xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
+xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
+xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
+xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
+  -->
 <feed xmlns="http://www.w3.org/2005/Atom">
 
   <title>Example Feed</title>
   <link href="http://example.org/"/>
   <updated>2010-08-22T18:30:02Z</updated>
 
   <author>
     <name>John Doe</name>