@cordova-ohos/cordova-plugin-whitelist 1.3.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +204 -0
- package/OAT.xml +79 -0
- package/README.md +111 -0
- package/package.json +29 -0
- package/plugin.xml +47 -0
- package/src/main/cpp/Whitelist/Whitelist.h +228 -0
- package/src/main/cpp/Whitelist/WhitelistPlugin.cpp +42 -0
- package/src/main/cpp/Whitelist/WhitelistPlugin.h +34 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
Copyright (c) 2025 Huawei Device, Inc. Ltd. and <马弓手>.
|
|
192
|
+
|
|
193
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
194
|
+
you may not use this file except in compliance with the License.
|
|
195
|
+
You may obtain a copy of the License at
|
|
196
|
+
|
|
197
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
198
|
+
|
|
199
|
+
Unless required by applicable law or agreed to in writing, software
|
|
200
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
201
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
202
|
+
See the License for the specific language governing permissions and
|
|
203
|
+
limitations under the License.
|
|
204
|
+
|
package/OAT.xml
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
+
<!-- Copyright (c) 2021 Huawei Device Co., Ltd.
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
|
|
16
|
+
This is the configuration file template for OpenHarmony OSS Audit Tool, please copy it to your project root dir and modify it refer to OpenHarmony/tools_oat/README.
|
|
17
|
+
|
|
18
|
+
-->
|
|
19
|
+
|
|
20
|
+
<configuration>
|
|
21
|
+
<oatconfig>
|
|
22
|
+
<licensefile></licensefile>
|
|
23
|
+
<policylist>
|
|
24
|
+
<policy name="projectPolicy" desc="">
|
|
25
|
+
<!--policyitem type="compatibility" name="GPL-2.0+" path="abc/.*" desc="Process that runs independently, invoked by the X process."/-->
|
|
26
|
+
<!--policyitem type="license" name="LGPL" path="abc/.*" desc="Dynamically linked by module X"/-->
|
|
27
|
+
<policyitem type="copyright" name="Copyright (c) 2025 Huawei Device, Inc. Ltd. and <马弓手>." path=".*" rule="may" group="defaultGroup" filefilter="copyrightPolicyFilter" desc="Developed by Huawei Device,Inc. Ltd. and <马弓手>"/>
|
|
28
|
+
</policy>
|
|
29
|
+
</policylist>
|
|
30
|
+
<filefilterlist>
|
|
31
|
+
<filefilter name="defaultFilter" desc="Files not to check">
|
|
32
|
+
<!--filteritem type="filename" name="*.uvwxyz" desc="Describe the reason for filtering scan results"/-->
|
|
33
|
+
<!--filteritem type="filepath" name="abcdefg/.*.uvwxyz" desc="Describe the reason for filtering scan results"/-->
|
|
34
|
+
<!--filteritem type="filepath" name="projectroot/[a-zA-Z0-9]{20,}.sh" desc="Temp files"/-->
|
|
35
|
+
</filefilter>
|
|
36
|
+
<filefilter name="defaultPolicyFilter" desc="Filters for compatibility,license header policies">
|
|
37
|
+
<!--filteritem type="filename" name="*.uvwxyz" desc="Describe the reason for filtering scan results"/-->
|
|
38
|
+
<!--filteritem type="filepath" name="abcdefg/.*.uvwxyz" desc="Describe the reason for filtering scan results"/-->
|
|
39
|
+
<!--filteritem type="filepath" name="projectroot/[a-zA-Z0-9]{20,}.sh" desc="Temp files"/-->
|
|
40
|
+
</filefilter>
|
|
41
|
+
<filefilter name="copyrightPolicyFilter" desc="Filters for copyright header policies">
|
|
42
|
+
<!--filteritem type="filename" name="*.uvwxyz" desc="Describe the reason for filtering scan results"/-->
|
|
43
|
+
<!--filteritem type="filepath" name="abcdefg/.*.uvwxyz" desc="Describe the reason for filtering scan results"/-->
|
|
44
|
+
<!--filteritem type="filepath" name="projectroot/[a-zA-Z0-9]{20,}.sh" desc="Temp files"/-->
|
|
45
|
+
</filefilter>
|
|
46
|
+
<filefilter name="licenseFileNamePolicyFilter" desc="Filters for LICENSE file policies">
|
|
47
|
+
<!--filteritem type="filename" name="*.uvwxyz" desc="Describe the reason for filtering scan results"/-->
|
|
48
|
+
<!--filteritem type="filepath" name="abcdefg/.*.uvwxyz" desc="Describe the reason for filtering scan results"/-->
|
|
49
|
+
<!--filteritem type="filepath" name="projectroot/[a-zA-Z0-9]{20,}.sh" desc="Temp files"/-->
|
|
50
|
+
</filefilter>
|
|
51
|
+
<filefilter name="readmeFileNamePolicyFilter" desc="Filters for README file policies">
|
|
52
|
+
<!--filteritem type="filename" name="*.uvwxyz" desc="Describe the reason for filtering scan results"/-->
|
|
53
|
+
<!--filteritem type="filepath" name="abcdefg/.*.uvwxyz" desc="Describe the reason for filtering scan results"/-->
|
|
54
|
+
<!--filteritem type="filepath" name="projectroot/[a-zA-Z0-9]{20,}.sh" desc="Temp files"/-->
|
|
55
|
+
</filefilter>
|
|
56
|
+
<filefilter name="readmeOpenSourcefileNamePolicyFilter" desc="Filters for README.OpenSource file policies">
|
|
57
|
+
<!--filteritem type="filename" name="*.uvwxyz" desc="Describe the reason for filtering scan results"/-->
|
|
58
|
+
<!--filteritem type="filepath" name="abcdefg/.*.uvwxyz" desc="Describe the reason for filtering scan results"/-->
|
|
59
|
+
<!--filteritem type="filepath" name="projectroot/[a-zA-Z0-9]{20,}.sh" desc="Temp files"/-->
|
|
60
|
+
</filefilter>
|
|
61
|
+
<filefilter name="binaryFileTypePolicyFilter" desc="Filters for binary file policies">
|
|
62
|
+
<!--filteritem type="filename" name="*.uvwxyz" desc="Describe the reason for filtering scan results"/-->
|
|
63
|
+
<!--filteritem type="filepath" name="abcdefg/.*.uvwxyz" desc="Describe the reason for filtering scan results"/-->
|
|
64
|
+
<!--filteritem type="filepath" name="projectroot/[a-zA-Z0-9]{20,}.sh" desc="Temp files"/-->
|
|
65
|
+
</filefilter>
|
|
66
|
+
|
|
67
|
+
</filefilterlist>
|
|
68
|
+
<licensematcherlist>
|
|
69
|
+
<!--licensematcher name="uvwxyz License" desc="If the scanning result is InvalidLicense, you can define matching rules here. Note that quotation marks must be escaped.">
|
|
70
|
+
<licensetext name="
|
|
71
|
+
uvwxyz license textA xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
|
72
|
+
" desc=""/>
|
|
73
|
+
<licensetext name="
|
|
74
|
+
uvwxyz license textB xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
|
75
|
+
" desc=""/>
|
|
76
|
+
</licensematcher-->
|
|
77
|
+
</licensematcherlist>
|
|
78
|
+
</oatconfig>
|
|
79
|
+
</configuration>
|
package/README.md
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
# cordova-plugin-whitelist
|
|
2
|
+
|
|
3
|
+
Cordova 官方核心插件,用于控制应用对外部资源的访问权限,包括网络请求、外部应用跳转和 WebView 导航。它通过白名单机制有效防范跨站请求伪造(CSRF)、恶意 URL 跳转等安全风险,是保障 Cordova 应用安全性的关键组件。
|
|
4
|
+
|
|
5
|
+
**重要提示:** <span style="color:red">该插件在Cordova10.0开始,已在Android和iOS中废弃,插件已失效,因此该插件也在OHOS系统中功能失效,但保留插件接口,主要兼容老版本的Android或iOS项目移植使用,如果您是新项目,无需安装该插件。</span>
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
## 安装指南
|
|
9
|
+
|
|
10
|
+
### 1. 基础安装(推荐)
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
# 安装hcordova
|
|
14
|
+
npm install -g hcordova
|
|
15
|
+
|
|
16
|
+
# 全平台安装
|
|
17
|
+
hcordova plugin add cordova-plugin-whitelist
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
### 2. 安装指定版本
|
|
21
|
+
|
|
22
|
+
如需兼容特定 Cordova 版本,可指定插件版本号:
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
# 安装 1.0.0 稳定版
|
|
26
|
+
|
|
27
|
+
hcordova plugin add cordova-plugin-whitelist@1.0.0 --platform ohos
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
### 3. 开发版安装
|
|
31
|
+
|
|
32
|
+
如需测试最新功能,可从 GitCode 仓库安装开发分支:
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
hcordova plugin add https://gitcode.com/OpenHarmony-Cordova/cordova-plugin-whitelist.git --platform ohos
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### 4. 卸载插件
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
#全平台卸载
|
|
43
|
+
hcordova plugin remove cordova-plugin-whitelist
|
|
44
|
+
|
|
45
|
+
#指定OHOS卸载
|
|
46
|
+
hcordova plugin remove cordova-plugin-whitelist --platform ohos
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## 核心功能说明
|
|
50
|
+
|
|
51
|
+
插件通过三类白名单规则实现精细化权限控制,所有配置均在项目根目录的 `config.xml` 中完成
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
| 白名单类型 | 控制范围 | 配置标签 | 适用平台 |
|
|
55
|
+
| ---------- | ---------------------------------------------------- | -------------------- | --------- |
|
|
56
|
+
| 网络请求白名单 | 控制应用发起的 XMLHttpRequest、WebSocket、图片 / 脚本 / 字体等资源加载请求 | `<access>` | 所有支持平台 |
|
|
57
|
+
| 导航白名单 | 控制应用内 WebView 可直接导航到的外部 URL(不跳转外部浏览器) | `<allow-navigation>` | 所有支持平台 |
|
|
58
|
+
|
|
59
|
+
## 详细配置指南
|
|
60
|
+
|
|
61
|
+
### 1. 网络请求白名单(`<access>` 标签)
|
|
62
|
+
|
|
63
|
+
控制应用所有网络资源加载,支持通配符、协议限制和子域名匹配。
|
|
64
|
+
|
|
65
|
+
#### 基础语法
|
|
66
|
+
|
|
67
|
+
```xml
|
|
68
|
+
<!-- 推荐配置 -->
|
|
69
|
+
<access origin="*" />
|
|
70
|
+
|
|
71
|
+
<!-- 允许访问指定域名 -->
|
|
72
|
+
<access origin="https://api.example.com" />
|
|
73
|
+
|
|
74
|
+
<!-- 允许访问指定域名及其所有子域名 -->
|
|
75
|
+
<access origin="https://*.example.com" />
|
|
76
|
+
|
|
77
|
+
<!-- 允许访问所有 HTTPS/WSS 安全域名(推荐) -->
|
|
78
|
+
<access origin="https://*" />
|
|
79
|
+
|
|
80
|
+
<!-- 允许访问指定端口(需完整指定协议+域名+端口) -->
|
|
81
|
+
<access origin="https://api.example.com:8443" />
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### 2. 导航白名单(`<allow-navigation>` 标签)
|
|
85
|
+
|
|
86
|
+
控制 WebView 可直接导航的 URL,未配置的 URL 会触发系统浏览器打开(或被拦截)。
|
|
87
|
+
|
|
88
|
+
#### 常用配置
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
```xml
|
|
92
|
+
<!-- 推荐配置 -->
|
|
93
|
+
<allow-navigation href="*" />
|
|
94
|
+
|
|
95
|
+
<!-- 允许导航到应用主域名下所有路径 -->
|
|
96
|
+
<allow-navigation href="https://app.example.com/*" />
|
|
97
|
+
|
|
98
|
+
<!-- 允许导航到子域名 -->
|
|
99
|
+
<allow-navigation href="https://*.app.example.com" />
|
|
100
|
+
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
## 许可证
|
|
104
|
+
|
|
105
|
+
本插件基于 **Apache License 2.0** 开源,详见 [LICENSE](LICENSE) 文件。
|
|
106
|
+
|
|
107
|
+
## 资源参考
|
|
108
|
+
|
|
109
|
+
1. **OHOS**:[https://gitcode.com/OpenHarmony-Cordova/cordova-plugin-whitelist](https://gitcode.com/OpenHarmony-Cordova/cordova-plugin-whitelist)
|
|
110
|
+
|
|
111
|
+
2. **Android、ios插件说明**:[https://www.npmjs.com/package/cordova-plugin-whitelist](https://www.npmjs.com/package/cordova-plugin-whitelist)
|
package/package.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@cordova-ohos/cordova-plugin-whitelist",
|
|
3
|
+
"version": "1.3.5",
|
|
4
|
+
"description": "Cordova whitelist Plugin",
|
|
5
|
+
"cordova": {
|
|
6
|
+
"id": "cordova-plugin-whitelist",
|
|
7
|
+
"platforms": [
|
|
8
|
+
"ohos"
|
|
9
|
+
]
|
|
10
|
+
},
|
|
11
|
+
"repository": "gitcode:OpenHarmony-Cordova/cordova-plugin-whitelist",
|
|
12
|
+
"bugs": "https://gitcode.com/OpenHarmony-Cordova/cordova-plugin-whitelist/issues",
|
|
13
|
+
"keywords": [
|
|
14
|
+
"cordova",
|
|
15
|
+
"whitelist",
|
|
16
|
+
"ecosystem:cordova",
|
|
17
|
+
"cordova-openharmony"
|
|
18
|
+
],
|
|
19
|
+
"engines": {
|
|
20
|
+
"cordovaDependencies": {
|
|
21
|
+
"1.3.5": {
|
|
22
|
+
"@cordova-ohos/ohos": ">=2.0.0",
|
|
23
|
+
"hcordova":">=1.0.0"
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"author": " Huawei Device, Inc. Ltd. and <马弓手>",
|
|
28
|
+
"license": "Apache-2.0"
|
|
29
|
+
}
|
package/plugin.xml
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
+
<!--
|
|
3
|
+
Copyright (c) 2025 Huawei Device, Inc. Ltd. and <马弓手>.
|
|
4
|
+
|
|
5
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
you may not use this file except in compliance with the License.
|
|
7
|
+
You may obtain a copy of the License at
|
|
8
|
+
|
|
9
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
|
|
11
|
+
Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
See the License for the specific language governing permissions and
|
|
15
|
+
limitations under the License.
|
|
16
|
+
-->
|
|
17
|
+
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
|
|
18
|
+
id="cordova-plugin-whitelist"
|
|
19
|
+
version="1.3.5">
|
|
20
|
+
<name>whitelist</name>
|
|
21
|
+
<description>Cordova whitelist Plugin</description>
|
|
22
|
+
<license>Apache 2.0</license>
|
|
23
|
+
<keywords>cordova,whitelist</keywords>
|
|
24
|
+
<repo>https://gitcode.com/OpenHarmony-Cordova/cordova-plugin-whitelist.git</repo>
|
|
25
|
+
<issue>https://gitcode.com/OpenHarmony-Cordova/cordova-plugin-whitelist/issues</issue>
|
|
26
|
+
|
|
27
|
+
<engines>
|
|
28
|
+
<engine name="@cordova-ohos/ohos" version=">=2.0.0" />
|
|
29
|
+
</engines>
|
|
30
|
+
|
|
31
|
+
<platform name="ohos">
|
|
32
|
+
<config-file target="src/main/resources/rawfile/config.xml" parent="/*" modules-targets-name="default">
|
|
33
|
+
<feature name="Whitelist">
|
|
34
|
+
<param name="android-package" value="WhitelistPlugin" />
|
|
35
|
+
<param name="onload" value="true" />
|
|
36
|
+
</feature>
|
|
37
|
+
</config-file>
|
|
38
|
+
|
|
39
|
+
<CMakeLists target="src/main/cpp/CMakeLists.txt" modules-name="cordova">
|
|
40
|
+
<param target="add_library" value="Whitelist/WhitelistPlugin.cpp"/>
|
|
41
|
+
</CMakeLists>
|
|
42
|
+
|
|
43
|
+
<source-file type="h" src="src/main/cpp/Whitelist/Whitelist.h" target-dir="src/main/cpp/Whitelist" modules-name="cordova"/>
|
|
44
|
+
<source-file type="h" src="src/main/cpp/Whitelist/WhitelistPlugin.h" target-dir="src/main/cpp/Whitelist" modules-name="cordova"/>
|
|
45
|
+
<source-file type="cpp" src="src/main/cpp/Whitelist/WhitelistPlugin.cpp" target-dir="src/main/cpp/Whitelist" modules-name="cordova"/>
|
|
46
|
+
</platform>
|
|
47
|
+
</plugin>
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2025 Huawei Device, Inc. Ltd. and <马弓手>.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
#ifndef MYAPPLICATION_WHITELIST_H
|
|
18
|
+
#define MYAPPLICATION_WHITELIST_H
|
|
19
|
+
#include <iostream>
|
|
20
|
+
#include <vector>
|
|
21
|
+
#include <string>
|
|
22
|
+
#include <regex>
|
|
23
|
+
#include <memory>
|
|
24
|
+
#include <stdexcept>
|
|
25
|
+
|
|
26
|
+
class Whitelist {
|
|
27
|
+
class URLPattern {
|
|
28
|
+
public:
|
|
29
|
+
std::shared_ptr<std::regex> scheme;
|
|
30
|
+
std::shared_ptr<std::regex> host;
|
|
31
|
+
std::shared_ptr<int> port;
|
|
32
|
+
std::shared_ptr<std::regex> path;
|
|
33
|
+
private:
|
|
34
|
+
std::string regexFromPattern(const std::string& pattern, bool allowWildcards)
|
|
35
|
+
{
|
|
36
|
+
const std::string toReplace = ".[]{}()^$?+|";
|
|
37
|
+
std::string regexStr;
|
|
38
|
+
|
|
39
|
+
for (char c : pattern) {
|
|
40
|
+
if (c == '*' && allowWildcards) {
|
|
41
|
+
regexStr += ".";
|
|
42
|
+
} else if (toReplace.find(c) != std::string::npos) {
|
|
43
|
+
regexStr += '\\';
|
|
44
|
+
}
|
|
45
|
+
regexStr += c;
|
|
46
|
+
}
|
|
47
|
+
return regexStr;
|
|
48
|
+
}
|
|
49
|
+
public:
|
|
50
|
+
URLPattern(const std::string& scheme, const std::string& host, const std::string& port, const std::string& path)
|
|
51
|
+
{
|
|
52
|
+
// Scheme处理
|
|
53
|
+
if (scheme.empty() || scheme == "*") {
|
|
54
|
+
this->scheme = nullptr;
|
|
55
|
+
} else {
|
|
56
|
+
std::string schemeRegex = regexFromPattern(scheme, false);
|
|
57
|
+
this->scheme = make_shared<std::regex>(schemeRegex, std::regex_constants::icase);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Host处理
|
|
61
|
+
if (host == "*") {
|
|
62
|
+
this->host = nullptr;
|
|
63
|
+
} else if (host.size() >= 2 && host.substr(0, 2) == "*.") {
|
|
64
|
+
std::string hostRegex = "([a-z0-9.-]*\\.)?" + regexFromPattern(host.substr(2), false);
|
|
65
|
+
this->host = make_shared<std::regex>(hostRegex, std::regex_constants::icase);
|
|
66
|
+
} else {
|
|
67
|
+
std::string hostRegex = regexFromPattern(host, false);
|
|
68
|
+
this->host = make_shared<std::regex>(hostRegex, std::regex_constants::icase);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// Port处理
|
|
72
|
+
if (port.empty() || port == "*") {
|
|
73
|
+
this->port = nullptr;
|
|
74
|
+
} else {
|
|
75
|
+
int portNum = stoi(port);
|
|
76
|
+
if (portNum < 0 || portNum > 65535) {
|
|
77
|
+
throw std::invalid_argument("Port must be between 0 and 65535");
|
|
78
|
+
}
|
|
79
|
+
this->port = std::make_shared<int>(portNum);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// Path处理
|
|
83
|
+
if (path.empty() || path == "/*") {
|
|
84
|
+
this->path = nullptr;
|
|
85
|
+
} else {
|
|
86
|
+
std::string pathRegex = regexFromPattern(path, true);
|
|
87
|
+
this->path = make_shared<std::regex>(pathRegex);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
bool matches(const std::string& uri) const
|
|
92
|
+
{
|
|
93
|
+
size_t schemeEnd = uri.find("://");
|
|
94
|
+
if (schemeEnd == std::string::npos) {
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
std::string schemeStr = uri.substr(0, schemeEnd);
|
|
99
|
+
std::string remaining = uri.substr(schemeEnd + 3);
|
|
100
|
+
|
|
101
|
+
size_t hostStart = 0;
|
|
102
|
+
size_t hostEnd = remaining.find('/');
|
|
103
|
+
std::string hostPortStr = (hostEnd == std::string::npos) ? remaining : remaining.substr(0, hostEnd);
|
|
104
|
+
|
|
105
|
+
size_t portStart = hostPortStr.find(':');
|
|
106
|
+
std::string hostStr = (portStart == std::string::npos) ? hostPortStr : hostPortStr.substr(0, portStart);
|
|
107
|
+
std::string portStr = (portStart == std::string::npos) ?"" : hostPortStr.substr(portStart + 1);
|
|
108
|
+
|
|
109
|
+
std::string pathStr = (hostEnd == std::string::npos) ?"/" : remaining.substr(hostEnd);
|
|
110
|
+
|
|
111
|
+
int portNum = -1;
|
|
112
|
+
if (!portStr.empty()) {
|
|
113
|
+
portNum = stoi(portStr);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// 检查各个部分的匹配
|
|
117
|
+
if (scheme && !regex_match(schemeStr, *scheme)) {
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (host && !regex_match(hostStr, *host)) {
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (port) {
|
|
126
|
+
if (portNum == -1) {
|
|
127
|
+
// 如果没有明确指定端口,使用默认端口
|
|
128
|
+
if (schemeStr == "http") portNum = 80;
|
|
129
|
+
else if (schemeStr == "https") portNum = 443;
|
|
130
|
+
else if (schemeStr == "ftp") portNum = 21;
|
|
131
|
+
}
|
|
132
|
+
if (portNum != *port) {
|
|
133
|
+
return false;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
if (path && !regex_match(pathStr, *path)) {
|
|
137
|
+
return false;
|
|
138
|
+
}
|
|
139
|
+
return true;
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
std::vector<URLPattern> whiteList;
|
|
143
|
+
bool isLimitedAccess;
|
|
144
|
+
public:
|
|
145
|
+
|
|
146
|
+
Whitelist() : isLimitedAccess(false)
|
|
147
|
+
{
|
|
148
|
+
}
|
|
149
|
+
~Whitelist()
|
|
150
|
+
{
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
void addWhiteListEntry(const std::string& origin)
|
|
154
|
+
{
|
|
155
|
+
if (isLimitedAccess) {
|
|
156
|
+
return; // 已经是无限制访问模式
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// 无限制访问网络资源
|
|
160
|
+
if (origin == "*") {
|
|
161
|
+
isLimitedAccess = true;
|
|
162
|
+
whiteList.clear();
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// 特定的访问规则
|
|
167
|
+
std::regex partsRegex("^((\\*|[A-Za-z-]+):(//)?)?(\\*|((\\*\\.)?[^*/:]+))?(:(\\d+))?(/.*)?");
|
|
168
|
+
std::smatch matches;
|
|
169
|
+
|
|
170
|
+
if (regex_match(origin, matches, partsRegex)) {
|
|
171
|
+
std::string scheme = matches[2].str();
|
|
172
|
+
std::string host = matches[4].str();
|
|
173
|
+
|
|
174
|
+
// 特殊处理:file和content协议允许空主机
|
|
175
|
+
if ((scheme == "file" || scheme == "content") && host.empty()) {
|
|
176
|
+
host = "*";
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
std::string port = matches[8].str();
|
|
180
|
+
std::string path = matches[9].str();
|
|
181
|
+
|
|
182
|
+
if (scheme.empty()) {
|
|
183
|
+
// 如果没有指定协议,默认添加http和https
|
|
184
|
+
whiteList.emplace_back("http", host, port, path);
|
|
185
|
+
whiteList.emplace_back("https", host, port, path);
|
|
186
|
+
} else {
|
|
187
|
+
whiteList.emplace_back(scheme, host, port, path);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
bool isUrlWhiteListed(const std::string& uri) const
|
|
193
|
+
{
|
|
194
|
+
// 如果是无限制访问模式
|
|
195
|
+
if (isLimitedAccess) {
|
|
196
|
+
return true;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// 在白名单中查找匹配
|
|
200
|
+
for (const auto& pattern : whiteList) {
|
|
201
|
+
if (pattern.matches(uri)) {
|
|
202
|
+
return true;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
return false;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// 清空白名单
|
|
210
|
+
void clear()
|
|
211
|
+
{
|
|
212
|
+
whiteList.clear();
|
|
213
|
+
isLimitedAccess = false;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// 获取白名单大小
|
|
217
|
+
size_t size() const
|
|
218
|
+
{
|
|
219
|
+
return whiteList.size();
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// 检查是否是无限制访问模式
|
|
223
|
+
bool isUnlimited() const
|
|
224
|
+
{
|
|
225
|
+
return isLimitedAccess;
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
#endif //MYAPPLICATION_WHITELIST_H
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2025 Huawei Device, Inc. Ltd. and <马弓手>.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
#include "WhitelistPlugin.h"
|
|
18
|
+
#include "CordovaViewController.h"
|
|
19
|
+
|
|
20
|
+
REGISTER_PLUGIN_CLASS(WhitelistPlugin)
|
|
21
|
+
bool WhitelistPlugin::execute(const std::string& action, cJSON* args, CallbackContext cbc)
|
|
22
|
+
{
|
|
23
|
+
return true;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
void WhitelistPlugin::pluginInitialize()
|
|
27
|
+
{
|
|
28
|
+
std::vector<std::string>& vecHostName = *(reinterpret_cast<CordovaViewController*>(Application::g_cordovaViewController))->getConfigHostName();
|
|
29
|
+
for (int i = 0; i < vecHostName.size(); i++) {
|
|
30
|
+
if (vecHostName[i] == "*") {
|
|
31
|
+
m_whitelist.addWhiteListEntry("*");
|
|
32
|
+
} else {
|
|
33
|
+
m_whitelist.addWhiteListEntry(vecHostName[i]);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
bool WhitelistPlugin::shouldAllowRequest(const std::string& url)
|
|
40
|
+
{
|
|
41
|
+
return m_whitelist.isUrlWhiteListed(url);
|
|
42
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2025 Huawei Device, Inc. Ltd. and <马弓手>.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
#ifndef MYAPPLICATION_WHITELISTPLUGIN_H
|
|
18
|
+
#define MYAPPLICATION_WHITELISTPLUGIN_H
|
|
19
|
+
|
|
20
|
+
#include "CordovaPlugin.h"
|
|
21
|
+
#include "Whitelist.h"
|
|
22
|
+
class WhitelistPlugin : public CordovaPlugin {
|
|
23
|
+
Whitelist m_whitelist;
|
|
24
|
+
public:
|
|
25
|
+
WhitelistPlugin() {
|
|
26
|
+
}
|
|
27
|
+
~WhitelistPlugin() {
|
|
28
|
+
}
|
|
29
|
+
bool execute(const std::string& action, cJSON* args, CallbackContext cbc) override;
|
|
30
|
+
void pluginInitialize() override;
|
|
31
|
+
bool shouldAllowRequest(const std::string& url) override;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
#endif //MYAPPLICATION_WHITELISTPLUGIN_H
|