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

Allowing/disallowing subdomains #260

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,13 @@ If you want to serve sites or just single resources over plain `http` instead of

}
```

* Stricter whitelist matching (allow only specified domains, without subdomains):

```java
mWebView.addPermittedHostname("example.org"); // will match example.org, www.example.org, example2.example.org, etc.
mWebView.setAllowSubdomains(false); // will only match example.org
```

## Contributing

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public interface Listener {
protected WeakReference<Activity> mActivity;
protected WeakReference<Fragment> mFragment;
protected Listener mListener;
protected boolean mAllowSubdomains = true;
protected final List<String> mPermittedHostnames = new LinkedList<String>();
/** File upload callback for platform versions prior to Android 5.0 */
protected ValueCallback<Uri> mFileUploadCallbackFirst;
Expand Down Expand Up @@ -340,6 +341,14 @@ public void addHttpHeader(final String name, final String value) {
public void removeHttpHeader(final String name) {
mHttpHeaders.remove(name);
}

public void setAllowSubdomains(boolean b){
mAllowSubdomains = b;
}

public boolean getAllowSubdomains(){
return mAllowSubdomains;
}

public void addPermittedHostname(String hostname) {
mPermittedHostnames.add(hostname);
Expand Down Expand Up @@ -503,7 +512,7 @@ public void onReceivedError(WebView view, int errorCode, String description, Str

@Override
public boolean shouldOverrideUrlLoading(final WebView view, final String url) {
if (!isPermittedUrl(url)) {
if (!isPermittedUrl(url, mAllowSubdomains)) {
// if a listener is available
if (mListener != null) {
// inform the listener about the request
Expand Down Expand Up @@ -1106,8 +1115,12 @@ protected static String makeUrlUnique(final String url) {

return unique.toString();
}

public boolean isPermittedUrl(final String url) {
isPermittedUrl(url, true)
}

public boolean isPermittedUrl(final String url, boolean allowSubdomains) {
// if the permitted hostnames have not been restricted to a specific set
if (mPermittedHostnames.size() == 0) {
// all hostnames are allowed
Expand Down Expand Up @@ -1142,7 +1155,7 @@ public boolean isPermittedUrl(final String url) {
// for every hostname in the set of permitted hosts
for (String expectedHost : mPermittedHostnames) {
// if the two hostnames match or if the actual host is a subdomain of the expected host
if (actualHost.equals(expectedHost) || actualHost.endsWith("." + expectedHost)) {
if (actualHost.equals(expectedHost) || (allowSubdomains && actualHost.endsWith("." + expectedHost))) {
// the actual hostname of the URL to be checked is allowed
return true;
}
Expand Down