common.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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.1.9').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.6.1');
  18. global.DOMPurify = require('./purify-1.0.3');
  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. base64String = alnumString.concat(['+','/','=']).concat(
  27. a2zString.map(function(c) {
  28. return c.toUpperCase();
  29. })
  30. ),
  31. schemas = ['ftp','gopher','http','https','ws','wss'],
  32. supportedLanguages = ['de', 'es', 'fr', 'it', 'no', 'pl', 'pt', 'oc', 'ru', 'sl', 'zh'],
  33. mimeTypes = ['image/png', 'application/octet-stream'],
  34. formats = ['plaintext', 'markdown', 'syntaxhighlighting'],
  35. /**
  36. * character to HTML entity lookup table
  37. *
  38. * @see {@link https://github.com/janl/mustache.js/blob/master/mustache.js#L60}
  39. */
  40. entityMap = {
  41. '&': '&',
  42. '<': '&lt;',
  43. '>': '&gt;',
  44. '"': '&quot;',
  45. "'": '&#39;',
  46. '/': '&#x2F;',
  47. '`': '&#x60;',
  48. '=': '&#x3D;'
  49. },
  50. logFile = fs.createWriteStream('test.log'),
  51. mimeFile = fs.createReadStream('/etc/mime.types'),
  52. mimeLine = '';
  53. // redirect console messages to log file
  54. console.info = console.warn = console.error = function () {
  55. logFile.write(Array.prototype.slice.call(arguments).join('') + '\n');
  56. };
  57. // populate mime types from environment
  58. mimeFile.on('data', function(data) {
  59. mimeLine += data;
  60. var index = mimeLine.indexOf('\n');
  61. while (index > -1) {
  62. var line = mimeLine.substring(0, index);
  63. mimeLine = mimeLine.substring(index + 1);
  64. parseMime(line);
  65. index = mimeLine.indexOf('\n');
  66. }
  67. });
  68. mimeFile.on('end', function() {
  69. if (mimeLine.length > 0) {
  70. parseMime(mimeLine);
  71. }
  72. });
  73. function parseMime(line) {
  74. // ignore comments
  75. var index = line.indexOf('#');
  76. if (index > -1) {
  77. line = line.substring(0, index);
  78. }
  79. // ignore bits after tabs
  80. index = line.indexOf('\t');
  81. if (index > -1) {
  82. line = line.substring(0, index);
  83. }
  84. if (line.length > 0) {
  85. mimeTypes.push(line);
  86. }
  87. }
  88. // common testing helper functions
  89. /**
  90. * convert all applicable characters to HTML entities
  91. *
  92. * @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}
  93. * @name htmlEntities
  94. * @function
  95. * @param {string} str
  96. * @return {string} escaped HTML
  97. */
  98. exports.htmlEntities = function(str) {
  99. return String(str).replace(
  100. /[&<>"'`=\/]/g, function(s) {
  101. return entityMap[s];
  102. });
  103. };
  104. // provides random lowercase characters from a to z
  105. exports.jscA2zString = function() {
  106. return jsc.elements(a2zString);
  107. };
  108. // provides random lowercase alpha numeric characters (a to z and 0 to 9)
  109. exports.jscAlnumString = function() {
  110. return jsc.elements(alnumString);
  111. };
  112. // provides random characters allowed in GET queries
  113. exports.jscQueryString = function() {
  114. return jsc.elements(queryString);
  115. };
  116. // provides random characters allowed in base64 encoded strings
  117. exports.jscBase64String = function() {
  118. return jsc.elements(base64String);
  119. };
  120. // provides a random URL schema supported by the whatwg-url library
  121. exports.jscSchemas = function() {
  122. return jsc.elements(schemas);
  123. };
  124. // provides a random supported language string
  125. exports.jscSupportedLanguages = function() {
  126. return jsc.elements(supportedLanguages);
  127. };
  128. // provides a random mime type
  129. exports.jscMimeTypes = function() {
  130. return jsc.elements(mimeTypes);
  131. };
  132. // provides a random PrivateBin paste formatter
  133. exports.jscFormats = function() {
  134. return jsc.elements(formats);
  135. };