-
Notifications
You must be signed in to change notification settings - Fork 97
Guard getErrorParts against non-JSON-safe thrown values #826
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
+64
−2
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import {getErrorParts} from '../../../lib/storage/errors'; | ||
|
|
||
| describe('getErrorParts', () => { | ||
| it('should extract name and message from Error instances', () => { | ||
| expect(getErrorParts(new TypeError('Could not be cloned'))).toEqual({ | ||
| name: 'typeerror', | ||
| message: 'could not be cloned', | ||
| }); | ||
| }); | ||
|
|
||
| it('should lowercase string throws', () => { | ||
| expect(getErrorParts('Quota exceeded')).toEqual({name: '', message: 'quota exceeded'}); | ||
| }); | ||
|
|
||
| it('should treat null and undefined as empty', () => { | ||
| expect(getErrorParts(null)).toEqual({name: '', message: ''}); | ||
| expect(getErrorParts(undefined)).toEqual({name: '', message: ''}); | ||
| }); | ||
|
|
||
| it('should serialize plain objects so classifiers can match fields', () => { | ||
| expect(getErrorParts({message: 'QuotaExceededError'})).toEqual({ | ||
| name: '', | ||
| message: '{"message":"quotaexceedederror"}', | ||
| }); | ||
| }); | ||
|
|
||
| it('should not throw on circular objects', () => { | ||
| const circular: {self?: unknown} = {}; | ||
| circular.self = circular; | ||
|
|
||
| expect(() => getErrorParts(circular)).not.toThrow(); | ||
| expect(getErrorParts(circular)).toEqual({name: '', message: '[object object]'}); | ||
| }); | ||
|
|
||
| it('should not throw on BigInt', () => { | ||
| const value = BigInt(1); | ||
| expect(() => getErrorParts(value)).not.toThrow(); | ||
| expect(getErrorParts(value)).toEqual({name: '', message: '1'}); | ||
| }); | ||
| }); |
Oops, something went wrong.
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.
When an
ErrororDOMExceptionoriginates in another realm, such as an iframe, bothinstanceofchecks are false and itsnameandmessageproperties are non-enumerable, so this returns{}. The previousString(error)path retained text such asError: QuotaExceededError; losing it now causesclassifyIDBErrorto returnUNKNOWNinstead ofCAPACITY(and similarly hides other recognizable messages). Read error-likename/messagefields structurally or fall back toStringwhen JSON serialization erases them.Useful? React with 👍 / 👎.
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.
Cross-realm
instanceofis a real JS thing, but it does not apply here.This classifier only sees throws from our own IndexedDB / SQLite providers in the same window. Those are same-realm
Error/DOMException, so the existinginstanceofchecks still match. We do not load Onyx inside an iframe that would produce a foreign-realm error.Even in the hypothetical miss, this is not a regression vs main: main already used
String(error)only afterinstanceoffailed.JSON.stringifyof anErroris{}, so we would losename/messageeither way.classifyIDBErrormatchingCAPACITYfromQuotaExceededErrorstill goes through theinstanceofpath.Not changing this.