Skip to content

Commit b18a037

Browse files
authored
fix: Crash when reading corrupted envelope (#4297)
Added a safe guard to prevent crashing when reading corrupted envelope
1 parent a0cc9d6 commit b18a037

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Changelog
22

3+
4+
## Unreleased
5+
6+
### Fixes
7+
8+
- Crash when reading corrupted envelope (#4297)
9+
310
## 8.35.0
411

512
### Features

Sources/Sentry/SentrySerialization.m

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,14 @@ + (SentryEnvelope *_Nullable)envelopeWithData:(NSData *)data
208208
if (endOfEnvelope == i) {
209209
i++; // 0 byte attachment
210210
}
211+
212+
if (bodyLength > 0 && data.length < (i + 1 + bodyLength)) {
213+
SENTRY_LOG_ERROR(@"Envelope is corrupted or has invalid data. Trying to read %li "
214+
@"bytes by skiping %li from a buffer of %li bytes.",
215+
(unsigned long)data.length, (unsigned long)bodyLength, (long)(i + 1));
216+
return nil;
217+
}
218+
211219
NSData *itemBody = [data subdataWithRange:NSMakeRange(i + 1, bodyLength)];
212220
SentryEnvelopeItem *envelopeItem = [[SentryEnvelopeItem alloc] initWithHeader:itemHeader
213221
data:itemBody];

Tests/SentryTests/Helper/SentrySerializationTests.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,17 @@ class SentrySerializationTests: XCTestCase {
267267
XCTAssertNil(actual)
268268
}
269269

270+
func testReturnNilForCorruptedEnvelope() throws {
271+
let envelope = SentryEnvelope(event: Event(error: NSError(domain: "test", code: -1, userInfo: nil)))
272+
let data = try XCTUnwrap(SentrySerialization.data(with: envelope))
273+
274+
let corruptedData = data[0..<data.count - 1]
275+
276+
let unserialized = SentrySerialization.envelope(with: corruptedData)
277+
278+
XCTAssertNil(unserialized)
279+
}
280+
270281
private func serializeEnvelope(envelope: SentryEnvelope) -> Data {
271282
var serializedEnvelope: Data = Data()
272283
do {

0 commit comments

Comments
 (0)