diff --git a/.github/workflows/build-ios-release.yml b/.github/workflows/build-ios-release.yml index f2e393a6..6b3f6cdb 100644 --- a/.github/workflows/build-ios-release.yml +++ b/.github/workflows/build-ios-release.yml @@ -62,11 +62,8 @@ jobs: env: SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }} run: | - if [[ -n "$SENTRY_AUTH_TOKEN" ]]; then - brew install sentry-cli - else - echo "::warning::SENTRY_AUTH_TOKEN is missing. The iOS dSYMs will stay in the local archive." - fi + : "${SENTRY_AUTH_TOKEN:?SENTRY_AUTH_TOKEN is required to upload iOS release dSYMs}" + brew install sentry-cli - name: Restore opam root id: opam-root diff --git a/apple/App/fastlane/Fastfile b/apple/App/fastlane/Fastfile index 37c9d8d4..d585b1ba 100644 --- a/apple/App/fastlane/Fastfile +++ b/apple/App/fastlane/Fastfile @@ -10,6 +10,7 @@ default_platform(:ios) platform :ios do desc "Push a new beta build to TestFlight" lane :beta do + UI.user_error!("SENTRY_AUTH_TOKEN is required to upload iOS release dSYMs") if ENV["SENTRY_AUTH_TOKEN"].to_s.strip.empty? setup_ci app_store_connect_api_key( @@ -55,7 +56,7 @@ platform :ios do configuration: "Release", archive_path: archive_path, derived_data_path: File.join(repo_root, "apple/.build/archives", native_cache_key), - xcargs: "-allowProvisioningUpdates -skipPackagePluginValidation -skipMacroValidation CURRENT_PROJECT_VERSION=#{build_number} LOGSEQ_BUILD_REVISION=#{revision}" + xcargs: "-allowProvisioningUpdates -skipPackagePluginValidation -skipMacroValidation DEBUG_INFORMATION_FORMAT=dwarf-with-dsym CURRENT_PROJECT_VERSION=#{build_number} LOGSEQ_BUILD_REVISION=#{revision}" } build_app(**build_options, skip_package_ipa: true) @@ -70,10 +71,10 @@ platform :ios do sh("python3", verifier, ipa, *verification_args, "--report", File.join(release_dir, "ipa-verification.json")) - unless ENV["SENTRY_AUTH_TOKEN"].to_s.strip.empty? - sh("sentry-cli", "debug-files", "upload", "--org", "logseq", "--project", "logseq-ios", - "--wait", File.join(archive_path, "dSYMs")) - end + dsym_dir = File.join(archive_path, "dSYMs") + UI.user_error!("Archive contains no app dSYM") unless File.directory?(File.join(dsym_dir, "Logseq.app.dSYM")) + sh("sentry-cli", "debug-files", "upload", "--org", "logseq", "--project", "logseq-ios", + "--include-sources", "--wait", dsym_dir) upload_to_testflight( ipa: ipa, diff --git a/apple/Sources/Logseq/LGRuntime.swift b/apple/Sources/Logseq/LGRuntime.swift index c67a4557..4d0c1290 100644 --- a/apple/Sources/Logseq/LGRuntime.swift +++ b/apple/Sources/Logseq/LGRuntime.swift @@ -146,6 +146,9 @@ public final class LGRuntime { authenticationCode: Int = 0 ) throws { guard !isStarted else { return } + #if os(iOS) + LogseqCrashReporting.runtime(.start) + #endif lastAuthoritativeCoreResponse = nil lastCoreSyncProjection = nil authoritativeCoreResponseInvalidated = false @@ -165,6 +168,9 @@ public final class LGRuntime { invalidatePendingPatchApplies() try apply(initialPatch) isStarted = true + #if os(iOS) + LogseqCrashReporting.runtime(.started) + #endif while !pendingCoreResponses.isEmpty { let response = pendingCoreResponses.removeFirst() try applyCoreResponse(response, synchronously: true) @@ -436,6 +442,10 @@ public final class LGRuntime { } private func drainEffects() async { + #if os(iOS) + LogseqCrashReporting.runtime(.drainBegin) + defer { LogseqCrashReporting.runtime(.drainEnd) } + #endif defer { isDrainingEffects = false } let epoch = patchApplyEpoch while true { diff --git a/apple/Sources/Logseq/LogseqCrashReporting.swift b/apple/Sources/Logseq/LogseqCrashReporting.swift index cb52101a..f107f076 100644 --- a/apple/Sources/Logseq/LogseqCrashReporting.swift +++ b/apple/Sources/Logseq/LogseqCrashReporting.swift @@ -16,9 +16,10 @@ enum LogseqCrashReporting { options.environment = environment options.sendDefaultPii = false options.enableAutoSessionTracking = false - options.enableAutoBreadcrumbTracking = false - options.enableNetworkBreadcrumbs = false - options.maxBreadcrumbs = 0 + options.enableAutoBreadcrumbTracking = true + options.enableNetworkBreadcrumbs = true + options.maxBreadcrumbs = 50 + options.beforeBreadcrumb = { breadcrumb in safeBreadcrumb(breadcrumb) } options.beforeSend = scrub } if let revision = Bundle.main.object(forInfoDictionaryKey: "LogseqRevision") as? String { @@ -26,6 +27,61 @@ enum LogseqCrashReporting { } } + nonisolated static func safeBreadcrumb(_ breadcrumb: Breadcrumb) -> Breadcrumb? { + let data = breadcrumb.data ?? [:] + var safe: [String: Any] = [:] + switch breadcrumb.category { + case "app.lifecycle": + guard let state = data["state"] as? String, + ["foreground", "background", "active", "inactive"].contains(state) else { return nil } + safe["state"] = state + case "device.event": + guard data["action"] as? String == "LOW_MEMORY" else { return nil } + safe["action"] = "LOW_MEMORY" + case "device.connectivity": + guard let state = data["connectivity"] as? String, + ["wifi", "cellular", "ethernet", "none", "offline"].contains(state) else { return nil } + safe["connectivity"] = state + case "ui.lifecycle": + guard let screen = data["screen"] as? String, + !screen.isEmpty, screen.count < 200, + screen.range(of: "^[A-Za-z_][A-Za-z0-9_.]*(?:<[A-Za-z0-9_., <>]+>)?$", options: .regularExpression) != nil else { return nil } + safe["screen"] = screen + case "http": + guard let method = data["method"] as? String, + ["GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS"].contains(method), + let rawURL = data["url"] as? String, + let url = URLComponents(string: rawURL), + let scheme = url.scheme, ["https", "http"].contains(scheme), + let host = url.host else { return nil } + safe["method"] = method + safe["host"] = host + if let status = data["status_code"] as? Int, (100...599).contains(status) { + safe["status_code"] = status + } + case "runtime": + guard let state = data["state"] as? String, + ["start", "started", "drain_begin", "drain_end"].contains(state) else { return nil } + safe["state"] = state + default: + return nil + } + let result = Breadcrumb(level: breadcrumb.level, category: breadcrumb.category) + result.timestamp = breadcrumb.timestamp + result.data = safe + return result + } + + enum RuntimeState: String { + case start, started, drainBegin = "drain_begin", drainEnd = "drain_end" + } + + nonisolated static func runtime(_ state: RuntimeState) { + let breadcrumb = Breadcrumb(level: .info, category: "runtime") + breadcrumb.data = ["state": state.rawValue] + SentrySDK.addBreadcrumb(breadcrumb) + } + nonisolated static func scrub(_ event: Event) -> Event? { // Exception reasons can contain graph names, paths, or user content. event.exceptions?.forEach { exception in diff --git a/apple/Tests/UI/sentry-callback-isolation.swift b/apple/Tests/UI/sentry-callback-isolation.swift index 7e332647..10c2a3a4 100644 --- a/apple/Tests/UI/sentry-callback-isolation.swift +++ b/apple/Tests/UI/sentry-callback-isolation.swift @@ -15,6 +15,35 @@ import SwiftUI precondition(!Thread.isMainThread) // Exercise the actual installed callback without uploading test events. let callback = SentrySDK.internal.options.beforeSend! + let beforeBreadcrumb = SentrySDK.internal.options.beforeBreadcrumb! + let cases: [(String, [String: Any], [String: String]?)] = [ + ("app.lifecycle", ["state": "active", "title": "private"], ["state": "active"]), + ("device.event", ["action": "LOW_MEMORY"], ["action": "LOW_MEMORY"]), + ("device.connectivity", ["connectivity": "wifi"], ["connectivity": "wifi"]), + ("ui.lifecycle", ["screen": "Logseq.Controller", "title": "private"], ["screen": "Logseq.Controller"]), + ("ui.lifecycle", ["screen": "UIHostingController>", "title": "private"], ["screen": "UIHostingController>"]), + ("http", ["method": "GET", "url": "https://user:token@example.com/private?q=secret#title"], ["method": "GET", "host": "example.com"]), + ("runtime", ["state": "drain_begin"], ["state": "drain_begin"]), + ("console", ["message": "private"], nil), + ("ui.click", ["title": "private"], nil), + ("unknown", [:], nil), + ("app.lifecycle", [:], nil), + ("http", ["method": "GET", "url": "not a URL"], nil), + ("ui.lifecycle", ["screen": "private title"], nil) + ] + for (category, data, expected) in cases { + let crumb = Breadcrumb(level: .info, category: category) + crumb.data = data + crumb.message = "private message" + let result = beforeBreadcrumb(crumb) + if let expected { + precondition(result?.data as? [String: String] == expected) + precondition(result?.message == nil) + } else { + precondition(result == nil) + } + } + print("PASS: background beforeBreadcrumb retains only allowlisted diagnostic fields; drops content, empty and malformed breadcrumbs") SentrySDK.close() let event = Event(level: .error) let typed = Exception(value: "private graph/path", type: "ExampleError")