ArpitB

Bug 1563246 - Reload the Public Suffix List when data is upd

Aug 16th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 7.52 KB | None | 0 0
  1. diff --git a/netwerk/dns/nsEffectiveTLDService.cpp b/netwerk/dns/nsEffectiveTLDService.cpp
  2. --- a/netwerk/dns/nsEffectiveTLDService.cpp
  3. +++ b/netwerk/dns/nsEffectiveTLDService.cpp
  4. @@ -9,18 +9,23 @@
  5.  // http://wiki.mozilla.org/Gecko:Effective_TLD_Service
  6.  
  7.  #include "mozilla/ArrayUtils.h"
  8. +#include "mozilla/AutoMemMap.h"
  9.  #include "mozilla/HashFunctions.h"
  10.  #include "mozilla/Maybe.h"
  11.  #include "mozilla/MemoryReporting.h"
  12. +#include "mozilla/ResultExtensions.h"
  13.  
  14.  #include "MainThreadUtils.h"
  15. +#include "nsCRT.h"
  16.  #include "nsEffectiveTLDService.h"
  17. +#include "nsIFile.h"
  18.  #include "nsIIDNService.h"
  19. -#include "nsNetUtil.h"
  20. -#include "prnetdb.h"
  21. +#include "nsIObserverService.h"
  22.  #include "nsIURI.h"
  23.  #include "nsNetCID.h"
  24. +#include "nsNetUtil.h"
  25.  #include "nsServiceManagerUtils.h"
  26. +#include "prnetdb.h"
  27.  
  28.  namespace etld_dafsa {
  29.  
  30. @@ -32,7 +37,7 @@ namespace etld_dafsa {
  31.  using namespace mozilla;
  32.  
  33.  NS_IMPL_ISUPPORTS(nsEffectiveTLDService, nsIEffectiveTLDService,
  34. -                  nsIMemoryReporter)
  35. +                  nsIMemoryReporter, nsIObserver)
  36.  
  37.  // ----------------------------------------------------------------------
  38.  
  39. @@ -42,6 +47,9 @@ nsEffectiveTLDService::nsEffectiveTLDSer
  40.      : mIDNService(), mGraph(etld_dafsa::kDafsa) {}
  41.  
  42.  nsresult nsEffectiveTLDService::Init() {
  43. +  nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
  44. +  obs->AddObserver(this, "public-suffix-list-updated", false);
  45. +
  46.    if (gService) {
  47.      return NS_ERROR_ALREADY_INITIALIZED;
  48.    }
  49. @@ -56,6 +64,30 @@ nsresult nsEffectiveTLDService::Init() {
  50.    return NS_OK;
  51.  }
  52.  
  53. +NS_IMETHODIMP nsEffectiveTLDService::Observe(nsISupports* aSubject,
  54. +                                             const char* aTopic,
  55. +                                             const char16_t* aData) {
  56. +  /*
  57. +   * Signal sent from netwerk/dns/PublicSuffixList.jsm
  58. +   * aSubject is the nsIFile object for dafsa.bin
  59. +   * aData is the absolute path to the dafsa.bin file (not used)
  60. +   */
  61. +  if (aSubject && (nsCRT::strcmp(aTopic, "public-suffix-list-updated") == 0)) {
  62. +    nsCOMPtr<nsIFile> mDafsaBinFile(do_QueryInterface(aSubject));
  63. +    NS_ENSURE_TRUE(mDafsaBinFile, NS_ERROR_ILLEGAL_VALUE);
  64. +
  65. +    loader::AutoMemMap mMap;
  66. +    MOZ_TRY(mMap.init(mDafsaBinFile));
  67. +
  68. +    uint32_t size = mMap.size();
  69. +    const unsigned char* remoteDafsaPtr = mMap.get<unsigned char>().get();
  70. +
  71. +    nsEffectiveTLDService::mGraph =
  72. +        mozilla::Dafsa(MakeSpan(remoteDafsaPtr, size));
  73. +  }
  74. +  return NS_OK;
  75. +}
  76. +
  77.  nsEffectiveTLDService::~nsEffectiveTLDService() {
  78.    UnregisterWeakMemoryReporter(this);
  79.    if (mIDNService) {
  80. diff --git a/netwerk/dns/nsEffectiveTLDService.h b/netwerk/dns/nsEffectiveTLDService.h
  81. --- a/netwerk/dns/nsEffectiveTLDService.h
  82. +++ b/netwerk/dns/nsEffectiveTLDService.h
  83. @@ -8,23 +8,27 @@
  84.  
  85.  #include "nsIEffectiveTLDService.h"
  86.  
  87. -#include "nsHashKeys.h"
  88. -#include "nsIMemoryReporter.h"
  89. -#include "nsString.h"
  90. -#include "nsCOMPtr.h"
  91.  #include "mozilla/Attributes.h"
  92.  #include "mozilla/Dafsa.h"
  93.  #include "mozilla/MemoryReporting.h"
  94.  #include "mozilla/MruCache.h"
  95.  
  96. +#include "nsCOMPtr.h"
  97. +#include "nsHashKeys.h"
  98. +#include "nsIMemoryReporter.h"
  99. +#include "nsIObserver.h"
  100. +#include "nsString.h"
  101. +
  102.  class nsIIDNService;
  103.  
  104.  class nsEffectiveTLDService final : public nsIEffectiveTLDService,
  105. +                                    public nsIObserver,
  106.                                      public nsIMemoryReporter {
  107.   public:
  108.    NS_DECL_ISUPPORTS
  109.    NS_DECL_NSIEFFECTIVETLDSERVICE
  110.    NS_DECL_NSIMEMORYREPORTER
  111. +  NS_DECL_NSIOBSERVER
  112.  
  113.    nsEffectiveTLDService();
  114.    nsresult Init();
  115. diff --git a/netwerk/dns/tests/unit/data/fake_public_suffix_list.dat b/netwerk/dns/tests/unit/data/fake_public_suffix_list.dat
  116. new file mode 100644
  117. --- /dev/null
  118. +++ b/netwerk/dns/tests/unit/data/fake_public_suffix_list.dat
  119. @@ -0,0 +1,56 @@
  120. +// This Source Code Form is subject to the terms of the Mozilla Public
  121. +// License, v. 2.0. If a copy of the MPL was not distributed with this
  122. +// file, You can obtain one at https://mozilla.org/MPL/2.0/.
  123. +
  124. +// This is a fake suffix list created only for the purposes of testing,
  125. +// The real PSL is rather large thus this file is kept small by cutting out most of the suffixes
  126. +
  127. +// The Original list can be found at https://publicsuffix.org/list/public_suffix_list.dat,
  128. +// Learn more about the PSL at https://publicsuffix.org.
  129. +
  130. +// .xpcshelltest is the fake domain created specifically for the the tests while
  131. +// the others are ripped from the real list, serving as examples.
  132. +
  133. +// This fake public suffix list was used to create the binary file dafsa_xpcShellTest.bin
  134. +// found at netwerk/dns/tests/unit/data/ along with this file.
  135. +
  136. +// The list created with help of netwerk/dns/prepare_tlds.py and xpcom/ds/tools/make_dafsa.py
  137. +// To rebuild the DAFSA, simply pull the two python scripts and run
  138. +// $ python3 prepare_tlds.py fake_public_suffix_list.dat --bin > dafsa_xpcShellTest.bin
  139. +
  140. +
  141. +// ===BEGIN ICANN DOMAINS===
  142. +
  143. +// xpcshelltest : Used in tests
  144. +xpcshelltest
  145. +website.xpcshelltest
  146. +com.xpcshelltest
  147. +edu.xpcshelltest
  148. +gov.xpcshelltest
  149. +net.xpcshelltest
  150. +mil.xpcshelltest
  151. +org.xpcshelltest
  152. +
  153. +// ac : https://en.wikipedia.org/wiki/.ac
  154. +ac
  155. +coc.ac
  156. +com.ac
  157. +edu.ac
  158. +gov.ac
  159. +net.ac
  160. +mil.ac
  161. +org.ac
  162. +
  163. +// bj : https://en.wikipedia.org/wiki/.bj
  164. +bj
  165. +asso.bj
  166. +barreau.bj
  167. +gouv.bj
  168. +
  169. +// bm : http://www.bermudanic.bm/dnr-text.txt
  170. +bm
  171. +com.bm
  172. +edu.bm
  173. +gov.bm
  174. +net.bm
  175. +org.bm
  176. diff --git a/netwerk/dns/tests/unit/test_nsEffectiveTLDService_Reload_DAFSA.js b/netwerk/dns/tests/unit/test_nsEffectiveTLDService_Reload_DAFSA.js
  177. new file mode 100644
  178. --- /dev/null
  179. +++ b/netwerk/dns/tests/unit/test_nsEffectiveTLDService_Reload_DAFSA.js
  180. @@ -0,0 +1,32 @@
  181. +"use strict";
  182. +
  183. +const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
  184. +const eTLDService = Services.eTLD;
  185. +
  186. +const SIGNAL = "public-suffix-list-updated";
  187. +
  188. +add_task(async () => {
  189. +  info("Fake Suffix does not exist in current PSL.");
  190. +
  191. +  let suffix = eTLDService.getPublicSuffixFromHost("website.xpcshelltest");
  192. +  Assert.notEqual(
  193. +    suffix, // suffix is actually "xpcshelltest"
  194. +    "website.xpcshelltest",
  195. +    "Fake Suffix does not exist in current PSL."
  196. +  );
  197. +});
  198. +
  199. +add_task(async () => {
  200. +  info("Fake Suffix now exists in PSL after DAFSA reload.");
  201. +
  202. +  // reload the PSL with fake data containing .xpcshelltest
  203. +  const fakeDafsaFile = do_get_file("data/dafsa_xpcShellTest.bin");
  204. +  Services.obs.notifyObservers(fakeDafsaFile, SIGNAL, fakeDafsaFile.path);
  205. +
  206. +  let suffix = eTLDService.getPublicSuffixFromHost("website.xpcshelltest");
  207. +  Assert.equal(
  208. +    suffix,
  209. +    "website.xpcshelltest",
  210. +    "Fake Suffix now exists in PSL after DAFSA reload."
  211. +  );
  212. +});
  213. diff --git a/netwerk/dns/tests/unit/xpcshell.ini b/netwerk/dns/tests/unit/xpcshell.ini
  214. --- a/netwerk/dns/tests/unit/xpcshell.ini
  215. +++ b/netwerk/dns/tests/unit/xpcshell.ini
  216. @@ -4,3 +4,4 @@ firefox-appdir = browser
  217.  support-files = data/**
  218.  
  219.  [test_PublicSuffixList.js]
  220. +[test_nsEffectiveTLDService_Reload_DAFSA.js]
  221. diff --git a/xpcom/ds/Dafsa.h b/xpcom/ds/Dafsa.h
  222. --- a/xpcom/ds/Dafsa.h
  223. +++ b/xpcom/ds/Dafsa.h
  224. @@ -33,6 +33,11 @@ class Dafsa {
  225.     */
  226.    explicit Dafsa(const Graph& aData) : mData(aData) {}
  227.  
  228. +  Dafsa& operator=(Dafsa other) {
  229. +    mData = Graph(other.mData);
  230. +    return *this;
  231. +  }
  232. +
  233.    ~Dafsa() = default;
  234.  
  235.    /**
  236. @@ -46,7 +51,7 @@ class Dafsa {
  237.    static const int kKeyNotFound;
  238.  
  239.   private:
  240. -  const Graph mData;
  241. +  Graph mData;
  242.  };
  243.  
  244.  }  // namespace mozilla
Add Comment
Please, Sign In to add comment