redact.js 3.7 KB

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