Skip to content

Commit 55d14af

Browse files
fix test cases
1 parent 1d0d3c8 commit 55d14af

File tree

4 files changed

+158
-7
lines changed

4 files changed

+158
-7
lines changed

Auth0/SensitiveDataRedactor.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ struct SensitiveDataRedactor {
1414
/// Note: These logs are only for debugging purposes and never persisted in production.
1515
///
1616
/// - Parameter data: The response data to redact.
17-
/// - Returns: A JSON string with sensitive fields replaced by `<REDACTED>`, or `nil` if not valid JSON.
17+
/// - Returns: A JSON string with sensitive fields replaced by `<REDACTED>` if valid JSON, otherwise returns the data decoded as a UTF-8 string, or `nil` if decoding fails.
1818
static func redact(_ data: Data) -> String? {
1919
do {
2020
// Attempt to parse as JSON
@@ -32,8 +32,8 @@ struct SensitiveDataRedactor {
3232
return String(data: redactedData, encoding: .utf8) ?? "<REDACTED>"
3333

3434
} catch {
35-
// If not JSON, return nil
36-
return nil
35+
// If not JSON, return try converting data to string
36+
return String(data: data, encoding: .utf8)
3737
}
3838
}
3939
}

Auth0Tests/LoggerSpec.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,11 @@ class LoggerSpec: QuickSpec {
189189
}
190190

191191
it("should log response body") {
192-
let json = "{key: \"\(UUID().uuidString)\"}"
193-
logger.trace(response: response, data: json.data(using: .utf8))
194-
expect(output.messages).to(contain(json))
192+
let jsonDict: [String: String] = ["key": UUID().uuidString]
193+
let jsonData = try! JSONSerialization.data(withJSONObject: jsonDict, options: [.prettyPrinted])
194+
let expectedJson = String(data: jsonData, encoding: .utf8)!
195+
logger.trace(response: response, data: jsonData)
196+
expect(output.messages).to(contain("API Response: \(expectedJson)"))
195197
}
196198

197199
it("should log nothing for non http response") {
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
import Quick
2+
import Nimble
3+
4+
@testable import Auth0
5+
6+
class SensitiveDataRedactorSpec: QuickSpec {
7+
8+
override class func spec() {
9+
10+
describe("SensitiveDataRedactor") {
11+
12+
context("redact()") {
13+
14+
it("should redact access_token") {
15+
let json = """
16+
{
17+
"access_token": "secret_access_token_value",
18+
"token_type": "Bearer",
19+
"expires_in": 86400
20+
}
21+
"""
22+
let data = json.data(using: .utf8)!
23+
24+
let result = SensitiveDataRedactor.redact(data)
25+
26+
expect(result).toNot(beNil())
27+
expect(result).to(contain("<REDACTED>"))
28+
expect(result).toNot(contain("secret_access_token_value"))
29+
expect(result).to(contain("token_type"))
30+
expect(result).to(contain("expires_in"))
31+
}
32+
33+
it("should redact id_token") {
34+
let json = """
35+
{
36+
"id_token": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.payload.signature",
37+
"token_type": "Bearer"
38+
}
39+
"""
40+
let data = json.data(using: .utf8)!
41+
42+
let result = SensitiveDataRedactor.redact(data)
43+
44+
expect(result).toNot(beNil())
45+
expect(result).to(contain("<REDACTED>"))
46+
expect(result).toNot(contain("eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9"))
47+
}
48+
49+
it("should redact refresh_token") {
50+
let json = """
51+
{
52+
"access_token": "access_value",
53+
"refresh_token": "refresh_value"
54+
}
55+
"""
56+
let data = json.data(using: .utf8)!
57+
58+
let result = SensitiveDataRedactor.redact(data)
59+
60+
expect(result).toNot(beNil())
61+
expect(result).to(contain("<REDACTED>"))
62+
expect(result).toNot(contain("access_value"))
63+
expect(result).toNot(contain("refresh_value"))
64+
}
65+
66+
it("should redact multiple sensitive fields") {
67+
let json = """
68+
{
69+
"access_token": "at_123",
70+
"id_token": "it_456",
71+
"refresh_token": "rt_789",
72+
"scope": "openid profile email"
73+
}
74+
"""
75+
let data = json.data(using: .utf8)!
76+
77+
let result = SensitiveDataRedactor.redact(data)
78+
79+
expect(result).toNot(beNil())
80+
expect(result).to(contain("<REDACTED>"))
81+
expect(result).toNot(contain("at_123"))
82+
expect(result).toNot(contain("it_456"))
83+
expect(result).toNot(contain("rt_789"))
84+
expect(result).to(contain("scope"))
85+
expect(result).to(contain("openid profile email"))
86+
}
87+
88+
it("should preserve non-sensitive fields") {
89+
let json = """
90+
{
91+
"user_id": "auth0|123456",
92+
"email": "[email protected]",
93+
"access_token": "secret"
94+
}
95+
"""
96+
let data = json.data(using: .utf8)!
97+
98+
let result = SensitiveDataRedactor.redact(data)
99+
100+
expect(result).toNot(beNil())
101+
expect(result).to(contain("user_id"))
102+
expect(result).to(contain("auth0|123456"))
103+
expect(result).to(contain("email"))
104+
expect(result).to(contain("[email protected]"))
105+
expect(result).toNot(contain("secret"))
106+
}
107+
108+
it("should return non-JSON String for non-JSON data") {
109+
let plainText = "This is not JSON"
110+
let data = plainText.data(using: .utf8)!
111+
112+
let result = SensitiveDataRedactor.redact(data)
113+
114+
expect(result).to(contain(plainText))
115+
}
116+
117+
it("should handle empty JSON") {
118+
let json = "{}"
119+
let data = json.data(using: .utf8)!
120+
121+
let result = SensitiveDataRedactor.redact(data)
122+
123+
expect(result).toNot(beNil())
124+
let expectedJson = try! JSONSerialization.data(withJSONObject: [:], options: [.prettyPrinted])
125+
let expected = String(data: expectedJson, encoding: .utf8)!
126+
expect(result).to(equal(expected))
127+
}
128+
129+
it("should handle JSON with no sensitive fields") {
130+
let json = """
131+
{
132+
"username": "john",
133+
"age": 30
134+
}
135+
"""
136+
let data = json.data(using: .utf8)!
137+
138+
let result = SensitiveDataRedactor.redact(data)
139+
140+
expect(result).toNot(beNil())
141+
expect(result).to(contain("username"))
142+
expect(result).to(contain("john"))
143+
expect(result).to(contain("age"))
144+
expect(result).toNot(contain("<REDACTED>"))
145+
}
146+
}
147+
}
148+
}
149+
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,4 +441,4 @@ Please do not report security vulnerabilities on the public GitHub issue tracker
441441

442442
<p align="center">Auth0 is an easy-to-implement, adaptable authentication and authorization platform. To learn more check out <a href="https://auth0.com/why-auth0">Why Auth0?</a></p>
443443

444-
<p align="center">This project is licensed under the MIT license. See the <a href="./LICENSE"> LICENSE</a> file for more info.</p>
444+
<p align="center">This project is licensed under the MIT license. See the <a href="./LICENSE"> LICENSE</a> file for more info.</p>

0 commit comments

Comments
 (0)