@onekeyfe/react-native-sni-connect 6.7.0-57 → 6.7.0-59

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -9,6 +9,7 @@ package com.sniconnect
9
9
  */
10
10
  internal object SniConnectLogger {
11
11
  private const val TAG = "SniConnect"
12
+ private val requestIdHexRun = Regex("([0-9a-fA-F]{32})(?=[0-9a-fA-F]{32})")
12
13
 
13
14
  private val logClass: Class<*>? by lazy {
14
15
  try {
@@ -42,7 +43,18 @@ internal object SniConnectLogger {
42
43
 
43
44
  fun event(name: String, vararg fields: Pair<String, Any?>): String =
44
45
  (listOf("event" to name) + fields).joinToString(separator = " ") { (key, value) ->
45
- "$key=${sanitize(value)}"
46
+ val logValue = sanitize(value)
47
+ // Keep long diagnostic IDs recoverable without matching the logger's private-key filter.
48
+ var displayedValue = logValue
49
+ var formatSuffix = ""
50
+ if (key == "requestId" && value is String &&
51
+ (value != logValue || requestIdHexRun.containsMatchIn(value))) {
52
+ displayedValue = java.net.URLEncoder.encode(value, "UTF-8")
53
+ .replace("+", "%20").replace("-", "%2D").replace("*", "%2A")
54
+ .replace(requestIdHexRun, "$1-")
55
+ formatSuffix = " requestIdFormat=uri-hex32"
56
+ }
57
+ "$key=$displayedValue$formatSuffix"
46
58
  }
47
59
 
48
60
  fun shortHash(value: String?): String {
@@ -151,7 +151,7 @@ class SniConnectModule(reactContext: ReactApplicationContext) :
151
151
  SniConnectLogger.event(
152
152
  "sni_cache_evict",
153
153
  "hostname" to eldest.key.hostname,
154
- "ipHash" to SniConnectLogger.shortHash(eldest.key.ip),
154
+ "ip" to eldest.key.ip,
155
155
  "cacheSize" to size,
156
156
  "limit" to MAX_CLIENTS,
157
157
  "reason" to "max_cached_clients",
@@ -252,7 +252,7 @@ class SniConnectModule(reactContext: ReactApplicationContext) :
252
252
  SniConnectLogger.info(
253
253
  SniConnectLogger.event(
254
254
  "sni_cancel",
255
- "requestIdHash" to SniConnectLogger.shortHash(requestId),
255
+ "requestId" to requestId,
256
256
  "success" to true,
257
257
  ),
258
258
  )
@@ -261,7 +261,7 @@ class SniConnectModule(reactContext: ReactApplicationContext) :
261
261
  SniConnectLogger.warn(
262
262
  SniConnectLogger.event(
263
263
  "sni_cancel",
264
- "requestIdHash" to SniConnectLogger.shortHash(requestId),
264
+ "requestId" to requestId,
265
265
  "success" to false,
266
266
  ),
267
267
  )
@@ -348,9 +348,9 @@ class SniConnectModule(reactContext: ReactApplicationContext) :
348
348
  SniConnectLogger.info(
349
349
  SniConnectLogger.event(
350
350
  "sni_request_start",
351
- "requestIdHash" to SniConnectLogger.shortHash(config.requestId),
351
+ "requestId" to config.requestId,
352
352
  "hostname" to config.hostname.lowercase(Locale.US),
353
- "ipHash" to SniConnectLogger.shortHash(config.ip),
353
+ "ip" to config.ip,
354
354
  "ipFamily" to SniConnectLogger.ipFamily(config.ip),
355
355
  "method" to config.method,
356
356
  "timeoutMs" to config.timeoutMillis,
@@ -379,9 +379,9 @@ class SniConnectModule(reactContext: ReactApplicationContext) :
379
379
  "result" to "error",
380
380
  "code" to code,
381
381
  "nativeErrorClass" to e.javaClass.simpleName,
382
- "requestIdHash" to SniConnectLogger.shortHash(config.requestId),
382
+ "requestId" to config.requestId,
383
383
  "hostname" to config.hostname.lowercase(Locale.US),
384
- "ipHash" to SniConnectLogger.shortHash(config.ip),
384
+ "ip" to config.ip,
385
385
  "ipFamily" to SniConnectLogger.ipFamily(config.ip),
386
386
  "method" to config.method,
387
387
  "timeoutMs" to config.timeoutMillis,
@@ -401,9 +401,9 @@ class SniConnectModule(reactContext: ReactApplicationContext) :
401
401
  "sni_request_result",
402
402
  "result" to "response",
403
403
  "status" to currentResponse.code,
404
- "requestIdHash" to SniConnectLogger.shortHash(config.requestId),
404
+ "requestId" to config.requestId,
405
405
  "hostname" to config.hostname.lowercase(Locale.US),
406
- "ipHash" to SniConnectLogger.shortHash(config.ip),
406
+ "ip" to config.ip,
407
407
  "ipFamily" to SniConnectLogger.ipFamily(config.ip),
408
408
  "method" to config.method,
409
409
  "timeoutMs" to config.timeoutMillis,
@@ -448,9 +448,9 @@ class SniConnectModule(reactContext: ReactApplicationContext) :
448
448
  "result" to "error",
449
449
  "code" to code,
450
450
  "nativeErrorClass" to error.javaClass.simpleName,
451
- "requestIdHash" to SniConnectLogger.shortHash(config.requestId),
451
+ "requestId" to config.requestId,
452
452
  "hostname" to config.hostname.lowercase(Locale.US),
453
- "ipHash" to SniConnectLogger.shortHash(config.ip),
453
+ "ip" to config.ip,
454
454
  "ipFamily" to SniConnectLogger.ipFamily(config.ip),
455
455
  "method" to config.method,
456
456
  "timeoutMs" to config.timeoutMillis,
@@ -515,9 +515,9 @@ class SniConnectModule(reactContext: ReactApplicationContext) :
515
515
  "result" to "error",
516
516
  "code" to code,
517
517
  "nativeErrorClass" to error.javaClass.simpleName,
518
- "requestIdHash" to SniConnectLogger.shortHash(config.requestId),
518
+ "requestId" to config.requestId,
519
519
  "hostname" to config.hostname.lowercase(Locale.US),
520
- "ipHash" to SniConnectLogger.shortHash(config.ip),
520
+ "ip" to config.ip,
521
521
  "ipFamily" to SniConnectLogger.ipFamily(config.ip),
522
522
  "method" to config.method,
523
523
  "timeoutMs" to config.timeoutMillis,
@@ -551,7 +551,7 @@ class SniConnectModule(reactContext: ReactApplicationContext) :
551
551
  SniConnectLogger.warn(
552
552
  SniConnectLogger.event(
553
553
  "sni_duplicate_request_id",
554
- "requestIdHash" to SniConnectLogger.shortHash(requestId),
554
+ "requestId" to requestId,
555
555
  "action" to "cancel_previous",
556
556
  ),
557
557
  )
@@ -612,7 +612,7 @@ class SniConnectModule(reactContext: ReactApplicationContext) :
612
612
  SniConnectLogger.event(
613
613
  "sni_transport_config",
614
614
  "hostname" to key.hostname,
615
- "ipHash" to SniConnectLogger.shortHash(key.ip),
615
+ "ip" to key.ip,
616
616
  "ipFamily" to SniConnectLogger.ipFamily(key.ip),
617
617
  "proxyMode" to "no_proxy",
618
618
  "pinnedResolver" to true,
@@ -74,9 +74,9 @@ final class SniConnectImpl: NSObject {
74
74
  ("code", "SNI_INVALID_CONFIG"),
75
75
  ("nativeErrorClass", String(describing: type(of: error))),
76
76
  ("stage", "validate_config"),
77
- ("requestIdHash", SniConnectLog.shortHash(config.requestId)),
77
+ ("requestId", config.requestId),
78
78
  ("hostname", config.hostname.lowercased()),
79
- ("ipHash", SniConnectLog.shortHash(config.ip)),
79
+ ("ip", config.ip),
80
80
  ("ipFamily", SniConnectLog.ipFamily(config.ip)),
81
81
  ("method", config.method.uppercased()),
82
82
  ("timeoutMs", Int(config.effectiveTotalTimeout)),
@@ -118,9 +118,9 @@ final class SniConnectImpl: NSObject {
118
118
  ("result", "error"),
119
119
  ("code", "SNI_UNKNOWN_ERROR"),
120
120
  ("nativeErrorClass", String(describing: type(of: error))),
121
- ("requestIdHash", SniConnectLog.shortHash(config.requestId)),
121
+ ("requestId", config.requestId),
122
122
  ("hostname", config.hostname.lowercased()),
123
- ("ipHash", SniConnectLog.shortHash(config.ip)),
123
+ ("ip", config.ip),
124
124
  ("ipFamily", SniConnectLog.ipFamily(config.ip)),
125
125
  ("method", config.method.uppercased()),
126
126
  ("timeoutMs", Int(config.effectiveTotalTimeout)),
@@ -112,7 +112,7 @@ final class SniConnectSessionInvalidationDelegate: NSObject, URLSessionDataDeleg
112
112
  let releasedResolver = resolverLease.release()
113
113
  SniConnectLog.info(SniConnectLog.event("sni_session_invalidated", [
114
114
  ("hostname", hostname),
115
- ("ipHash", SniConnectLog.shortHash(ip)),
115
+ ("ip", ip),
116
116
  ("releasedResolver", releasedResolver),
117
117
  ("success", error == nil),
118
118
  ]))
@@ -410,7 +410,7 @@ final class SniConnectClient {
410
410
  )
411
411
  SniConnectLog.info(SniConnectLog.event("sni_transport_config", [
412
412
  ("hostname", hostname),
413
- ("ipHash", SniConnectLog.shortHash(ip)),
413
+ ("ip", ip),
414
414
  ("ipFamily", SniConnectLog.ipFamily(ip)),
415
415
  ("proxyMode", "no_proxy"),
416
416
  ("pinnedResolver", true),
@@ -459,7 +459,7 @@ final class SniConnectClient {
459
459
  case .waitForResolverSlots(let resolverLeases):
460
460
  SniConnectLog.info(SniConnectLog.event("sni_resolver_slot_wait", [
461
461
  ("hostname", key.hostname),
462
- ("ipHash", SniConnectLog.shortHash(config.ip)),
462
+ ("ip", config.ip),
463
463
  ("waitCount", resolverLeases.count),
464
464
  ]))
465
465
  for resolverLease in resolverLeases {
@@ -510,7 +510,7 @@ final class SniConnectClient {
510
510
  }
511
511
  SniConnectLog.info(SniConnectLog.event("sni_cache_evict", [
512
512
  ("hostname", evictedKey.hostname),
513
- ("ipHash", SniConnectLog.shortHash(evictedKey.canonicalIP)),
513
+ ("ip", evictedKey.canonicalIP),
514
514
  ("cacheSize", sessionCache.count),
515
515
  ("limit", Self.maxCachedSessions),
516
516
  ("reason", forPendingInsert ? "max_cached_sessions_pending_insert" : "max_cached_sessions"),
@@ -581,14 +581,14 @@ final class SniConnectClient {
581
581
  guard let self = self, let token = self.requestTokensById.removeValue(forKey: requestId),
582
582
  let task = self.activeTasksByToken.removeValue(forKey: token) else {
583
583
  SniConnectLog.warn(SniConnectLog.event("sni_cancel", [
584
- ("requestIdHash", SniConnectLog.shortHash(requestId)),
584
+ ("requestId", requestId),
585
585
  ("success", false),
586
586
  ]))
587
587
  return false
588
588
  }
589
589
  task.cancel()
590
590
  SniConnectLog.info(SniConnectLog.event("sni_cancel", [
591
- ("requestIdHash", SniConnectLog.shortHash(requestId)),
591
+ ("requestId", requestId),
592
592
  ("success", true),
593
593
  ]))
594
594
  return true
@@ -638,7 +638,7 @@ final class SniConnectClient {
638
638
  let previousTask = self.activeTasksByToken.removeValue(forKey: previousToken) {
639
639
  previousTask.cancel()
640
640
  SniConnectLog.warn(SniConnectLog.event("sni_duplicate_request_id", [
641
- ("requestIdHash", SniConnectLog.shortHash(requestId)),
641
+ ("requestId", requestId),
642
642
  ("action", "cancel_previous"),
643
643
  ]))
644
644
  }
@@ -695,9 +695,9 @@ final class SniConnectClient {
695
695
  ("result", "error"),
696
696
  ("code", sniError.code),
697
697
  ("nativeErrorClass", String(describing: type(of: error))),
698
- ("requestIdHash", SniConnectLog.shortHash(config.requestId)),
698
+ ("requestId", config.requestId),
699
699
  ("hostname", config.hostname.lowercased()),
700
- ("ipHash", SniConnectLog.shortHash(config.ip)),
700
+ ("ip", config.ip),
701
701
  ("ipFamily", SniConnectLog.ipFamily(config.ip)),
702
702
  ("method", config.method.uppercased()),
703
703
  ("timeoutMs", Int(config.effectiveTotalTimeout)),
@@ -728,9 +728,9 @@ final class SniConnectClient {
728
728
  ("result", "error"),
729
729
  ("code", sniError.code),
730
730
  ("nativeErrorClass", String(describing: type(of: error))),
731
- ("requestIdHash", SniConnectLog.shortHash(config.requestId)),
731
+ ("requestId", config.requestId),
732
732
  ("hostname", config.hostname.lowercased()),
733
- ("ipHash", SniConnectLog.shortHash(config.ip)),
733
+ ("ip", config.ip),
734
734
  ("ipFamily", SniConnectLog.ipFamily(config.ip)),
735
735
  ("method", method),
736
736
  ("timeoutMs", Int(config.effectiveTotalTimeout)),
@@ -793,9 +793,9 @@ final class SniConnectClient {
793
793
  let request = mutableRequest as URLRequest
794
794
 
795
795
  SniConnectLog.info(SniConnectLog.event("sni_request_start", [
796
- ("requestIdHash", SniConnectLog.shortHash(config.requestId)),
796
+ ("requestId", config.requestId),
797
797
  ("hostname", config.hostname.lowercased()),
798
- ("ipHash", SniConnectLog.shortHash(config.ip)),
798
+ ("ip", config.ip),
799
799
  ("ipFamily", SniConnectLog.ipFamily(config.ip)),
800
800
  ("method", method),
801
801
  ("timeoutMs", Int(config.effectiveTotalTimeout)),
@@ -833,9 +833,9 @@ final class SniConnectClient {
833
833
  let resultLog = SniConnectLog.event("sni_request_result", [
834
834
  ("result", "response"),
835
835
  ("status", status),
836
- ("requestIdHash", SniConnectLog.shortHash(config.requestId)),
836
+ ("requestId", config.requestId),
837
837
  ("hostname", config.hostname.lowercased()),
838
- ("ipHash", SniConnectLog.shortHash(config.ip)),
838
+ ("ip", config.ip),
839
839
  ("ipFamily", SniConnectLog.ipFamily(config.ip)),
840
840
  ("method", method),
841
841
  ("timeoutMs", Int(config.effectiveTotalTimeout)),
@@ -862,9 +862,9 @@ final class SniConnectClient {
862
862
  ("result", "error"),
863
863
  ("code", error.code),
864
864
  ("nativeErrorClass", String(describing: type(of: error))),
865
- ("requestIdHash", SniConnectLog.shortHash(config.requestId)),
865
+ ("requestId", config.requestId),
866
866
  ("hostname", config.hostname.lowercased()),
867
- ("ipHash", SniConnectLog.shortHash(config.ip)),
867
+ ("ip", config.ip),
868
868
  ("ipFamily", SniConnectLog.ipFamily(config.ip)),
869
869
  ("method", method),
870
870
  ("timeoutMs", Int(config.effectiveTotalTimeout)),
@@ -878,9 +878,9 @@ final class SniConnectClient {
878
878
  ("result", "error"),
879
879
  ("code", sniError.code),
880
880
  ("nativeErrorClass", String(describing: type(of: error))),
881
- ("requestIdHash", SniConnectLog.shortHash(config.requestId)),
881
+ ("requestId", config.requestId),
882
882
  ("hostname", config.hostname.lowercased()),
883
- ("ipHash", SniConnectLog.shortHash(config.ip)),
883
+ ("ip", config.ip),
884
884
  ("ipFamily", SniConnectLog.ipFamily(config.ip)),
885
885
  ("method", method),
886
886
  ("timeoutMs", Int(config.effectiveTotalTimeout)),
@@ -114,7 +114,7 @@ final class SniConnectPinnedResolverRegistry {
114
114
  guard domain.caseInsensitiveCompare(config.hostname) == .orderedSame else {
115
115
  SniConnectCoreDiagnostics.warn(SniConnectCoreDiagnostics.event("sni_pinned_dns_unexpected_host", [
116
116
  ("expectedHost", config.hostname),
117
- ("requestedHostHash", SniConnectCoreDiagnostics.shortHash(domain.lowercased())),
117
+ ("requestedHost", domain.lowercased()),
118
118
  ("result", "fail_closed"),
119
119
  ]))
120
120
  return nil
@@ -23,7 +23,20 @@ enum SniConnectLog {
23
23
  static func event(_ name: String, _ fields: [(String, Any?)]) -> String {
24
24
  let normalizedFields: [(String, Any?)] = [("event", name)] + fields
25
25
  let pairs = normalizedFields.map { key, value in
26
- "\(key)=\(sanitize(value))"
26
+ let logValue = sanitize(value)
27
+ // Keep long diagnostic IDs recoverable without matching the logger's private-key filter.
28
+ var displayedValue = logValue
29
+ var formatSuffix = ""
30
+ if key == "requestId", let requestId = value as? String,
31
+ requestId != logValue || requestId.range(of: "[0-9a-fA-F]{64}", options: .regularExpression) != nil {
32
+ let allowed = CharacterSet(charactersIn: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_.")
33
+ displayedValue = (requestId.addingPercentEncoding(withAllowedCharacters: allowed) ?? "unavailable")
34
+ .replacingOccurrences(
35
+ of: "([0-9a-fA-F]{32})(?=[0-9a-fA-F]{32})", with: "$1-", options: .regularExpression
36
+ )
37
+ formatSuffix = " requestIdFormat=uri-hex32"
38
+ }
39
+ return "\(key)=\(displayedValue)\(formatSuffix)"
27
40
  }
28
41
  return pairs.joined(separator: " ")
29
42
  }
@@ -526,7 +526,7 @@ final class SniConnectRequestLimiter: @unchecked Sendable {
526
526
  ("limit", maxPendingRequests),
527
527
  ("reason", "max_pending_requests"),
528
528
  ("hostname", hostname.lowercased()),
529
- ("ipHash", SniConnectCoreDiagnostics.shortHash(ip)),
529
+ ("ip", ip),
530
530
  ]))
531
531
  immediateError = SniConnectValidation.ValidationError.resourceLimit(
532
532
  "Too many pending SNI requests"
@@ -3,6 +3,23 @@ import XCTest
3
3
 
4
4
  final class SniConnectValidationTests: XCTestCase {
5
5
 
6
+ func testUnexpectedResolverHostIsLoggedInPlaintextAndStillRejected() throws {
7
+ let registry = SniConnectPinnedResolverRegistry()
8
+ let resolverClass: AnyClass = try registry.resolverClass(hostname: "example.com", ip: "93.184.216.34") {
9
+ NSObject.self
10
+ }
11
+ var messages: [String] = []
12
+ let previousSink = SniConnectCoreDiagnostics.warnSink
13
+ SniConnectCoreDiagnostics.warnSink = { messages.append($0) }
14
+ defer { SniConnectCoreDiagnostics.warnSink = previousSink }
15
+
16
+ XCTAssertNil(registry.resolve(domain: "Unexpected.example.com", resolverClass: resolverClass))
17
+ XCTAssertEqual(messages.count, 1)
18
+ XCTAssertTrue(messages[0].contains("requestedHost=unexpected.example.com"))
19
+ XCTAssertTrue(messages[0].contains("result=fail_closed"))
20
+ XCTAssertFalse(messages[0].contains("requestedHostHash="))
21
+ }
22
+
6
23
  func testSessionWithoutForwardingDataDelegateAllowsResponse() {
7
24
  switch SniConnectSessionDelegatePolicy.responseDispositionWithoutForwardingDelegate {
8
25
  case .allow:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onekeyfe/react-native-sni-connect",
3
- "version": "6.7.0-57",
3
+ "version": "6.7.0-59",
4
4
  "description": "A React Native library for SNI-based HTTP requests with DNS caching and request management",
5
5
  "main": "./lib/module/index.js",
6
6
  "types": "./lib/typescript/src/index.d.ts",