44#import " SentryEnvelopeAttachmentHeader.h"
55#import " SentryEnvelopeItemType.h"
66#import " SentryError.h"
7+ #import " SentryInternalDefines.h"
78#import " SentryLevelMapper.h"
89#import " SentryLogC.h"
910#import " SentryModels+Serializable.h"
@@ -61,8 +62,9 @@ + (NSData *_Nullable)dataWithEnvelope:(SentryEnvelope *)envelope
6162 }
6263 [envelopeData appendData: header];
6364
65+ NSData *_Nonnull const newLineData = [NSData dataWithBytes: " \n " length: 1 ];
6466 for (int i = 0 ; i < envelope.items .count ; ++i) {
65- [envelopeData appendData: [ @" \n " dataUsingEncoding: NSUTF8StringEncoding] ];
67+ [envelopeData appendData: newLineData ];
6668 NSDictionary *serializedItemHeaderData = [envelope.items[i].header serialize ];
6769
6870 NSData *itemHeader = [SentrySerialization dataWithJSONObject: serializedItemHeaderData];
@@ -71,7 +73,7 @@ + (NSData *_Nullable)dataWithEnvelope:(SentryEnvelope *)envelope
7173 return nil ;
7274 }
7375 [envelopeData appendData: itemHeader];
74- [envelopeData appendData: [ @" \n " dataUsingEncoding: NSUTF8StringEncoding] ];
76+ [envelopeData appendData: newLineData ];
7577 [envelopeData appendData: envelope.items[i].data];
7678 }
7779
@@ -110,21 +112,27 @@ + (SentryEnvelope *_Nullable)envelopeWithData:(NSData *)data
110112 }
111113
112114 SentrySdkInfo *sdkInfo = nil ;
113- if (nil != headerDictionary[@" sdk" ]) {
114- sdkInfo = [[SentrySdkInfo alloc ] initWithDict: headerDictionary[@" sdk" ]];
115+ if (nil != headerDictionary[@" sdk" ] &&
116+ [headerDictionary[@" sdk" ] isKindOfClass: [NSDictionary class ]]) {
117+ sdkInfo = [[SentrySdkInfo alloc ]
118+ initWithDict: SENTRY_UNWRAP_NULLABLE (NSDictionary , headerDictionary[@" sdk" ])];
115119 }
116120
117121 SentryTraceContext *traceContext = nil ;
118- if (nil != headerDictionary[@" trace" ]) {
119- traceContext = [[SentryTraceContext alloc ] initWithDict: headerDictionary[@" trace" ]];
122+ if (nil != headerDictionary[@" trace" ] &&
123+ [headerDictionary[@" trace" ] isKindOfClass: [NSDictionary class ]]) {
124+ traceContext = [[SentryTraceContext alloc ]
125+ initWithDict: SENTRY_UNWRAP_NULLABLE (NSDictionary , headerDictionary[@" trace" ])];
120126 }
121127
122128 envelopeHeader = [[SentryEnvelopeHeader alloc ] initWithId: eventId
123129 sdkInfo: sdkInfo
124130 traceContext: traceContext];
125131
126- if (headerDictionary[@" sent_at" ] != nil ) {
127- envelopeHeader.sentAt = sentry_fromIso8601String (headerDictionary[@" sent_at" ]);
132+ if (headerDictionary[@" sent_at" ] != nil &&
133+ [headerDictionary[@" sent_at" ] isKindOfClass: [NSString class ]]) {
134+ envelopeHeader.sentAt = sentry_fromIso8601String (
135+ SENTRY_UNWRAP_NULLABLE (NSString , headerDictionary[@" sent_at" ]));
128136 }
129137
130138 break ;
@@ -165,11 +173,13 @@ + (SentryEnvelope *_Nullable)envelopeWithData:(NSData *)data
165173 SENTRY_LOG_ERROR (@" Failed to parse envelope item header %@ " , error);
166174 return nil ;
167175 }
168- NSString *_Nullable type = [headerDictionary valueForKey: @" type" ];
169- if (nil == type ) {
176+ NSString *_Nullable nullableType = [headerDictionary valueForKey: @" type" ];
177+ if (nil == nullableType ) {
170178 SENTRY_LOG_ERROR (@" Envelope item type is required." );
171179 break ;
172180 }
181+ NSString *_Nonnull type = SENTRY_UNWRAP_NULLABLE (NSString , nullableType);
182+
173183 NSNumber *bodyLengthNumber = [headerDictionary valueForKey: @" length" ];
174184 NSUInteger bodyLength = [bodyLengthNumber unsignedIntegerValue ];
175185 if (endOfEnvelope == i && bodyLength != 0 ) {
@@ -259,13 +269,28 @@ + (SentrySession *_Nullable)sessionWithData:(NSData *)sessionData
259269 return session;
260270}
261271
262- + (NSData *)dataWithReplayRecording : (SentryReplayRecording *)replayRecording
272+ + (NSData *_Nullable )dataWithReplayRecording : (SentryReplayRecording *)replayRecording
263273{
264274 NSMutableData *recording = [NSMutableData data ];
265- [recording appendData: [SentrySerialization
266- dataWithJSONObject: [replayRecording headerForReplayRecording ]]];
267- [recording appendData: [@" \n " dataUsingEncoding: NSUTF8StringEncoding]];
268- [recording appendData: [SentrySerialization dataWithJSONObject: [replayRecording serialize ]]];
275+
276+ NSData *_Nullable headerData =
277+ [SentrySerialization dataWithJSONObject: [replayRecording headerForReplayRecording ]];
278+ if (headerData == nil ) {
279+ SENTRY_LOG_ERROR (@" Failed to serialize replay recording header." );
280+ return nil ;
281+ }
282+ [recording appendData: SENTRY_UNWRAP_NULLABLE (NSData , headerData)];
283+
284+ NSData *_Nonnull const newLineData = [NSData dataWithBytes: " \n " length: 1 ];
285+ [recording appendData: newLineData];
286+
287+ NSData *_Nullable replayData =
288+ [SentrySerialization dataWithJSONObject: [replayRecording serialize ]];
289+ if (replayData == nil ) {
290+ SENTRY_LOG_ERROR (@" Failed to serialize replay recording data." );
291+ return nil ;
292+ }
293+ [recording appendData: SENTRY_UNWRAP_NULLABLE (NSData , replayData)];
269294 return recording;
270295}
271296
0 commit comments