dsn.js 3.5 KB

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