租房小程序前端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.4 KiB

3 months ago
  1. 'use strict';
  2. // See http://www.robvanderwoude.com/escapechars.php
  3. const metaCharsRegExp = /([()\][%!^"`<>&|;, *?])/g;
  4. function escapeCommand(arg) {
  5. // Escape meta chars
  6. arg = arg.replace(metaCharsRegExp, '^$1');
  7. return arg;
  8. }
  9. function escapeArgument(arg, doubleEscapeMetaChars) {
  10. // Convert to string
  11. arg = `${arg}`;
  12. // Algorithm below is based on https://qntm.org/cmd
  13. // It's slightly altered to disable JS backtracking to avoid hanging on specially crafted input
  14. // Please see https://github.com/moxystudio/node-cross-spawn/pull/160 for more information
  15. // Sequence of backslashes followed by a double quote:
  16. // double up all the backslashes and escape the double quote
  17. arg = arg.replace(/(?=(\\+?)?)\1"/g, '$1$1\\"');
  18. // Sequence of backslashes followed by the end of the string
  19. // (which will become a double quote later):
  20. // double up all the backslashes
  21. arg = arg.replace(/(?=(\\+?)?)\1$/, '$1$1');
  22. // All other backslashes occur literally
  23. // Quote the whole thing:
  24. arg = `"${arg}"`;
  25. // Escape meta chars
  26. arg = arg.replace(metaCharsRegExp, '^$1');
  27. // Double escape meta chars if necessary
  28. if (doubleEscapeMetaChars) {
  29. arg = arg.replace(metaCharsRegExp, '^$1');
  30. }
  31. return arg;
  32. }
  33. module.exports.command = escapeCommand;
  34. module.exports.argument = escapeArgument;