Skip to content

Commit 25dd8f0

Browse files
committed
Guard length of traceparent header before splitting
1 parent 767a260 commit 25dd8f0

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

Sources/W3CTraceContext/TraceContext.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift W3C Trace Context open source project
44
//
5-
// Copyright (c) YEARS Moritz Lang and the Swift W3C Trace Context project authors
5+
// Copyright (c) 2020 Moritz Lang and the Swift W3C Trace Context project authors
66
// Licensed under Apache License v2.0
77
//
88
// See LICENSE.txt for license information

Sources/W3CTraceContext/TraceParent.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift W3C Trace Context open source project
44
//
5-
// Copyright (c) YEARS Moritz Lang and the Swift W3C Trace Context project authors
5+
// Copyright (c) 2020 Moritz Lang and the Swift W3C Trace Context project authors
66
// Licensed under Apache License v2.0
77
//
88
// See LICENSE.txt for license information
@@ -36,6 +36,8 @@ extension W3C.TraceParent: Equatable {
3636

3737
extension W3C.TraceParent: RawRepresentable {
3838
public init?(rawValue: String) {
39+
guard rawValue.count == 55 else { return nil }
40+
3941
let components = rawValue.split(separator: "-")
4042
guard components.count == 4 else { return nil }
4143

Tests/W3CTraceContextTests/TraceParentRawRepresentableTests.swift

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift W3C Trace Context open source project
44
//
5-
// Copyright (c) YEARS Moritz Lang and the Swift W3C Trace Context project authors
5+
// Copyright (c) 2020 Moritz Lang and the Swift W3C Trace Context project authors
66
// Licensed under Apache License v2.0
77
//
88
// See LICENSE.txt for license information
@@ -57,13 +57,23 @@ final class TraceParentRawRepresentableTests: XCTestCase {
5757
XCTAssertEqual(traceParent.rawValue, "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01")
5858
}
5959

60+
func testDecodeFailsWithTooLongRawValue() {
61+
let rawValue = String(repeating: "42", count: 1000)
62+
XCTAssertUninitializedTraceParent(rawValue)
63+
}
64+
65+
func testDecodeFailsWithTooShortRawValue() {
66+
let rawValue = "too-short"
67+
XCTAssertUninitializedTraceParent(rawValue)
68+
}
69+
6070
func testDecodeFailsWithTooManyComponents() {
61-
let rawValue = "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01-additional-components"
71+
let rawValue = "00-0af7651916cd43dd8448eb211c803-b7ad6b7169203331-01-12"
6272
XCTAssertUninitializedTraceParent(rawValue)
6373
}
6474

6575
func testDecodeFailsWithTooFewComponents() {
66-
let rawValue = "00-0af7651916cd43dd8448eb211c80319c"
76+
let rawValue = "00-0af7651916cd43dd8448eb211c803-b7ad6b7169203331000000"
6777
XCTAssertUninitializedTraceParent(rawValue)
6878
}
6979

@@ -78,7 +88,7 @@ final class TraceParentRawRepresentableTests: XCTestCase {
7888
}
7989

8090
func testDecodeFailsWithTooShortTraceID() {
81-
let rawValue = "00-tooshort-b7ad6b7169203331-01"
91+
let rawValue = "00-tooshort-b7ad6b7169203331-01432436432435434234234234"
8292
XCTAssertUninitializedTraceParent(rawValue)
8393
}
8494

@@ -88,7 +98,7 @@ final class TraceParentRawRepresentableTests: XCTestCase {
8898
}
8999

90100
func testDecodeFailsWithTooShortParentID() {
91-
let rawValue = "00-0af7651916cd43dd8448eb211c80319c-tooshort-01"
101+
let rawValue = "00-0af7651916cd43dd8448eb211c80319c-tooshort-0131434343"
92102
XCTAssertUninitializedTraceParent(rawValue)
93103
}
94104

@@ -98,12 +108,12 @@ final class TraceParentRawRepresentableTests: XCTestCase {
98108
}
99109

100110
func testDecodeFailsWithTooLongTraceFlags() {
101-
let rawValue = "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-toolong"
111+
let rawValue = "00-0af7651916cd43dd8448eb211c80319c-b7ad6b71692-toolong"
102112
XCTAssertUninitializedTraceParent(rawValue)
103113
}
104114

105115
func testDecodeFailsWithTooShortTraceFlags() {
106-
let rawValue = "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-0"
116+
let rawValue = "00-0af7651916cd43dd8448eb211c80319c-b7ad6b71692033311-0"
107117
XCTAssertUninitializedTraceParent(rawValue)
108118
}
109119

0 commit comments

Comments
 (0)