-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(allow-list)!: integrate and refactor core plugin (#1138)
* refactor!: naming to allow list * feat(allow-list): integrate core plugin * refactor(allow-list): cleanup * refactor: drop deprecated launch-external case for CustomConfigParser::handleStartTag * fix: apply review comments
- Loading branch information
Showing
14 changed files
with
279 additions
and
29 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,157 @@ | ||
/* | ||
Licensed to the Apache Software Foundation (ASF) under one | ||
or more contributor license agreements. See the NOTICE file | ||
distributed with this work for additional information | ||
regarding copyright ownership. The ASF licenses this file | ||
to you under the Apache License, Version 2.0 (the | ||
"License"); you may not use this file except in compliance | ||
with the License. You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, | ||
software distributed under the License is distributed on an | ||
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
KIND, either express or implied. See the License for the | ||
specific language governing permissions and limitations | ||
under the License. | ||
*/ | ||
|
||
package org.apache.cordova; | ||
|
||
import org.apache.cordova.CordovaPlugin; | ||
import org.apache.cordova.ConfigXmlParser; | ||
import org.apache.cordova.LOG; | ||
import org.apache.cordova.AllowList; | ||
import org.xmlpull.v1.XmlPullParser; | ||
|
||
import android.content.Context; | ||
|
||
public class AllowListPlugin extends CordovaPlugin { | ||
public static final String PLUGIN_NAME = "CordovaAllowListPlugin"; | ||
protected static final String LOG_TAG = "CordovaAllowListPlugin"; | ||
|
||
private AllowList allowedNavigations; | ||
private AllowList allowedIntents; | ||
private AllowList allowedRequests; | ||
|
||
// Used when instantiated via reflection by PluginManager | ||
public AllowListPlugin() { } | ||
|
||
// These can be used by embedders to allow Java-configuration of an allow list. | ||
public AllowListPlugin(Context context) { | ||
this(new AllowList(), new AllowList(), null); | ||
new CustomConfigXmlParser().parse(context); | ||
} | ||
|
||
public AllowListPlugin(XmlPullParser xmlParser) { | ||
this(new AllowList(), new AllowList(), null); | ||
new CustomConfigXmlParser().parse(xmlParser); | ||
} | ||
|
||
public AllowListPlugin(AllowList allowedNavigations, AllowList allowedIntents, AllowList allowedRequests) { | ||
if (allowedRequests == null) { | ||
allowedRequests = new AllowList(); | ||
allowedRequests.addAllowListEntry("file:///*", false); | ||
allowedRequests.addAllowListEntry("data:*", false); | ||
} | ||
|
||
this.allowedNavigations = allowedNavigations; | ||
this.allowedIntents = allowedIntents; | ||
this.allowedRequests = allowedRequests; | ||
} | ||
|
||
@Override | ||
public void pluginInitialize() { | ||
if (this.allowedNavigations == null) { | ||
this.allowedNavigations = new AllowList(); | ||
this.allowedIntents = new AllowList(); | ||
this.allowedRequests = new AllowList(); | ||
|
||
new CustomConfigXmlParser().parse(webView.getContext()); | ||
} | ||
} | ||
|
||
private class CustomConfigXmlParser extends ConfigXmlParser { | ||
@Override | ||
public void handleStartTag(XmlPullParser xml) { | ||
String strNode = xml.getName(); | ||
if (strNode.equals("content")) { | ||
String startPage = xml.getAttributeValue(null, "src"); | ||
allowedNavigations.addAllowListEntry(startPage, false); | ||
} else if (strNode.equals("allow-navigation")) { | ||
String origin = xml.getAttributeValue(null, "href"); | ||
if ("*".equals(origin)) { | ||
allowedNavigations.addAllowListEntry("http://*/*", false); | ||
allowedNavigations.addAllowListEntry("https://*/*", false); | ||
allowedNavigations.addAllowListEntry("data:*", false); | ||
} else { | ||
allowedNavigations.addAllowListEntry(origin, false); | ||
} | ||
} else if (strNode.equals("allow-intent")) { | ||
String origin = xml.getAttributeValue(null, "href"); | ||
allowedIntents.addAllowListEntry(origin, false); | ||
} else if (strNode.equals("access")) { | ||
String origin = xml.getAttributeValue(null, "origin"); | ||
|
||
if (origin != null) { | ||
if ("*".equals(origin)) { | ||
allowedRequests.addAllowListEntry("http://*/*", false); | ||
allowedRequests.addAllowListEntry("https://*/*", false); | ||
} else { | ||
String subdomains = xml.getAttributeValue(null, "subdomains"); | ||
allowedRequests.addAllowListEntry(origin, (subdomains != null) && (subdomains.compareToIgnoreCase("true") == 0)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void handleEndTag(XmlPullParser xml) { } | ||
} | ||
|
||
@Override | ||
public Boolean shouldAllowNavigation(String url) { | ||
return this.allowedNavigations.isUrlAllowListed(url) | ||
? true | ||
: null; // default policy | ||
} | ||
|
||
@Override | ||
public Boolean shouldAllowRequest(String url) { | ||
return (this.shouldAllowNavigation(url) || this.allowedRequests.isUrlAllowListed(url)) | ||
? true | ||
: null; // default policy | ||
} | ||
|
||
@Override | ||
public Boolean shouldOpenExternalUrl(String url) { | ||
return (this.allowedIntents.isUrlAllowListed(url)) | ||
? true | ||
: null; // default policy | ||
} | ||
|
||
public AllowList getAllowedNavigations() { | ||
return this.allowedNavigations; | ||
} | ||
|
||
public void setAllowedNavigations(AllowList allowedNavigations) { | ||
this.allowedNavigations = allowedNavigations; | ||
} | ||
|
||
public AllowList getAllowedIntents() { | ||
return this.allowedIntents; | ||
} | ||
|
||
public void setAllowedIntents(AllowList allowedIntents) { | ||
this.allowedIntents = allowedIntents; | ||
} | ||
|
||
public AllowList getAllowedRequests() { | ||
return this.allowedRequests; | ||
} | ||
|
||
public void setAllowedRequests(AllowList allowedRequests) { | ||
this.allowedRequests = allowedRequests; | ||
} | ||
} |
This file contains 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 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 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 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 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 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 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
45 changes: 45 additions & 0 deletions
45
test/androidx/app/src/main/assets/www/allowlist/index.html
This file contains 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,45 @@ | ||
<!-- | ||
Licensed to the Apache Software Foundation (ASF) under one | ||
or more contributor license agreements. See the NOTICE file | ||
distributed with this work for additional information | ||
regarding copyright ownership. The ASF licenses this file | ||
to you under the Apache License, Version 2.0 (the | ||
"License"); you may not use this file except in compliance | ||
with the License. You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, | ||
software distributed under the License is distributed on an | ||
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
KIND, either express or implied. See the License for the | ||
specific language governing permissions and limitations | ||
under the License. | ||
--> | ||
<!DOCTYPE HTML> | ||
<html> | ||
<head> | ||
<meta name="viewport" content="width=320, user-scalable=no" /> | ||
<meta http-equiv="Content-type" content="text/html; charset=utf-8"> | ||
<title>Cordova Tests</title> | ||
<link rel="stylesheet" href="../master.css" type="text/css" media="screen" title="no title"> | ||
<script type="text/javascript" charset="utf-8" src="../cordova.js"></script> | ||
<script type="text/javascript" charset="utf-8" src="../main.js"></script> | ||
</head> | ||
<body onload="init();" id="stage" class="theme"> | ||
<h1>Allow List Page 1</h1> | ||
<div id="info"> | ||
<h4>Cordova: <span id="cordova"> </span></h4> | ||
<h4>Deviceready: <span id="deviceready"> </span></h4> | ||
</div> | ||
<div id="info"> | ||
Loading Page 2 should be successful.<br> | ||
Loading Page 3 should be in web browser.<br> | ||
Loading Page 2 with target=_blank should be in web browser? <br> | ||
(THIS DOESN'T HAPPEN.) https://issues.apache.org/jira/browse/CB-362 | ||
</div> | ||
<a href="index2.html" class="btn large">Page 2</a> | ||
<a href="http://www.google.com" class="btn large">Page 3</a> | ||
<a href="index2.html" class="btn large" target="_blank">Page 2 with target=_blank</a> | ||
</body> | ||
</html> |
Oops, something went wrong.