DomainValidator.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. 'use strict';
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. /**
  4. * Licensed Materials - Property of IBM
  5. * IBM Business Analytics (C) Copyright IBM Corp. 2019
  6. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  7. */
  8. /**
  9. * @class DomainValidator
  10. * @hideconstructor
  11. * @classdesc This class provides DomainValidator
  12. */
  13. define([], function () {
  14. var DomainValidator = function () {
  15. function DomainValidator(configValue) {
  16. _classCallCheck(this, DomainValidator);
  17. this._validDomainListInitialized = false;
  18. this._validDomainList = null;
  19. this._validDomains = '';
  20. this.init(configValue);
  21. }
  22. DomainValidator.prototype.init = function init(configValue) {
  23. var _this = this;
  24. this._validDomainListInitialized = true;
  25. if (configValue && configValue !== 'myDefault') {
  26. this._validDomains = configValue;
  27. this._validDomainList = [];
  28. configValue.toLowerCase().split(',').forEach(function (host) {
  29. var hostAndPort = host.trim().split(':');
  30. var hostname = hostAndPort[0];
  31. var allowAllSubDomains = false,
  32. allowFirstSubDomain = false;
  33. if (hostname.indexOf('*.') === 0) {
  34. allowAllSubDomains = true;
  35. allowFirstSubDomain = true;
  36. hostname = hostname.substring(2);
  37. } else if (hostname.indexOf('.') === 0) {
  38. allowFirstSubDomain = true;
  39. hostname = hostname.substring(1);
  40. }
  41. _this._validDomainList.push({
  42. hostname: hostname,
  43. port: hostAndPort[1],
  44. allowFirstSubDomain: allowFirstSubDomain,
  45. allowAllSubDomains: allowAllSubDomains
  46. });
  47. });
  48. }
  49. };
  50. DomainValidator.prototype.isAllowedDomain = function isAllowedDomain(url) {
  51. if (!url || !this._validDomainListInitialized) {
  52. return false;
  53. }
  54. if (!this._validDomainList) {
  55. //If a valid domain list has not been specified, then all domains are valid.
  56. return true;
  57. }
  58. var findHost = url.toLowerCase() + '/';
  59. var hostname = void 0,
  60. port = void 0,
  61. hasPort = false;
  62. if (findHost.indexOf('://') !== -1) {
  63. findHost = findHost.substring(findHost.indexOf('://') + 3);
  64. findHost = findHost.substring(0, findHost.indexOf('/'));
  65. if (findHost.indexOf('@') !== -1) {
  66. //If user info is present in the url, strip that out
  67. findHost = findHost.substring(findHost.indexOf('@') + 1);
  68. }
  69. findHost = findHost.split(':');
  70. hostname = findHost[0];
  71. port = findHost[1];
  72. } else {
  73. hostname = window.location.hostname;
  74. port = window.location.port;
  75. }
  76. hasPort = !!port;
  77. return !!this._validDomainList.find(function (validDomainEntry) {
  78. //Validate port if present in the valid domain entry.
  79. if (validDomainEntry.port && (!hasPort || validDomainEntry.port !== port)) {
  80. return false;
  81. }
  82. if (validDomainEntry.hostname === hostname) {
  83. return true;
  84. } else if (validDomainEntry.allowFirstSubDomain) {
  85. var baseHost = '.' + validDomainEntry.hostname;
  86. if (hostname.substring(hostname.length - baseHost.length, hostname.length) !== baseHost) {
  87. return false; //Host name doesn't start with the fixed portion.
  88. }
  89. if (validDomainEntry.allowAllSubDomains) {
  90. //If a * was used, then any additonal subdomains are valid.
  91. return true;
  92. }
  93. var subdomain = hostname.substring(0, hostname.length - baseHost.length);
  94. return subdomain.indexOf('.') === -1;
  95. }
  96. return false;
  97. });
  98. };
  99. DomainValidator.prototype.getAllowedDomains = function getAllowedDomains() {
  100. return this._validDomains;
  101. };
  102. DomainValidator.prototype.isAllowedProtocol = function isAllowedProtocol(url) {
  103. if (!url) {
  104. return false;
  105. }
  106. url = url.toLowerCase();
  107. // https:// is always allowed - i.e. both in pages served from HTTP or
  108. // HTTPS
  109. // // is always allowed as it will copy whichever protocol the
  110. // containing page is using
  111. // http:// is only allowed if the containing page is using HTTP as well
  112. if (url.indexOf('https://') === 0 || url.indexOf('//') === 0) {
  113. return true;
  114. }
  115. return false;
  116. };
  117. return DomainValidator;
  118. }();
  119. return DomainValidator;
  120. });
  121. //# sourceMappingURL=DomainValidator.js.map