common.js 3.3 KB

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