[React Native] Patch RCTFileReaderModule
readAsDataURL
to prevent crash when type
is nil
#5475
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.
Fixes #5100
Why
The null check for
type
doesn't actually do anything here, becausetype
will never benil
, but rather it will be eitherNSString
orNSNull
. We need to actually convertNSNull
tonil
first before the check.Breakdown
React Native handles the response for a request in this block of code: https://github.com/facebook/react-native/blob/303e0ed7641409acf2d852c077f6be426afd7a0c/packages/react-native/Libraries/Blob/RCTBlobManager.mm#L314-L326
Here, we see that values of
nil
- which[response MIMEType]
will return when nocontent-type
is provided in the response and the actual type cannot be determined (https://developer.apple.com/documentation/foundation/nsurlresponse/1411613-mimetype) - gets converted toNSNull
byRCTNullIfNil
. This is fine, since we cannot havenil
in a dictionary so we have to do the conversion.When we get back over to
readAsDataURL
, we see that we grab the type from the dictionary and check if itsnil
before callinglength
on the string. https://github.com/facebook/react-native/blob/303e0ed7641409acf2d852c077f6be426afd7a0c/packages/react-native/Libraries/Blob/RCTFileReaderModule.mm#L74-L77However, this check is dubious, because the value will never actually be
nil
. It will always either beNSString
orNSNull
because of theRCTNullIfNil
call made above. The[RCTConvert NSString]
made when getting the type from the dictionary will simply returnNSNull
here, becauseNSNull
is an objc class, which will merely be returned as such by the call (https://github.com/facebook/react-native/blob/303e0ed7641409acf2d852c077f6be426afd7a0c/packages/react-native/React/Base/RCTConvert.mm#L38-L60)The trick here is to first use
RCTNilIfNull
to convert tonil
if needed, then continue making the!= nil
check.Test Plan
Nothing should break. The only way I can repro the crash right now is to add
nosniff
to the response headers inx-content-type-options
for something likeregisterPush
orupdateSeen
. Verified that this does fix the problem though.