Mitigation Strategy: Backpressure Handling
Description:
- Identify Critical Observables: Analyze Observables interacting with high-volume sources (network, files, user input).
- Choose a Backpressure Strategy: Select the appropriate RxKotlin operator:
onBackpressureBuffer
: Buffers events (configure buffer size).onBackpressureDrop
: Discards events.onBackpressureLatest
: Keeps only the latest event.sample
: Emits the most recent item within a time window.
- Apply the Operator: Insert the operator into the Observable chain (near the source).
- Consider Flowable: Refactor to
Flowable
for built-in backpressure support. - Monitor and Tune: Monitor performance and adjust parameters.
Threats Mitigated:
- Uncontrolled Resource Consumption (DoS): (Severity: High)
- Application Crashes: (Severity: High)
- Performance Degradation: (Severity: Medium)
Impact:
- Uncontrolled Resource Consumption (DoS): Risk significantly reduced.
- Application Crashes: Risk significantly reduced.
- Performance Degradation: Risk significantly reduced.
Currently Implemented: [Example: NetworkDataFetcher.kt
(onBackpressureBuffer), UserActivityStream.kt
(onBackpressureLatest)]
Missing Implementation: [Example: LogFileReader.kt
(needs onBackpressureBuffer or Flowable)]
Mitigation Strategy: Timeout and Retry with Limits (Using RxKotlin Operators)
Description:
- Identify External Interactions: Find Observables interacting with external services.
- Apply
timeout
: Add the RxKotlintimeout
operator, specifying a time limit. - Apply
retry
(with Limits): Add the RxKotlinretry
operator. Limit retries:retry(maxRetries)
orretryWhen
(with backoff). - Error Handling: Ensure the
onError
handler manages timeout/retry failures.
Threats Mitigated:
- Uncontrolled Resource Consumption (DoS): (Severity: High)
- Application Hangs: (Severity: High)
- Resource Leaks: (Severity: Medium)
- Infinite Retry Loops: (Severity: Medium)
Impact:
- Uncontrolled Resource Consumption (DoS): Risk significantly reduced.
- Application Hangs: Risk significantly reduced.
- Resource Leaks: Risk reduced.
- Infinite Retry Loops: Risk eliminated.
Currently Implemented: [Example: ApiService.kt
(timeout: 5s, retries: 3)]
Missing Implementation: [Example: DatabaseQueryExecutor.kt
(needs timeout and retry)]
Mitigation Strategy: Rate Limiting (Using RxKotlin Operators)
Description:
- Identify High-Frequency Events: Find Observables with high event frequency.
- Choose a Rate Limiting Operator: Select the appropriate RxKotlin operator:
throttleFirst
: First item in a time window.throttleLast
: Last item in a time window.debounce
: Emits after inactivity.sample
: Most recent item in a time window.
- Apply the Operator: Insert the operator into the chain (near the source).
- Tune the Time Window: Adjust the time window parameter.
Threats Mitigated:
- Uncontrolled Resource Consumption (DoS): (Severity: High)
- Performance Degradation: (Severity: Medium)
- External Service Overload: (Severity: Medium)
Impact:
- Uncontrolled Resource Consumption (DoS): Risk significantly reduced.
- Performance Degradation: Risk significantly reduced.
- External Service Overload: Risk reduced.
Currently Implemented: [Example: SearchSuggestionsProvider.kt
(debounce)]
Missing Implementation: [Example: SensorDataStream.kt
(needs throttleFirst or sample)]
Mitigation Strategy: Resource Management with using
Description:
- Identify Resource Acquisition: Find Observables acquiring resources.
- Use the
using
Operator: Wrap resource acquisition and Observable creation in RxKotlin'susing
operator:- Resource Factory: Creates the resource.
- Observable Factory: Takes the resource, returns an Observable.
- Resource Disposer: Releases the resource.
- Ensure Proper Disposal: The
Resource Disposer
must reliably release the resource.
Threats Mitigated:
- Resource Leaks: (Severity: Medium)
- Uncontrolled Resource Consumption (DoS): (Severity: Medium)
- Application Instability: (Severity: Medium)
Impact:
- Resource Leaks: Risk significantly reduced.
- Uncontrolled Resource Consumption (DoS): Risk indirectly reduced.
- Application Instability: Risk reduced.
Currently Implemented: [Example: DatabaseConnectionManager.kt
]
Missing Implementation: [Example: FileDownloader.kt
]
Mitigation Strategy: Careful Subscription Management
Description:
- Identify Subscriptions: Find all Observable subscriptions.
- Dispose Subscriptions: Ensure every
Disposable
is disposed of when no longer needed (e.g.,onDestroy
). - Use
CompositeDisposable
: Manage multiple subscriptions withCompositeDisposable
; calldispose()
on it. - Avoid Long-Lived Subscriptions: Be cautious with long-lived subscriptions in short-lived components.
Threats Mitigated:
- Memory Leaks: (Severity: Medium)
- Unexpected Behavior: (Severity: Medium)
- Resource Leaks: (Severity: Medium)
Impact:
- Memory Leaks: Risk significantly reduced.
- Unexpected Behavior: Risk significantly reduced.
- Resource Leaks: Risk indirectly reduced.
Currently Implemented: [Example: Android Activities/Fragments (CompositeDisposable, onDestroy)]
Missing Implementation: [Example: Some background services]
Mitigation Strategy: Error Handling (Using RxKotlin Operators and onError
)
Description:
- Identify Observable Chains: Examine all Observable chains.
- Implement
onError
Handlers: Every subscription must have anonError
handler:- Log the error (without sensitive data).
- Attempt recovery (if possible).
- Inform the user appropriately.
- Use Error Handling Operators: Consider RxKotlin operators:
onErrorResumeNext
(fallback Observable),onErrorReturnItem
(default value). Choose carefully. - Centralized Error Handling (Optional): Consider a centralized mechanism.
Threats Mitigated:
- Application Crashes: (Severity: High)
- Unexpected Behavior: (Severity: Medium)
- Data Loss/Corruption: (Severity: High)
- Information Disclosure: (Severity: Medium)
Impact:
- Application Crashes: Risk significantly reduced.
- Unexpected Behavior: Risk significantly reduced.
- Data Loss/Corruption: Risk reduced.
- Information Disclosure: Risk reduced.
Currently Implemented: [Example: Partially implemented; some onError handlers missing/incomplete]
Missing Implementation: [Example: LegacyDataProcessor.kt
]
Mitigation Strategy: Cross-Thread Data Races (Using observeOn
and subscribeOn
)
Description:
- Identify Shared Mutable State: Find mutable data accessed by multiple Observables/threads.
- Prefer Immutability: Refactor to use immutable data structures (if possible).
- Use
observeOn
andsubscribeOn
: Explicitly specify threads with RxKotlin'sobserveOn
(downstream) andsubscribeOn
(Observable's work). - Synchronization (If Necessary): If mutable state is required, use synchronization (e.g.,
synchronized
,AtomicReference
). - Thread Confinement: Consider confining mutable state to a single thread.
Threats Mitigated:
- Data Races: (Severity: High)
- Application Crashes: (Severity: High)
- Unexpected Behavior: (Severity: Medium)
Impact:
- Data Races: Risk significantly reduced (or eliminated).
- Application Crashes: Risk reduced.
- Unexpected Behavior: Risk significantly reduced.
Currently Implemented: [Example: Partially; observeOn/subscribeOn used inconsistently]
Missing Implementation: [Example: SharedDataCache.kt
(missing synchronization)]
Mitigation Strategy: Side-Effect Management (Using doOn...
operators)
Description:
- Identify Side Effects: Identify all side effects within Observable chains.
- Use
doOn...
Operators Carefully: Use RxKotlin operators likedoOnNext
,doOnError
, anddoOnComplete
for side effects, but minimize their complexity. - Isolate Side Effects: If possible, isolate side effects to the subscriber.
- Consider
using
: Useusing
for resource management. - Document Side-Effects: Clearly document any side-effects.
Threats Mitigated: * Unexpected Behavior: (Severity: Medium) * Data Races (Indirectly): (Severity: Medium) * Testing Difficulties: (Severity: Low)
Impact: * Unexpected Behavior: Risk reduced. * Data Races (Indirectly): Risk reduced. * Testing Difficulties: Risk reduced.
Currently Implemented: [Example: Partially; doOnNext for logging, but other side effects scattered]
Missing Implementation: [Example: DataUpdater.kt
(side effects in map)]