-
-
Notifications
You must be signed in to change notification settings - Fork 360
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
Draft
antonis
wants to merge
3
commits into
main
Choose a base branch
from
fix/native-getdatafromuri-restriction
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.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
163 changes: 163 additions & 0 deletions
163
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,163 @@ | ||
| 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 allowsSafDocumentsAuthorities() { | ||
| assertTrue( | ||
| RNSentryModuleImpl.isAllowedUri( | ||
| mockUri("content", "com.android.providers.media.documents", "/doc"), ctx)); | ||
| assertTrue( | ||
| RNSentryModuleImpl.isAllowedUri( | ||
| mockUri("content", "com.android.externalstorage.documents", "/doc"), ctx)); | ||
| assertTrue( | ||
| 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No unit tests for iOS path validation function
The
RNSentryIsPathUnderAllowedRootsfunction implements security-critical path validation logic but has no corresponding unit tests, unlike the Android implementation which has comprehensive tests inRNSentryUriValidationTest.java. This validation function handles path traversal prevention and symlink resolution - edge cases that should be tested to ensure bypass attempts are properly blocked.Verification
Searched for iOS test files using patterns Test, Tests, Spec in packages/core/ios and found none. Searched for references to RNSentryIsPathUnderAllowedRoots and found only the implementation in RNSentry.mm. Confirmed Android has comprehensive tests in packages/core/android/src/test/java/io/sentry/react/RNSentryUriValidationTest.java covering traversal escape, prefix collision, and directory boundary cases.
Identified by Warden
code-review·6RE-5C3