Skip to content

Commit f3357bb

Browse files
committed
fix: update resource server validation to use allowedExternalUrls
1 parent c270577 commit f3357bb

File tree

3 files changed

+34
-15
lines changed

3 files changed

+34
-15
lines changed

packages/browser/src/__legacy__/helpers/authentication-helper.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
OIDCEndpoints,
3030
TokenResponse,
3131
extractPkceStorageKeyFromState,
32+
Config,
3233
} from '@asgardeo/javascript';
3334
import {SPAHelper} from './spa-helper';
3435
import {
@@ -103,26 +104,26 @@ export class AuthenticationHelper<T extends MainThreadClientConfig | WebWorkerCl
103104
config: SPACustomGrantConfig,
104105
enableRetrievingSignOutURLFromSession?: (config: SPACustomGrantConfig) => void,
105106
): Promise<User | Response> {
107+
const _config: Config = (await this._storageManager.getConfigData()) as Config;
106108
let useDefaultEndpoint = true;
107109
let matches = false;
108110

109111
// If the config does not contains a token endpoint, default token endpoint will be used.
110112
if (config?.tokenEndpoint) {
111113
useDefaultEndpoint = false;
112114

113-
for (const baseUrl of [
114-
...((await this._storageManager.getConfigData())?.resourceServerURLs ?? []),
115-
(config as any).baseUrl,
116-
]) {
115+
for (const baseUrl of [...(_config?.allowedExternalUrls ?? []), (config as any).baseUrl]) {
117116
if (baseUrl && config.tokenEndpoint?.startsWith(baseUrl)) {
118117
matches = true;
119118
break;
120119
}
121120
}
122121
}
122+
123123
if (config.shouldReplayAfterRefresh) {
124124
this._storageManager.setTemporaryDataParameter(CUSTOM_GRANT_CONFIG, JSON.stringify(config));
125125
}
126+
126127
if (useDefaultEndpoint || matches) {
127128
return this._authenticationClient
128129
.exchangeToken(config)
@@ -147,9 +148,9 @@ export class AuthenticationHelper<T extends MainThreadClientConfig | WebWorkerCl
147148
new AsgardeoAuthException(
148149
'SPA-MAIN_THREAD_CLIENT-RCG-IV01',
149150
'Request to the provided endpoint is prohibited.',
150-
'Requests can only be sent to resource servers specified by the `resourceServerURLs`' +
151+
'Requests can only be sent to resource servers specified by the `allowedExternalUrls`' +
151152
' attribute while initializing the SDK. The specified token endpoint in this request ' +
152-
'cannot be found among the `resourceServerURLs`',
153+
'cannot be found among the `allowedExternalUrls`',
153154
),
154155
);
155156
}
@@ -224,9 +225,9 @@ export class AuthenticationHelper<T extends MainThreadClientConfig | WebWorkerCl
224225
enableRetrievingSignOutURLFromSession?: (config: SPACustomGrantConfig) => void,
225226
): Promise<HttpResponse> {
226227
let matches = false;
227-
const config = await this._storageManager.getConfigData();
228+
const config: Config = (await this._storageManager.getConfigData()) as Config;
228229

229-
for (const baseUrl of [...((await config?.resourceServerURLs) ?? []), (config as any).baseUrl]) {
230+
for (const baseUrl of [...(config?.allowedExternalUrls ?? []), (config as any).baseUrl]) {
230231
if (baseUrl && requestConfig?.url?.startsWith(baseUrl)) {
231232
matches = true;
232233

@@ -319,9 +320,9 @@ export class AuthenticationHelper<T extends MainThreadClientConfig | WebWorkerCl
319320
new AsgardeoAuthException(
320321
'SPA-AUTH_HELPER-HR-IV02',
321322
'Request to the provided endpoint is prohibited.',
322-
'Requests can only be sent to resource servers specified by the `resourceServerURLs`' +
323+
'Requests can only be sent to resource servers specified by the `allowedExternalUrls`' +
323324
' attribute while initializing the SDK. The specified endpoint in this request ' +
324-
'cannot be found among the `resourceServerURLs`',
325+
'cannot be found among the `allowedExternalUrls`',
325326
),
326327
);
327328
}
@@ -335,12 +336,12 @@ export class AuthenticationHelper<T extends MainThreadClientConfig | WebWorkerCl
335336
httpFinishCallback?: () => void,
336337
): Promise<HttpResponse[] | undefined> {
337338
let matches = true;
338-
const config = await this._storageManager.getConfigData();
339+
const config: Config = (await this._storageManager.getConfigData()) as Config;
339340

340341
for (const requestConfig of requestConfigs) {
341342
let urlMatches = false;
342343

343-
for (const baseUrl of [...((await config)?.resourceServerURLs ?? []), (config as any).baseUrl]) {
344+
for (const baseUrl of [...(config?.allowedExternalUrls ?? []), (config as any).baseUrl]) {
344345
if (baseUrl && requestConfig.url?.startsWith(baseUrl)) {
345346
urlMatches = true;
346347

@@ -436,9 +437,9 @@ export class AuthenticationHelper<T extends MainThreadClientConfig | WebWorkerCl
436437
throw new AsgardeoAuthException(
437438
'SPA-AUTH_HELPER-HRA-IV02',
438439
'Request to the provided endpoint is prohibited.',
439-
'Requests can only be sent to resource servers specified by the `resourceServerURLs`' +
440+
'Requests can only be sent to resource servers specified by the `allowedExternalUrls`' +
440441
' attribute while initializing the SDK. The specified endpoint in this request ' +
441-
'cannot be found among the `resourceServerURLs`',
442+
'cannot be found among the `allowedExternalUrls`',
442443
);
443444
}
444445
}

packages/browser/src/__legacy__/models/client-config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export interface SPAConfig {
2828
syncSession?: boolean;
2929
checkSessionInterval?: number;
3030
sessionRefreshInterval?: number;
31-
resourceServerURLs?: string[];
31+
allowedExternalUrls?: string[];
3232
authParams?: Record<string, string>;
3333
periodicTokenRefresh?: boolean;
3434
autoLogoutOnTokenRefreshError?: boolean;

packages/javascript/src/models/config.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,24 @@ export interface BaseConfig<T = unknown> extends WithPreferences {
7878
*/
7979
afterSignOutUrl?: string | undefined;
8080

81+
/**
82+
* A list of external API base URLs that the SDK is allowed to attach access tokens to when making HTTP requests.
83+
*
84+
* When making authenticated HTTP requests using the SDK's HTTP client, the access token will only be attached
85+
* to requests whose URLs start with one of these specified base URLs. This provides a security layer by
86+
* preventing tokens from being sent to unauthorized servers.
87+
*
88+
* @remarks
89+
* - This is only applicable when the storage type is `webWorker`.
90+
* - Each URL should be a base URL without trailing slashes (e.g., "https://api.example.com").
91+
* - The SDK will check if the request URL starts with any of these base URLs before attaching the token.
92+
* - If a request is made to a URL that doesn't match any of these base URLs, an error will be thrown.
93+
*
94+
* @example
95+
* allowedExternalUrls: ["https://api.example.com", "https://api.another-service.com"]
96+
*/
97+
allowedExternalUrls?: string[];
98+
8199
/**
82100
* Optional organization handle for the Organization in Asgardeo.
83101
* This is used to identify the organization in the Asgardeo identity server in cases like Branding, etc.

0 commit comments

Comments
 (0)