common.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. 'use strict';
  2. // testing prerequisites
  3. global.jsc = require('jsverify');
  4. global.jsdom = require('jsdom-global');
  5. global.cleanup = global.jsdom();
  6. global.fs = require('fs');
  7. // application libraries to test
  8. global.$ = global.jQuery = require('./jquery-3.1.1');
  9. global.sjcl = require('./sjcl-1.0.6');
  10. global.Base64 = require('./base64-2.1.9').Base64;
  11. global.RawDeflate = require('./rawdeflate-0.5').RawDeflate;
  12. global.RawDeflate.inflate = require('./rawinflate-0.3').RawDeflate.inflate;
  13. require('./prettify');
  14. global.prettyPrint = window.PR.prettyPrint;
  15. global.prettyPrintOne = window.PR.prettyPrintOne;
  16. global.showdown = require('./showdown-1.6.1');
  17. global.DOMPurify = require('./purify.min');
  18. require('./bootstrap-3.3.7');
  19. require('./privatebin');
  20. // internal variables
  21. var a2zString = ['a','b','c','d','e','f','g','h','i','j','k','l','m',
  22. 'n','o','p','q','r','s','t','u','v','w','x','y','z'],
  23. alnumString = a2zString.concat(['0','1','2','3','4','5','6','7','8','9']),
  24. queryString = alnumString.concat(['+','%','&','.','*','-','_']),
  25. base64String = alnumString.concat(['+','/','=']).concat(
  26. a2zString.map(function(c) {
  27. return c.toUpperCase();
  28. })
  29. ),
  30. schemas = ['ftp','gopher','http','https','ws','wss'],
  31. supportedLanguages = ['de', 'es', 'fr', 'it', 'no', 'pl', 'pt', 'oc', 'ru', 'sl', 'zh'],
  32. mimeTypes = ['image/png', 'application/octet-stream'],
  33. /**
  34. * character to HTML entity lookup table
  35. *
  36. * @see {@link https://github.com/janl/mustache.js/blob/master/mustache.js#L60}
  37. */
  38. entityMap = {
  39. '&': '&',
  40. '<': '&lt;',
  41. '>': '&gt;',
  42. '"': '&quot;',
  43. "'": '&#39;',
  44. '/': '&#x2F;',
  45. '`': '&#x60;',
  46. '=': '&#x3D;'
  47. },
  48. logFile = fs.createWriteStream('test.log'),
  49. mimeFile = fs.createReadStream('/etc/mime.types'),
  50. mimeLine = '';
  51. // redirect console messages to log file
  52. console.info = console.warn = console.error = function () {
  53. logFile.write(Array.prototype.slice.call(arguments).join('') + '\n');
  54. }
  55. // populate mime types from environment
  56. mimeFile.on('data', function(data) {
  57. mimeLine += data;
  58. var index = mimeLine.indexOf('\n');
  59. while (index > -1) {
  60. var line = mimeLine.substring(0, index);
  61. mimeLine = mimeLine.substring(index + 1);
  62. parseMime(line);
  63. index = mimeLine.indexOf('\n');
  64. }
  65. });
  66. mimeFile.on('end', function() {
  67. if (mimeLine.length > 0) {
  68. parseMime(mimeLine);
  69. }
  70. });
  71. function parseMime(line) {
  72. // ignore comments
  73. var index = line.indexOf('#');
  74. if (index > -1) {
  75. line = line.substring(0, index);
  76. }
  77. // ignore bits after tabs
  78. index = line.indexOf('\t');
  79. if (index > -1) {
  80. line = line.substring(0, index);
  81. }
  82. if (line.length > 0) {
  83. mimeTypes.push(line);
  84. }
  85. }
  86. // common testing helper functions
  87. /**
  88. * convert all applicable characters to HTML entities
  89. *
  90. * @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}
  91. * @name htmlEntities
  92. * @function
  93. * @param {string} str
  94. * @return {string} escaped HTML
  95. */
  96. exports.htmlEntities = function(str) {
  97. return String(str).replace(
  98. /[&<>"'`=\/]/g, function(s) {
  99. return entityMap[s];
  100. });
  101. }
  102. // provides random lowercase characters from a to z
  103. exports.jscA2zString = function() {
  104. return jsc.elements(a2zString);
  105. }
  106. // provides random lowercase alpha numeric characters (a to z and 0 to 9)
  107. exports.jscAlnumString = function() {
  108. return jsc.elements(alnumString);
  109. }
  110. // provides random characters allowed in GET queries
  111. exports.jscQueryString = function() {
  112. return jsc.elements(queryString);
  113. }
  114. // provides random characters allowed in base64 encoded strings
  115. exports.jscBase64String = function() {
  116. return jsc.elements(base64String);
  117. }
  118. // provides a random URL schema supported by the whatwg-url library
  119. exports.jscSchemas = function() {
  120. return jsc.elements(schemas);
  121. }
  122. // provides a random supported language string
  123. exports.jscSupportedLanguages = function() {
  124. return jsc.elements(supportedLanguages);
  125. }