-
Notifications
You must be signed in to change notification settings - Fork 363
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix leaking windows message event listener #422
Merged
stevehobbsdev
merged 6 commits into
auth0:master
from
yinzara:fix/popup-message-listener-cleanup
Oct 29, 2020
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
386bce1
Fix leaking event listener on loginWithPopup
yinzara e71b55f
iframe symbol can just be a const
1f05af2
Fix tests
d630dde
Move event listener removal and add test assertions
da6594b
Reinstate removeEventListener inside iframeEventHandler
bd6158a
Merge branch 'master' into fix/popup-message-listener-cleanup
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,37 +43,47 @@ export const runIframe = ( | |
) => { | ||
return new Promise<AuthenticationResult>((res, rej) => { | ||
const iframe = window.document.createElement('iframe'); | ||
|
||
iframe.setAttribute('width', '0'); | ||
iframe.setAttribute('height', '0'); | ||
iframe.style.display = 'none'; | ||
|
||
const removeIframe = () => { | ||
if (window.document.body.contains(iframe)) { | ||
window.document.body.removeChild(iframe); | ||
window.removeEventListener('message', iframeEventHandler, false); | ||
} | ||
}; | ||
|
||
let iframeEventHandler: (e: MessageEvent) => void; | ||
|
||
const timeoutSetTimeoutId = setTimeout(() => { | ||
rej(new TimeoutError()); | ||
removeIframe(); | ||
}, timeoutInSeconds * 1000); | ||
|
||
const iframeEventHandler = function (e: MessageEvent) { | ||
iframeEventHandler = function (e: MessageEvent) { | ||
if (e.origin != eventOrigin) return; | ||
if (!e.data || e.data.type !== 'authorization_response') return; | ||
|
||
const eventSource = e.source; | ||
|
||
if (eventSource) { | ||
(eventSource as any).close(); | ||
} | ||
|
||
e.data.response.error | ||
? rej(GenericError.fromPayload(e.data.response)) | ||
: res(e.data.response); | ||
|
||
clearTimeout(timeoutSetTimeoutId); | ||
window.removeEventListener('message', iframeEventHandler, false); | ||
|
||
// Delay the removal of the iframe to prevent hanging loading status | ||
// in Chrome: https://github.com/auth0/auth0-spa-js/issues/240 | ||
setTimeout(removeIframe, CLEANUP_IFRAME_TIMEOUT_IN_SECONDS * 1000); | ||
}; | ||
|
||
window.addEventListener('message', iframeEventHandler, false); | ||
window.document.body.appendChild(iframe); | ||
iframe.setAttribute('src', authorizeUrl); | ||
|
@@ -107,20 +117,30 @@ export const runPopup = (authorizeUrl: string, config: PopupConfigOptions) => { | |
} | ||
|
||
return new Promise<AuthenticationResult>((resolve, reject) => { | ||
let popupEventListener; | ||
|
||
const timeoutId = setTimeout(() => { | ||
reject(new PopupTimeoutError(popup)); | ||
window.removeEventListener('message', popupEventListener, false); | ||
}, (config.timeoutInSeconds || DEFAULT_AUTHORIZE_TIMEOUT_IN_SECONDS) * 1000); | ||
window.addEventListener('message', e => { | ||
|
||
popupEventListener = function (e: MessageEvent) { | ||
if (!e.data || e.data.type !== 'authorization_response') { | ||
return; | ||
} | ||
|
||
clearTimeout(timeoutId); | ||
window.removeEventListener('message', popupEventListener, false); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This one is fine because you're removing the event listener in the successful and unsuccessful (timed out) cases |
||
popup.close(); | ||
|
||
if (e.data.response.error) { | ||
return reject(GenericError.fromPayload(e.data.response)); | ||
} | ||
|
||
resolve(e.data.response); | ||
}); | ||
}; | ||
|
||
window.addEventListener('message', e => popupEventListener(e)); | ||
}); | ||
}; | ||
|
||
|
@@ -367,9 +387,7 @@ export const validateCrypto = () => { | |
} | ||
if (typeof getCryptoSubtle() === 'undefined') { | ||
throw new Error(` | ||
auth0-spa-js must run on a secure origin. | ||
See https://github.com/auth0/auth0-spa-js/blob/master/FAQ.md#why-do-i-get-auth0-spa-js-must-run-on-a-secure-origin | ||
for more information. | ||
auth0-spa-js must run on a secure origin. See https://github.com/auth0/auth0-spa-js/blob/master/FAQ.md#why-do-i-get-auth0-spa-js-must-run-on-a-secure-origin for more information. | ||
`); | ||
} | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the danger of removing this one would be that, if you did 2 successful
getTokenSilently({ ignoreCache: true })
withinCLEANUP_IFRAME_TIMEOUT_IN_SECONDS
seconds of each other, the firstiframeEventHandler
would still be around and handle the secondgetTokenSilently
'spostMessage
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, good catch - I'll reinstate it.