Skip to content

Commit 01fe9e6

Browse files
netbegithub-actions[bot]
authored andcommitted
fix: log query parameter - WPB-22183 (#4074)
1 parent 99c8648 commit 01fe9e6

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

WireLogging/Sources/WireLogging/Network/RequestLog.swift

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,38 @@ struct RequestLog: Codable {
7979
}
8080

8181
public extension URL {
82+
8283
var endpointRemoteLogDescription: String {
83-
absoluteString
84+
var components = URLComponents(string: absoluteString)
85+
let searchContactPath = "/search/contacts"
86+
87+
let path = components?.path ?? ""
88+
guard path.contains(searchContactPath) else {
89+
return absoluteString
90+
}
91+
92+
// redact query param
93+
var queryComponents = components?.queryItems ?? []
94+
queryComponents.enumerated().forEach { item in
95+
var redactedItem = item.element
96+
if redactedItem.name == "q" {
97+
redactedItem.value = "***"
98+
}
99+
queryComponents[item.offset] = redactedItem
100+
}
101+
102+
components?.queryItems = queryComponents
103+
104+
var endpoint = [
105+
"\(components?.scheme ?? ""):/",
106+
components?.host,
107+
path.trimmingCharacters(in: CharacterSet(charactersIn: "/"))
108+
]
109+
.compactMap(\.self)
110+
.filter { !$0.isEmpty }
111+
.joined(separator: "/")
112+
endpoint.append(components?.query?.isEmpty == false ? "?\(components!.query!)" : "")
113+
return endpoint
84114
}
85115
}
86116

WireLogging/Tests/WireLogging/Network/RequestLogTests.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,24 @@ class RequestLogTests: XCTestCase {
5050
)
5151
}
5252

53+
func testParsingEndpointWithSearchContactsRedactedQueryParams() throws {
54+
let request =
55+
NSURLRequest(
56+
url: URL(
57+
string: "https://prod-nginz-https.wire.com/v13/search/contacts?q=test&size=10"
58+
)!
59+
)
60+
guard let sut: RequestLog = .init(request) else {
61+
XCTFail("could not create RequestLog")
62+
return
63+
}
64+
65+
XCTAssertEqual(
66+
sut.endpoint,
67+
"https://prod-nginz-https.wire.com/v13/search/contacts?q=***&size=10"
68+
)
69+
}
70+
5371
func testAuthorizationHeaderValueIsRedacted() throws {
5472
let request = NSMutableURLRequest(url: URL(string: "https://prod-nginz-https.wire.com/push/tokens")!)
5573
request.addValue("Bearer wertrtetetr42343242432456789p", forHTTPHeaderField: "Authorization")

0 commit comments

Comments
 (0)