@sparkvault/sdk-js 1.27.3 → 1.27.5

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.
@@ -61,14 +61,72 @@ class PopupBlockedError extends SparkVaultError {
61
61
  }
62
62
  }
63
63
 
64
+ /**
65
+ * SparkVault-owned domain registry.
66
+ *
67
+ * Keep platform domain decisions centralized: CSP, redirect safety, custom
68
+ * domain reservation, and Identity postMessage trust all depend on the same
69
+ * ownership boundary.
70
+ */
71
+
72
+ const SPARKVAULT_ROOT_DOMAIN = 'sparkvault.com';
73
+
74
+ const SPARKVAULT_PLATFORM_SV_HOSTS = Object.freeze([
75
+ 'auth.sv',
76
+ 'x.sv',
77
+ 'files.sv',
78
+ 'file.sv',
79
+ 'send.sv',
80
+ 'spark.sv',
81
+ 'by.sv',
82
+ 'at.sv',
83
+ 'db.sv'
84
+ ]);
85
+
86
+ const SPARKVAULT_WEBSOCKET_HOSTS = Object.freeze([
87
+ 'ws.sparkvault.com'
88
+ ]);
89
+
90
+ Object.freeze([
91
+ SPARKVAULT_ROOT_DOMAIN,
92
+ 'app.sparkvault.com',
93
+ 'api.sparkvault.com',
94
+ 'cdn.sparkvault.com',
95
+ 'forge.sparkvault.com',
96
+ 'img.sparkvault.com',
97
+ ...SPARKVAULT_WEBSOCKET_HOSTS,
98
+ ...SPARKVAULT_PLATFORM_SV_HOSTS
99
+ ]);
100
+
101
+ function normalizeHostname(hostname) {
102
+ if (typeof hostname !== 'string') return '';
103
+ return hostname.trim().toLowerCase().replace(/\.$/, '');
104
+ }
105
+
106
+ function isSparkVaultRootOrSubdomain(hostname) {
107
+ const normalized = normalizeHostname(hostname);
108
+ return normalized === SPARKVAULT_ROOT_DOMAIN || normalized.endsWith(`.${SPARKVAULT_ROOT_DOMAIN}`);
109
+ }
110
+
111
+ function isSparkVaultPlatformSvHost(hostname) {
112
+ return SPARKVAULT_PLATFORM_SV_HOSTS.includes(normalizeHostname(hostname));
113
+ }
114
+
115
+ function isSparkVaultOwnedHostname(hostname) {
116
+ return isSparkVaultRootOrSubdomain(hostname) || isSparkVaultPlatformSvHost(hostname);
117
+ }
118
+
64
119
  /**
65
120
  * SparkVault SDK Configuration
66
121
  */
122
+ function escapeRegExp(value) {
123
+ return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
124
+ }
125
+ const SPARKVAULT_PLATFORM_SV_DOWNLOAD_PATTERN = new RegExp(`(^|\\.)(${SPARKVAULT_PLATFORM_SV_HOSTS.map(escapeRegExp).join('|')})$`, 'i');
67
126
  // Hosts that are allowed to serve backend-issued ingot download URLs.
68
- // Keep in sync with the sdk-mobile default in packages/sdk-mobile/src/config.ts.
69
127
  const DEFAULT_ALLOWED_DOWNLOAD_HOST_PATTERNS = [
70
- /(^|\.)sparkvault\.com$/i,
71
- /(^|\.)(x|files|file|send|spark|by|at|db|auth)\.sv$/i,
128
+ new RegExp(`(^|\\.)${escapeRegExp(SPARKVAULT_ROOT_DOMAIN)}$`, 'i'),
129
+ SPARKVAULT_PLATFORM_SV_DOWNLOAD_PATTERN,
72
130
  /\.amazonaws\.com$/i,
73
131
  /\.cloudfront\.net$/i,
74
132
  ];
@@ -1102,11 +1160,24 @@ class IdentityApi extends SparkVaultApi {
1102
1160
  /**
1103
1161
  * Get OAuth redirect URL for social provider
1104
1162
  */
1105
- getSocialAuthUrl(provider, redirectUri, state) {
1163
+ getSocialAuthUrl(provider, redirectUri, state, authContext) {
1106
1164
  const params = new URLSearchParams({
1107
1165
  redirect_uri: redirectUri,
1108
1166
  state,
1109
1167
  });
1168
+ if (authContext?.authRequestId) {
1169
+ params.set('auth_request_id', authContext.authRequestId);
1170
+ }
1171
+ if (authContext?.simpleMode) {
1172
+ const simpleMode = authContext.simpleMode.sessionId
1173
+ ? { session_id: authContext.simpleMode.sessionId }
1174
+ : {
1175
+ success_url: authContext.simpleMode.successUrl,
1176
+ failure_url: authContext.simpleMode.failureUrl,
1177
+ state: authContext.simpleMode.state,
1178
+ };
1179
+ params.set('simple_mode', JSON.stringify(simpleMode));
1180
+ }
1110
1181
  return `${this.baseUrl}/social/${provider}?${params}`;
1111
1182
  }
1112
1183
  /**
@@ -1620,12 +1691,7 @@ class PasskeyHandler {
1620
1691
  isValidMessageOrigin(origin) {
1621
1692
  try {
1622
1693
  const url = new URL(origin);
1623
- const hostname = url.hostname;
1624
- // Allow sparkvault.com and subdomains
1625
- if (hostname === 'sparkvault.com' || hostname.endsWith('.sparkvault.com')) {
1626
- return true;
1627
- }
1628
- return false;
1694
+ return url.protocol === 'https:' && url.origin === origin && !url.port && isSparkVaultOwnedHostname(url.hostname);
1629
1695
  }
1630
1696
  catch {
1631
1697
  return false;
@@ -5879,7 +5945,7 @@ class IdentityRenderer {
5879
5945
  const redirectUri = window.location.origin + window.location.pathname;
5880
5946
  sessionStorage.setItem('sv_oauth_state', state);
5881
5947
  sessionStorage.setItem('sv_oauth_email', this.recipient);
5882
- const authUrl = this.api.getSocialAuthUrl(provider, redirectUri, state);
5948
+ const authUrl = this.api.getSocialAuthUrl(provider, redirectUri, state, this.verificationState.authContext);
5883
5949
  window.location.href = authUrl;
5884
5950
  }
5885
5951
  normalizeError(error) {
@@ -6719,6 +6785,10 @@ class IdentityModule {
6719
6785
  if (!token) {
6720
6786
  return null;
6721
6787
  }
6788
+ const expectedState = sessionStorage.getItem('sv_oauth_state');
6789
+ if (expectedState && params.get('state') !== expectedState) {
6790
+ throw new ValidationError('Invalid OAuth state');
6791
+ }
6722
6792
  const claims = await this.verifyToken(token);
6723
6793
  const result = {
6724
6794
  token,
@@ -6730,6 +6800,8 @@ class IdentityModule {
6730
6800
  if (typeof window.history?.replaceState === 'function') {
6731
6801
  window.history.replaceState(window.history.state, document.title, `${window.location.pathname}${window.location.search}`);
6732
6802
  }
6803
+ sessionStorage.removeItem('sv_oauth_state');
6804
+ sessionStorage.removeItem('sv_oauth_email');
6733
6805
  return result;
6734
6806
  }
6735
6807
  /**