Skip to content

Commit

Permalink
Remove DisallowedUriList and AllowedUriList in favour of using UriLis…
Browse files Browse the repository at this point in the history
…t for both allowing and disallowing URIs
  • Loading branch information
drathar committed Oct 2, 2024
1 parent b9ada68 commit ac85e3c
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 169 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@

import java.util.Arrays;

import dev.gerlot.securewebview.uri.AllowedUriList;
import dev.gerlot.securewebview.uri.AuthorityAndPathMatcher;
import dev.gerlot.securewebview.uri.AuthorityContainmentMatcher;
import dev.gerlot.securewebview.uri.DisallowedUriList;
import dev.gerlot.securewebview.uri.UriList;
import dev.gerlot.securewebview.uri.UriMatcher;
import dev.gerlot.securewebview.uri.Uris;

Expand All @@ -37,19 +36,19 @@ public class SecureWebView extends FrameLayout {
private boolean alwaysOpenPagesInWebView = false;
private boolean allowFileAccess = false;

private AllowedUriList allowedUriList = null;
private UriList allowedUriList = null;

private DisallowedUriList disallowedUriList = null;
private UriList disallowedUriList = null;

public void setAlwaysOpenPagesInWebView(boolean alwaysOpenPagesInWebView) {
this.alwaysOpenPagesInWebView = alwaysOpenPagesInWebView;
}

public void setAllowedUriList(AllowedUriList allowedUriList) {
public void setAllowedUriList(UriList allowedUriList) {
this.allowedUriList = allowedUriList;
}

public void setDisallowedUriList(DisallowedUriList disallowedUriList) {
public void setDisallowedUriList(UriList disallowedUriList) {
this.disallowedUriList = disallowedUriList;
}

Expand Down Expand Up @@ -172,7 +171,7 @@ private void init(final Context context) {
this.webView.getSettings().setAllowContentAccess(false);
this.webView.setWebViewClient(new SecureWebViewClient());

this.disallowedUriList = new DisallowedUriList();
this.disallowedUriList = new UriList();
addPopularSearchEnginesToDisallowedUriList();
addAiChatBotsToDisallowedUriList();
}
Expand Down Expand Up @@ -203,7 +202,7 @@ private boolean shouldBlockRequest(final Uri uri, boolean allowDataUri, boolean
return true;
}

return disallowedUriList.matches(uri);
return disallowedUriList != null && disallowedUriList.matches(uri);
}

public void setWebViewClient(WebViewClient client) {
Expand Down

This file was deleted.

This file was deleted.

29 changes: 25 additions & 4 deletions library/src/main/java/dev/gerlot/securewebview/uri/UriList.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
package dev.gerlot.securewebview.uri;

import android.net.Uri;

import androidx.annotation.NonNull;

import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

public abstract class UriList extends AbstractList<UriMatcher> {
/**
* A list of URIs which can be matched against certain matchers to allow or disallow opening them
* in a SecureWebView.
*/
public class UriList extends AbstractList<UriMatcher> implements UriMatcher {
protected final List<UriMatcher> mUriMatchers;

protected UriList() {
public UriList() {
mUriMatchers = new ArrayList<>();
}

protected UriList(UriMatcher... matchers) {
public UriList(UriMatcher... matchers) {
mUriMatchers = Arrays.asList(matchers);
}

protected UriList(Collection<UriMatcher> matchers) {
public UriList(Collection<UriMatcher> matchers) {
mUriMatchers = new ArrayList<>(matchers);
}

Expand Down Expand Up @@ -45,4 +53,17 @@ public void add(int index, UriMatcher element) {
public UriMatcher remove(int index) {
return mUriMatchers.remove(index);
}

@Override
public boolean matches(@NonNull Uri uri) {
boolean hasAnyMatches = false;

for (UriMatcher matcher : mUriMatchers) {
if (matcher.matches(uri)) {
hasAnyMatches = true;
}
}

return hasAnyMatches;
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package dev.gerlot.securewebview.uri;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import android.net.Uri;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;

import java.util.List;
import java.util.Objects;

@RunWith(RobolectricTestRunner.class)
public class UriListTest {

@Test
public void testMatches_emptyUriList() {
UriList uriList = new UriList();
assertFalse(uriList.matches(new Uri.Builder().authority("google.com").build()));
assertFalse(uriList.matches(new Uri.Builder().authority("bing.com").appendPath("search").build()));
}

@Test
public void testMatches_GoogleOnly() {
UriList uriList = new UriList(uri -> Objects.equals(uri.getAuthority(), "google.com"));
assertTrue(uriList.matches(new Uri.Builder().authority("google.com").build()));
assertFalse(uriList.matches(new Uri.Builder().authority("bing.com").appendPath("search").build()));
}

@Test
public void testMatches_GoogleAndBing() {
UriList uriList = new UriList(List.of(uri -> Objects.equals(uri.getAuthority(), "google.com"), uri -> Objects.equals(uri.getAuthority(), "bing.com")));
assertTrue(uriList.matches(new Uri.Builder().authority("google.com").build()));
assertTrue(uriList.matches(new Uri.Builder().authority("bing.com").appendPath("search").build()));
assertFalse(uriList.matches(new Uri.Builder().authority("duckduckgo.com").appendPath("search").build()));
}

}

0 comments on commit ac85e3c

Please sign in to comment.