|
| 1 | +package io.sentry.react; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertFalse; |
| 4 | +import static org.junit.Assert.assertTrue; |
| 5 | +import static org.mockito.Mockito.mock; |
| 6 | +import static org.mockito.Mockito.when; |
| 7 | + |
| 8 | +import android.content.Context; |
| 9 | +import android.net.Uri; |
| 10 | +import java.io.File; |
| 11 | +import java.io.IOException; |
| 12 | +import org.junit.Before; |
| 13 | +import org.junit.Rule; |
| 14 | +import org.junit.Test; |
| 15 | +import org.junit.rules.TemporaryFolder; |
| 16 | + |
| 17 | +public class RNSentryUriValidationTest { |
| 18 | + |
| 19 | + @Rule public TemporaryFolder tempFolder = new TemporaryFolder(); |
| 20 | + |
| 21 | + private Context ctx; |
| 22 | + private File filesDir; |
| 23 | + private File cacheDir; |
| 24 | + |
| 25 | + @Before |
| 26 | + public void setUp() throws IOException { |
| 27 | + filesDir = tempFolder.newFolder("files"); |
| 28 | + cacheDir = tempFolder.newFolder("cache"); |
| 29 | + |
| 30 | + ctx = mock(Context.class); |
| 31 | + when(ctx.getFilesDir()).thenReturn(filesDir); |
| 32 | + when(ctx.getCacheDir()).thenReturn(cacheDir); |
| 33 | + when(ctx.getExternalFilesDir(null)).thenReturn(null); |
| 34 | + when(ctx.getExternalCacheDir()).thenReturn(null); |
| 35 | + when(ctx.getPackageName()).thenReturn("com.example.app"); |
| 36 | + } |
| 37 | + |
| 38 | + private static Uri mockUri(String scheme, String authority, String path) { |
| 39 | + Uri u = mock(Uri.class); |
| 40 | + when(u.getScheme()).thenReturn(scheme); |
| 41 | + when(u.getAuthority()).thenReturn(authority); |
| 42 | + when(u.getPath()).thenReturn(path); |
| 43 | + return u; |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void rejectsNullScheme() { |
| 48 | + assertFalse(RNSentryModuleImpl.isAllowedUri(mockUri(null, null, null), ctx)); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void rejectsUnknownScheme() { |
| 53 | + assertFalse(RNSentryModuleImpl.isAllowedUri(mockUri("http", "example.com", "/foo"), ctx)); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void rejectsAndroidResourceScheme() { |
| 58 | + assertFalse(RNSentryModuleImpl.isAllowedUri(mockUri("android.resource", "pkg", "/1"), ctx)); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void allowsContentMediaAuthority() { |
| 63 | + assertTrue( |
| 64 | + RNSentryModuleImpl.isAllowedUri( |
| 65 | + mockUri("content", "media", "/external/images/media/42"), ctx)); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void allowsContentSchemeCaseInsensitive() { |
| 70 | + assertTrue(RNSentryModuleImpl.isAllowedUri(mockUri("CONTENT", "Media", "/anything"), ctx)); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void rejectsSafDocumentsAuthorities() { |
| 75 | + // SAF authorities are excluded from the allowlist: authority-only matching cannot verify |
| 76 | + // that the URI was granted in the current attach flow versus a previously persisted grant. |
| 77 | + assertFalse( |
| 78 | + RNSentryModuleImpl.isAllowedUri( |
| 79 | + mockUri("content", "com.android.providers.media.documents", "/doc"), ctx)); |
| 80 | + assertFalse( |
| 81 | + RNSentryModuleImpl.isAllowedUri( |
| 82 | + mockUri("content", "com.android.externalstorage.documents", "/doc"), ctx)); |
| 83 | + assertFalse( |
| 84 | + RNSentryModuleImpl.isAllowedUri( |
| 85 | + mockUri("content", "com.android.providers.downloads.documents", "/doc"), ctx)); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void allowsAppOwnFileProviderAuthority() { |
| 90 | + assertTrue( |
| 91 | + RNSentryModuleImpl.isAllowedUri( |
| 92 | + mockUri("content", "com.example.app.fileprovider", "/shared"), ctx)); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void rejectsForeignContentAuthority() { |
| 97 | + assertFalse( |
| 98 | + RNSentryModuleImpl.isAllowedUri( |
| 99 | + mockUri("content", "com.attacker.evil.provider", "/evil"), ctx)); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void rejectsNullAuthorityOnContentScheme() { |
| 104 | + assertFalse(RNSentryModuleImpl.isAllowedUri(mockUri("content", null, "/x"), ctx)); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + public void rejectsFileSchemeWithNullContext() { |
| 109 | + assertFalse(RNSentryModuleImpl.isAllowedUri(mockUri("file", null, "/tmp/x"), null)); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + public void rejectsFileOutsideAppDirs() { |
| 114 | + assertFalse(RNSentryModuleImpl.isAllowedUri(mockUri("file", null, "/etc/passwd"), ctx)); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + public void rejectsFileTargetingInternalDatabases() { |
| 119 | + assertFalse( |
| 120 | + RNSentryModuleImpl.isAllowedUri( |
| 121 | + mockUri("file", null, "/data/data/com.example.app/databases/app.db"), ctx)); |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + public void allowsFileInsideFilesDir() throws IOException { |
| 126 | + File target = new File(filesDir, "picked.jpg"); |
| 127 | + target.createNewFile(); |
| 128 | + assertTrue( |
| 129 | + RNSentryModuleImpl.isAllowedUri(mockUri("file", null, target.getAbsolutePath()), ctx)); |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + public void allowsFileInsideCacheDir() throws IOException { |
| 134 | + File target = new File(cacheDir, "thumb.png"); |
| 135 | + target.createNewFile(); |
| 136 | + assertTrue( |
| 137 | + RNSentryModuleImpl.isAllowedUri(mockUri("file", null, target.getAbsolutePath()), ctx)); |
| 138 | + } |
| 139 | + |
| 140 | + @Test |
| 141 | + public void rejectsFileWithTraversalEscape() throws IOException { |
| 142 | + File outside = new File(tempFolder.getRoot(), "outside.txt"); |
| 143 | + outside.createNewFile(); |
| 144 | + String traversal = filesDir.getAbsolutePath() + "/../outside.txt"; |
| 145 | + assertFalse(RNSentryModuleImpl.isAllowedUri(mockUri("file", null, traversal), ctx)); |
| 146 | + } |
| 147 | + |
| 148 | + @Test |
| 149 | + public void rejectsPrefixCollision() throws IOException { |
| 150 | + // If filesDir is <root>/files, a path <root>/filesEvil/victim must not match as inside |
| 151 | + // filesDir. |
| 152 | + File sibling = new File(tempFolder.getRoot(), "filesEvil"); |
| 153 | + assertTrue(sibling.mkdirs()); |
| 154 | + File victim = new File(sibling, "victim.txt"); |
| 155 | + victim.createNewFile(); |
| 156 | + assertFalse( |
| 157 | + RNSentryModuleImpl.isAllowedUri(mockUri("file", null, victim.getAbsolutePath()), ctx)); |
| 158 | + } |
| 159 | + |
| 160 | + @Test |
| 161 | + public void rejectsEmptyFilePath() { |
| 162 | + assertFalse(RNSentryModuleImpl.isAllowedUri(mockUri("file", null, ""), ctx)); |
| 163 | + assertFalse(RNSentryModuleImpl.isAllowedUri(mockUri("file", null, null), ctx)); |
| 164 | + } |
| 165 | +} |
0 commit comments