dsn.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. Object.defineProperty(exports, '__esModule', { value: true });
  2. const debugBuild = require('./debug-build.js');
  3. const logger = require('./logger.js');
  4. /** Regular expression used to parse a Dsn. */
  5. const DSN_REGEX = /^(?:(\w+):)\/\/(?:(\w+)(?::(\w+)?)?@)([\w.-]+)(?::(\d+))?\/(.+)/;
  6. function isValidProtocol(protocol) {
  7. return protocol === 'http' || protocol === 'https';
  8. }
  9. /**
  10. * Renders the string representation of this Dsn.
  11. *
  12. * By default, this will render the public representation without the password
  13. * component. To get the deprecated private representation, set `withPassword`
  14. * to true.
  15. *
  16. * @param withPassword When set to true, the password will be included.
  17. */
  18. function dsnToString(dsn, withPassword = false) {
  19. const { host, path, pass, port, projectId, protocol, publicKey } = dsn;
  20. return (
  21. `${protocol}://${publicKey}${withPassword && pass ? `:${pass}` : ''}` +
  22. `@${host}${port ? `:${port}` : ''}/${path ? `${path}/` : path}${projectId}`
  23. );
  24. }
  25. /**
  26. * Parses a Dsn from a given string.
  27. *
  28. * @param str A Dsn as string
  29. * @returns Dsn as DsnComponents or undefined if @param str is not a valid DSN string
  30. */
  31. function dsnFromString(str) {
  32. const match = DSN_REGEX.exec(str);
  33. if (!match) {
  34. // This should be logged to the console
  35. logger.consoleSandbox(() => {
  36. // eslint-disable-next-line no-console
  37. console.error(`Invalid Sentry Dsn: ${str}`);
  38. });
  39. return undefined;
  40. }
  41. const [protocol, publicKey, pass = '', host, port = '', lastPath] = match.slice(1);
  42. let path = '';
  43. let projectId = lastPath;
  44. const split = projectId.split('/');
  45. if (split.length > 1) {
  46. path = split.slice(0, -1).join('/');
  47. projectId = split.pop() ;
  48. }
  49. if (projectId) {
  50. const projectMatch = projectId.match(/^\d+/);
  51. if (projectMatch) {
  52. projectId = projectMatch[0];
  53. }
  54. }
  55. return dsnFromComponents({ host, pass, path, projectId, port, protocol: protocol , publicKey });
  56. }
  57. function dsnFromComponents(components) {
  58. return {
  59. protocol: components.protocol,
  60. publicKey: components.publicKey || '',
  61. pass: components.pass || '',
  62. host: components.host,
  63. port: components.port || '',
  64. path: components.path || '',
  65. projectId: components.projectId,
  66. };
  67. }
  68. function validateDsn(dsn) {
  69. if (!debugBuild.DEBUG_BUILD) {
  70. return true;
  71. }
  72. const { port, projectId, protocol } = dsn;
  73. const requiredComponents = ['protocol', 'publicKey', 'host', 'projectId'];
  74. const hasMissingRequiredComponent = requiredComponents.find(component => {
  75. if (!dsn[component]) {
  76. logger.logger.error(`Invalid Sentry Dsn: ${component} missing`);
  77. return true;
  78. }
  79. return false;
  80. });
  81. if (hasMissingRequiredComponent) {
  82. return false;
  83. }
  84. if (!projectId.match(/^\d+$/)) {
  85. logger.logger.error(`Invalid Sentry Dsn: Invalid projectId ${projectId}`);
  86. return false;
  87. }
  88. if (!isValidProtocol(protocol)) {
  89. logger.logger.error(`Invalid Sentry Dsn: Invalid protocol ${protocol}`);
  90. return false;
  91. }
  92. if (port && isNaN(parseInt(port, 10))) {
  93. logger.logger.error(`Invalid Sentry Dsn: Invalid port ${port}`);
  94. return false;
  95. }
  96. return true;
  97. }
  98. /**
  99. * Creates a valid Sentry Dsn object, identifying a Sentry instance and project.
  100. * @returns a valid DsnComponents object or `undefined` if @param from is an invalid DSN source
  101. */
  102. function makeDsn(from) {
  103. const components = typeof from === 'string' ? dsnFromString(from) : dsnFromComponents(from);
  104. if (!components || !validateDsn(components)) {
  105. return undefined;
  106. }
  107. return components;
  108. }
  109. exports.dsnFromString = dsnFromString;
  110. exports.dsnToString = dsnToString;
  111. exports.makeDsn = makeDsn;
  112. //# sourceMappingURL=dsn.js.map