Skip to content

Commit fa5c53b

Browse files
authored
Auth: Cleans up stale or completed auth details from storage (#20725)
* fix: cleans up stale PKCE keys after auth regardless of success or error * fix: cleans up stale PKCE data on logout
1 parent 43ac322 commit fa5c53b

File tree

2 files changed

+45
-7
lines changed

2 files changed

+45
-7
lines changed

src/Umbraco.Web.UI.Client/src/external/openid/src/redirect_based_handler.ts

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,41 @@ export class RedirectRequestHandler extends AuthorizationRequestHandler {
7777
});
7878
}
7979

80+
/**
81+
* Cleanup all stale authorization requests and configurations from storage.
82+
* This scans localStorage for any keys matching the appauth patterns and removes them,
83+
* including the authorization request handle key.
84+
*/
85+
public cleanupStaleAuthorizationData(): Promise<void> {
86+
// Check if we're in a browser environment with localStorage
87+
if (typeof window === 'undefined' || !window.localStorage) {
88+
return Promise.resolve();
89+
}
90+
91+
const keysToRemove: string[] = [];
92+
93+
// Scan localStorage for all appauth-related keys
94+
for (let i = 0; i < window.localStorage.length; i++) {
95+
const key = window.localStorage.key(i);
96+
if (
97+
key &&
98+
(key.includes('_appauth_authorization_request') ||
99+
key.includes('_appauth_authorization_service_configuration') ||
100+
key === AUTHORIZATION_REQUEST_HANDLE_KEY)
101+
) {
102+
keysToRemove.push(key);
103+
}
104+
}
105+
106+
// Remove all found stale keys
107+
const removePromises = keysToRemove.map((key) => this.storageBackend.removeItem(key));
108+
return Promise.all(removePromises).then(() => {
109+
if (keysToRemove.length > 0) {
110+
log(`Cleaned up ${keysToRemove.length} stale authorization data entries`);
111+
}
112+
});
113+
}
114+
80115
/**
81116
* Attempts to introspect the contents of storage backend and completes the
82117
* request.
@@ -119,12 +154,8 @@ export class RedirectRequestHandler extends AuthorizationRequestHandler {
119154
} else {
120155
authorizationResponse = new AuthorizationResponse({ code: code, state: state });
121156
}
122-
// cleanup state
123-
return Promise.all([
124-
this.storageBackend.removeItem(AUTHORIZATION_REQUEST_HANDLE_KEY),
125-
this.storageBackend.removeItem(authorizationRequestKey(handle)),
126-
this.storageBackend.removeItem(authorizationServiceConfigurationKey(handle)),
127-
]).then(() => {
157+
// cleanup all authorization data including current and stale entries
158+
return this.cleanupStaleAuthorizationData().then(() => {
128159
log('Delivering authorization response');
129160
return {
130161
request: request,
@@ -134,7 +165,10 @@ export class RedirectRequestHandler extends AuthorizationRequestHandler {
134165
});
135166
} else {
136167
log('Mismatched request (state and request_uri) dont match.');
137-
return Promise.resolve(null);
168+
// cleanup all authorization data even on mismatch to prevent stale PKCE data
169+
return this.cleanupStaleAuthorizationData().then(() => {
170+
return null;
171+
});
138172
}
139173
})
140174
);

src/Umbraco.Web.UI.Client/src/packages/core/auth/auth-flow.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,10 @@ export class UmbAuthFlow {
247247

248248
// clear the internal state
249249
this.#tokenResponse.setValue(undefined);
250+
251+
// Also cleanup any OAuth/PKCE artifacts that may still be in localStorage
252+
// This is a defense-in-depth measure during logout
253+
await this.#authorizationHandler.cleanupStaleAuthorizationData();
250254
}
251255

252256
/**

0 commit comments

Comments
 (0)