test.js 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489
  1. 'use strict';
  2. var jsc = require('jsverify'),
  3. jsdom = require('jsdom-global'),
  4. cleanup = jsdom(),
  5. a2zString = ['a','b','c','d','e','f','g','h','i','j','k','l','m',
  6. 'n','o','p','q','r','s','t','u','v','w','x','y','z'],
  7. alnumString = a2zString.concat(['0','1','2','3','4','5','6','7','8','9']),
  8. queryString = alnumString.concat(['+','%','&','.','*','-','_']),
  9. base64String = alnumString.concat(['+','/','=']).concat(
  10. a2zString.map(function(c) {
  11. return c.toUpperCase();
  12. })
  13. ),
  14. // schemas supported by the whatwg-url library
  15. schemas = ['ftp','gopher','http','https','ws','wss'],
  16. supportedLanguages = ['de', 'es', 'fr', 'it', 'no', 'pl', 'pt', 'oc', 'ru', 'sl', 'zh'],
  17. /**
  18. * character to HTML entity lookup table
  19. *
  20. * @see {@link https://github.com/janl/mustache.js/blob/master/mustache.js#L60}
  21. */
  22. entityMap = {
  23. '&': '&',
  24. '<': '&lt;',
  25. '>': '&gt;',
  26. '"': '&quot;',
  27. "'": '&#39;',
  28. '/': '&#x2F;',
  29. '`': '&#x60;',
  30. '=': '&#x3D;'
  31. },
  32. logFile = require('fs').createWriteStream('test.log');
  33. global.$ = global.jQuery = require('./jquery-3.1.1');
  34. global.sjcl = require('./sjcl-1.0.6');
  35. global.Base64 = require('./base64-2.1.9').Base64;
  36. global.RawDeflate = require('./rawdeflate-0.5').RawDeflate;
  37. global.RawDeflate.inflate = require('./rawinflate-0.3').RawDeflate.inflate;
  38. require('./prettify');
  39. global.prettyPrint = window.PR.prettyPrint;
  40. global.prettyPrintOne = window.PR.prettyPrintOne;
  41. global.showdown = require('./showdown-1.6.1');
  42. global.DOMPurify = require('./purify-1.0.3');
  43. require('./bootstrap-3.3.7');
  44. require('./privatebin');
  45. // redirect console messages to log file
  46. console.info = console.warn = console.error = function () {
  47. logFile.write(Array.prototype.slice.call(arguments).join('') + '\n');
  48. }
  49. /**
  50. * convert all applicable characters to HTML entities
  51. *
  52. * @see {@link https://www.owasp.org/index.php/XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet#RULE_.231_-_HTML_Escape_Before_Inserting_Untrusted_Data_into_HTML_Element_Content}
  53. * @name htmlEntities
  54. * @function
  55. * @param {string} str
  56. * @return {string} escaped HTML
  57. */
  58. function htmlEntities(str) {
  59. return String(str).replace(
  60. /[&<>"'`=\/]/g, function(s) {
  61. return entityMap[s];
  62. });
  63. }
  64. describe('Helper', function () {
  65. describe('secondsToHuman', function () {
  66. after(function () {
  67. cleanup();
  68. });
  69. jsc.property('returns an array with a number and a word', 'integer', function (number) {
  70. var result = $.PrivateBin.Helper.secondsToHuman(number);
  71. return Array.isArray(result) &&
  72. result.length === 2 &&
  73. result[0] === parseInt(result[0], 10) &&
  74. typeof result[1] === 'string';
  75. });
  76. jsc.property('returns seconds on the first array position', 'integer 59', function (number) {
  77. return $.PrivateBin.Helper.secondsToHuman(number)[0] === number;
  78. });
  79. jsc.property('returns seconds on the second array position', 'integer 59', function (number) {
  80. return $.PrivateBin.Helper.secondsToHuman(number)[1] === 'second';
  81. });
  82. jsc.property('returns minutes on the first array position', 'integer 60 3599', function (number) {
  83. return $.PrivateBin.Helper.secondsToHuman(number)[0] === Math.floor(number / 60);
  84. });
  85. jsc.property('returns minutes on the second array position', 'integer 60 3599', function (number) {
  86. return $.PrivateBin.Helper.secondsToHuman(number)[1] === 'minute';
  87. });
  88. jsc.property('returns hours on the first array position', 'integer 3600 86399', function (number) {
  89. return $.PrivateBin.Helper.secondsToHuman(number)[0] === Math.floor(number / (60 * 60));
  90. });
  91. jsc.property('returns hours on the second array position', 'integer 3600 86399', function (number) {
  92. return $.PrivateBin.Helper.secondsToHuman(number)[1] === 'hour';
  93. });
  94. jsc.property('returns days on the first array position', 'integer 86400 5184000', function (number) {
  95. return $.PrivateBin.Helper.secondsToHuman(number)[0] === Math.floor(number / (60 * 60 * 24));
  96. });
  97. jsc.property('returns days on the second array position', 'integer 86400 5184000', function (number) {
  98. return $.PrivateBin.Helper.secondsToHuman(number)[1] === 'day';
  99. });
  100. // max safe integer as per http://ecma262-5.com/ELS5_HTML.htm#Section_8.5
  101. jsc.property('returns months on the first array position', 'integer 5184000 9007199254740991', function (number) {
  102. return $.PrivateBin.Helper.secondsToHuman(number)[0] === Math.floor(number / (60 * 60 * 24 * 30));
  103. });
  104. jsc.property('returns months on the second array position', 'integer 5184000 9007199254740991', function (number) {
  105. return $.PrivateBin.Helper.secondsToHuman(number)[1] === 'month';
  106. });
  107. });
  108. // this test is not yet meaningful using jsdom, as it does not contain getSelection support.
  109. // TODO: This needs to be tested using a browser.
  110. describe('selectText', function () {
  111. this.timeout(30000);
  112. jsc.property(
  113. 'selection contains content of given ID',
  114. jsc.nearray(jsc.nearray(jsc.elements(alnumString))),
  115. 'nearray string',
  116. function (ids, contents) {
  117. var html = '',
  118. result = true;
  119. ids.forEach(function(item, i) {
  120. html += '<div id="' + item.join('') + '">' + htmlEntities(contents[i] || contents[0]) + '</div>';
  121. });
  122. var clean = jsdom(html);
  123. ids.forEach(function(item, i) {
  124. $.PrivateBin.Helper.selectText(item.join(''));
  125. // TODO: As per https://github.com/tmpvar/jsdom/issues/321 there is no getSelection in jsdom, yet.
  126. // Once there is one, uncomment the line below to actually check the result.
  127. //result *= (contents[i] || contents[0]) === window.getSelection().toString();
  128. });
  129. clean();
  130. return Boolean(result);
  131. }
  132. );
  133. });
  134. describe('urls2links', function () {
  135. after(function () {
  136. cleanup();
  137. });
  138. jsc.property(
  139. 'ignores non-URL content',
  140. 'string',
  141. function (content) {
  142. return content === $.PrivateBin.Helper.urls2links(content);
  143. }
  144. );
  145. jsc.property(
  146. 'replaces URLs with anchors',
  147. 'string',
  148. jsc.elements(['http', 'https', 'ftp']),
  149. jsc.nearray(jsc.elements(a2zString)),
  150. jsc.array(jsc.elements(queryString)),
  151. jsc.array(jsc.elements(queryString)),
  152. 'string',
  153. function (prefix, schema, address, query, fragment, postfix) {
  154. var query = query.join(''),
  155. fragment = fragment.join(''),
  156. url = schema + '://' + address.join('') + '/?' + query + '#' + fragment,
  157. prefix = htmlEntities(prefix),
  158. postfix = ' ' + htmlEntities(postfix);
  159. // special cases: When the query string and fragment imply the beginning of an HTML entity, eg. &#0 or &#x
  160. if (
  161. query.slice(-1) === '&' &&
  162. (parseInt(fragment.substring(0, 1), 10) >= 0 || fragment.charAt(0) === 'x' )
  163. )
  164. {
  165. url = schema + '://' + address.join('') + '/?' + query.substring(0, query.length - 1);
  166. postfix = '';
  167. }
  168. return prefix + '<a href="' + url + '" rel="nofollow">' + url + '</a>' + postfix === $.PrivateBin.Helper.urls2links(prefix + url + postfix);
  169. }
  170. );
  171. jsc.property(
  172. 'replaces magnet links with anchors',
  173. 'string',
  174. jsc.array(jsc.elements(queryString)),
  175. 'string',
  176. function (prefix, query, postfix) {
  177. var url = 'magnet:?' + query.join('').replace(/^&+|&+$/gm,''),
  178. prefix = htmlEntities(prefix),
  179. postfix = htmlEntities(postfix);
  180. return prefix + '<a href="' + url + '" rel="nofollow">' + url + '</a> ' + postfix === $.PrivateBin.Helper.urls2links(prefix + url + ' ' + postfix);
  181. }
  182. );
  183. });
  184. describe('sprintf', function () {
  185. after(function () {
  186. cleanup();
  187. });
  188. jsc.property(
  189. 'replaces %s in strings with first given parameter',
  190. 'string',
  191. '(small nearray) string',
  192. 'string',
  193. function (prefix, params, postfix) {
  194. prefix = prefix.replace(/%(s|d)/g, '%%');
  195. params[0] = params[0].replace(/%(s|d)/g, '%%');
  196. postfix = postfix.replace(/%(s|d)/g, '%%');
  197. var result = prefix + params[0] + postfix;
  198. params.unshift(prefix + '%s' + postfix);
  199. return result === $.PrivateBin.Helper.sprintf.apply(this, params);
  200. }
  201. );
  202. jsc.property(
  203. 'replaces %d in strings with first given parameter',
  204. 'string',
  205. '(small nearray) nat',
  206. 'string',
  207. function (prefix, params, postfix) {
  208. prefix = prefix.replace(/%(s|d)/g, '%%');
  209. postfix = postfix.replace(/%(s|d)/g, '%%');
  210. var result = prefix + params[0] + postfix;
  211. params.unshift(prefix + '%d' + postfix);
  212. return result === $.PrivateBin.Helper.sprintf.apply(this, params);
  213. }
  214. );
  215. jsc.property(
  216. 'replaces %d in strings with 0 if first parameter is not a number',
  217. 'string',
  218. '(small nearray) falsy',
  219. 'string',
  220. function (prefix, params, postfix) {
  221. prefix = prefix.replace(/%(s|d)/g, '%%');
  222. postfix = postfix.replace(/%(s|d)/g, '%%');
  223. var result = prefix + '0' + postfix;
  224. params.unshift(prefix + '%d' + postfix);
  225. return result === $.PrivateBin.Helper.sprintf.apply(this, params)
  226. }
  227. );
  228. jsc.property(
  229. 'replaces %d and %s in strings in order',
  230. 'string',
  231. 'nat',
  232. 'string',
  233. 'string',
  234. 'string',
  235. function (prefix, uint, middle, string, postfix) {
  236. prefix = prefix.replace(/%(s|d)/g, '%%');
  237. middle = middle.replace(/%(s|d)/g, '%%');
  238. postfix = postfix.replace(/%(s|d)/g, '%%');
  239. var params = [prefix + '%d' + middle + '%s' + postfix, uint, string],
  240. result = prefix + uint + middle + string + postfix;
  241. return result === $.PrivateBin.Helper.sprintf.apply(this, params);
  242. }
  243. );
  244. jsc.property(
  245. 'replaces %d and %s in strings in reverse order',
  246. 'string',
  247. 'nat',
  248. 'string',
  249. 'string',
  250. 'string',
  251. function (prefix, uint, middle, string, postfix) {
  252. prefix = prefix.replace(/%(s|d)/g, '%%');
  253. middle = middle.replace(/%(s|d)/g, '%%');
  254. postfix = postfix.replace(/%(s|d)/g, '%%');
  255. var params = [prefix + '%s' + middle + '%d' + postfix, string, uint],
  256. result = prefix + string + middle + uint + postfix;
  257. return result === $.PrivateBin.Helper.sprintf.apply(this, params);
  258. }
  259. );
  260. });
  261. describe('getCookie', function () {
  262. this.timeout(30000);
  263. jsc.property(
  264. 'returns the requested cookie',
  265. 'nearray asciinestring',
  266. 'nearray asciistring',
  267. function (labels, values) {
  268. var selectedKey = '', selectedValue = '',
  269. cookieArray = [],
  270. count = 0;
  271. labels.forEach(function(item, i) {
  272. // deliberatly using a non-ascii key for replacing invalid characters
  273. var key = item.replace(/[\s;,=]/g, Array(i+2).join('£')),
  274. value = (values[i] || values[0]).replace(/[\s;,=]/g, '');
  275. cookieArray.push(key + '=' + value);
  276. if (Math.random() < 1 / i || selectedKey === key)
  277. {
  278. selectedKey = key;
  279. selectedValue = value;
  280. }
  281. });
  282. var clean = jsdom('', {cookie: cookieArray}),
  283. result = $.PrivateBin.Helper.getCookie(selectedKey);
  284. clean();
  285. return result === selectedValue;
  286. }
  287. );
  288. });
  289. describe('baseUri', function () {
  290. this.timeout(30000);
  291. before(function () {
  292. $.PrivateBin.Helper.reset();
  293. });
  294. jsc.property(
  295. 'returns the URL without query & fragment',
  296. jsc.elements(schemas),
  297. jsc.nearray(jsc.elements(a2zString)),
  298. jsc.array(jsc.elements(queryString)),
  299. 'string',
  300. function (schema, address, query, fragment) {
  301. var expected = schema + '://' + address.join('') + '/',
  302. clean = jsdom('', {url: expected + '?' + query.join('') + '#' + fragment}),
  303. result = $.PrivateBin.Helper.baseUri();
  304. $.PrivateBin.Helper.reset();
  305. clean();
  306. return expected === result;
  307. }
  308. );
  309. });
  310. describe('htmlEntities', function () {
  311. after(function () {
  312. cleanup();
  313. });
  314. jsc.property(
  315. 'removes all HTML entities from any given string',
  316. 'string',
  317. function (string) {
  318. var result = htmlEntities(string);
  319. return !(/[<>"'`=\/]/.test(result)) && !(string.indexOf('&') > -1 && !(/&amp;/.test(result)));
  320. }
  321. );
  322. });
  323. });
  324. describe('I18n', function () {
  325. describe('translate', function () {
  326. before(function () {
  327. $.PrivateBin.I18n.reset();
  328. });
  329. jsc.property(
  330. 'returns message ID unchanged if no translation found',
  331. 'string',
  332. function (messageId) {
  333. messageId = messageId.replace(/%(s|d)/g, '%%');
  334. var plurals = [messageId, messageId + 's'],
  335. fake = [messageId],
  336. result = $.PrivateBin.I18n.translate(messageId);
  337. $.PrivateBin.I18n.reset();
  338. var alias = $.PrivateBin.I18n._(messageId);
  339. $.PrivateBin.I18n.reset();
  340. var p_result = $.PrivateBin.I18n.translate(plurals);
  341. $.PrivateBin.I18n.reset();
  342. var p_alias = $.PrivateBin.I18n._(plurals);
  343. $.PrivateBin.I18n.reset();
  344. var f_result = $.PrivateBin.I18n.translate(fake);
  345. $.PrivateBin.I18n.reset();
  346. var f_alias = $.PrivateBin.I18n._(fake);
  347. $.PrivateBin.I18n.reset();
  348. return messageId === result && messageId === alias &&
  349. messageId === p_result && messageId === p_alias &&
  350. messageId === f_result && messageId === f_alias;
  351. }
  352. );
  353. jsc.property(
  354. 'replaces %s in strings with first given parameter',
  355. 'string',
  356. '(small nearray) string',
  357. 'string',
  358. function (prefix, params, postfix) {
  359. prefix = prefix.replace(/%(s|d)/g, '%%');
  360. params[0] = params[0].replace(/%(s|d)/g, '%%');
  361. postfix = postfix.replace(/%(s|d)/g, '%%');
  362. var translation = prefix + params[0] + postfix;
  363. params.unshift(prefix + '%s' + postfix);
  364. var result = $.PrivateBin.I18n.translate.apply(this, params);
  365. $.PrivateBin.I18n.reset();
  366. var alias = $.PrivateBin.I18n._.apply(this, params);
  367. $.PrivateBin.I18n.reset();
  368. return translation === result && translation === alias;
  369. }
  370. );
  371. });
  372. describe('getPluralForm', function () {
  373. before(function () {
  374. $.PrivateBin.I18n.reset();
  375. });
  376. jsc.property(
  377. 'returns valid key for plural form',
  378. jsc.elements(supportedLanguages),
  379. 'integer',
  380. function(language, n) {
  381. $.PrivateBin.I18n.reset(language);
  382. var result = $.PrivateBin.I18n.getPluralForm(n);
  383. // arabic seems to have the highest plural count with 6 forms
  384. return result >= 0 && result <= 5;
  385. }
  386. );
  387. });
  388. // loading of JSON via AJAX needs to be tested in the browser, this just mocks it
  389. // TODO: This needs to be tested using a browser.
  390. describe('loadTranslations', function () {
  391. this.timeout(30000);
  392. before(function () {
  393. $.PrivateBin.I18n.reset();
  394. });
  395. jsc.property(
  396. 'downloads and handles any supported language',
  397. jsc.elements(supportedLanguages),
  398. function(language) {
  399. var clean = jsdom('', {url: 'https://privatebin.net/', cookie: ['lang=' + language]});
  400. $.PrivateBin.I18n.reset('en');
  401. $.PrivateBin.I18n.loadTranslations();
  402. $.PrivateBin.I18n.reset(language, require('../i18n/' + language + '.json'));
  403. var result = $.PrivateBin.I18n.translate('en'),
  404. alias = $.PrivateBin.I18n._('en');
  405. clean();
  406. return language === result && language === alias;
  407. }
  408. );
  409. });
  410. });
  411. describe('CryptTool', function () {
  412. describe('cipher & decipher', function () {
  413. this.timeout(30000);
  414. it('can en- and decrypt any message', function () {
  415. jsc.check(jsc.forall(
  416. 'string',
  417. 'string',
  418. 'string',
  419. function (key, password, message) {
  420. return message === $.PrivateBin.CryptTool.decipher(
  421. key,
  422. password,
  423. $.PrivateBin.CryptTool.cipher(key, password, message)
  424. );
  425. }
  426. ),
  427. // reducing amount of checks as running 100 takes about 5 minutes
  428. {tests: 5, quiet: true});
  429. });
  430. // The below static unit tests are included to ensure deciphering of "classic"
  431. // SJCL based pastes still works
  432. it(
  433. 'supports PrivateBin v1 ciphertext (SJCL & Base64 2.1.9)',
  434. function () {
  435. // Of course you can easily decipher the following texts, if you like.
  436. // Bonus points for finding their sources and hidden meanings.
  437. var paste1 = $.PrivateBin.CryptTool.decipher(
  438. '6t2qsmLyfXIokNCL+3/yl15rfTUBQvm5SOnFPvNE7Q8=',
  439. // -- "That's amazing. I've got the same combination on my luggage."
  440. Array.apply(0, Array(6)).map(function(_,b) { return b + 1; }).join(''),
  441. '{"iv":"4HNFIl7eYbCh6HuShctTIA==","v":1,"iter":10000,"ks":256,"ts":128,"mode":"gcm","adata":"","cipher":"aes","salt":"u0lQvePq6L0=","ct":"fGPUVrDyaVr1ZDGb+kqQ3CPEW8x4YKGfzHDmA0Vjkh250aWNe7Cnigkps9aaFVMX9AaerrTp3yZbojJtNqVGMfLdUTu+53xmZHqRKxCCqSfDNSNoW4Oxk5OVgAtRyuG4bXHDsWTXDNz2xceqzVFqhkwTwlUchrV7uuFK/XUKTNjPFM744moivIcBbfM2FOeKlIFs8RYPYuvqQhp2rMLlNGwwKh//4kykQsHMQDeSDuJl8stMQzgWR/btUBZuwNZEydkMH6IPpTdf5WTSrZ+wC2OK0GutCm4UaEe6txzaTMfu+WRVu4PN6q+N+2zljWJ1XdpVcN/i0Sv4QVMym0Xa6y0eccEhj/69o47PmExmMMeEwExImPalMNT9JUSiZdOZJ/GdzwrwoIuq1mdQR6vSH+XJ/8jXJQ7bjjJVJYXTcT0Di5jixArI2Kpp1GGlGVFbLgPugwU1wczg+byqeDOAECXRRnQcogeaJtVcRwXwfy4j3ORFcblYMilxyHqKBewcYPRVBGtBs50cVjSIkAfR84rnc1nfvnxK/Gmm+4VBNHI6ODWNpRolVMCzXjbKYnV3Are5AgSpsTqaGl41VJGpcco6cAwi4K0Bys1seKR+bLSdUgqRrkEqSRSdu3/VTu9HhEk8an0rjTE4CBB5/LMn16p0TGLoOb32odKFIEtpanVvLjeyiVMvSxcgYLNnTi/5FiaAC4pJxRD+AZHedU1FICUeEXxIcac/4E5qjkHjX9SpQtLl80QLIVnjNliZm7QLB/nKu7W8Jb0+/CiTdV3Q9LhxlH4ciprnX+W0B00BKYFHnL9jRVzKdXhf1EHydbXMAfpCjHAXIVCkFakJinQBDIIw/SC6Yig0u0ddEID2B7LYAP1iE4RZwzTrxCB+ke2jQr8c20Jj6u6ShFOPC9DCw9XupZ4HAalVG00kSgjus+b8zrVji3/LKEhb4EBzp1ctBJCFTeXwej8ZETLoXTylev5dlwZSYAbuBPPcbFR/xAIPx3uDabd1E1gTqUc68ICIGhd197Mb2eRWiSvHr5SPsASerMxId6XA6+iQlRiI+NDR+TGVNmCnfxSlyPFMOHGTmslXOGIqGfBR8l4ft8YVZ70lCwmwTuViGc75ULSf9mM57/LmRzQFMYQtvI8IFK9JaQEMY5xz0HLtR4iyQUUdwR9e0ytBNdWF2a2WPDEnJuY/QJo4GzTlgv4QUxMXI5htsn2rf0HxCFu7Po8DNYLxTS+67hYjDIYWYaEIc8LXWMLyDm9C5fARPJ4F2BIWgzgzkNj+dVjusft2XnziamWdbS5u3kuRlVuz5LQj+R5imnqQAincdZTkTT1nYx+DatlOLllCYIHffpI="}'
  442. ),
  443. paste2 = $.PrivateBin.CryptTool.decipher(
  444. 's9pmKZKOBN7EVvHpTA8jjLFH3Xlz/0l8lB4+ONPACrM=',
  445. '', // no password
  446. '{"iv":"WA42mdxIVXUwBqZu7JYNiw==","v":1,"iter":10000,"ks":256,"ts":128,"mode":"gcm","adata":"","cipher":"aes","salt":"jN6CjbQMJCM=","ct":"kYYMo5DFG1+w0UHiYXT5pdV0IUuXxzOlslkW/c3DRCbGFROCVkAskHce7HoRczee1N9c5MhHjVMJUIZE02qIS8UyHdJ/GqcPVidTUcj9rnDNWsTXkjVv8jCwHS/cwmAjDTWpwp5ThECN+ov/wNp/NdtTj8Qj7f/T3rfZIOCWfwLH9s4Des35UNcUidfPTNQ1l0Gm0X+r98CCUSYZjQxkZc6hRZBLPQ8EaNVooUwd5eP4GiYlmSDNA0wOSA+5isPYxomVCt+kFf58VBlNhpfNi7BLYAUTPpXT4SfH5drR9+C7NTeZ+tTCYjbU94PzYItOpu8vgnB1/a6BAM5h3m9w+giUb0df4hgTWeZnZxLjo5BN8WV+kdTXMj3/Vv0gw0DQrDcCuX/cBAjpy3lQGwlAN1vXoOIyZJUjMpQRrOLdKvLB+zcmVNtGDbgnfP2IYBzk9NtodpUa27ne0T0ZpwOPlVwevsIVZO224WLa+iQmmHOWDFFpVDlS0t0fLfOk7Hcb2xFsTxiCIiyKMho/IME1Du3X4e6BVa3hobSSZv0rRtNgY1KcyYPrUPW2fxZ+oik3y9SgGvb7XpjVIta8DWlDWRfZ9kzoweWEYqz9IA8Xd373RefpyuWI25zlHoX3nwljzsZU6dC//h/Dt2DNr+IAvKO3+u23cWoB9kgcZJ2FJuqjLvVfCF+OWcig7zs2pTYJW6Rg6lqbBCxiUUlae6xJrjfv0pzD2VYCLY7v1bVTagppwKzNI3WaluCOrdDYUCxUSe56yd1oAoLPRVbYvomRboUO6cjQhEknERyvt45og2kORJOEJayHW+jZgR0Y0jM3Nk17ubpij2gHxNx9kiLDOiCGSV5mn9mV7qd3HHcOMSykiBgbyzjobi96LT2dIGLeDXTIdPOog8wyobO4jWq0GGs0vBB8oSYXhHvixZLcSjX2KQuHmEoWzmJcr3DavdoXZmAurGWLKjzEdJc5dSD/eNr99gjHX7wphJ6umKMM+fn6PcbYJkhDh2GlJL5COXjXfm/5aj/vuyaRRWZMZtmnYpGAtAPg7AUG"}'
  447. );
  448. if (!paste1.includes('securely packed in iron') || !paste2.includes('Sol is right')) {
  449. throw Error('v1 (SJCL based) pastes could not be deciphered');
  450. }
  451. }
  452. );
  453. it(
  454. 'supports ZeroBin ciphertext (SJCL & Base64 1.7)',
  455. function () {
  456. var newBase64 = global.Base64;
  457. global.Base64 = require('./base64-1.7').Base64;
  458. jsdom();
  459. delete require.cache[require.resolve('./privatebin')];
  460. require('./privatebin');
  461. // Of course you can easily decipher the following texts, if you like.
  462. // Bonus points for finding their sources and hidden meanings.
  463. var paste1 = $.PrivateBin.CryptTool.decipher(
  464. '6t2qsmLyfXIokNCL+3/yl15rfTUBQvm5SOnFPvNE7Q8=',
  465. // -- "That's amazing. I've got the same combination on my luggage."
  466. Array.apply(0, Array(6)).map(function(_,b) { return b + 1; }).join(''),
  467. '{"iv":"aTnR2qBL1CAmLX8FdWe3VA==","v":1,"iter":10000,"ks":256,"ts":128,"mode":"gcm","adata":"","cipher":"aes","salt":"u0lQvePq6L0=","ct":"A3nBTvICZtYy6xqbIJE0c8Veored5lMJUGgGUm4581wjrPFlU0Q0tUZSf+RUUoZj2jqDa4kiyyZ5YNMe30hNMV0oVSalNhRgD9svVMnPuF162IbyhVCwr7ULjT981CHxVlGNqGqmIU6L/XixgdArxAA8x1GCrfAkBWWGeq8Qw5vJPG/RCHpwR4Wy3azrluqeyERBzmaOQjO/kM35TiI6IrLYFyYyL7upYlxAaxS0XBMZvN8QU8Lnerwvh5JVC6OkkKrhogajTJIKozCF79yI78c50LUh7tTuI3Yoh7+fXxhoODvQdYFmoiUlrutN7Y5ZMRdITvVu8fTYtX9c7Fiufmcq5icEimiHp2g1bvfpOaGOsFT+XNFgC9215jcp5mpBdN852xs7bUtw+nDrf+LsDEX6iRpRZ+PYgLDN5xQT1ByEtYbeP+tO38pnx72oZdIB3cj8UkOxnxdNiZM5YB5egn4jUj1fHot1I69WoTiUJipZ5PIATv7ScymRB+AYzjxjurQ9lVfX9QtAbEH2dhdmoUo3IDRSXpWNCe9RC1aUIyWfZO7oI7FEohNscHNTLEcT+wFnFUPByLlXmjNZ7FKeNpvUm3jTY4t4sbZH8o2dUl624PAw1INcJ6FKqWGWwoFT2j1MYC+YV/LkLTdjuWfayvwLMh27G/FfKCRbW36vqinegqpPDylsx9+3oFkEw3y5Z8+44oN91rE/4Md7JhPJeRVlFC9TNCj4dA+EVhbbQqscvSnIH2uHkMw7mNNo7xba/YT9KoPDaniqnYqb+q2pX1WNWE7dLS2wfroMAS3kh8P22DAV37AeiNoD2PcI6ZcHbRdPa+XRrRcJhSPPW7UQ0z4OvBfjdu/w390QxAxSxvZewoh49fKKB6hTsRnZb4tpHkjlww=="}'
  468. ),
  469. paste2 = $.PrivateBin.CryptTool.decipher(
  470. 's9pmKZKOBN7EVvHpTA8jjLFH3Xlz/0l8lB4+ONPACrM=',
  471. '', // no password
  472. '{"iv":"Z7lAZQbkrqGMvruxoSm6Pw==","v":1,"iter":10000,"ks":256,"ts":128,"mode":"gcm","adata":"","cipher":"aes","salt":"jN6CjbQMJCM=","ct":"PuOPWB3i2FPcreSrLYeQf84LdE8RHjsc+MGtiOr4b7doNyWKYtkNorbRadxaPnEee2/Utrp1MIIfY5juJSy8RGwEPX5ciWcYe6EzsXWznsnvhmpKNj9B7eIIrfSbxfy8E2e/g7xav1nive+ljToka3WT1DZ8ILQd/NbnJeHWaoSEOfvz8+d8QJPb1tNZvs7zEY95DumQwbyOsIMKAvcZHJ9OJNpujXzdMyt6DpcFcqlldWBZ/8q5rAUTw0HNx/rCgbhAxRYfNoTLIcMM4L0cXbPSgCjwf5FuO3EdE13mgEDhcClW79m0QvcnIh8xgzYoxLbp0+AwvC/MbZM8savN/0ieWr2EKkZ04ggiOIEyvfCUuNprQBYO+y8kKduNEN6by0Yf4LRCPfmwN+GezDLuzTnZIMhPbGqUAdgV6ExqK2ULEEIrQEMoOuQIxfoMhqLlzG79vXGt2O+BY+4IiYfvmuRLks4UXfyHqxPXTJg48IYbGs0j4TtJPUgp3523EyYLwEGyVTAuWhYAmVIwd/hoV7d7tmfcF73w9dufDFI3LNca2KxzBnWNPYvIZKBwWbq8ncxkb191dP6mjEi7NnhqVk5A6vIBbu4AC5PZf76l6yep4xsoy/QtdDxCMocCXeAML9MQ9uPQbuspOKrBvMfN5igA1kBqasnxI472KBNXsdZnaDddSVUuvhTcETM="}'
  473. );
  474. global.Base64 = newBase64;
  475. jsdom();
  476. delete require.cache[require.resolve('./privatebin')];
  477. require('./privatebin');
  478. if (!paste1.includes('securely packed in iron') || !paste2.includes('Sol is right')) {
  479. throw Error('v1 (SJCL based) pastes could not be deciphered');
  480. }
  481. }
  482. );
  483. });
  484. describe('isEntropyReady & addEntropySeedListener', function () {
  485. it(
  486. 'lets us know that enough entropy is collected or make us wait for it',
  487. function(done) {
  488. if ($.PrivateBin.CryptTool.isEntropyReady()) {
  489. done();
  490. } else {
  491. $.PrivateBin.CryptTool.addEntropySeedListener(function() {
  492. done();
  493. });
  494. }
  495. }
  496. );
  497. });
  498. describe('getSymmetricKey', function () {
  499. var keys = [];
  500. // the parameter is used to ensure the test is run more then one time
  501. jsc.property(
  502. 'returns random, non-empty keys',
  503. 'nat',
  504. function(n) {
  505. var key = $.PrivateBin.CryptTool.getSymmetricKey(),
  506. result = (key !== '' && keys.indexOf(key) === -1);
  507. keys.push(key);
  508. return result;
  509. }
  510. );
  511. });
  512. describe('Base64.js vs SJCL.js vs abab.js', function () {
  513. jsc.property(
  514. 'these all return the same base64 string',
  515. 'string',
  516. function(string) {
  517. var base64 = Base64.toBase64(string),
  518. sjcl = global.sjcl.codec.base64.fromBits(global.sjcl.codec.utf8String.toBits(string)),
  519. abab = window.btoa(Base64.utob(string));
  520. return base64 === sjcl && sjcl === abab;
  521. }
  522. );
  523. });
  524. });
  525. describe('Model', function () {
  526. describe('getExpirationDefault', function () {
  527. before(function () {
  528. $.PrivateBin.Model.reset();
  529. cleanup();
  530. });
  531. jsc.property(
  532. 'returns the contents of the element with id "pasteExpiration"',
  533. 'array asciinestring',
  534. 'string',
  535. 'small nat',
  536. function (keys, value, key) {
  537. keys = keys.map(htmlEntities);
  538. value = htmlEntities(value);
  539. var content = keys.length > key ? keys[key] : (keys.length > 0 ? keys[0] : 'null'),
  540. contents = '<select id="pasteExpiration" name="pasteExpiration">';
  541. keys.forEach(function(item) {
  542. contents += '<option value="' + item + '"';
  543. if (item === content) {
  544. contents += ' selected="selected"';
  545. }
  546. contents += '>' + value + '</option>';
  547. });
  548. contents += '</select>';
  549. $('body').html(contents);
  550. var result = htmlEntities(
  551. $.PrivateBin.Model.getExpirationDefault()
  552. );
  553. $.PrivateBin.Model.reset();
  554. return content === result;
  555. }
  556. );
  557. });
  558. describe('getFormatDefault', function () {
  559. before(function () {
  560. $.PrivateBin.Model.reset();
  561. cleanup();
  562. });
  563. jsc.property(
  564. 'returns the contents of the element with id "pasteFormatter"',
  565. 'array asciinestring',
  566. 'string',
  567. 'small nat',
  568. function (keys, value, key) {
  569. keys = keys.map(htmlEntities);
  570. value = htmlEntities(value);
  571. var content = keys.length > key ? keys[key] : (keys.length > 0 ? keys[0] : 'null'),
  572. contents = '<select id="pasteFormatter" name="pasteFormatter">';
  573. keys.forEach(function(item) {
  574. contents += '<option value="' + item + '"';
  575. if (item === content) {
  576. contents += ' selected="selected"';
  577. }
  578. contents += '>' + value + '</option>';
  579. });
  580. contents += '</select>';
  581. $('body').html(contents);
  582. var result = htmlEntities(
  583. $.PrivateBin.Model.getFormatDefault()
  584. );
  585. $.PrivateBin.Model.reset();
  586. return content === result;
  587. }
  588. );
  589. });
  590. describe('hasCipherData', function () {
  591. before(function () {
  592. $.PrivateBin.Model.reset();
  593. cleanup();
  594. });
  595. jsc.property(
  596. 'checks if the element with id "cipherdata" contains any data',
  597. 'asciistring',
  598. function (value) {
  599. value = htmlEntities(value).trim();
  600. $('body').html('<div id="cipherdata">' + value + '</div>');
  601. $.PrivateBin.Model.init();
  602. var result = $.PrivateBin.Model.hasCipherData();
  603. $.PrivateBin.Model.reset();
  604. return (value.length > 0) === result;
  605. }
  606. );
  607. });
  608. describe('getCipherData', function () {
  609. before(function () {
  610. $.PrivateBin.Model.reset();
  611. cleanup();
  612. });
  613. jsc.property(
  614. 'returns the contents of the element with id "cipherdata"',
  615. 'asciistring',
  616. function (value) {
  617. value = htmlEntities(value).trim();
  618. $('body').html('<div id="cipherdata">' + value + '</div>');
  619. $.PrivateBin.Model.init();
  620. var result = htmlEntities(
  621. $.PrivateBin.Model.getCipherData()
  622. );
  623. $.PrivateBin.Model.reset();
  624. return value === result;
  625. }
  626. );
  627. });
  628. describe('getPasteId', function () {
  629. this.timeout(30000);
  630. before(function () {
  631. $.PrivateBin.Model.reset();
  632. cleanup();
  633. });
  634. jsc.property(
  635. 'returns the query string without separator, if any',
  636. jsc.nearray(jsc.elements(a2zString)),
  637. jsc.nearray(jsc.elements(a2zString)),
  638. jsc.nearray(jsc.elements(queryString)),
  639. 'string',
  640. function (schema, address, query, fragment) {
  641. var queryString = query.join(''),
  642. clean = jsdom('', {
  643. url: schema.join('') + '://' + address.join('') +
  644. '/?' + queryString + '#' + fragment
  645. }),
  646. result = $.PrivateBin.Model.getPasteId();
  647. $.PrivateBin.Model.reset();
  648. clean();
  649. return queryString === result;
  650. }
  651. );
  652. jsc.property(
  653. 'throws exception on empty query string',
  654. jsc.nearray(jsc.elements(a2zString)),
  655. jsc.nearray(jsc.elements(a2zString)),
  656. 'string',
  657. function (schema, address, fragment) {
  658. var clean = jsdom('', {
  659. url: schema.join('') + '://' + address.join('') +
  660. '/#' + fragment
  661. }),
  662. result = false;
  663. try {
  664. $.PrivateBin.Model.getPasteId();
  665. }
  666. catch(err) {
  667. result = true;
  668. }
  669. $.PrivateBin.Model.reset();
  670. clean();
  671. return result;
  672. }
  673. );
  674. });
  675. describe('getPasteKey', function () {
  676. this.timeout(30000);
  677. jsc.property(
  678. 'returns the fragment of the URL',
  679. jsc.nearray(jsc.elements(a2zString)),
  680. jsc.nearray(jsc.elements(a2zString)),
  681. jsc.array(jsc.elements(queryString)),
  682. jsc.nearray(jsc.elements(base64String)),
  683. function (schema, address, query, fragment) {
  684. var fragmentString = fragment.join(''),
  685. clean = jsdom('', {
  686. url: schema.join('') + '://' + address.join('') +
  687. '/?' + query.join('') + '#' + fragmentString
  688. }),
  689. result = $.PrivateBin.Model.getPasteKey();
  690. $.PrivateBin.Model.reset();
  691. clean();
  692. return fragmentString === result;
  693. }
  694. );
  695. jsc.property(
  696. 'returns the fragment stripped of trailing query parts',
  697. jsc.nearray(jsc.elements(a2zString)),
  698. jsc.nearray(jsc.elements(a2zString)),
  699. jsc.array(jsc.elements(queryString)),
  700. jsc.nearray(jsc.elements(base64String)),
  701. jsc.array(jsc.elements(queryString)),
  702. function (schema, address, query, fragment, trail) {
  703. var fragmentString = fragment.join(''),
  704. clean = jsdom('', {
  705. url: schema.join('') + '://' + address.join('') + '/?' +
  706. query.join('') + '#' + fragmentString + '&' + trail.join('')
  707. }),
  708. result = $.PrivateBin.Model.getPasteKey();
  709. $.PrivateBin.Model.reset();
  710. clean();
  711. return fragmentString === result;
  712. }
  713. );
  714. jsc.property(
  715. 'throws exception on empty fragment of the URL',
  716. jsc.nearray(jsc.elements(a2zString)),
  717. jsc.nearray(jsc.elements(a2zString)),
  718. jsc.array(jsc.elements(queryString)),
  719. function (schema, address, query) {
  720. var clean = jsdom('', {
  721. url: schema.join('') + '://' + address.join('') +
  722. '/?' + query.join('')
  723. }),
  724. result = false;
  725. try {
  726. $.PrivateBin.Model.getPasteKey();
  727. }
  728. catch(err) {
  729. result = true;
  730. }
  731. $.PrivateBin.Model.reset();
  732. clean();
  733. return result;
  734. }
  735. );
  736. });
  737. describe('getTemplate', function () {
  738. before(function () {
  739. $.PrivateBin.Model.reset();
  740. cleanup();
  741. });
  742. jsc.property(
  743. 'returns the contents of the element with id "[name]template"',
  744. jsc.nearray(jsc.elements(alnumString)),
  745. jsc.nearray(jsc.elements(a2zString)),
  746. jsc.nearray(jsc.elements(alnumString)),
  747. function (id, element, value) {
  748. id = id.join('');
  749. element = element.join('');
  750. value = value.join('').trim();
  751. // <br>, <hr>, <img> and <wbr> tags can't contain strings,
  752. // table tags can't be alone, so test with a <p> instead
  753. if (['br', 'col', 'hr', 'img', 'tr', 'td', 'th', 'wbr'].indexOf(element) >= 0) {
  754. element = 'p';
  755. }
  756. $('body').html(
  757. '<div id="templates"><' + element + ' id="' + id +
  758. 'template">' + value + '</' + element + '></div>'
  759. );
  760. $.PrivateBin.Model.init();
  761. var template = '<' + element + ' id="' + id + '">' + value +
  762. '</' + element + '>',
  763. result = $.PrivateBin.Model.getTemplate(id).wrap('<p/>').parent().html();
  764. $.PrivateBin.Model.reset();
  765. return template === result;
  766. }
  767. );
  768. });
  769. });
  770. describe('UiHelper', function () {
  771. // TODO: As per https://github.com/tmpvar/jsdom/issues/1565 there is no navigation support in jsdom, yet.
  772. // for now we use a mock function to trigger the event
  773. describe('historyChange', function () {
  774. this.timeout(30000);
  775. before(function () {
  776. $.PrivateBin.Helper.reset();
  777. });
  778. jsc.property(
  779. 'redirects to home, when the state is null',
  780. jsc.elements(schemas),
  781. jsc.nearray(jsc.elements(a2zString)),
  782. function (schema, address) {
  783. var expected = schema + '://' + address.join('') + '/',
  784. clean = jsdom('', {url: expected});
  785. // make window.location.href writable
  786. Object.defineProperty(window.location, 'href', {
  787. writable: true,
  788. value: window.location.href
  789. });
  790. $.PrivateBin.UiHelper.mockHistoryChange();
  791. $.PrivateBin.Helper.reset();
  792. var result = window.location.href;
  793. clean();
  794. return expected === result;
  795. }
  796. );
  797. jsc.property(
  798. 'does not redirect to home, when a new paste is created',
  799. jsc.elements(schemas),
  800. jsc.nearray(jsc.elements(a2zString)),
  801. jsc.array(jsc.elements(queryString)),
  802. jsc.nearray(jsc.elements(base64String)),
  803. function (schema, address, query, fragment) {
  804. var expected = schema + '://' + address.join('') + '/' + '?' +
  805. query.join('') + '#' + fragment.join(''),
  806. clean = jsdom('', {url: expected});
  807. // make window.location.href writable
  808. Object.defineProperty(window.location, 'href', {
  809. writable: true,
  810. value: window.location.href
  811. });
  812. $.PrivateBin.UiHelper.mockHistoryChange([
  813. {type: 'newpaste'}, '', expected
  814. ]);
  815. $.PrivateBin.Helper.reset();
  816. var result = window.location.href;
  817. clean();
  818. return expected === result;
  819. }
  820. );
  821. });
  822. describe('reloadHome', function () {
  823. this.timeout(30000);
  824. before(function () {
  825. $.PrivateBin.Helper.reset();
  826. });
  827. jsc.property(
  828. 'redirects to home',
  829. jsc.elements(schemas),
  830. jsc.nearray(jsc.elements(a2zString)),
  831. jsc.array(jsc.elements(queryString)),
  832. jsc.nearray(jsc.elements(base64String)),
  833. function (schema, address, query, fragment) {
  834. var expected = schema + '://' + address.join('') + '/',
  835. clean = jsdom('', {
  836. url: expected + '?' + query.join('') + '#' + fragment.join('')
  837. });
  838. // make window.location.href writable
  839. Object.defineProperty(window.location, 'href', {
  840. writable: true,
  841. value: window.location.href
  842. });
  843. $.PrivateBin.UiHelper.reloadHome();
  844. $.PrivateBin.Helper.reset();
  845. var result = window.location.href;
  846. clean();
  847. return expected === result;
  848. }
  849. );
  850. });
  851. describe('isVisible', function () {
  852. // TODO As per https://github.com/tmpvar/jsdom/issues/1048 there is no layout support in jsdom, yet.
  853. // once it is supported or a workaround is found, uncomment the section below
  854. /*
  855. before(function () {
  856. $.PrivateBin.Helper.reset();
  857. });
  858. jsc.property(
  859. 'detect visible elements',
  860. jsc.nearray(jsc.elements(alnumString)),
  861. jsc.nearray(jsc.elements(a2zString)),
  862. function (id, element) {
  863. id = id.join('');
  864. element = element.join('');
  865. var clean = jsdom(
  866. '<' + element + ' id="' + id + '"></' + element + '>'
  867. );
  868. var result = $.PrivateBin.UiHelper.isVisible($('#' + id));
  869. clean();
  870. return result;
  871. }
  872. );
  873. */
  874. });
  875. describe('scrollTo', function () {
  876. // TODO Did not find a way to test that, see isVisible test above
  877. });
  878. });
  879. describe('Alert', function () {
  880. describe('showStatus', function () {
  881. before(function () {
  882. cleanup();
  883. });
  884. jsc.property(
  885. 'shows a status message',
  886. jsc.array(jsc.elements(alnumString)),
  887. jsc.array(jsc.elements(alnumString)),
  888. function (icon, message) {
  889. icon = icon.join('');
  890. message = message.join('');
  891. var expected = '<div id="status" role="alert" ' +
  892. 'class="statusmessage alert alert-info"><span ' +
  893. 'class="glyphicon glyphicon-' + icon +
  894. '" aria-hidden="true"></span> ' + message + '</div>';
  895. $('body').html(
  896. '<div id="status" role="alert" class="statusmessage ' +
  897. 'alert alert-info hidden"><span class="glyphicon ' +
  898. 'glyphicon-info-sign" aria-hidden="true"></span> </div>'
  899. );
  900. $.PrivateBin.Alert.init();
  901. $.PrivateBin.Alert.showStatus(message, icon);
  902. var result = $('body').html();
  903. return expected === result;
  904. }
  905. );
  906. });
  907. describe('showError', function () {
  908. before(function () {
  909. cleanup();
  910. });
  911. jsc.property(
  912. 'shows an error message',
  913. jsc.array(jsc.elements(alnumString)),
  914. jsc.array(jsc.elements(alnumString)),
  915. function (icon, message) {
  916. icon = icon.join('');
  917. message = message.join('');
  918. var expected = '<div id="errormessage" role="alert" ' +
  919. 'class="statusmessage alert alert-danger"><span ' +
  920. 'class="glyphicon glyphicon-' + icon +
  921. '" aria-hidden="true"></span> ' + message + '</div>';
  922. $('body').html(
  923. '<div id="errormessage" role="alert" class="statusmessage ' +
  924. 'alert alert-danger hidden"><span class="glyphicon ' +
  925. 'glyphicon-alert" aria-hidden="true"></span> </div>'
  926. );
  927. $.PrivateBin.Alert.init();
  928. $.PrivateBin.Alert.showError(message, icon);
  929. var result = $('body').html();
  930. return expected === result;
  931. }
  932. );
  933. });
  934. describe('showRemaining', function () {
  935. before(function () {
  936. cleanup();
  937. });
  938. jsc.property(
  939. 'shows remaining time',
  940. jsc.array(jsc.elements(alnumString)),
  941. jsc.array(jsc.elements(alnumString)),
  942. 'integer',
  943. function (message, string, number) {
  944. message = message.join('');
  945. string = string.join('');
  946. var expected = '<div id="remainingtime" role="alert" ' +
  947. 'class="alert alert-info"><span ' +
  948. 'class="glyphicon glyphicon-fire" aria-hidden="true">' +
  949. '</span> ' + string + message + number + '</div>';
  950. $('body').html(
  951. '<div id="remainingtime" role="alert" class="hidden ' +
  952. 'alert alert-info"><span class="glyphicon ' +
  953. 'glyphicon-fire" aria-hidden="true"></span> </div>'
  954. );
  955. $.PrivateBin.Alert.init();
  956. $.PrivateBin.Alert.showRemaining(['%s' + message + '%d', string, number]);
  957. var result = $('body').html();
  958. return expected === result;
  959. }
  960. );
  961. });
  962. describe('showLoading', function () {
  963. before(function () {
  964. cleanup();
  965. });
  966. jsc.property(
  967. 'shows a loading message',
  968. jsc.array(jsc.elements(alnumString)),
  969. jsc.array(jsc.elements(alnumString)),
  970. 'integer',
  971. function (icon, message, number) {
  972. icon = icon.join('');
  973. message = message.join('');
  974. var default_message = 'Loading…';
  975. if (message.length == 0) {
  976. message = default_message;
  977. }
  978. var expected = '<ul class="nav navbar-nav"><li ' +
  979. 'id="loadingindicator" class="navbar-text"><span ' +
  980. 'class="glyphicon glyphicon-' + icon +
  981. '" aria-hidden="true"></span> ' + message + '</li></ul>';
  982. $('body').html(
  983. '<ul class="nav navbar-nav"><li id="loadingindicator" ' +
  984. 'class="navbar-text hidden"><span class="glyphicon ' +
  985. 'glyphicon-time" aria-hidden="true"></span> ' +
  986. default_message + '</li></ul>'
  987. );
  988. $.PrivateBin.Alert.init();
  989. $.PrivateBin.Alert.showLoading(message, number, icon);
  990. var result = $('body').html();
  991. return expected === result;
  992. }
  993. );
  994. });
  995. describe('hideLoading', function () {
  996. before(function () {
  997. cleanup();
  998. });
  999. it(
  1000. 'hides the loading message',
  1001. function() {
  1002. $('body').html(
  1003. '<ul class="nav navbar-nav"><li id="loadingindicator" ' +
  1004. 'class="navbar-text"><span class="glyphicon ' +
  1005. 'glyphicon-time" aria-hidden="true"></span> ' +
  1006. 'Loading…</li></ul>'
  1007. );
  1008. $('body').addClass('loading');
  1009. $.PrivateBin.Alert.init();
  1010. $.PrivateBin.Alert.hideLoading();
  1011. return !$('body').hasClass('loading') &&
  1012. $('#loadingindicator').hasClass('hidden');
  1013. }
  1014. );
  1015. });
  1016. describe('hideMessages', function () {
  1017. before(function () {
  1018. cleanup();
  1019. });
  1020. it(
  1021. 'hides all messages',
  1022. function() {
  1023. $('body').html(
  1024. '<div id="status" role="alert" class="statusmessage ' +
  1025. 'alert alert-info"><span class="glyphicon ' +
  1026. 'glyphicon-info-sign" aria-hidden="true"></span> </div>' +
  1027. '<div id="errormessage" role="alert" class="statusmessage ' +
  1028. 'alert alert-danger"><span class="glyphicon ' +
  1029. 'glyphicon-alert" aria-hidden="true"></span> </div>'
  1030. );
  1031. $.PrivateBin.Alert.init();
  1032. $.PrivateBin.Alert.hideMessages();
  1033. return $('#statusmessage').hasClass('hidden') &&
  1034. $('#errormessage').hasClass('hidden');
  1035. }
  1036. );
  1037. });
  1038. describe('setCustomHandler', function () {
  1039. before(function () {
  1040. cleanup();
  1041. });
  1042. jsc.property(
  1043. 'calls a given handler function',
  1044. 'nat 3',
  1045. jsc.array(jsc.elements(alnumString)),
  1046. function (trigger, message) {
  1047. message = message.join('');
  1048. var handlerCalled = false,
  1049. default_message = 'Loading…',
  1050. functions = [
  1051. $.PrivateBin.Alert.showStatus,
  1052. $.PrivateBin.Alert.showError,
  1053. $.PrivateBin.Alert.showRemaining,
  1054. $.PrivateBin.Alert.showLoading
  1055. ];
  1056. if (message.length == 0) {
  1057. message = default_message;
  1058. }
  1059. $('body').html(
  1060. '<ul class="nav navbar-nav"><li id="loadingindicator" ' +
  1061. 'class="navbar-text hidden"><span class="glyphicon ' +
  1062. 'glyphicon-time" aria-hidden="true"></span> ' +
  1063. default_message + '</li></ul>' +
  1064. '<div id="remainingtime" role="alert" class="hidden ' +
  1065. 'alert alert-info"><span class="glyphicon ' +
  1066. 'glyphicon-fire" aria-hidden="true"></span> </div>' +
  1067. '<div id="status" role="alert" class="statusmessage ' +
  1068. 'alert alert-info"><span class="glyphicon ' +
  1069. 'glyphicon-info-sign" aria-hidden="true"></span> </div>' +
  1070. '<div id="errormessage" role="alert" class="statusmessage ' +
  1071. 'alert alert-danger"><span class="glyphicon ' +
  1072. 'glyphicon-alert" aria-hidden="true"></span> </div>'
  1073. );
  1074. $.PrivateBin.Alert.init();
  1075. $.PrivateBin.Alert.setCustomHandler(function(id, $element) {
  1076. handlerCalled = true;
  1077. return jsc.random(0, 1) ? true : $element;
  1078. });
  1079. functions[trigger](message);
  1080. return handlerCalled;
  1081. }
  1082. );
  1083. });
  1084. });
  1085. describe('PasteStatus', function () {
  1086. describe('createPasteNotification', function () {
  1087. this.timeout(30000);
  1088. before(function () {
  1089. cleanup();
  1090. });
  1091. jsc.property(
  1092. 'creates a notification after a successfull paste upload',
  1093. jsc.elements(schemas),
  1094. jsc.nearray(jsc.elements(a2zString)),
  1095. jsc.array(jsc.elements(queryString)),
  1096. 'string',
  1097. jsc.elements(schemas),
  1098. jsc.nearray(jsc.elements(a2zString)),
  1099. jsc.array(jsc.elements(queryString)),
  1100. function (
  1101. schema1, address1, query1, fragment1,
  1102. schema2, address2, query2
  1103. ) {
  1104. var expected1 = schema1 + '://' + address1.join('') + '/?' +
  1105. encodeURI(query1.join('').replace(/^&+|&+$/gm,'') + '#' + fragment1),
  1106. expected2 = schema2 + '://' + address2.join('') + '/?' +
  1107. encodeURI(query2.join('')),
  1108. clean = jsdom();
  1109. $('body').html('<div><div id="deletelink"></div><div id="pastelink"></div></div>');
  1110. $.PrivateBin.PasteStatus.init();
  1111. $.PrivateBin.PasteStatus.createPasteNotification(expected1, expected2);
  1112. var result1 = $('#pasteurl')[0].href,
  1113. result2 = $('#deletelink a')[0].href;
  1114. clean();
  1115. return result1 == expected1 && result2 == expected2;
  1116. }
  1117. );
  1118. });
  1119. describe('showRemainingTime', function () {
  1120. this.timeout(30000);
  1121. before(function () {
  1122. cleanup();
  1123. });
  1124. jsc.property(
  1125. 'shows burn after reading message or remaining time',
  1126. 'bool',
  1127. 'nat',
  1128. jsc.nearray(jsc.elements(a2zString)),
  1129. jsc.nearray(jsc.elements(a2zString)),
  1130. jsc.array(jsc.elements(queryString)),
  1131. 'string',
  1132. function (
  1133. burnafterreading, remaining_time,
  1134. schema, address, query, fragment
  1135. ) {
  1136. var clean = jsdom('', {
  1137. url: schema.join('') + '://' + address.join('') +
  1138. '/?' + queryString + '#' + fragment
  1139. });
  1140. $('body').html('<div id="remainingtime" class="hidden"></div>');
  1141. $.PrivateBin.PasteStatus.init();
  1142. $.PrivateBin.PasteStatus.showRemainingTime({
  1143. 'burnafterreading': burnafterreading,
  1144. 'remaining_time': remaining_time,
  1145. 'expire_date': remaining_time ? ((new Date()).getTime() / 1000) + remaining_time : 0
  1146. });
  1147. if (burnafterreading) {
  1148. var result = $('#remainingtime').hasClass('foryoureyesonly') &&
  1149. !$('#remainingtime').hasClass('hidden');
  1150. } else if (remaining_time) {
  1151. var result =!$('#remainingtime').hasClass('foryoureyesonly') &&
  1152. !$('#remainingtime').hasClass('hidden');
  1153. } else {
  1154. var result = $('#remainingtime').hasClass('hidden') &&
  1155. !$('#remainingtime').hasClass('foryoureyesonly');
  1156. }
  1157. clean();
  1158. return result;
  1159. }
  1160. );
  1161. });
  1162. describe('hideMessages', function () {
  1163. before(function () {
  1164. cleanup();
  1165. });
  1166. it(
  1167. 'hides all messages',
  1168. function() {
  1169. $('body').html(
  1170. '<div id="remainingtime"></div><div id="pastesuccess"></div>'
  1171. );
  1172. $.PrivateBin.PasteStatus.init();
  1173. $.PrivateBin.PasteStatus.hideMessages();
  1174. return $('#remainingtime').hasClass('hidden') &&
  1175. $('#pastesuccess').hasClass('hidden');
  1176. }
  1177. );
  1178. });
  1179. });
  1180. describe('Prompt', function () {
  1181. // TODO: this does not test the prompt() fallback, since that isn't available
  1182. // in nodejs -> replace the prompt in the "page" template with a modal
  1183. describe('requestPassword & getPassword', function () {
  1184. this.timeout(30000);
  1185. before(function () {
  1186. cleanup();
  1187. });
  1188. jsc.property(
  1189. 'returns the password fed into the dialog',
  1190. 'string',
  1191. function (password) {
  1192. password = password.replace(/\r+/g, '');
  1193. var clean = jsdom('', {url: 'ftp://example.com/#0'});
  1194. $('body').html(
  1195. '<div id="passwordmodal" class="modal fade" role="dialog">' +
  1196. '<div class="modal-dialog"><div class="modal-content">' +
  1197. '<div class="modal-body"><form id="passwordform" role="form">' +
  1198. '<div class="form-group"><input id="passworddecrypt" ' +
  1199. 'type="password" class="form-control" placeholder="Enter ' +
  1200. 'password"></div><button type="submit">Decrypt</button>' +
  1201. '</form></div></div></div></div><div id="cipherdata">{}</div>'
  1202. );
  1203. $.PrivateBin.Model.init();
  1204. $.PrivateBin.Prompt.init();
  1205. $.PrivateBin.Prompt.requestPassword();
  1206. $('#passworddecrypt').val(password);
  1207. $('#passwordform').submit();
  1208. var result = $.PrivateBin.Prompt.getPassword();
  1209. clean();
  1210. return result == password;
  1211. }
  1212. );
  1213. });
  1214. });
  1215. describe('Editor', function () {
  1216. describe('show, hide, getText, setText & isPreview', function () {
  1217. this.timeout(30000);
  1218. before(function () {
  1219. cleanup();
  1220. });
  1221. jsc.property(
  1222. 'returns text fed into the textarea, handles editor tabs',
  1223. 'string',
  1224. function (text) {
  1225. var clean = jsdom(),
  1226. results = [];
  1227. $('body').html(
  1228. '<ul id="editorTabs" class="nav nav-tabs hidden"><li ' +
  1229. 'role="presentation" class="active"><a id="messageedit" ' +
  1230. 'href="#">Editor</a></li><li role="presentation"><a ' +
  1231. 'id="messagepreview" href="#">Preview</a></li></ul><div ' +
  1232. 'id="placeholder" class="hidden">+++ no paste text +++</div>' +
  1233. '<div id="prettymessage" class="hidden"><pre id="prettyprint" ' +
  1234. 'class="prettyprint linenums:1"></pre></div><div ' +
  1235. 'id="plaintext" class="hidden"></div><p><textarea ' +
  1236. 'id="message" name="message" cols="80" rows="25" ' +
  1237. 'class="form-control hidden"></textarea></p>'
  1238. );
  1239. $.PrivateBin.Editor.init();
  1240. results.push(
  1241. $('#editorTabs').hasClass('hidden') &&
  1242. $('#message').hasClass('hidden')
  1243. );
  1244. $.PrivateBin.Editor.show();
  1245. results.push(
  1246. !$('#editorTabs').hasClass('hidden') &&
  1247. !$('#message').hasClass('hidden')
  1248. );
  1249. $.PrivateBin.Editor.hide();
  1250. results.push(
  1251. $('#editorTabs').hasClass('hidden') &&
  1252. $('#message').hasClass('hidden')
  1253. );
  1254. $.PrivateBin.Editor.show();
  1255. $.PrivateBin.Editor.focusInput();
  1256. results.push(
  1257. $.PrivateBin.Editor.getText().length == 0
  1258. );
  1259. $.PrivateBin.Editor.setText(text);
  1260. results.push(
  1261. $.PrivateBin.Editor.getText() == $('#message').val()
  1262. );
  1263. $.PrivateBin.Editor.setText();
  1264. results.push(
  1265. !$.PrivateBin.Editor.isPreview() &&
  1266. !$('#message').hasClass('hidden')
  1267. );
  1268. $('#messagepreview').click();
  1269. results.push(
  1270. $.PrivateBin.Editor.isPreview() &&
  1271. $('#message').hasClass('hidden')
  1272. );
  1273. $('#messageedit').click();
  1274. results.push(
  1275. !$.PrivateBin.Editor.isPreview() &&
  1276. !$('#message').hasClass('hidden')
  1277. );
  1278. clean();
  1279. return results.every(element => element);
  1280. }
  1281. );
  1282. });
  1283. });
  1284. describe('PasteViewer', function () {
  1285. describe('run, hide, getText, setText, getFormat, setFormat & isPrettyPrinted', function () {
  1286. this.timeout(30000);
  1287. before(function () {
  1288. cleanup();
  1289. });
  1290. jsc.property(
  1291. 'displays text according to format',
  1292. jsc.elements(['plaintext', 'markdown', 'syntaxhighlighting']),
  1293. 'nestring',
  1294. function (format, text) {
  1295. var clean = jsdom(),
  1296. results = [];
  1297. $('body').html(
  1298. '<div id="placeholder" class="hidden">+++ no paste text ' +
  1299. '+++</div><div id="prettymessage" class="hidden"><pre ' +
  1300. 'id="prettyprint" class="prettyprint linenums:1"></pre>' +
  1301. '</div><div id="plaintext" class="hidden"></div>'
  1302. );
  1303. $.PrivateBin.PasteViewer.init();
  1304. $.PrivateBin.PasteViewer.setFormat(format);
  1305. $.PrivateBin.PasteViewer.setText('');
  1306. results.push(
  1307. $('#placeholder').hasClass('hidden') &&
  1308. $('#prettymessage').hasClass('hidden') &&
  1309. $('#plaintext').hasClass('hidden') &&
  1310. $.PrivateBin.PasteViewer.getFormat() == format &&
  1311. $.PrivateBin.PasteViewer.getText() == ''
  1312. );
  1313. $.PrivateBin.PasteViewer.run();
  1314. results.push(
  1315. !$('#placeholder').hasClass('hidden') &&
  1316. $('#prettymessage').hasClass('hidden') &&
  1317. $('#plaintext').hasClass('hidden')
  1318. );
  1319. $.PrivateBin.PasteViewer.hide();
  1320. results.push(
  1321. $('#placeholder').hasClass('hidden') &&
  1322. $('#prettymessage').hasClass('hidden') &&
  1323. $('#plaintext').hasClass('hidden')
  1324. );
  1325. $.PrivateBin.PasteViewer.setText(text);
  1326. $.PrivateBin.PasteViewer.run();
  1327. results.push(
  1328. $('#placeholder').hasClass('hidden') &&
  1329. !$.PrivateBin.PasteViewer.isPrettyPrinted() &&
  1330. $.PrivateBin.PasteViewer.getText() == $('<div />').text(text).html()
  1331. );
  1332. if (format == 'markdown') {
  1333. results.push(
  1334. $('#prettymessage').hasClass('hidden') &&
  1335. !$('#plaintext').hasClass('hidden')
  1336. );
  1337. } else {
  1338. results.push(
  1339. !$('#prettymessage').hasClass('hidden') &&
  1340. $('#plaintext').hasClass('hidden')
  1341. );
  1342. }
  1343. clean();
  1344. return results.every(element => element);
  1345. }
  1346. );
  1347. jsc.property(
  1348. 'sanitizes XSS',
  1349. jsc.elements(['plaintext', 'markdown', 'syntaxhighlighting']),
  1350. 'string',
  1351. // https://www.owasp.org/index.php/XSS_Filter_Evasion_Cheat_Sheet
  1352. jsc.elements([
  1353. '<PLAINTEXT>',
  1354. '></SCRIPT>">\'><SCRIPT>alert(String.fromCharCode(88,83,83))</SCRIPT>',
  1355. '\'\';!--"<XSS>=&{()}',
  1356. '<SCRIPT SRC=http://example.com/xss.js></SCRIPT>',
  1357. '\'">><marquee><img src=x onerror=confirm(1)></marquee>"></plaintext\\></|\\><plaintext/onmouseover=prompt(1)><script>prompt(1)</script>@gmail.com<isindex formaction=javascript:alert(/XSS/) type=submit>\'-->"></script><script>alert(document.cookie)</script>"><img/id="confirm&lpar;1)"/alt="/"src="/"onerror=eval(id)>\'">',
  1358. '<IMG SRC="javascript:alert(\'XSS\');">',
  1359. '<IMG SRC=javascript:alert(\'XSS\')>',
  1360. '<IMG SRC=JaVaScRiPt:alert(\'XSS\')>',
  1361. '<IMG SRC=javascript:alert(&quot;XSS&quot;)>',
  1362. '<IMG SRC=`javascript:alert("RSnake says, \'XSS\'")`>',
  1363. '<a onmouseover="alert(document.cookie)">xxs link</a>',
  1364. '<a onmouseover=alert(document.cookie)>xxs link</a>',
  1365. '<IMG """><SCRIPT>alert("XSS")</SCRIPT>">',
  1366. '<IMG SRC=javascript:alert(String.fromCharCode(88,83,83))>',
  1367. '<IMG STYLE="xss:expr/*XSS*/ession(alert(\'XSS\'))">',
  1368. '<FRAMESET><FRAME SRC="javascript:alert(\'XSS\');"></FRAMESET>',
  1369. '<TABLE BACKGROUND="javascript:alert(\'XSS\')">',
  1370. '<TABLE><TD BACKGROUND="javascript:alert(\'XSS\')">',
  1371. '<SCRIPT>document.write("<SCRI");</SCRIPT>PT SRC="httx://xss.rocks/xss.js"></SCRIPT>'
  1372. ]),
  1373. 'string',
  1374. function (format, prefix, xss, suffix) {
  1375. var clean = jsdom(),
  1376. text = prefix + xss + suffix;
  1377. $('body').html(
  1378. '<div id="placeholder" class="hidden">+++ no paste text ' +
  1379. '+++</div><div id="prettymessage" class="hidden"><pre ' +
  1380. 'id="prettyprint" class="prettyprint linenums:1"></pre>' +
  1381. '</div><div id="plaintext" class="hidden"></div>'
  1382. );
  1383. $.PrivateBin.PasteViewer.init();
  1384. $.PrivateBin.PasteViewer.setFormat(format);
  1385. $.PrivateBin.PasteViewer.setText(text);
  1386. $.PrivateBin.PasteViewer.run();
  1387. var result = $('body').html().indexOf(xss) === -1;
  1388. clean();
  1389. return result;
  1390. }
  1391. );
  1392. });
  1393. });