diff --git a/dari-ios/Sources/Dari/PayloadMasker.swift b/dari-ios/Sources/Dari/PayloadMasker.swift index b386e74..91243f1 100644 --- a/dari-ios/Sources/Dari/PayloadMasker.swift +++ b/dari-ios/Sources/Dari/PayloadMasker.swift @@ -9,13 +9,14 @@ package enum PayloadMasker { // Matched against keys lowercased with separators removed, e.g. "Access-Token" -> "accesstoken". private static let sensitiveKeyParts = [ "token", "password", "passwd", "secret", "authorization", "cookie", "apikey", "sessionid", "cardnumber", + "credential", "privatekey", "signature", ] - private static let sensitiveKeys: Set = ["cvv", "cvc", "ssn", "otp", "pin"] + private static let sensitiveKeys: Set = ["cvv", "cvc", "ssn", "otp", "pin", "auth"] private static let pairNameFields: Set = ["name", "key"] private static let pairValueField = "value" private static let keyValue = try! NSRegularExpression( - pattern: #"(?i)\b([\w.\-]*(?:token|password|passwd|secret|authorization|cookie|api[_\-]?key|session[_\-]?id|card[_\-]?number)[\w.\-]*)(["']?\s*[:=]\s*)("(?:[^"\\]|\\.)*"|'[^']*'|[^\s,;&}\]]+)"# + pattern: #"(?i)\b([\w.\-]*(?:token|password|passwd|secret|authorization|cookie|api[_\-]?key|session[_\-]?id|card[_\-]?number|credential|private[_\-]?key|signature)[\w.\-]*)(["']?\s*[:=]\s*)("(?:[^"\\]|\\.)*"|'[^']*'|[^\s,;&}\]]+)"# ) private static let authScheme = try! NSRegularExpression(pattern: #"(?i)\b(Bearer|Basic)\s+[A-Za-z0-9._~+/\-]+=*"#) diff --git a/dari-ios/Tests/DariTests/PayloadMaskerTests.swift b/dari-ios/Tests/DariTests/PayloadMaskerTests.swift index 3c92f17..facf508 100644 --- a/dari-ios/Tests/DariTests/PayloadMaskerTests.swift +++ b/dari-ios/Tests/DariTests/PayloadMaskerTests.swift @@ -18,6 +18,16 @@ final class PayloadMaskerTests: XCTestCase { XCTAssertEqual(PayloadMasker.maskText("plain"), "plain") } + func testMasksCredentialPrivateKeyAndSignatureKeys() throws { + let masked = try XCTUnwrap(PayloadMasker.maskText(#"{"credential":"c","private_key":"p","X-Signature":"s","auth":"a","author":"kim"}"#)) + let object = try XCTUnwrap(JSONSerialization.jsonObject(with: Data(masked.utf8)) as? [String: Any]) + for key in ["credential", "private_key", "X-Signature", "auth"] { + XCTAssertEqual(object[key] as? String, "***", key) + } + XCTAssertEqual(object["author"] as? String, "kim") + XCTAssertEqual(PayloadMasker.maskText("privateKey=p&id=1"), "privateKey=***&id=1") + } + func testMasksValueOfSensitiveNameValuePair() throws { let masked = try XCTUnwrap(PayloadMasker.maskText( #"[{"name":"Authorization","value":"xyz"},{"Key":"X-Api-Key","Value":{"v":"k"}},{"name":"Accept","value":"json"},{"name":7,"value":"keep"},{"NAME":"id","key":"Cookie","VALUE":"c"}]"# diff --git a/dari/src/main/kotlin/com/easyhooon/dari/masking/PayloadMasker.kt b/dari/src/main/kotlin/com/easyhooon/dari/masking/PayloadMasker.kt index 831ed14..d1a8638 100644 --- a/dari/src/main/kotlin/com/easyhooon/dari/masking/PayloadMasker.kt +++ b/dari/src/main/kotlin/com/easyhooon/dari/masking/PayloadMasker.kt @@ -19,14 +19,15 @@ internal object PayloadMasker { // Matched against keys lowercased with separators removed, e.g. "Access-Token" -> "accesstoken". private val SENSITIVE_KEY_PARTS = listOf( "token", "password", "passwd", "secret", "authorization", "cookie", "apikey", "sessionid", "cardnumber", + "credential", "privatekey", "signature", ) - private val SENSITIVE_KEYS = setOf("cvv", "cvc", "ssn", "otp", "pin") + private val SENSITIVE_KEYS = setOf("cvv", "cvc", "ssn", "otp", "pin", "auth") private val PAIR_NAME_FIELDS = setOf("name", "key") private const val PAIR_VALUE_FIELD = "value" // `key: value` / `key=value` in decoded protobuf text, query strings and other non-JSON text. private val KEY_VALUE = Regex( - """(?i)\b([\w.\-]*(?:token|password|passwd|secret|authorization|cookie|api[_\-]?key|session[_\-]?id|card[_\-]?number)[\w.\-]*)(["']?\s*[:=]\s*)("(?:[^"\\]|\\.)*"|'[^']*'|[^\s,;&}\]]+)""", + """(?i)\b([\w.\-]*(?:token|password|passwd|secret|authorization|cookie|api[_\-]?key|session[_\-]?id|card[_\-]?number|credential|private[_\-]?key|signature)[\w.\-]*)(["']?\s*[:=]\s*)("(?:[^"\\]|\\.)*"|'[^']*'|[^\s,;&}\]]+)""", ) private val AUTH_SCHEME = Regex("""(?i)\b(Bearer|Basic)\s+[A-Za-z0-9._~+/\-]+=*""") diff --git a/dari/src/test/kotlin/com/easyhooon/dari/masking/PayloadMaskerTest.kt b/dari/src/test/kotlin/com/easyhooon/dari/masking/PayloadMaskerTest.kt index cd15297..9f54a4d 100644 --- a/dari/src/test/kotlin/com/easyhooon/dari/masking/PayloadMaskerTest.kt +++ b/dari/src/test/kotlin/com/easyhooon/dari/masking/PayloadMaskerTest.kt @@ -59,6 +59,17 @@ class PayloadMaskerTest { assertTrue(PayloadMasker.isSensitiveKey("PIN")) assertFalse(PayloadMasker.isSensitiveKey("pinned")) assertFalse(PayloadMasker.isSensitiveKey("lessons")) + assertTrue(PayloadMasker.isSensitiveKey("Auth")) + assertFalse(PayloadMasker.isSensitiveKey("author")) + } + + @Test + fun `masks credential, private key and signature keys`() { + assertEquals( + """{"credential":"***","private_key":"***","X-Signature":"***","auth":"***","author":"kim"}""", + PayloadMasker.maskText("""{"credential":"c","private_key":"p","X-Signature":"s","auth":"a","author":"kim"}"""), + ) + assertEquals("privateKey=***&id=1", PayloadMasker.maskText("privateKey=p&id=1")) } @Test diff --git a/documentation/content/docs/ko/masking.mdx b/documentation/content/docs/ko/masking.mdx index 6cc4a63..4165580 100644 --- a/documentation/content/docs/ko/masking.mdx +++ b/documentation/content/docs/ko/masking.mdx @@ -20,8 +20,8 @@ description: Dari가 무엇을 어떤 기준으로 가리는지, 어디서 켜 `{`나 `[`로 시작하고 JSON으로 파싱되는 본문은 모든 깊이를 검사합니다. 다음 중 하나에 해당하면 민감한 키로 봅니다. -- 키를 소문자로 바꾸고 영문자와 숫자만 남겼을 때, `token`, `password`, `passwd`, `secret`, `authorization`, `cookie`, `apikey`, `sessionid`, `cardnumber` 중 하나를 **포함**합니다. -- 같은 방식으로 정리한 키가 `cvv`, `cvc`, `ssn`, `otp`, `pin` 중 하나와 **정확히 같습니다**. +- 키를 소문자로 바꾸고 영문자와 숫자만 남겼을 때, `token`, `password`, `passwd`, `secret`, `authorization`, `cookie`, `apikey`, `sessionid`, `cardnumber`, `credential`, `privatekey`, `signature` 중 하나를 **포함**합니다. +- 같은 방식으로 정리한 키가 `cvv`, `cvc`, `ssn`, `otp`, `pin`, `auth` 중 하나와 **정확히 같습니다**. 민감한 키의 값은 `"***"`로 바뀝니다. 값이 객체나 배열이어도 통째로 바뀝니다. 그 밖의 키에 들어 있는 문자열 값은 텍스트로 한 번 더 검사하므로, JSON 안에 문자열로 들어간 JSON도 가려집니다. @@ -46,7 +46,7 @@ description: Dari가 무엇을 어떤 기준으로 가리는지, 어디서 켜 ### 2. 텍스트 본문 -디코딩한 protobuf, 쿼리 문자열, 로그 형태 텍스트처럼 JSON이 아닌 본문은 `key: value`나 `key=value` 형태를 찾습니다. 키에 `token`, `password`, `passwd`, `secret`, `authorization`, `cookie`, `api key`, `session id`, `card number` 중 하나가 들어 있으면 가립니다. 마지막 세 개는 단어 사이에 `_`, `-`가 있거나 아무것도 없어도 됩니다. 대소문자는 구분하지 않습니다. 정확히 같아야 하는 단어(`cvv`, `pin` 등)는 텍스트에서는 찾지 **않습니다**. 짧은 단어라 일반 텍스트에도 너무 자주 나오기 때문입니다. +디코딩한 protobuf, 쿼리 문자열, 로그 형태 텍스트처럼 JSON이 아닌 본문은 `key: value`나 `key=value` 형태를 찾습니다. 키에 `token`, `password`, `passwd`, `secret`, `authorization`, `cookie`, `api key`, `session id`, `card number`, `credential`, `private key`, `signature` 중 하나가 들어 있으면 가립니다. `api key`, `session id`, `card number`, `private key`는 단어 사이에 `_`, `-`가 있거나 아무것도 없어도 됩니다. 대소문자는 구분하지 않습니다. 정확히 같아야 하는 단어(`cvv`, `pin` 등)는 텍스트에서는 찾지 **않습니다**. 짧은 단어라 일반 텍스트에도 너무 자주 나오기 때문입니다. ```text a=1&token=xyz&b=2 → a=1&token=***&b=2 diff --git a/documentation/content/docs/masking.mdx b/documentation/content/docs/masking.mdx index adf71ca..1769f5c 100644 --- a/documentation/content/docs/masking.mdx +++ b/documentation/content/docs/masking.mdx @@ -20,8 +20,8 @@ The rules look at **key names**, not at values. The exceptions are `Bearer` and A body that starts with `{` or `[` and parses as JSON is walked at every depth. A key is sensitive when either is true: -- After lowercasing and removing everything but letters and digits, the key **contains** one of `token`, `password`, `passwd`, `secret`, `authorization`, `cookie`, `apikey`, `sessionid`, `cardnumber`. -- The normalized key **equals** one of `cvv`, `cvc`, `ssn`, `otp`, `pin`. +- After lowercasing and removing everything but letters and digits, the key **contains** one of `token`, `password`, `passwd`, `secret`, `authorization`, `cookie`, `apikey`, `sessionid`, `cardnumber`, `credential`, `privatekey`, `signature`. +- The normalized key **equals** one of `cvv`, `cvc`, `ssn`, `otp`, `pin`, `auth`. The value of a sensitive key is replaced with `"***"`, even if that value is an object or an array. String values under other keys are checked again as text, so stringified JSON inside JSON is masked too. @@ -46,7 +46,7 @@ becomes ### 2. Text bodies -Bodies that aren't JSON, such as decoded protobuf, query strings and log-style text, are matched as `key: value` or `key=value`. The key matches when it contains one of `token`, `password`, `passwd`, `secret`, `authorization`, `cookie`, `api key`, `session id`, `card number`. In the last three, `_`, `-` or nothing may sit between the words. Case doesn't matter. The exact-match words (`cvv`, `pin` and so on) are **not** matched in text, because short words like these appear in ordinary text too often. +Bodies that aren't JSON, such as decoded protobuf, query strings and log-style text, are matched as `key: value` or `key=value`. The key matches when it contains one of `token`, `password`, `passwd`, `secret`, `authorization`, `cookie`, `api key`, `session id`, `card number`, `credential`, `private key`, `signature`. In `api key`, `session id`, `card number` and `private key`, `_`, `-` or nothing may sit between the words. Case doesn't matter. The exact-match words (`cvv`, `pin` and so on) are **not** matched in text, because short words like these appear in ordinary text too often. ```text a=1&token=xyz&b=2 → a=1&token=***&b=2