Skip to content
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

fix: Crash when reading corrupted envelope #4297

Merged
merged 14 commits into from
Aug 26, 2024
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog


## Unreleased

### Fixes

- Crash when reading corrupted envelope (#4297)

## 8.35.0

### Features
Expand Down
8 changes: 8 additions & 0 deletions Sources/Sentry/SentrySerialization.m
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,14 @@ + (SentryEnvelope *_Nullable)envelopeWithData:(NSData *)data
if (endOfEnvelope == i) {
i++; // 0 byte attachment
}

if (bodyLength > 0 && data.length < (i + 1 + bodyLength)) {
SENTRY_LOG_ERROR(@"Envelope is corrupted or has invalid data. Trying to read %li "
@"bytes by skiping %li from a buffer of %li bytes.",
(unsigned long)data.length, (unsigned long)bodyLength, (long)(i + 1));
return nil;
}

NSData *itemBody = [data subdataWithRange:NSMakeRange(i + 1, bodyLength)];
SentryEnvelopeItem *envelopeItem = [[SentryEnvelopeItem alloc] initWithHeader:itemHeader
data:itemBody];
Expand Down
11 changes: 11 additions & 0 deletions Tests/SentryTests/Helper/SentrySerializationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,17 @@ class SentrySerializationTests: XCTestCase {
XCTAssertNil(actual)
}

func testReturnNilForCorruptedEnvelope() throws {
let envelope = SentryEnvelope(event: Event(error: NSError(domain: "test", code: -1, userInfo: nil)))
let data = try XCTUnwrap(SentrySerialization.data(with: envelope))

let corruptedData = data[0..<data.count - 1]

let unserialized = SentrySerialization.envelope(with: corruptedData)
brustolin marked this conversation as resolved.
Show resolved Hide resolved

XCTAssertNil(unserialized)
}

private func serializeEnvelope(envelope: SentryEnvelope) -> Data {
var serializedEnvelope: Data = Data()
do {
Expand Down
Loading