From 1490fea345095f053fc2e56033501efcd34de274 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cihat=20Gu=CC=88ndu=CC=88z?= Date: Fri, 1 Nov 2024 15:16:47 +0100 Subject: [PATCH 1/2] Log errors to console when reserved keys are being used by users --- .../TelemetryDeck/Signals/SignalManager.swift | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/Sources/TelemetryDeck/Signals/SignalManager.swift b/Sources/TelemetryDeck/Signals/SignalManager.swift index 5ab10c0..8e7ec21 100644 --- a/Sources/TelemetryDeck/Signals/SignalManager.swift +++ b/Sources/TelemetryDeck/Signals/SignalManager.swift @@ -18,6 +18,15 @@ protocol SignalManageable { final class SignalManager: SignalManageable, @unchecked Sendable { static let minimumSecondsToPassBetweenRequests: Double = 10 + static let reservedKeysLowercased: Set = Set( + [ + "type", "clientUser", "appID", "sessionID", "floatValue", + "newSessionBegan", "platform", "systemVersion", "majorSystemVersion", "majorMinorSystemVersion", "appVersion", "buildNumber", + "isSimulator", "isDebug", "isTestFlight", "isAppStore", "modelName", "architecture", "operatingSystem", "targetEnvironment", + "locale", "region", "appLanguage", "preferredLanguage", "telemetryClientVersion", + ].map { $0.lowercased() } + ) + private var signalCache: SignalCache let configuration: TelemetryManagerConfiguration @@ -75,6 +84,24 @@ final class SignalManager: SignalManageable, @unchecked Sendable { customUserID: String?, configuration: TelemetryManagerConfiguration ) { + // warn users about reserved keys to avoid unexpected behavior + if signalName.lowercased().hasPrefix("telemetrydeck.") || Self.reservedKeysLowercased.contains(signalName.lowercased()) { + configuration.logHandler?.log( + .error, + message: "Sending signal with reserved key '\(signalName)' will cause unexpected behavior. Please use another name instead." + ) + } + + for parameterKey in parameters.keys { + if parameterKey.lowercased().hasPrefix("telemetrydeck.") || Self.reservedKeysLowercased.contains(parameterKey.lowercased()) { + configuration.logHandler?.log( + .error, + message: "Sending parameter with reserved key '\(parameterKey)' will cause unexpected behavior. Please use another key instead." + ) + } + } + + // enqueue signal to sending cache DispatchQueue.main.async { let defaultUserIdentifier = self.defaultUserIdentifier let defaultParameters = DefaultSignalPayload.parameters From 73172d593cfc04476b4ad2a0ef919f8b5f03ee34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cihat=20Gu=CC=88ndu=CC=88z?= Date: Fri, 1 Nov 2024 15:24:31 +0100 Subject: [PATCH 2/2] Provide a more clear error when the TelemetryDeck prefix is used --- .../TelemetryDeck/Signals/SignalManager.swift | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/Sources/TelemetryDeck/Signals/SignalManager.swift b/Sources/TelemetryDeck/Signals/SignalManager.swift index 8e7ec21..838664f 100644 --- a/Sources/TelemetryDeck/Signals/SignalManager.swift +++ b/Sources/TelemetryDeck/Signals/SignalManager.swift @@ -85,15 +85,25 @@ final class SignalManager: SignalManageable, @unchecked Sendable { configuration: TelemetryManagerConfiguration ) { // warn users about reserved keys to avoid unexpected behavior - if signalName.lowercased().hasPrefix("telemetrydeck.") || Self.reservedKeysLowercased.contains(signalName.lowercased()) { + if signalName.lowercased().hasPrefix("telemetrydeck.") { configuration.logHandler?.log( .error, - message: "Sending signal with reserved key '\(signalName)' will cause unexpected behavior. Please use another name instead." + message: "Sending signal with reserved prefix 'TelemetryDeck.' will cause unexpected behavior. Please use another prefix instead." + ) + } else if Self.reservedKeysLowercased.contains(signalName.lowercased()) { + configuration.logHandler?.log( + .error, + message: "Sending signal with reserved name '\(signalName)' will cause unexpected behavior. Please use another name instead." ) } for parameterKey in parameters.keys { - if parameterKey.lowercased().hasPrefix("telemetrydeck.") || Self.reservedKeysLowercased.contains(parameterKey.lowercased()) { + if parameterKey.lowercased().hasPrefix("telemetrydeck.") { + configuration.logHandler?.log( + .error, + message: "Sending parameter with reserved key prefix 'TelemetryDeck.' will cause unexpected behavior. Please use another prefix instead." + ) + } else if Self.reservedKeysLowercased.contains(parameterKey.lowercased()) { configuration.logHandler?.log( .error, message: "Sending parameter with reserved key '\(parameterKey)' will cause unexpected behavior. Please use another key instead."