-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1843 from DataDog/ncreated/RUM-3183/fix-crash-on-…
…accessing-allHTTPHeaderFields RUM-3183 fix: Fix crash on accessing `request.allHTTPHeaderFields`
- Loading branch information
Showing
9 changed files
with
102 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
DatadogInternal/Sources/NetworkInstrumentation/URLSession/ImmutableRequest.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
* This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
* Copyright 2019-Present Datadog, Inc. | ||
*/ | ||
|
||
import Foundation | ||
|
||
/// An immutable version of `URLRequest`. | ||
/// | ||
/// Introduced in response to concerns raised in https://github.com/DataDog/dd-sdk-ios/issues/1638 | ||
/// it makes a copy of request attributes, safeguarding against potential thread safety issues arising from concurrent | ||
/// mutations (see more context in https://github.com/DataDog/dd-sdk-ios/pull/1767 ). | ||
public struct ImmutableRequest { | ||
/// The URL of the request. | ||
public let url: URL? | ||
/// The HTTP method of the request. | ||
public let httpMethod: String? | ||
/// The value of `x-datadog-origin` header (if any). | ||
public let ddOriginHeaderValue: String? | ||
/// A reference to the original `URLRequest` object provided during initialization. Direct use is discouraged | ||
/// due to thread safety concerns. Instead, necessary attributes should be accessed through `ImmutableRequest` fields. | ||
public let unsafeOriginal: URLRequest | ||
|
||
public init(request: URLRequest) { | ||
self.url = request.url | ||
self.httpMethod = request.httpMethod | ||
// RUM-3183: As observed in https://github.com/DataDog/dd-sdk-ios/issues/1638, accessing `request.allHTTPHeaderFields` is not | ||
// safe and can lead to crashes with undefined root cause. To avoid issues we should prefer `request.value(forHTTPHeaderField:)` | ||
// when interacting with `URLRequest`. | ||
self.ddOriginHeaderValue = request.value(forHTTPHeaderField: TracingHTTPHeaders.originField) | ||
self.unsafeOriginal = request | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
DatadogInternal/Tests/NetworkInstrumentation/ImmutableRequestTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
* This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
* Copyright 2019-Present Datadog, Inc. | ||
*/ | ||
|
||
import XCTest | ||
import TestUtilities | ||
import DatadogInternal | ||
|
||
class ImmutableRequestTests: XCTestCase { | ||
func testReadingURL() { | ||
let original: URLRequest = .mockWith(url: "https://example.com") | ||
let immutable = ImmutableRequest(request: original) | ||
XCTAssertEqual(immutable.url, original.url) | ||
} | ||
|
||
func testReadingHTTPMethod() { | ||
let original: URLRequest = .mockWith(httpMethod: .mockRandom()) | ||
let immutable = ImmutableRequest(request: original) | ||
XCTAssertEqual(immutable.httpMethod, original.httpMethod) | ||
} | ||
|
||
func testReadingDatadogOriginHeader() { | ||
let expectedValue: String = .mockRandom(length: 128) | ||
let original: URLRequest = .mockWith( | ||
headers: [ | ||
TracingHTTPHeaders.originField: expectedValue | ||
] | ||
) | ||
let immutable = ImmutableRequest(request: original) | ||
XCTAssertEqual(immutable.ddOriginHeaderValue, expectedValue) | ||
} | ||
|
||
func testPreservingUnsafeOriginal() { | ||
let original: URLRequest = .mockAny() | ||
let immutable = ImmutableRequest(request: original) | ||
XCTAssertEqual(immutable.unsafeOriginal, original) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters