redact.js 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. var desc = Object.getOwnPropertyDescriptor(m, k);
  5. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  6. desc = { enumerable: true, get: function() { return m[k]; } };
  7. }
  8. Object.defineProperty(o, k2, desc);
  9. }) : (function(o, m, k, k2) {
  10. if (k2 === undefined) k2 = k;
  11. o[k2] = m[k];
  12. }));
  13. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  14. Object.defineProperty(o, "default", { enumerable: true, value: v });
  15. }) : function(o, v) {
  16. o["default"] = v;
  17. });
  18. var __importStar = (this && this.__importStar) || (function () {
  19. var ownKeys = function(o) {
  20. ownKeys = Object.getOwnPropertyNames || function (o) {
  21. var ar = [];
  22. for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
  23. return ar;
  24. };
  25. return ownKeys(o);
  26. };
  27. return function (mod) {
  28. if (mod && mod.__esModule) return mod;
  29. var result = {};
  30. if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
  31. __setModuleDefault(result, mod);
  32. return result;
  33. };
  34. })();
  35. Object.defineProperty(exports, "__esModule", { value: true });
  36. exports.redactValidConnectionString = redactValidConnectionString;
  37. exports.redactConnectionString = redactConnectionString;
  38. const index_1 = __importStar(require("./index"));
  39. function redactValidConnectionString(inputUrl, options) {
  40. const url = inputUrl.clone();
  41. const replacementString = options?.replacementString ?? '_credentials_';
  42. const redactUsernames = options?.redactUsernames ?? true;
  43. if ((url.username || url.password) && redactUsernames) {
  44. url.username = replacementString;
  45. url.password = '';
  46. }
  47. else if (url.password) {
  48. url.password = replacementString;
  49. }
  50. if (url.searchParams.has('authMechanismProperties')) {
  51. const props = new index_1.CommaAndColonSeparatedRecord(url.searchParams.get('authMechanismProperties'));
  52. if (props.get('AWS_SESSION_TOKEN')) {
  53. props.set('AWS_SESSION_TOKEN', replacementString);
  54. url.searchParams.set('authMechanismProperties', props.toString());
  55. }
  56. }
  57. if (url.searchParams.has('tlsCertificateKeyFilePassword')) {
  58. url.searchParams.set('tlsCertificateKeyFilePassword', replacementString);
  59. }
  60. if (url.searchParams.has('proxyUsername') && redactUsernames) {
  61. url.searchParams.set('proxyUsername', replacementString);
  62. }
  63. if (url.searchParams.has('proxyPassword')) {
  64. url.searchParams.set('proxyPassword', replacementString);
  65. }
  66. return url;
  67. }
  68. function redactConnectionString(uri, options) {
  69. const replacementString = options?.replacementString ?? '<credentials>';
  70. const redactUsernames = options?.redactUsernames ?? true;
  71. let parsed;
  72. try {
  73. parsed = new index_1.default(uri);
  74. }
  75. catch {
  76. }
  77. if (parsed) {
  78. options = { ...options, replacementString: '___credentials___' };
  79. return parsed
  80. .redact(options)
  81. .toString()
  82. .replace(/___credentials___/g, replacementString);
  83. }
  84. const R = replacementString;
  85. const replacements = [
  86. uri => uri.replace(redactUsernames ? /(\/\/)(.*)(@)/g : /(\/\/[^@]*:)(.*)(@)/g, `$1${R}$3`),
  87. uri => uri.replace(/(AWS_SESSION_TOKEN(:|%3A))([^,&]+)/gi, `$1${R}`),
  88. uri => uri.replace(/(tlsCertificateKeyFilePassword=)([^&]+)/gi, `$1${R}`),
  89. uri => (redactUsernames ? uri.replace(/(proxyUsername=)([^&]+)/gi, `$1${R}`) : uri),
  90. uri => uri.replace(/(proxyPassword=)([^&]+)/gi, `$1${R}`)
  91. ];
  92. for (const replacer of replacements) {
  93. uri = replacer(uri);
  94. }
  95. return uri;
  96. }
  97. //# sourceMappingURL=redact.js.map