1
0

Check.js 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. 'use strict';
  2. const common = require('../common');
  3. const fc = require('fast-check');
  4. /* global Legacy, WebCrypto */
  5. describe('Check', function () {
  6. describe('init', function () {
  7. this.timeout(30000);
  8. it('returns false and shows error, if a bot UA is detected', () => {
  9. fc.assert(fc.property(
  10. fc.string(),
  11. fc.constantFrom('Bot', 'bot'),
  12. fc.string(),
  13. function (prefix, botBit, suffix) {
  14. const clean = globalThis.cleanup(
  15. '<html><body><div id="errormessage" class="hidden"></div>' +
  16. '</body></html>', {
  17. 'userAgent': prefix + botBit + suffix
  18. }
  19. );
  20. Legacy.Check.init();
  21. const result = Legacy.Check.getInit() && !Legacy.Check.getStatus();
  22. clean();
  23. return result;
  24. }
  25. ),
  26. {numRuns: 10});
  27. });
  28. it('shows error, if no webcrypto is detected', () => {
  29. fc.assert(fc.property(
  30. fc.boolean(),
  31. fc.constantFrom('localhost', '127.0.0.1', '[::1]', ''),
  32. fc.array(common.fcA2zString(), {minLength: 1}),
  33. fc.constantFrom('.onion', '.i2p', ''),
  34. function (secureProtocol, localhost, domain, tld) {
  35. const isDomain = localhost === '',
  36. isSecureContext = secureProtocol || !isDomain || tld.length > 0,
  37. clean = globalThis.cleanup(
  38. '<html><body><div id="errormessage" class="hidden"></div>' +
  39. '<div id="oldnotice" class="hidden"></div>' +
  40. '<div id="insecurecontextnotice" class="hidden"></div></body></html>',
  41. {
  42. 'url': (secureProtocol ? 'https' : 'http' ) + '://' +
  43. (isDomain ? domain.join('') + tld : localhost) + '/'
  44. }
  45. );
  46. Legacy.Check.init();
  47. const result1 = Legacy.Check.getInit() && !Legacy.Check.getStatus(),
  48. result2 = isSecureContext === (document.getElementById('insecurecontextnotice').className === 'hidden'),
  49. result3 = (document.getElementById('oldnotice').className !== 'hidden');
  50. clean();
  51. if (result1 && result2 && result3) {
  52. return true;
  53. }
  54. console.log(result1, result2, result3);
  55. return false;
  56. }
  57. ));
  58. });
  59. it('shows error, if HTTP only site is detected', () => {
  60. fc.assert(fc.property(
  61. fc.boolean(),
  62. fc.array(common.fcA2zString(), {minLength: 1}),
  63. function (secureProtocol, domain) {
  64. const clean = globalThis.cleanup(
  65. '<html><body><div id="httpnotice" class="hidden"></div>' +
  66. '</body></html>',
  67. {
  68. 'url': (secureProtocol ? 'https' : 'http' ) + '://' + domain.join('') + '/'
  69. }
  70. );
  71. Object.defineProperty(window, 'crypto', {
  72. value: new WebCrypto(),
  73. configurable: true,
  74. enumerable: true,
  75. writable: false
  76. });
  77. Legacy.Check.init();
  78. const result1 = Legacy.Check.getInit() && Legacy.Check.getStatus(),
  79. result2 = secureProtocol === (document.getElementById('httpnotice').className === 'hidden');
  80. clean();
  81. if (result1 && result2) {
  82. return true;
  83. }
  84. console.log(result1, result2);
  85. return false;
  86. }
  87. ));
  88. });
  89. });
  90. });