legacy.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. * shows an error message
  175. *
  176. * @private
  177. * @name Check.showError
  178. * @param {string} message
  179. * @function
  180. */
  181. function showError(message)
  182. {
  183. var $error = $('#errormessage'),
  184. $glyphIcon = $error.find(':first'),
  185. $element;
  186. if ($glyphIcon.length) {
  187. // if there is an icon, we need to provide an inner element
  188. // to translate the message into, instead of the parent
  189. $element = $('<span>');
  190. $error.html(' ').prepend($glyphIcon).append($element);
  191. } else {
  192. $element = $error;
  193. }
  194. if (message.indexOf('<a') === -1) {
  195. $element.text(message);
  196. } else {
  197. $element.html(message);
  198. }
  199. $error.removeClass('hidden');
  200. }
  201. /**
  202. * returns if the check has concluded
  203. *
  204. * @name Check.getInit
  205. * @function
  206. * @return {bool}
  207. */
  208. me.getInit = function()
  209. {
  210. return init;
  211. };
  212. /**
  213. * returns the current status of the check
  214. *
  215. * @name Check.getStatus
  216. * @function
  217. * @return {bool}
  218. */
  219. me.getStatus = function()
  220. {
  221. return status;
  222. };
  223. /**
  224. * init on application start, returns an all-clear signal
  225. *
  226. * @name Check.init
  227. * @function
  228. */
  229. me.init = function()
  230. {
  231. // prevent bots from viewing a paste and potentially deleting data
  232. // when burn-after-reading is set
  233. if (isBadBot()) {
  234. showError('I love you too, bot…');
  235. init = true;
  236. return;
  237. }
  238. if (isOldBrowser()) {
  239. // some browsers (Chrome based ones) would have webcrypto support if using HTTPS
  240. if (!isSecureContext()) {
  241. showError(
  242. 'Your browser may require an HTTPS connection to support the WebCrypto API. Try <a href="%s">switching to HTTPS</a>.'.replace(
  243. '%s',
  244. 'https' + window.location.href.slice(4)
  245. )
  246. );
  247. }
  248. $('#oldnotice').removeClass('hidden');
  249. init = true;
  250. return;
  251. }
  252. if (!isSecureContext()) {
  253. $('#httpnotice').removeClass('hidden');
  254. }
  255. init = true;
  256. // only if everything passed, we set the status to true
  257. status = true;
  258. };
  259. return me;
  260. })();
  261. return {
  262. Check: Check
  263. };
  264. })(jQuery);