test_network_security-hpkp.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <!DOCTYPE HTML>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf8">
  5. <title>Test for the network actor (HPKP detection)</title>
  6. <script type="text/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  7. <script type="text/javascript;version=1.8" src="common.js"></script>
  8. <!-- Any copyright is dedicated to the Public Domain.
  9. - http://creativecommons.org/publicdomain/zero/1.0/ -->
  10. </head>
  11. <body>
  12. <p>Test for the network actor (HPKP detection)</p>
  13. <iframe src="https://example.com/chrome/devtools/shared/webconsole/test/network_requests_iframe.html"></iframe>
  14. <script class="testbody" type="text/javascript;version=1.8">
  15. SimpleTest.waitForExplicitFinish();
  16. let gCurrentTestCase = -1;
  17. const HPKP_ENABLED_PREF = "security.cert_pinning.hpkp.enabled";
  18. const PROCESS_HPKP_FROM_NON_BUILTIN_ROOTS_PREF = "security.cert_pinning.process_headers_from_non_builtin_roots";
  19. // Static pins tested by unit/test_security-info-static-hpkp.js.
  20. const TEST_CASES = [
  21. {
  22. desc: "no Public Key Pinning",
  23. url: "https://example.com",
  24. usesPinning: false,
  25. },
  26. {
  27. desc: "dynamic Public Key Pinning with this request",
  28. url: "https://include-subdomains.pinning-dynamic.example.com/" +
  29. "browser/browser/base/content/test/general/pinning_headers.sjs",
  30. usesPinning: true,
  31. },
  32. {
  33. desc: "dynamic Public Key Pinning with previous request",
  34. url: "https://include-subdomains.pinning-dynamic.example.com/",
  35. usesPinning: true,
  36. }
  37. ];
  38. function startTest()
  39. {
  40. Services.prefs.setBoolPref(HPKP_ENABLED_PREF, true);
  41. Services.prefs.setBoolPref(PROCESS_HPKP_FROM_NON_BUILTIN_ROOTS_PREF, true);
  42. SimpleTest.registerCleanupFunction(() => {
  43. Services.prefs.setBoolPref(HPKP_ENABLED_PREF, false);
  44. Services.prefs.setBoolPref(PROCESS_HPKP_FROM_NON_BUILTIN_ROOTS_PREF, false);
  45. // Reset pinning state.
  46. let gSSService = Cc["@mozilla.org/ssservice;1"]
  47. .getService(Ci.nsISiteSecurityService);
  48. let gIOService = Cc["@mozilla.org/network/io-service;1"]
  49. .getService(Ci.nsIIOService);
  50. for (let {url} of TEST_CASES) {
  51. let uri = gIOService.newURI(url, null, null);
  52. gSSService.removeState(Ci.nsISiteSecurityService.HEADER_HPKP, uri, 0);
  53. }
  54. });
  55. info("Test detection of Public Key Pinning.");
  56. removeEventListener("load", startTest);
  57. attachConsoleToTab(["NetworkActivity"], onAttach);
  58. }
  59. function onAttach(aState, aResponse)
  60. {
  61. onNetworkEventUpdate = onNetworkEventUpdate.bind(null, aState);
  62. aState.dbgClient.addListener("networkEventUpdate", onNetworkEventUpdate);
  63. runNextCase(aState);
  64. }
  65. function runNextCase(aState) {
  66. gCurrentTestCase++;
  67. if (gCurrentTestCase === TEST_CASES.length) {
  68. info("Tests ran. Cleaning up.");
  69. closeDebugger(aState, SimpleTest.finish);
  70. return;
  71. }
  72. let { desc, url } = TEST_CASES[gCurrentTestCase];
  73. info("Testing site with " + desc);
  74. let iframe = document.querySelector("iframe").contentWindow;
  75. iframe.wrappedJSObject.makeXhrCallback("GET", url);
  76. }
  77. function onNetworkEventUpdate(aState, aType, aPacket)
  78. {
  79. function onSecurityInfo(packet) {
  80. let data = TEST_CASES[gCurrentTestCase];
  81. is(packet.securityInfo.hpkp, data.usesPinning,
  82. "Public Key Pinning detected correctly.");
  83. runNextCase(aState);
  84. }
  85. if (aPacket.updateType === "securityInfo") {
  86. aState.client.getSecurityInfo(aPacket.from, onSecurityInfo);
  87. }
  88. }
  89. addEventListener("load", startTest);
  90. </script>
  91. </body>
  92. </html>