@baukit/suite-client 0.18.3 → 0.19.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/urls.ts CHANGED
@@ -85,13 +85,22 @@ function hasInvalidUrlCharacters(value: string): boolean {
85
85
  return false;
86
86
  }
87
87
 
88
+ function isLoopbackHost(hostname: string): boolean {
89
+ return (
90
+ hostname === 'localhost' ||
91
+ hostname === '[::1]' ||
92
+ /^127\.(?:\d{1,3}\.){2}\d{1,3}$/.test(hostname)
93
+ );
94
+ }
95
+
96
+ function hasAllowedScheme(url: URL, allowLoopback: boolean): boolean {
97
+ if (url.protocol === 'https:') return true;
98
+ return url.protocol === 'http:' && allowLoopback && isLoopbackHost(url.hostname);
99
+ }
100
+
88
101
  function authorizeEndpoint(value: string, allowLoopback = false): URL {
89
102
  const url = new URL(value);
90
103
  const authority = value.split('://')[1]?.split(/[/?#]/, 1)[0];
91
- const loopback =
92
- url.hostname === 'localhost' ||
93
- url.hostname === '[::1]' ||
94
- /^127\.(?:\d{1,3}\.){2}\d{1,3}$/.test(url.hostname);
95
104
  if (
96
105
  authority === undefined ||
97
106
  authority.includes('@') ||
@@ -100,7 +109,7 @@ function authorizeEndpoint(value: string, allowLoopback = false): URL {
100
109
  url.password !== '' ||
101
110
  value.includes('?') ||
102
111
  value.includes('#') ||
103
- (url.protocol !== 'https:' && !(url.protocol === 'http:' && allowLoopback && loopback))
112
+ !hasAllowedScheme(url, allowLoopback)
104
113
  ) {
105
114
  throw new TypeError('Invalid suite authorize URL.');
106
115
  }