common.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. global.WebCrypto = require('node-webcrypto-ossl');
  9. // application libraries to test
  10. global.$ = global.jQuery = require('./jquery-3.4.1');
  11. global.RawDeflate = require('./rawinflate-0.3').RawDeflate;
  12. global.zlib = require('./zlib-1.2.11').zlib;
  13. require('./prettify');
  14. global.prettyPrint = window.PR.prettyPrint;
  15. global.prettyPrintOne = window.PR.prettyPrintOne;
  16. global.showdown = require('./showdown-1.9.0');
  17. global.DOMPurify = require('./purify-1.0.7');
  18. global.baseX = require('./base-x-3.0.5.1').baseX;
  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. digitString = ['0','1','2','3','4','5','6','7','8','9'],
  25. alnumString = a2zString.concat(digitString),
  26. hexString = digitString.concat(['a','b','c','d','e','f']),
  27. queryString = alnumString.concat(['+','%','&','.','*','-','_']),
  28. hashString = queryString.concat(['!']),
  29. base64String = alnumString.concat(['+','/','=']).concat(
  30. a2zString.map(function(c) {
  31. return c.toUpperCase();
  32. })
  33. ),
  34. schemas = ['ftp','gopher','http','https','ws','wss'],
  35. supportedLanguages = ['de', 'es', 'fr', 'it', 'no', 'pl', 'pt', 'oc', 'ru', 'sl', 'zh'],
  36. mimeTypes = ['image/png', 'application/octet-stream'],
  37. formats = ['plaintext', 'markdown', 'syntaxhighlighting'],
  38. /**
  39. * character to HTML entity lookup table
  40. *
  41. * @see {@link https://github.com/janl/mustache.js/blob/master/mustache.js#L60}
  42. */
  43. entityMap = {
  44. '&': '&',
  45. '<': '&lt;',
  46. '>': '&gt;',
  47. '"': '&quot;',
  48. "'": '&#39;',
  49. '/': '&#x2F;',
  50. '`': '&#x60;',
  51. '=': '&#x3D;'
  52. },
  53. mimeFile = fs.createReadStream('/etc/mime.types'),
  54. mimeLine = '';
  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. exports.atob = atob;
  88. exports.btoa = btoa;
  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 hexadecimal notation
  113. exports.jscHexString = function() {
  114. return jsc.elements(hexString);
  115. };
  116. // provides random characters allowed in GET queries
  117. exports.jscQueryString = function() {
  118. return jsc.elements(queryString);
  119. };
  120. // provides random characters allowed in hash queries
  121. exports.jscHashString = function() {
  122. return jsc.elements(hashString);
  123. };
  124. // provides random characters allowed in base64 encoded strings
  125. exports.jscBase64String = function() {
  126. return jsc.elements(base64String);
  127. };
  128. // provides a random URL schema supported by the whatwg-url library
  129. exports.jscSchemas = function() {
  130. return jsc.elements(schemas);
  131. };
  132. // provides a random supported language string
  133. exports.jscSupportedLanguages = function() {
  134. return jsc.elements(supportedLanguages);
  135. };
  136. // provides a random mime type
  137. exports.jscMimeTypes = function() {
  138. return jsc.elements(mimeTypes);
  139. };
  140. // provides a random PrivateBin paste formatter
  141. exports.jscFormats = function() {
  142. return jsc.elements(formats);
  143. };