legacy.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. * @type {string[]}
  54. * @readonly
  55. */
  56. var badBotUA = [
  57. // Generic bot identifiers
  58. 'bot',
  59. 'Bot',
  60. 'crawler',
  61. 'Crawler',
  62. 'spider',
  63. 'Spider',
  64. 'scraper',
  65. 'Scraper',
  66. // Search Engines
  67. 'Mediapartners-Google',
  68. 'BingPreview',
  69. 'Yahoo! Slurp',
  70. // SEO & Analytics
  71. 'Screaming Frog',
  72. // Social Media
  73. 'facebookexternalhit',
  74. // AI & LLM
  75. 'ChatGPT-User',
  76. 'anthropic-ai',
  77. // Security Scanners
  78. 'CensysInspect',
  79. 'Shodan',
  80. // Other Common Crawlers
  81. '80legs',
  82. 'ia_archiver',
  83. 'Teoma',
  84. ];
  85. /**
  86. * whitelist of top level domains to consider a secure context,
  87. * regardless of protocol
  88. *
  89. * @private
  90. * @enum {Array}
  91. * @readonly
  92. */
  93. var tld = [
  94. '.onion',
  95. '.i2p'
  96. ];
  97. /**
  98. * whitelist of hostnames to consider a secure context,
  99. * regardless of protocol
  100. *
  101. * @private
  102. * @enum {Array}
  103. * @readonly
  104. */
  105. // whitelists of TLDs & local hostnames
  106. var hostname = [
  107. 'localhost',
  108. '127.0.0.1',
  109. '[::1]'
  110. ];
  111. /**
  112. * check if the context is secure
  113. *
  114. * @private
  115. * @name Check.isSecureContext
  116. * @function
  117. * @return {bool}
  118. */
  119. function isSecureContext()
  120. {
  121. // use .isSecureContext if available
  122. if (window.isSecureContext === true || window.isSecureContext === false) {
  123. return window.isSecureContext;
  124. }
  125. // HTTPS is considered secure
  126. if (window.location.protocol === 'https:') {
  127. return true;
  128. }
  129. // filter out actually secure connections over HTTP
  130. for (var i = 0; i < tld.length; i++) {
  131. if (
  132. window.location.hostname.indexOf(
  133. tld[i],
  134. window.location.hostname.length - tld[i].length
  135. ) !== -1
  136. ) {
  137. return true;
  138. }
  139. }
  140. // whitelist localhost for development
  141. for (var j = 0; j < hostname.length; j++) {
  142. if (window.location.hostname === hostname[j]) {
  143. return true;
  144. }
  145. }
  146. // totally INSECURE http protocol!
  147. return false;
  148. }
  149. /**
  150. * checks whether this is a bot we dislike
  151. *
  152. * @private
  153. * @name Check.isBadBot
  154. * @function
  155. * @return {bool}
  156. */
  157. function isBadBot() {
  158. // check whether a bot user agent part can be found in the current
  159. // user agent
  160. for (var i = 0; i < badBotUA.length; i++) {
  161. if (navigator.userAgent.indexOf(badBotUA[i]) !== -1) {
  162. return true;
  163. }
  164. }
  165. return false;
  166. }
  167. /**
  168. * checks whether this is an unsupported browser, via feature detection
  169. *
  170. * @private
  171. * @name Check.isOldBrowser
  172. * @function
  173. * @return {bool}
  174. */
  175. function isOldBrowser() {
  176. // webcrypto support
  177. if (!(
  178. 'crypto' in window &&
  179. 'getRandomValues' in window.crypto &&
  180. 'subtle' in window.crypto &&
  181. 'encrypt' in window.crypto.subtle &&
  182. 'decrypt' in window.crypto.subtle &&
  183. 'Uint8Array' in window &&
  184. 'Uint32Array' in window
  185. )) {
  186. return true;
  187. }
  188. return false;
  189. }
  190. /**
  191. * shows an error message
  192. *
  193. * @private
  194. * @name Check.showError
  195. * @param {string} message
  196. * @function
  197. */
  198. function showError(message)
  199. {
  200. var element = document.getElementById('errormessage');
  201. if (message.indexOf('<a') === -1) {
  202. element.appendChild(
  203. document.createTextNode(message)
  204. );
  205. } else {
  206. element.innerHTML = message;
  207. }
  208. removeHiddenFromId('errormessage');
  209. }
  210. /**
  211. * removes "hidden" CSS class from element with given ID
  212. *
  213. * @private
  214. * @name Check.removeHiddenFromId
  215. * @param {string} id
  216. * @function
  217. */
  218. function removeHiddenFromId(id)
  219. {
  220. var element = document.getElementById(id);
  221. if (element) {
  222. element.className = element.className.replace(/\bhidden\b/g, '');
  223. }
  224. }
  225. /**
  226. * returns if the check has concluded
  227. *
  228. * @name Check.getInit
  229. * @function
  230. * @return {bool}
  231. */
  232. me.getInit = function()
  233. {
  234. return init;
  235. };
  236. /**
  237. * returns the current status of the check
  238. *
  239. * @name Check.getStatus
  240. * @function
  241. * @return {bool}
  242. */
  243. me.getStatus = function()
  244. {
  245. return status;
  246. };
  247. /**
  248. * init on application start, returns an all-clear signal
  249. *
  250. * @name Check.init
  251. * @function
  252. */
  253. me.init = function()
  254. {
  255. // prevent early init
  256. if (typeof document === 'undefined' || typeof navigator === 'undefined' || typeof window === 'undefined') {
  257. return;
  258. }
  259. // prevent bots from viewing a document and potentially deleting data
  260. // when burn-after-reading is set
  261. if (isBadBot()) {
  262. showError('I love you too, bot…');
  263. init = true;
  264. return;
  265. }
  266. if (isOldBrowser()) {
  267. // some browsers (Chrome based ones) would have webcrypto support if using HTTPS
  268. if (!isSecureContext()) {
  269. removeHiddenFromId('insecurecontextnotice');
  270. }
  271. removeHiddenFromId('oldnotice');
  272. init = true;
  273. return;
  274. }
  275. if (!isSecureContext()) {
  276. removeHiddenFromId('httpnotice');
  277. }
  278. init = true;
  279. // only if everything passed, we set the status to true
  280. status = true;
  281. };
  282. return me;
  283. })();
  284. // main application start, called when DOM is fully loaded
  285. if (document.readyState === 'complete' || (!document.attachEvent && document.readyState === 'interactive')) {
  286. Check.init();
  287. } else {
  288. if (document.addEventListener) {
  289. // first choice is DOMContentLoaded event
  290. document.addEventListener('DOMContentLoaded', Check.init, false);
  291. // backup is window load event
  292. window.addEventListener('load', Check.init, false);
  293. } else {
  294. // must be IE
  295. document.attachEvent('onreadystatechange', Check.init);
  296. window.attachEvent('onload', Check.init);
  297. }
  298. }
  299. this.Legacy = {
  300. Check: Check
  301. };
  302. }).call(this);