-
Notifications
You must be signed in to change notification settings - Fork 362
fix(logging): sanitize message strings and migrate printToConsole to os.Logger #492
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ManthanNimodiya
wants to merge
5
commits into
RunanywhereAI:main
Choose a base branch
from
ManthanNimodiya:fix/logging-sanitize-message-strings-246
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fd38f61
fix(logging): sanitize message strings and migrate printToConsole to …
ManthanNimodiya 19b12e2
fix(logging): address review — fix guard, narrow regex, restore confi…
ManthanNimodiya 1fa88b8
fix review comments: preconditionFailure for regex, XCTAssertEqual fo…
ManthanNimodiya a7226f9
fix pattern ordering: run Bearer regex before key=value to prevent JW…
ManthanNimodiya 2f9b0d3
Merge branch 'main' into fix/logging-sanitize-message-strings-246
ManthanNimodiya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
207 changes: 207 additions & 0 deletions
207
sdk/runanywhere-swift/Tests/RunAnywhereTests/SecurityLoggingTests.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,207 @@ | ||
| // | ||
| // SecurityLoggingTests.swift | ||
| // RunAnywhere SDK | ||
| // | ||
| // Verifies that sensitive values (API keys, tokens, passwords) are redacted | ||
| // before they reach any log destination — both from message strings and | ||
| // metadata dictionaries. Regression guard for issue #246. | ||
| // | ||
|
|
||
| import XCTest | ||
| @testable import RunAnywhere | ||
|
|
||
| // MARK: - Capturing Destination | ||
|
|
||
| /// Test-only LogDestination that records every LogEntry written to it. | ||
| private final class CapturingDestination: LogDestination, @unchecked Sendable { | ||
| static let destinationID = "test.security.capturing" | ||
|
|
||
| var identifier: String { Self.destinationID } | ||
| var isAvailable: Bool { true } | ||
|
|
||
| private(set) var captured: [LogEntry] = [] | ||
|
|
||
| func write(_ entry: LogEntry) { captured.append(entry) } | ||
| func flush() {} | ||
| func reset() { captured.removeAll() } | ||
| } | ||
|
|
||
| // MARK: - SecurityLoggingTests | ||
|
|
||
| final class SecurityLoggingTests: XCTestCase { | ||
|
|
||
| private var destination: CapturingDestination! | ||
| private var originalConfig: LoggingConfiguration! | ||
|
|
||
| override func setUp() { | ||
| super.setUp() | ||
| // Snapshot config so tearDown can restore it — Logging.shared is a | ||
| // process-wide singleton and mutated state would bleed into other test classes. | ||
| originalConfig = Logging.shared.configuration | ||
|
|
||
| destination = CapturingDestination() | ||
|
|
||
| var config = Logging.shared.configuration | ||
| config.enableLocalLogging = false // suppress os.Logger console output in CI | ||
| config.enableSentryLogging = false | ||
| config.minLogLevel = .debug | ||
| // NOTE: both local + Sentry are disabled here, so the guard in log() would | ||
| // short-circuit before reaching CapturingDestination. The guard was updated to | ||
| // also pass when !currentDestinations.isEmpty, which is true once we addDestination. | ||
| Logging.shared.configure(config) | ||
| Logging.shared.addDestination(destination) | ||
| } | ||
|
ManthanNimodiya marked this conversation as resolved.
|
||
|
|
||
| override func tearDown() { | ||
| Logging.shared.removeDestination(destination) | ||
| Logging.shared.configure(originalConfig) // restore original state | ||
| super.tearDown() | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| // MARK: - Message string sanitization | ||
|
|
||
| func testApiKeyEqualsInMessageIsRedacted() { | ||
| Logging.shared.log(level: .info, category: "Test", | ||
| message: "Configured with apiKey=sk-prod-abc123") | ||
| let msg = destination.captured.first?.message ?? "" | ||
| XCTAssertFalse(msg.contains("sk-prod-abc123"), "API key value must not appear in log") | ||
| XCTAssertTrue(msg.contains("[REDACTED]"), "Redaction marker must be present") | ||
| } | ||
|
|
||
| func testApiKeyColonInMessageIsRedacted() { | ||
| Logging.shared.log(level: .info, category: "Test", | ||
| message: "api_key: supersecret") | ||
| let msg = destination.captured.first?.message ?? "" | ||
| XCTAssertFalse(msg.contains("supersecret")) | ||
| XCTAssertTrue(msg.contains("[REDACTED]")) | ||
| } | ||
|
|
||
| func testPasswordInMessageIsRedacted() { | ||
| Logging.shared.log(level: .warning, category: "Test", | ||
| message: "Login attempt password=hunter2 failed") | ||
| let msg = destination.captured.first?.message ?? "" | ||
| XCTAssertFalse(msg.contains("hunter2")) | ||
| XCTAssertTrue(msg.contains("[REDACTED]")) | ||
| } | ||
|
|
||
| func testBearerTokenInMessageIsRedacted() { | ||
| Logging.shared.log(level: .error, category: "Test", | ||
| message: "Request rejected — Authorization: Bearer eyJhbGciOiJIUzI1NiJ9.payload.sig") | ||
| let msg = destination.captured.first?.message ?? "" | ||
| XCTAssertFalse(msg.contains("eyJhbGciOiJIUzI1NiJ9.payload.sig")) | ||
| XCTAssertTrue(msg.contains("[REDACTED]")) | ||
| } | ||
|
|
||
| func testAuthorizationBearerJWTIsFullyRedacted() { | ||
| // Regression for pattern-ordering bug: Pattern 1 must not consume "Bearer" as the | ||
| // credential value before Pattern 2 captures the actual JWT. | ||
| // Expected: both the scheme value and the header key are scrubbed; JWT never appears. | ||
| let input = "Authorization: Bearer eyJhbGciOiJIUzI1NiJ9.payload.sig" | ||
| Logging.shared.log(level: .info, category: "Test", message: input) | ||
| let msg = destination.captured.first?.message ?? "" | ||
| XCTAssertFalse(msg.contains("eyJhbGciOiJIUzI1NiJ9.payload.sig"), | ||
| "JWT must not appear in log output") | ||
| XCTAssertTrue(msg.contains("[REDACTED]"), | ||
| "Redaction marker must be present") | ||
| } | ||
|
|
||
| func testSecretInMessageIsRedacted() { | ||
| Logging.shared.log(level: .debug, category: "Test", | ||
| message: "secret=xyzTopSecret") | ||
| let msg = destination.captured.first?.message ?? "" | ||
| XCTAssertFalse(msg.contains("xyzTopSecret")) | ||
| XCTAssertTrue(msg.contains("[REDACTED]")) | ||
| } | ||
|
|
||
| func testTokenColonInMessageIsRedacted() { | ||
| Logging.shared.log(level: .info, category: "Test", | ||
| message: "Stored token: abc.def.ghi") | ||
| let msg = destination.captured.first?.message ?? "" | ||
| XCTAssertFalse(msg.contains("abc.def.ghi")) | ||
| XCTAssertTrue(msg.contains("[REDACTED]")) | ||
| } | ||
|
|
||
| func testNonSensitiveMessagePassesThroughUnchanged() { | ||
| let plain = "Model qwen3-0.6b loaded in 340ms" | ||
| Logging.shared.log(level: .info, category: "Test", message: plain) | ||
| XCTAssertEqual(destination.captured.first?.message, plain) | ||
| } | ||
|
|
||
| func testEmptyMessagePassesThroughUnchanged() { | ||
| Logging.shared.log(level: .info, category: "Test", message: "") | ||
| XCTAssertEqual(destination.captured.first?.message, "") | ||
| } | ||
|
|
||
| // MARK: - False-positive regression | ||
|
|
||
| func testTokenCountPhraseIsNotRedacted() { | ||
| // "token" followed by a noun, not a separator — must not trigger | ||
| let msg = "token count: 5" | ||
| Logging.shared.log(level: .info, category: "Test", message: msg) | ||
| XCTAssertEqual(destination.captured.first?.message, msg, | ||
| "'token count: 5' must pass through unchanged") | ||
| } | ||
|
|
||
| func testPasswordRequiredPhraseIsNotRedacted() { | ||
| let msg = "password required for this operation" | ||
| Logging.shared.log(level: .info, category: "Test", message: msg) | ||
| XCTAssertEqual(destination.captured.first?.message, msg, | ||
| "'password required' must pass through unchanged — no = or : separator present") | ||
| } | ||
|
|
||
| func testBasicEnglishPhraseIsNotRedacted() { | ||
| // "basic" is excluded from the pattern entirely to avoid prose false-positives | ||
| let msg = "basic authentication is disabled" | ||
| Logging.shared.log(level: .info, category: "Test", message: msg) | ||
| XCTAssertEqual(destination.captured.first?.message, msg, | ||
| "'basic' in plain English must pass through unchanged") | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| // MARK: - Metadata sanitization (regression) | ||
|
|
||
| func testApiKeyInMetadataIsRedacted() { | ||
| Logging.shared.log(level: .info, category: "Test", message: "Init", | ||
| metadata: ["apiKey": "sk-supersecret"]) | ||
| let meta = destination.captured.first?.metadata ?? [:] | ||
| XCTAssertEqual(meta["apiKey"], "[REDACTED]", "apiKey metadata value must be redacted") | ||
| } | ||
|
|
||
| func testPasswordInMetadataIsRedacted() { | ||
| Logging.shared.log(level: .info, category: "Test", message: "Auth", | ||
| metadata: ["password": "letmein"]) | ||
| let meta = destination.captured.first?.metadata ?? [:] | ||
| XCTAssertEqual(meta["password"], "[REDACTED]") | ||
| } | ||
|
|
||
| func testNonSensitiveMetadataPassesThroughUnchanged() { | ||
| Logging.shared.log(level: .info, category: "Test", message: "Info", | ||
| metadata: ["modelId": "qwen3-0.6b", "duration_ms": 200]) | ||
| let meta = destination.captured.first?.metadata ?? [:] | ||
| XCTAssertEqual(meta["modelId"], "qwen3-0.6b") | ||
| } | ||
|
|
||
| // MARK: - logError call-site sanitization | ||
|
|
||
| func testLogErrorWithSensitiveAdditionalInfoIsRedacted() { | ||
| struct FakeError: Error, LocalizedError { | ||
| var errorDescription: String? { "Request failed" } | ||
| } | ||
| let logger = SDKLogger(category: "Test") | ||
| logger.logError(FakeError(), additionalInfo: "token=leaked-value-xyz") | ||
| let msg = destination.captured.first?.message ?? "" | ||
| XCTAssertFalse(msg.contains("leaked-value-xyz"), | ||
| "Sensitive value in additionalInfo must be redacted via sanitizeMessage") | ||
| XCTAssertTrue(msg.contains("[REDACTED]")) | ||
| } | ||
|
|
||
| func testLogErrorWithCleanAdditionalInfoPassesThrough() { | ||
| struct FakeError: Error, LocalizedError { | ||
| var errorDescription: String? { "Timeout" } | ||
| } | ||
| let logger = SDKLogger(category: "Test") | ||
| logger.logError(FakeError(), additionalInfo: "retrying request") | ||
| let msg = destination.captured.first?.message ?? "" | ||
| XCTAssertTrue(msg.contains("retrying request")) | ||
| XCTAssertFalse(msg.contains("[REDACTED]")) | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.