Skip to content

Commit 2b802f3

Browse files
vzaidmanfacebook-github-bot
authored andcommitted
Add headers validation check to prevent crash (facebook#55749)
Summary: Add defensive checks when processing custom headers to ensure: 1. Header keys are valid NSString instances before using them 2. Header values are successfully converted before adding to the request This prevents potential crashes when invalid header data (non-string keys or values that fail conversion) is passed from JavaScript to the WebSocket module. Changelog: [Internal] Differential Revision: D94375533
1 parent c85e11d commit 2b802f3

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

packages/react-native/React/CoreModules/RCTWebSocketModule.mm

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,23 @@ - (void)invalidate
9494
// Load supplied headers
9595
if ([options.headers() isKindOfClass:NSDictionary.class]) {
9696
NSDictionary *headers = (NSDictionary *)options.headers();
97-
[headers enumerateKeysAndObjectsUsingBlock:^(NSString *key, id value, BOOL *stop) {
98-
[request addValue:[RCTConvert NSString:value] forHTTPHeaderField:key];
97+
[headers enumerateKeysAndObjectsUsingBlock:^(id key, id value, BOOL *stop) {
98+
NSString *headerKey = [RCTConvert NSString:key];
99+
NSString *headerValue = [RCTConvert NSString:value];
100+
101+
if (headerKey == nil || headerValue == nil) {
102+
RCTLogError(
103+
@"RCTWebSocketModule: Invalid header key and/or value types. "
104+
"Expected NSString for both, got key of type %@ and value of type %@.",
105+
NSStringFromClass([key class]),
106+
NSStringFromClass([value class]));
107+
}
108+
109+
if (headerKey == nil) {
110+
return;
111+
}
112+
113+
[request addValue:headerValue == nil ? @"" : headerValue forHTTPHeaderField:headerKey];
99114
}];
100115
}
101116

0 commit comments

Comments
 (0)