-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Fix intense CPU usage when sessionToken is invalid in liveQuery #5126
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -420,11 +420,21 @@ class ParseLiveQueryServer { | |
| .then(auth => { | ||
| return { auth, userId: auth && auth.user && auth.user.id }; | ||
| }) | ||
| .catch(() => { | ||
| // If you can't continue, let's just wrap it up and delete it. | ||
| // Next time, one will try again | ||
| this.authCache.del(sessionToken); | ||
| return {}; | ||
| .catch(error => { | ||
| // There was an error with the session token | ||
| const result = {}; | ||
| if (error && error.code === Parse.Error.INVALID_SESSION_TOKEN) { | ||
| // Store a resolved promise with the error for 10 minutes | ||
| result.error = error; | ||
| this.authCache.set( | ||
| sessionToken, | ||
| Promise.resolve(result), | ||
| 60 * 10 * 1000 | ||
| ); | ||
| } else { | ||
| this.authCache.del(sessionToken); | ||
| } | ||
| return result; | ||
| }); | ||
| this.authCache.set(sessionToken, authPromise); | ||
| return authPromise; | ||
|
|
@@ -482,25 +492,19 @@ class ParseLiveQueryServer { | |
| : 'find'; | ||
| } | ||
|
|
||
| async _matchesACL( | ||
| acl: any, | ||
| client: any, | ||
| requestId: number | ||
| ): Promise<boolean> { | ||
| // Return true directly if ACL isn't present, ACL is public read, or client has master key | ||
| if (!acl || acl.getPublicReadAccess() || client.hasMasterKey) { | ||
| return true; | ||
| } | ||
| // Check subscription sessionToken matches ACL first | ||
| const subscriptionInfo = client.getSubscriptionInfo(requestId); | ||
| if (typeof subscriptionInfo === 'undefined') { | ||
| async _verifyACL(acl: any, token: string) { | ||
| if (!token) { | ||
| return false; | ||
| } | ||
|
|
||
| // TODO: get auth there and de-duplicate code below to work with the same Auth obj. | ||
| const { auth, userId } = await this.getAuthForSessionToken( | ||
| subscriptionInfo.sessionToken | ||
| ); | ||
| const { auth, userId } = await this.getAuthForSessionToken(token); | ||
|
|
||
| // Getting the session token failed | ||
| // This means that no additional auth is available | ||
| // At this point, just bail out as no additional visibility can be inferred. | ||
| if (!auth || !userId) { | ||
| return false; | ||
| } | ||
| const isSubscriptionSessionTokenMatched = acl.getReadAccess(userId); | ||
| if (isSubscriptionSessionTokenMatched) { | ||
| return true; | ||
|
|
@@ -527,27 +531,40 @@ class ParseLiveQueryServer { | |
| } | ||
| return false; | ||
| }) | ||
| .then(async isRoleMatched => { | ||
| if (isRoleMatched) { | ||
| return Promise.resolve(true); | ||
| } | ||
|
|
||
| // Check client sessionToken matches ACL | ||
| const clientSessionToken = client.sessionToken; | ||
| if (clientSessionToken) { | ||
| const { userId } = await this.getAuthForSessionToken( | ||
|
Contributor
Author
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 was initally overlooked, the new implementation takes properly care of both tokens |
||
| clientSessionToken | ||
| ); | ||
| return acl.getReadAccess(userId); | ||
| } else { | ||
| return isRoleMatched; | ||
| } | ||
| }) | ||
| .catch(() => { | ||
| return false; | ||
| }); | ||
| } | ||
|
|
||
| async _matchesACL( | ||
| acl: any, | ||
| client: any, | ||
| requestId: number | ||
| ): Promise<boolean> { | ||
| // Return true directly if ACL isn't present, ACL is public read, or client has master key | ||
| if (!acl || acl.getPublicReadAccess() || client.hasMasterKey) { | ||
| return true; | ||
| } | ||
| // Check subscription sessionToken matches ACL first | ||
| const subscriptionInfo = client.getSubscriptionInfo(requestId); | ||
| if (typeof subscriptionInfo === 'undefined') { | ||
| return false; | ||
| } | ||
|
|
||
| const subscriptionToken = subscriptionInfo.sessionToken; | ||
| const clientSessionToken = client.sessionToken; | ||
|
|
||
| if (await this._verifyACL(acl, subscriptionToken)) { | ||
| return true; | ||
| } | ||
|
|
||
| if (await this._verifyACL(acl, clientSessionToken)) { | ||
| return true; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| _handleConnect(parseWebsocket: any, request: any): any { | ||
| if (!this._validateKeys(request, this.keyPairs)) { | ||
| Client.pushError(parseWebsocket, 4, 'Key in request is not valid'); | ||
|
|
||
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.
fail fast