-
-
Notifications
You must be signed in to change notification settings - Fork 359
fix(core): Restrict getDataFromUri native bridge methods #6045
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
69a3bc8
fix(core): Restrict getDataFromUri native bridge methods
antonis b51165d
docs(changelog): Link getDataFromUri entries to PR #6045
antonis 3c6721e
style(android): Apply google-java-format to test file
antonis b207edb
Merge remote-tracking branch 'origin/main' into fix/native-getdatafro…
antonis 8604b04
style(android): Satisfy PMD SimplifyBooleanReturns
antonis c84887b
style(ios): Apply clang-format to RNSentry.mm
antonis 934ee43
docs(changelog): Merge getDataFromUri entries into one
antonis 5764f31
Merge branch 'main' into fix/native-getdatafromuri-restriction
antonis ca3fd19
Merge remote-tracking branch 'origin/main' into fix/native-getdatafro…
antonis 7f971e5
fix(android): Drop SAF document authorities from getDataFromUri allow…
antonis 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
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
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
165 changes: 165 additions & 0 deletions
165
packages/core/android/src/test/java/io/sentry/react/RNSentryUriValidationTest.java
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,165 @@ | ||
| package io.sentry.react; | ||
|
|
||
| import static org.junit.Assert.assertFalse; | ||
| import static org.junit.Assert.assertTrue; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import android.content.Context; | ||
| import android.net.Uri; | ||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import org.junit.Before; | ||
| import org.junit.Rule; | ||
| import org.junit.Test; | ||
| import org.junit.rules.TemporaryFolder; | ||
|
|
||
| public class RNSentryUriValidationTest { | ||
|
|
||
| @Rule public TemporaryFolder tempFolder = new TemporaryFolder(); | ||
|
|
||
| private Context ctx; | ||
| private File filesDir; | ||
| private File cacheDir; | ||
|
|
||
| @Before | ||
| public void setUp() throws IOException { | ||
| filesDir = tempFolder.newFolder("files"); | ||
| cacheDir = tempFolder.newFolder("cache"); | ||
|
|
||
| ctx = mock(Context.class); | ||
| when(ctx.getFilesDir()).thenReturn(filesDir); | ||
| when(ctx.getCacheDir()).thenReturn(cacheDir); | ||
| when(ctx.getExternalFilesDir(null)).thenReturn(null); | ||
| when(ctx.getExternalCacheDir()).thenReturn(null); | ||
| when(ctx.getPackageName()).thenReturn("com.example.app"); | ||
| } | ||
|
|
||
| private static Uri mockUri(String scheme, String authority, String path) { | ||
| Uri u = mock(Uri.class); | ||
| when(u.getScheme()).thenReturn(scheme); | ||
| when(u.getAuthority()).thenReturn(authority); | ||
| when(u.getPath()).thenReturn(path); | ||
| return u; | ||
| } | ||
|
|
||
| @Test | ||
| public void rejectsNullScheme() { | ||
| assertFalse(RNSentryModuleImpl.isAllowedUri(mockUri(null, null, null), ctx)); | ||
| } | ||
|
|
||
| @Test | ||
| public void rejectsUnknownScheme() { | ||
| assertFalse(RNSentryModuleImpl.isAllowedUri(mockUri("http", "example.com", "/foo"), ctx)); | ||
| } | ||
|
|
||
| @Test | ||
| public void rejectsAndroidResourceScheme() { | ||
| assertFalse(RNSentryModuleImpl.isAllowedUri(mockUri("android.resource", "pkg", "/1"), ctx)); | ||
| } | ||
|
|
||
| @Test | ||
| public void allowsContentMediaAuthority() { | ||
| assertTrue( | ||
| RNSentryModuleImpl.isAllowedUri( | ||
| mockUri("content", "media", "/external/images/media/42"), ctx)); | ||
| } | ||
|
|
||
| @Test | ||
| public void allowsContentSchemeCaseInsensitive() { | ||
| assertTrue(RNSentryModuleImpl.isAllowedUri(mockUri("CONTENT", "Media", "/anything"), ctx)); | ||
| } | ||
|
|
||
| @Test | ||
| public void rejectsSafDocumentsAuthorities() { | ||
| // SAF authorities are excluded from the allowlist: authority-only matching cannot verify | ||
| // that the URI was granted in the current attach flow versus a previously persisted grant. | ||
| assertFalse( | ||
| RNSentryModuleImpl.isAllowedUri( | ||
| mockUri("content", "com.android.providers.media.documents", "/doc"), ctx)); | ||
| assertFalse( | ||
| RNSentryModuleImpl.isAllowedUri( | ||
| mockUri("content", "com.android.externalstorage.documents", "/doc"), ctx)); | ||
| assertFalse( | ||
| RNSentryModuleImpl.isAllowedUri( | ||
| mockUri("content", "com.android.providers.downloads.documents", "/doc"), ctx)); | ||
| } | ||
|
|
||
| @Test | ||
| public void allowsAppOwnFileProviderAuthority() { | ||
| assertTrue( | ||
| RNSentryModuleImpl.isAllowedUri( | ||
| mockUri("content", "com.example.app.fileprovider", "/shared"), ctx)); | ||
| } | ||
|
|
||
| @Test | ||
| public void rejectsForeignContentAuthority() { | ||
| assertFalse( | ||
| RNSentryModuleImpl.isAllowedUri( | ||
| mockUri("content", "com.attacker.evil.provider", "/evil"), ctx)); | ||
| } | ||
|
|
||
| @Test | ||
| public void rejectsNullAuthorityOnContentScheme() { | ||
| assertFalse(RNSentryModuleImpl.isAllowedUri(mockUri("content", null, "/x"), ctx)); | ||
| } | ||
|
|
||
| @Test | ||
| public void rejectsFileSchemeWithNullContext() { | ||
| assertFalse(RNSentryModuleImpl.isAllowedUri(mockUri("file", null, "/tmp/x"), null)); | ||
| } | ||
|
|
||
| @Test | ||
| public void rejectsFileOutsideAppDirs() { | ||
| assertFalse(RNSentryModuleImpl.isAllowedUri(mockUri("file", null, "/etc/passwd"), ctx)); | ||
| } | ||
|
|
||
| @Test | ||
| public void rejectsFileTargetingInternalDatabases() { | ||
| assertFalse( | ||
| RNSentryModuleImpl.isAllowedUri( | ||
| mockUri("file", null, "/data/data/com.example.app/databases/app.db"), ctx)); | ||
| } | ||
|
|
||
| @Test | ||
| public void allowsFileInsideFilesDir() throws IOException { | ||
| File target = new File(filesDir, "picked.jpg"); | ||
| target.createNewFile(); | ||
| assertTrue( | ||
| RNSentryModuleImpl.isAllowedUri(mockUri("file", null, target.getAbsolutePath()), ctx)); | ||
| } | ||
|
|
||
| @Test | ||
| public void allowsFileInsideCacheDir() throws IOException { | ||
| File target = new File(cacheDir, "thumb.png"); | ||
| target.createNewFile(); | ||
| assertTrue( | ||
| RNSentryModuleImpl.isAllowedUri(mockUri("file", null, target.getAbsolutePath()), ctx)); | ||
| } | ||
|
|
||
| @Test | ||
| public void rejectsFileWithTraversalEscape() throws IOException { | ||
| File outside = new File(tempFolder.getRoot(), "outside.txt"); | ||
| outside.createNewFile(); | ||
| String traversal = filesDir.getAbsolutePath() + "/../outside.txt"; | ||
| assertFalse(RNSentryModuleImpl.isAllowedUri(mockUri("file", null, traversal), ctx)); | ||
| } | ||
|
|
||
| @Test | ||
| public void rejectsPrefixCollision() throws IOException { | ||
| // If filesDir is <root>/files, a path <root>/filesEvil/victim must not match as inside | ||
| // filesDir. | ||
| File sibling = new File(tempFolder.getRoot(), "filesEvil"); | ||
| assertTrue(sibling.mkdirs()); | ||
| File victim = new File(sibling, "victim.txt"); | ||
| victim.createNewFile(); | ||
| assertFalse( | ||
| RNSentryModuleImpl.isAllowedUri(mockUri("file", null, victim.getAbsolutePath()), ctx)); | ||
| } | ||
|
|
||
| @Test | ||
| public void rejectsEmptyFilePath() { | ||
| assertFalse(RNSentryModuleImpl.isAllowedUri(mockUri("file", null, ""), ctx)); | ||
| assertFalse(RNSentryModuleImpl.isAllowedUri(mockUri("file", null, null), ctx)); | ||
| } | ||
| } |
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
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.