common.js 4.6 KB

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