Skip to content
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

RUM-5750 Use NO_EXPORT_FLAG for BroadcastReceiver on API above 26 #2170

Conversation

mariusc83
Copy link
Member

@mariusc83 mariusc83 commented Aug 7, 2024

What does this PR do?

This issue was reported by a customer. Their security check tool detected this issue in our SDK where we do not register a receiver with NO_EXPORT_FLAG in API 33 to 26. This method actually exists also below API 33 up to API 26 but the flag Context.NO_EXPORT_FLAG was not available so that's the reason we skipped it.

I chose not to use the ContextCompat.NO_EXPORT_FLAG just to not have to introduce this new dependency just for this case and added a 0x4 local flag myself (same value as the one in Context and ContextCompat). The worse thing it could happen is that this flag to change later but I checked and the SDK is not crashing it is just ignoring it. On the other side if the flag value (logic will change under the hood) we might end up with a different access rights for this receiver exposing again a security issue. I think this is highly unlikely but reviewers please have this in mind.

Motivation

What inspired you to submit this pull request?

Additional Notes

Anything else we should know when reviewing?

Review checklist (to be filled by reviewers)

  • Feature or bugfix MUST have appropriate tests (unit, integration, e2e)
  • Make sure you discussed the feature or bugfix with the maintaining team in an Issue
  • Make sure each commit and the PR mention the Issue number (cf the CONTRIBUTING doc)

@mariusc83 mariusc83 self-assigned this Aug 7, 2024
@mariusc83 mariusc83 force-pushed the mconstantin/rum-5750/handle-security-issue-on-broadcast-receivers branch from 1f74d01 to 1bac553 Compare August 7, 2024 09:44
fun registerReceiver(
context: Context,
filter: IntentFilter
): Intent? {
val intent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
context.registerReceiver(this, filter, Context.RECEIVER_NOT_EXPORTED)
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Copy link
Member Author

@mariusc83 mariusc83 Aug 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Normally we should use ContextCompat here:
https://developer.android.com/reference/androidx/core/content/ContextCompat#RECEIVER_NOT_EXPORTED()
but not sure if we should bring the ContextCompat dependency only for this reason. The drawback on this approach would be in case that Flag value will be changed in the future (quite unlikely) . he worse thing it could happen is that this flag to change later but I checked and the SDK is not crashing it is just ignoring it. On the other side if the flag value (logic will change under the hood) we might end up with a different access rights for this receiver exposing again a security issue. I think this is highly unlikely but reviewers please have this in mind.

@codecov-commenter
Copy link

codecov-commenter commented Aug 7, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 70.02%. Comparing base (5536f75) to head (6232b7d).
Report is 40 commits behind head on develop.

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #2170   +/-   ##
========================================
  Coverage    70.02%   70.02%           
========================================
  Files          726      726           
  Lines        27001    26992    -9     
  Branches      4525     4524    -1     
========================================
- Hits         18906    18900    -6     
- Misses        6819     6828    +9     
+ Partials      1276     1264   -12     
Files Coverage Δ
...droid/core/internal/receiver/ThreadSafeReceiver.kt 100.00% <100.00%> (ø)

... and 29 files with indirect coverage changes

@mariusc83 mariusc83 marked this pull request as ready for review August 7, 2024 11:18
@mariusc83 mariusc83 requested review from a team as code owners August 7, 2024 11:18
@@ -18,13 +18,15 @@ internal abstract class ThreadSafeReceiver : BroadcastReceiver() {

val isRegistered = AtomicBoolean(false)

@SuppressLint("UnspecifiedRegisterReceiverFlag")
@SuppressLint("WrongConstant", "UnspecifiedRegisterReceiverFlag")
Copy link
Member

@jonathanmos jonathanmos Aug 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Following on from the dev chat, if we have to suppress perhaps we should document the reason in a comment

testedReceiver = TestableThreadSafeReceiver()
}

// r
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this comment looks like it got redacted

@mariusc83 mariusc83 force-pushed the mconstantin/rum-5750/handle-security-issue-on-broadcast-receivers branch from 1bac553 to 6232b7d Compare August 12, 2024 07:21
mockIntentFilter,
ThreadSafeReceiver.RECEIVER_NOT_EXPORTED_COMPAT
)
assertThat(this.testedReceiver.isRegistered.get()).isTrue()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor: this can be removed. Same for other similar lines in this file.

Suggested change
assertThat(this.testedReceiver.isRegistered.get()).isTrue()
assertThat(testedReceiver.isRegistered.get()).isTrue()

@mariusc83 mariusc83 merged commit 2f2ab8b into develop Aug 12, 2024
22 checks passed
@mariusc83 mariusc83 deleted the mconstantin/rum-5750/handle-security-issue-on-broadcast-receivers branch August 12, 2024 12:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants