1
0

legacy.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. * @name Legacy
  10. * @namespace
  11. */
  12. /**
  13. * IMPORTANT NOTICE FOR DEVELOPERS:
  14. * The logic in this file is intended to run in legacy browsers. Avoid any use of:
  15. * - jQuery (doesn't work in older browsers)
  16. * - ES5 or newer in general
  17. * - const/let, use the traditional var declarations instead
  18. * - async/await or Promises, use traditional callbacks
  19. * - shorthand function notation "() => output", use the full "function() {return output;}" style
  20. * - IE doesn't support:
  21. * - URL(), use the traditional window.location object
  22. * - endsWith(), use indexof()
  23. * - yes, this logic needs to support IE 6, to at least display the error message
  24. */
  25. 'use strict';
  26. (function() {
  27. /**
  28. * compatibility check
  29. *
  30. * @name Check
  31. * @class
  32. */
  33. var Check = (function () {
  34. var me = {};
  35. /**
  36. * Status of the initial check, true means it passed
  37. *
  38. * @private
  39. * @prop {bool}
  40. */
  41. var status = false;
  42. /**
  43. * Initialization check did run
  44. *
  45. * @private
  46. * @prop {bool}
  47. */
  48. var init = false;
  49. /**
  50. * blacklist of UserAgents (parts) known to belong to a bot
  51. *
  52. * @private
  53. * @enum {Array}
  54. * @readonly
  55. */
  56. var badBotUA = [
  57. 'Bot',
  58. 'bot'
  59. ];
  60. /**
  61. * whitelist of top level domains to consider a secure context,
  62. * regardless of protocol
  63. *
  64. * @private
  65. * @enum {Array}
  66. * @readonly
  67. */
  68. var tld = [
  69. '.onion',
  70. '.i2p'
  71. ];
  72. /**
  73. * whitelist of hostnames to consider a secure context,
  74. * regardless of protocol
  75. *
  76. * @private
  77. * @enum {Array}
  78. * @readonly
  79. */
  80. // whitelists of TLDs & local hostnames
  81. var hostname = [
  82. 'localhost',
  83. '127.0.0.1',
  84. '[::1]'
  85. ];
  86. /**
  87. * check if the context is secure
  88. *
  89. * @private
  90. * @name Check.isSecureContext
  91. * @function
  92. * @return {bool}
  93. */
  94. function isSecureContext()
  95. {
  96. // use .isSecureContext if available
  97. if (window.isSecureContext === true || window.isSecureContext === false) {
  98. return window.isSecureContext;
  99. }
  100. // HTTPS is considered secure
  101. if (window.location.protocol === 'https:') {
  102. return true;
  103. }
  104. // filter out actually secure connections over HTTP
  105. for (var i = 0; i < tld.length; i++) {
  106. if (
  107. window.location.hostname.indexOf(
  108. tld[i],
  109. window.location.hostname.length - tld[i].length
  110. ) !== -1
  111. ) {
  112. return true;
  113. }
  114. }
  115. // whitelist localhost for development
  116. for (var j = 0; j < hostname.length; j++) {
  117. if (window.location.hostname === hostname[j]) {
  118. return true;
  119. }
  120. }
  121. // totally INSECURE http protocol!
  122. return false;
  123. }
  124. /**
  125. * checks whether this is a bot we dislike
  126. *
  127. * @private
  128. * @name Check.isBadBot
  129. * @function
  130. * @return {bool}
  131. */
  132. function isBadBot() {
  133. // check whether a bot user agent part can be found in the current
  134. // user agent
  135. for (var i = 0; i < badBotUA.length; i++) {
  136. if (navigator.userAgent.indexOf(badBotUA[i]) !== -1) {
  137. return true;
  138. }
  139. }
  140. return false;
  141. }
  142. /**
  143. * checks whether this is an unsupported browser, via feature detection
  144. *
  145. * @private
  146. * @name Check.isOldBrowser
  147. * @function
  148. * @return {bool}
  149. */
  150. function isOldBrowser() {
  151. // webcrypto support
  152. if (!(
  153. 'crypto' in window &&
  154. 'getRandomValues' in window.crypto &&
  155. 'subtle' in window.crypto &&
  156. 'encrypt' in window.crypto.subtle &&
  157. 'decrypt' in window.crypto.subtle &&
  158. 'Uint8Array' in window &&
  159. 'Uint32Array' in window
  160. )) {
  161. return true;
  162. }
  163. return false;
  164. }
  165. /**
  166. * shows an error message
  167. *
  168. * @private
  169. * @name Check.showError
  170. * @param {string} message
  171. * @function
  172. */
  173. function showError(message)
  174. {
  175. var element = document.getElementById('errormessage');
  176. if (message.indexOf('<a') === -1) {
  177. element.appendChild(
  178. document.createTextNode(message)
  179. );
  180. } else {
  181. element.innerHTML = message;
  182. }
  183. removeHiddenFromId('errormessage');
  184. }
  185. /**
  186. * removes "hidden" CSS class from element with given ID
  187. *
  188. * @private
  189. * @name Check.removeHiddenFromId
  190. * @param {string} id
  191. * @function
  192. */
  193. function removeHiddenFromId(id)
  194. {
  195. var element = document.getElementById(id);
  196. if (element) {
  197. element.className = element.className.replace(/\bhidden\b/g, '');
  198. }
  199. }
  200. /**
  201. * returns if the check has concluded
  202. *
  203. * @name Check.getInit
  204. * @function
  205. * @return {bool}
  206. */
  207. me.getInit = function()
  208. {
  209. return init;
  210. };
  211. /**
  212. * returns the current status of the check
  213. *
  214. * @name Check.getStatus
  215. * @function
  216. * @return {bool}
  217. */
  218. me.getStatus = function()
  219. {
  220. return status;
  221. };
  222. /**
  223. * init on application start, returns an all-clear signal
  224. *
  225. * @name Check.init
  226. * @function
  227. */
  228. me.init = function()
  229. {
  230. // prevent early init
  231. if (typeof document === 'undefined' || typeof navigator === 'undefined' || typeof window === 'undefined') {
  232. return;
  233. }
  234. // prevent bots from viewing a document and potentially deleting data
  235. // when burn-after-reading is set
  236. if (isBadBot()) {
  237. showError('I love you too, bot…');
  238. init = true;
  239. return;
  240. }
  241. if (isOldBrowser()) {
  242. // some browsers (Chrome based ones) would have webcrypto support if using HTTPS
  243. if (!isSecureContext()) {
  244. removeHiddenFromId('insecurecontextnotice');
  245. }
  246. removeHiddenFromId('oldnotice');
  247. init = true;
  248. return;
  249. }
  250. if (!isSecureContext()) {
  251. removeHiddenFromId('httpnotice');
  252. }
  253. init = true;
  254. // only if everything passed, we set the status to true
  255. status = true;
  256. };
  257. return me;
  258. })();
  259. // main application start, called when DOM is fully loaded
  260. if (document.readyState === 'complete' || (!document.attachEvent && document.readyState === 'interactive')) {
  261. Check.init();
  262. } else {
  263. if (document.addEventListener) {
  264. // first choice is DOMContentLoaded event
  265. document.addEventListener('DOMContentLoaded', Check.init, false);
  266. // backup is window load event
  267. window.addEventListener('load', Check.init, false);
  268. } else {
  269. // must be IE
  270. document.attachEvent('onreadystatechange', Check.init);
  271. window.attachEvent('onload', Check.init);
  272. }
  273. }
  274. this.Legacy = {
  275. Check: Check
  276. };
  277. }).call(this);