legacy.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /**
  2. * PrivateBin
  3. *
  4. * a zero-knowledge paste bin
  5. *
  6. * @see {@link https://github.com/PrivateBin/PrivateBin}
  7. * @copyright 2012 Sébastien SAUVAGE ({@link http://sebsauvage.net})
  8. * @license {@link https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License}
  9. * @version 1.3
  10. * @name Legacy
  11. * @namespace
  12. *
  13. * IMPORTANT NOTICE FOR DEVELOPERS:
  14. * The logic in this file is intended to run in legacy browsers. Avoid any use of:
  15. * - ES6 or newer in general
  16. * - const/let, use the traditional var declarations instead
  17. * - async/await or Promises, use traditional callbacks
  18. * - shorthand function notation "() => output", use the full "function() {return output;}" style
  19. * - IE doesn't support:
  20. * - URL(), use the traditional window.location object
  21. * - endsWith(), use indexof()
  22. * - yes, this logic needs to support IE 5 or 6, to at least display the error message
  23. */
  24. // main application start, called when DOM is fully loaded
  25. jQuery(document).ready(function() {
  26. 'use strict';
  27. // run main controller
  28. $.Legacy.Check.init();
  29. });
  30. jQuery.Legacy = (function($) {
  31. 'use strict';
  32. /**
  33. * compatibility check
  34. *
  35. * @name Check
  36. * @class
  37. */
  38. var Check = (function () {
  39. var me = {};
  40. /**
  41. * Status of the initial check, true means it passed
  42. *
  43. * @private
  44. * @prop {bool}
  45. */
  46. var status = false;
  47. /**
  48. * Initialization check did run
  49. *
  50. * @private
  51. * @prop {bool}
  52. */
  53. var init = false;
  54. /**
  55. * blacklist of UserAgents (parts) known to belong to a bot
  56. *
  57. * @private
  58. * @enum {Array}
  59. * @readonly
  60. */
  61. var badBotUA = [
  62. 'Bot',
  63. 'bot'
  64. ];
  65. /**
  66. * whitelist of top level domains to consider a secure context,
  67. * regardless of protocol
  68. *
  69. * @private
  70. * @enum {Array}
  71. * @readonly
  72. */
  73. var tld = [
  74. '.onion',
  75. '.i2p'
  76. ];
  77. /**
  78. * whitelist of hostnames to consider a secure context,
  79. * regardless of protocol
  80. *
  81. * @private
  82. * @enum {Array}
  83. * @readonly
  84. */
  85. // whitelists of TLDs & local hostnames
  86. var hostname = [
  87. 'localhost',
  88. '127.0.0.1',
  89. '[::1]'
  90. ];
  91. /**
  92. * check if the context is secure
  93. *
  94. * @private
  95. * @name Check.isSecureContext
  96. * @function
  97. * @return {bool}
  98. */
  99. function isSecureContext()
  100. {
  101. // use .isSecureContext if available
  102. if (window.isSecureContext === true || window.isSecureContext === false) {
  103. return window.isSecureContext;
  104. }
  105. // HTTP is obviously insecure
  106. if (window.location.protocol !== 'http:') {
  107. return true;
  108. }
  109. // filter out actually secure connections over HTTP
  110. for (var i = 0; i < tld.length; i++) {
  111. if (
  112. window.location.hostname.indexOf(
  113. tld[i],
  114. window.location.hostname.length - tld[i].length
  115. ) !== -1
  116. ) {
  117. return true;
  118. }
  119. }
  120. // whitelist localhost for development
  121. for (var j = 0; j < hostname.length; j++) {
  122. if (window.location.hostname === hostname[j]) {
  123. return true;
  124. }
  125. }
  126. // totally INSECURE http protocol!
  127. return false;
  128. }
  129. /**
  130. * checks whether this is a bot we dislike
  131. *
  132. * @private
  133. * @name Check.isBadBot
  134. * @function
  135. * @return {bool}
  136. */
  137. function isBadBot() {
  138. // check whether a bot user agent part can be found in the current
  139. // user agent
  140. for (var i = 0; i < badBotUA.length; i++) {
  141. if (navigator.userAgent.indexOf(badBotUA[i]) !== -1) {
  142. return true;
  143. }
  144. }
  145. return false;
  146. }
  147. /**
  148. * checks whether this is an unsupported browser, via feature detection
  149. *
  150. * @private
  151. * @name Check.isOldBrowser
  152. * @function
  153. * @return {bool}
  154. */
  155. function isOldBrowser() {
  156. // webcrypto support
  157. if (!(
  158. 'crypto' in window &&
  159. 'getRandomValues' in window.crypto &&
  160. 'subtle' in window.crypto &&
  161. 'encrypt' in window.crypto.subtle &&
  162. 'decrypt' in window.crypto.subtle &&
  163. 'Uint8Array' in window &&
  164. 'Uint32Array' in window
  165. )) {
  166. return true;
  167. }
  168. // not checking for async/await, ES6 or Promise support, as most
  169. // browsers introduced these earlier then webassembly and webcrypto:
  170. // https://github.com/PrivateBin/PrivateBin/pull/431#issuecomment-493129359
  171. return false;
  172. }
  173. /**
  174. * returns if the check has concluded
  175. *
  176. * @name Check.getInit
  177. * @function
  178. * @return {bool}
  179. */
  180. me.getInit = function()
  181. {
  182. return init;
  183. }
  184. /**
  185. * returns the current status of the check
  186. *
  187. * @name Check.getStatus
  188. * @function
  189. * @return {bool}
  190. */
  191. me.getStatus = function()
  192. {
  193. return status;
  194. }
  195. /**
  196. * init on application start, returns an all-clear signal
  197. *
  198. * @name Check.init
  199. * @function
  200. */
  201. me.init = function()
  202. {
  203. // prevent bots from viewing a paste and potentially deleting data
  204. // when burn-after-reading is set
  205. if (isBadBot()) {
  206. $.PrivateBin.Alert.showError('I love you too, bot…');
  207. init = true;
  208. return;
  209. }
  210. if (isOldBrowser()) {
  211. // some browsers (Chrome based ones) would have webcrypto support if using HTTPS
  212. if (!isSecureContext()) {
  213. $.PrivateBin.Alert.showError(['Your browser may require an HTTPS connection to support the WebCrypto API. Try <a href="%s">switching to HTTPS</a>.', 'https' + window.location.href.slice(4)]);
  214. }
  215. $('#oldnotice').removeClass('hidden');
  216. init = true;
  217. return;
  218. }
  219. if (!isSecureContext()) {
  220. $('#httpnotice').removeClass('hidden');
  221. }
  222. init = true;
  223. // only if everything passed, we set the status to true
  224. status = true;
  225. }
  226. return me;
  227. })();
  228. return {
  229. Check: Check
  230. };
  231. })(jQuery);