README-cn.md
1# Arkguard
2Arkguard 是Javascript和Typescript的源码混淆工具。
3
4# 在DevEco Studio中的用法
5Arkguard已经被集成了到SDK中。可以在DevEco Studio中很方便地使用。Arkguard只能用于Stage模型
6(不支持FA模型)。目前Arkguard只提供名称混淆的能力(因为其它混淆能力会劣化性能)。
7使用Arkguard可以混淆以下名称:
8* 参数名和局部变量名
9* 顶层作用域的名称
10* 属性名称
11
12Arkguard默认使能对参数名和局部变量名的混淆。顶层作用域名称和属性名称的混淆是默认关闭的,
13因为默认打开可能会导致运行时错误。你可以通过[混淆选项](#混淆选项)来开启它们。
14
15创建一个新工程的时候,配置文件`build-profile.json5`中会自动生成以下内容:
16```
17"arkOptions": {
18 "obfuscation": {
19 "ruleOptions": {
20 "enable": true,
21 "files": ["obfuscation-rules.txt"],
22 }
23 }
24}
25```
26创建一个新的library的时候,还会额外生成`consumerFiles`属性:
27```
28"arkOptions": {
29 "obfuscation": {
30 "ruleOptions": {
31 "enable": true,
32 "files": ["obfuscation-rules.txt"],
33 }
34 "consumerFiles": ["consumer-rules.txt"]
35 }
36}
37```
38
39要想开启混淆,需要满足下面的条件:
40* 属性`ruleOptions.enable`的值为`true`,并且所有依赖的library的`ruleOptions.enable`属性是`true`
41* 在release模式构建
42
43属性`ruleOptions.files`中指定的混淆配置文件会在构建HAP或HAR的时候被应用。
44
45属性`consumerFiles`中指定的混淆配置文件会在构建依赖这个library的工程或library时被应用。
46这些混淆配置文件的内容还会被合并到HAR包中的`obfuscation.txt`文件。
47
48当构建HAP或者HAR的时候,最终的混淆规则是自身的`ruleOptions.files`属性,依赖的library的`consumerFiles`属性,
49以及依赖的HAR包中的`obfuscation.txt`文件的合并。如果构建的是HAR,`obfuscation.txt`是自身的`consumerFiles`属性,
50依赖的library的`consumerFiles`属性,以及依赖的HAR包中的`obfuscation.txt`文件的合并。
51构建HAP不会生成`obfuscation.txt`。详细合并的策略可以查看[混淆规则合并策略](#混淆规则合并策略)。
52
53## 配置混淆规则
54在创建工程或library的时候,DevEco Studio会自动生成`obfuscation-rules.txt`和`consumer-rules.txt`文件,
55但是它们默认不会包含任何混淆规则。你可以在这些文件中写混淆规则,或者也可以将规则写在其它文件,
56然后将文件路径放到`ruleOptions.files`和`consumerFiles`中,如下面的例子所示。
57```
58"buildOption": {
59 "arkOptions": {
60 "obfuscation": {
61 "ruleOptions": {
62 "enable": true,
63 "files": ["obfuscation-rules.txt", "myrules.txt"],
64 }
65 "consumerFiles": ["consumer-rules.txt", "my-consumer-rules.txt"]
66 }
67 }
68}
69```
70
71在混淆规则文件中,你可以写[混淆选项](#混淆选项)和[保留选项](#保留选项)。
72
73### 混淆选项
74
75`-disable-obfuscation`
76
77关闭所有混淆。如果你使用这个选项,那么构建出来的HAR或HAR将不会被混淆。默认情况下,
78Arkguard只混淆参数名和局部变量名(通过将它们重新命名为随机的短名字)。
79
80`-enable-property-obfuscation`
81
82开启属性混淆。 如果你使用这个选项,那么所有的属性名都会被混淆,除了下面场景:
83* 被`import/export`的类或对象的属性名不会被混淆。注意: 只有直接导出的类或对象的属性名不会被混淆。
84比如下面例子中的属性名`data`不会被混淆。
85 ```
86 export class MyClass {
87 data: string;
88 }
89 ```
90对于间接导出的场景,比如`export MyClass`和`let a = MyClass; export a;`,如果你不想混淆它们的属性名,
91那么你需要使用[保留选项](#保留选项)来保留这些属性名。另外,对于直接导出的类或对象的属性的属性名,比如下面例子中的`name`和`age`, 如果你不想混淆它们,那么你也需要使用[保留选项](#保留选项)来保留这些属性名。
92 ```
93 export class MyClass {
94 person = {name: "123", age: 100};
95 }
96 ```
97* ArkUI组件中的属性名不会被混淆。比如下面例子中的`message`和`data`不会被混淆。
98 ```
99 @Component struct MyExample {
100 @State message: string = "hello";
101 data: number[] = [];
102 ...
103 }
104 ```
105* 被[保留选项](#保留选项)指定的属性名不会被混淆。
106* 系统API列表中的属性名不会被混淆。系统API列表是构建时从SDK中自动提取出来的一个名称列表。
107* 字符串字面量属性名不会被混淆。比如下面例子中的`"name"`和`"age"`不会被混淆。
108 ```
109 let person = {"name": "abc"};
110 person["age"] = 22;
111 ```
112 如果你想混淆字符串字面量属性名,你需要在该选项的基础上再使用`-enable-string-property-obfuscation`选项。比如
113 ```
114 -enable-property-obfuscation
115 -enable-string-property-obfuscation
116 ```
117 注意:如果你的代码里面有字符串属性名包含特殊字符(除了`a-z, A-Z, 0-9, _`之外的字符),比如`let obj = {"\n": 123, "": 4, " ": 5}`,我们建议不要开启`-enable-string-property-obfuscation`选项,因为当你不想混淆这些名字时,可能无法通过[保留选项](#保留选项)来指定保留这些名字。
118
119`-enable-toplevel-obfuscation`
120
121开启顶层作用域名称混淆。如果你使用这个选项,那么所有的顶层作用域的名称都会被混淆,除了下面场景:
122* 被`import/export`的名称不会被混淆。
123* 当前文件找不到声明的名称不会被混淆。
124* 被[保留选项](#保留选项)指定的顶层作用域名称不会被混淆。
125* 系统API列表中的顶层作用域名称不会被混淆。
126
127`-compact`
128
129去除不必要的空格符和所有的换行符。如果你使用这个选项,那么所有代码会被压缩到一行。
130
131`-remove-log`
132
133删除所有`console.*`语句。
134
135`-print-namecache` filepath
136
137将名称缓存保存到指定的文件路径。名称缓存包含名称混淆前后的映射。如果你使用了`-enable-property-obfuscation`或
138`-enable-toplevel-obfuscation`选项,并且你希望未来进行增量编译(比如热修复),那么你应该使用这个选项,
139并且将缓存文件保管好。
140
141`-apply-namecache` filepath
142
143复用指定的名称缓存文件。名字将会被混淆成缓存映射对应的名字,如果没有对应,将会被混淆成新的随机段名字。
144该选项应该在增量编译场景中被使用。
145
146默认情况下,DevEco Studio会在临时的缓存目录中保存缓存文件,并且在增量编译场景中自动应用该缓存文件。
147
148### 保留选项
149
150保留选项只有在使用`enable-property-obfuscation`或`enable-toplevel-obfuscation`选项时发挥作用。
151
152`-keep-property-name` [,modifiers,...]
153
154指定你想保留的属性名。比如下面的例子:
155```
156-keep-property-name
157age
158firstName
159lastName
160```
161
162**哪些属性名应该被保留?**
163
164为了保障混淆的正确性,我们建议你保留所有不通过点语法访问的属性。
165
166例子:
167```
168var obj = {x0: 0, x1: 0, x2: 0};
169for (var i = 0; i <= 2; i++) {
170 console.log(obj['x' + i]); // x0, x1, x2 应该被保留
171}
172
173Object.defineProperty(obj, 'y', {}); // y 应该被保留
174console.log(obj.y);
175
176obj.s = 0;
177let key = 's';
178console.log(obj[key]); // s 应该被保留
179
180obj.u = 0;
181console.log(obj.u); // u 可以被正确地混淆
182
183obj.t = 0;
184console.log(obj['t']); // 在开启字符串字面量属性名混淆时t和't'会被正确地混淆,但是我们建议保留
185
186obj['v'] = 0;
187console.log(obj['v']); // 在开启字符串字面量属性名混淆时'v'会被正确地混淆,但是我们建议保留
188```
189
190`-keep-global-name` [,modifiers,...]
191
192指定要保留的顶层作用域的名称。比如,
193```
194-keep-global-name
195Person
196printPersonName
197```
198
199**哪些顶层作用域的名称应该被保留?**
200
201在Javascript中全局变量是`globalThis`的属性。如果在代码中使用`globalThis`去访问全局变量,那么该变量名应该被保留。
202
203例子:
204```
205var a = 0;
206console.log(globalThis.a); // a 应该被保留
207
208function foo(){}
209globalThis.foo(); // foo 应该被保留
210
211var c = 0;
212console.log(c); // c 可以被正确地混淆
213
214function bar(){}
215bar(); // bar 可以被正确地混淆
216
217class MyClass {}
218let d = new MyClass(); // MyClass 可以被正确地混淆
219```
220
221`-keep-dts` filepath
222
223保留指定路径的`.d.ts`文件中的名称。这里的文件路径可以是一个目录,这种情况下目录中所有`.d.ts`文件中的名称都会被保留。
224如果在构建HAR时使用了这个选项,那么文件中的名称会被合并到最后的`obfuscation.txt`文件中。
225
226### 注释
227
228可以使用`#`在混淆规则文件中进行注释。每行以`#`开头的文本会被当做是注释,比如下面的例子:
229```
230# white list for MainAbility.ets
231-keep-global-name
232MyComponent
233GlobalFunction
234
235-keep-property-name # white list for dynamic property names
236firstName
237lastName
238age
239```
240构建HAR时,注释不会被合并到最后的`obfuscation.txt`文件中。
241
242### 混淆规则合并策略
243一个工程中经常会有许多混淆规则文件,这些文件来自于:
244* 主工程的`ruleOptions.files` (这里主工程我们指的是正在构建的工程)
245* 本地依赖的library中的`consumerFiles`选项中指定的文件
246* 远程依赖的HAR包中的`obfuscate.txt`文件
247
248当构建主工程的时候,这些文件中的混淆规则会按照下面的合并策略(伪代码)进行合并:
249```
250let `listRules` 表示上面提到的所有混淆规则文件的列表
251let finalRule = {
252 disableObfuscation: false,
253 enablePropertyObfuscation: false,
254 enableToplevelObfuscation: false,
255 compact: false,
256 removeLog: false,
257 keepPropertyName: [],
258 keepGlobalName: [],
259 keepDts: [],
260 printNamecache: string,
261 applyNamecache: string
262}
263for each file in `listRules`:
264 for each option in file:
265 switch(option) {
266 case -disable-obfuscation:
267 finalRule.disableObfuscation = true;
268 continue;
269 case -enable-property-obfuscation:
270 finalRule.enablePropertyObfuscation = true;
271 continue;
272 case -enable-toplevel-obfuscation:
273 finalRule.enableToplevelObfuscation = true;
274 continue;
275 case -compact:
276 finalRule.compact = true;
277 continue;
278 case -remove-log:
279 finalRule.removeLog = true;
280 continue;
281 case -print-namecache:
282 finalRule.printNamecache = #{指定的路径名};
283 continue;
284 case -apply-namecache:
285 finalRule.applyNamecache = #{指定的路径名};
286 continue;
287 case -keep-property-name:
288 finalRule.keepPropertyName.push(#{指定的名称});
289 continue;
290 case -keep-global-name:
291 finalRule.keepGlobalName.push(#{指定的名称});
292 continue;
293 case -keep-dts:
294 finalRule.keepDts.push(#{指定的路径});
295 continue;
296 }
297 end-for
298end-for
299```
300最后使用的混淆规则来自于对象`finalRule`。
301
302如果构建的是HAR,那么最终的`obfuscate.txt`文件内容来自于主工程和本地依赖的library的`consumerFiles`选项,
303以及依赖的HAR的`obfuscate.txt`文件的合并。合并策略和上面一样,除了以下的不同:
304* `-keep-dts`选项会被转换成`-keep-global-name`和`-keep-property-name`。
305* `-print-namecache`和`apply-namecache`选项会被忽略,不会出现在最后的`obfuscate.txt`文件中。
306
README.md
1# Arkguard
2Arkguard is a javascript and typescript obfuscation tool.
3For Chinese version please read [README-cn.md](README-cn.md)
4(中文版说明请查看[README-cn.md](README-cn.md)).
5
6# Usage in DevEco Studio
7Arkguard has been integrated into SDK. It is convenient to use Arkguard in DevEco Studio.
8In DevEco Studio, Arkguard can be enabled only in Stage Model (FA Model is not supported).
9For now only name obfuscations can be used in DevEco Studio (because other obfuscation
10abilities of Arkguard may hurt execution performance).
11You can obfuscate the following names:
12* parameter names and local variable names
13* names in global scope
14* property names
15
16We enable the obfuscation of parameter names and local variable names by default. However,
17global names obfuscation and property names obfuscation are disabled by default, as they may
18cause runtime error if they are enabled by default.
19You can enable them by [obfuscation options](#obfuscation-options).
20
21When you create a new project, the following config will be generated in `build-profile.json5`.
22```
23"arkOptions": {
24 "obfuscation": {
25 "ruleOptions": {
26 "enable": true,
27 "files": ["obfuscation-rules.txt"],
28 }
29 }
30}
31```
32When you create a new library, additional property `consumerFiles` will be added.
33```
34"arkOptions": {
35 "obfuscation": {
36 "ruleOptions": {
37 "enable": true,
38 "files": ["obfuscation-rules.txt"],
39 }
40 "consumerFiles": ["consumer-rules.txt"]
41 }
42}
43```
44
45To enable obfuscation, the following conditions should be satisfied:
46* the property `ruleOptions.enable` is `true` and the property `ruleOptions.enable` of every dependent library is `true`.
47* build in release mode
48
49The files in the property `ruleOptions.files` will be applied when you build HAP or HAR.
50
51The files in the property `consumerFiles` will be applied when you build the project or library which
52depends on this library. They will also be merged into a file `obfuscation.txt` in the resulting HAR.
53
54When you are building HAP or HAR, the final obfucation rules are combination of self's `ruleOptions.files`
55property, dependent libraries' `consumerFiles` properties and dependent HAR's `obfuscation.txt`.
56If you are building HAR, the content of `obfuscation.txt` is the combination of self's `consumerFiles` property,
57dependent libraries' `consumerFiles` properties and dependent HAR's `obfuscation.txt`. If you are building
58HAP, `obfuscation.txt` will not be generated. For more details, please jump to
59"[How Arkguard merges rules](#how-arkguard-merges-rules)".
60
61## Write rules
62
63The files `obfuscation-rules.txt` and `consumer-rules.txt` are created by DevEco Studio automatically, but they do not
64contain any rule by default. You can write rules in these files or include rules from other files, as the following
65example shows.
66```
67"buildOption": {
68 "arkOptions": {
69 "obfuscation": {
70 "ruleOptions": {
71 "enable": true,
72 "files": ["obfuscation-rules.txt", "myrules.txt"],
73 }
74 "consumerFiles": ["consumer-rules.txt", "my-consumer-rules.txt"]
75 }
76 }
77}
78```
79
80In rule files, you can write [obfuscation options](#obfuscation-options) and [keep options](#keep-options).
81
82### Obfuscation options
83
84`-disable-obfuscation`
85
86Specifies to disable all obfuscations. If you use this option, the resulting HAP or HAR will not be obfuscated. By default,
87Arkguard only obfuscates the parameter names and local variable names by assigning random short names to them.
88
89`-enable-property-obfuscation`
90
91Specifies to obfuscate the property names. If you use this option, all property names will be obfuscated except the
92following:
93* the property names of `import/export` classes or objects. Note: Only for directly exported classes or objects, their
94property names will be kept. For example, the property name `data` in
95 ```
96 export class MyClass {
97 data: string;
98 }
99 ```
100 will not be obfuscated.
101For 'indirectly export' cases such as `export MyClass` and `let a = MyClass; export a;`, if you do not want to obfuscate
102their property names, you need to use [keep options](#keep-options) to keep them. Besides, for the property names of properties of directly exported classes or objects, like `name` and `age` in the following example, if you do not want to obfuscate them, then you also need [keep options](#keep-options) to keep them.
103 ```
104 export class MyClass {
105 person = {name: "123", age: 100};
106 }
107 ```
108* the property names defined in UI components. For example, the property names `message` and `data` in
109 ```
110 @Component struct MyExample {
111 @State message: string = "hello";
112 data: number[] = [];
113 ...
114 }
115 ```
116 will not be obfuscated.
117* the property names that are specified by [keep options](#keep-options).
118* the property names in system API list. System API list is a name set which is extracted from SDK automatically by default.
119* the property names that are string literals. For example, the property names "name" and "age" in the following code will not be obfuscated.
120 ```
121 let person = {"name": "abc"};
122 person["age"] = 22;
123 ```
124 If you want to obfuscate these string literal property names, you should addtionally use the option `-enable-toplevel-obfuscation`. For example,
125 ```
126 -enable-property-obfuscation
127 -enable-string-property-obfuscation
128 ```
129 Note: If there are string literal property names which contain special characters (that is, all characters except
130 `a-z, A-Z, 0-9, _`, for example `let obj = {"\n": 123, "": 4, " ": 5}` then we would not suggest to enable the
131 option `-enable-string-property-obfuscation`, because [keep options](#keep-options) may not allow to keep these
132 names when you do not want to obfuscate them.
133
134Specifies to obfuscate the names in the global scope. If you use this option, all global names will be obfuscated
135except the following:
136* the `import/export` global names.
137* the global names that are not declared in the current file.
138* the global names that are specified by [keep options](#keep-options).
139* the global names in system API list.
140
141`-compact`
142
143Specifies to remove unnecessary blank spaces and all line feeds. If you use this option, all code will be compressed into
144one line.
145
146`-remove-log`
147
148Specifies to remove all `console.*` statements.
149
150`-print-namecache` filepath
151
152Specifies to print the name cache that contains the mapping from the old names to new names. The cache will printed to
153the given file. If you use `-enable-property-obfuscation` or `-enable-toplevel-obfuscation`, and you want incremental
154obfuscation in the future (for example, hot fix), then you should use this option and keep the resulting cache file
155carefully.
156
157`-apply-namecache` filepath
158
159Specifies to reuse the given cache file. The old names in the cache will receive the corresponding new names specified in
160the cache. Other names will receive new random short names. This option should be used in incremental obfuscation.
161
162By default, DevEco Studio will keep and update the namecache file in the temporary cache directory and apply the cache for
163incremental compilation.
164
165### Keep options
166
167Keep options are useful only when you use `enable-property-obfuscation` or `enable-toplevel-obfuscation`.
168
169`-keep-property-name` [,modifiers,...]
170
171Specifies property names that you want to keep. For example,
172```
173-keep-property-name
174age
175firstName
176lastName
177```
178
179**What property names should be kept?**
180
181For safety, we would suggest keeping all property names that are not accessed through dot syntax.
182
183Example:
184```
185var obj = {x0: 0, x1: 0, x2: 0};
186for (var i = 0; i <= 2; i++) {
187 console.log(obj['x' + i]); // x0, x1, x2 should be kept
188}
189
190Object.defineProperty(obj, 'y', {}); // y should be kept
191console.log(obj.y);
192
193obj.s = 0;
194let key = 's';
195console.log(obj[key]); // s should be kept
196
197obj.u = 0;
198console.log(obj.u); // u can be safely obfuscated
199
200obj.t = 0;
201console.log(obj['t']); // t and 't' can be safely obfuscated when `-enable-string-property-obfuscation`, but we suggest keeping t
202
203obj['v'] = 0;
204console.log(obj['v']); // 'v' can be safely obfuscated when `-enable-string-property-obfuscation`, but we suggest keeping v
205```
206
207`-keep-global-name` [,modifiers,...]
208
209Specifies names that you want to keep in the global scope. For example,
210```
211-keep-global-name
212Person
213printPersonName
214```
215
216**What global names should be kept?**
217
218It is known that in javascript the variables in the global scope are properties of `globalThis`. So if in your code
219you access a global variable as a property, then the global name should be kept.
220
221Example:
222```
223var a = 0;
224console.log(globalThis.a); // a should be kept
225
226function foo(){}
227globalThis.foo(); // foo should be kept
228
229var c = 0;
230console.log(c); // c can be safely obfuscated
231
232function bar(){}
233bar(); // bar can be safely obfuscated
234
235class MyClass {}
236let d = new MyClass(); // MyClass can be safely obfuscated
237```
238
239`-keep-dts` filepath
240
241Specifies to keep names in the given `.d.ts` file. Here filepath can be also a directory. If so, then the names in all
242`d.ts` files under the given directory will be kept.
243If your are building HAR with this option, then the kept names will be merged into the resulting `obfuscation.txt`.
244
245### Comments
246
247You can write comments in obfuscation rule file by using `#`. The line begins with `#` is treated as comment.
248For example,
249```
250# white list for MainAbility.ets
251-keep-global-name
252MyComponent
253GlobalFunction
254
255-keep-property-name # white list for dynamic property names
256firstName
257lastName
258age
259```
260If your are building HAR, comments will not be merged into the resulting `obfuscation.txt`.
261
262### How Arkguard merges rules
263Typically there may be serveral rule files in your project. These rule files come from:
264* `ruleOptions.files` in main project (Here by main project we mean the project you are building)
265* `consumerFiles` in local dependent libraries
266* `obfuscate.txt` in remote dependent HARs
267When building your main project, all these rules will be merged by the following strategy (in pseudo code):
268```
269let `listRules` be the list of all rule files that are mentioned above.
270let finalRule = {
271 disableObfuscation: false,
272 enablePropertyObfuscation: false,
273 enableToplevelObfuscation: false,
274 compact: false,
275 removeLog: false,
276 keepPropertyName: [],
277 keepGlobalName: [],
278 keepDts: [],
279 printNamecache: string,
280 applyNamecache: string
281}
282for each file in `listRules`:
283 for each option in file:
284 switch(option) {
285 case -disable-obfuscation:
286 finalRule.disableObfuscation = true;
287 continue;
288 case -enable-property-obfuscation:
289 finalRule.enablePropertyObfuscation = true;
290 continue;
291 case -enable-toplevel-obfuscation:
292 finalRule.enableToplevelObfuscation = true;
293 continue;
294 case -compact:
295 finalRule.compact = true;
296 continue;
297 case -remove-log:
298 finalRule.removeLog = true;
299 continue;
300 case -print-namecache:
301 finalRule.printNamecache = #{specified path};
302 continue;
303 case -apply-namecache:
304 finalRule.applyNamecache = #{specified path};
305 continue;
306 case -keep-property-name:
307 finalRule.keepPropertyName.push(#{specified names});
308 continue;
309 case -keep-global-name:
310 finalRule.keepGlobalName.push(#{specified names});
311 continue;
312 case -keep-dts:
313 finalRule.keepDts.push(#{specified file path});
314 continue;
315 }
316 end-for
317end-for
318```
319The final obfuscation rules are in the object `finalRule`.
320
321If you are building HAR, the resulting `obfuscate.txt` are obtained by merging the rules from `consumerFiles` in main
322project and local dependent libraries, and `obfuscate.txt` in remote dependent HARs. The merging strategy is the same
323except:
324* The `-keep-dts` option will be converted to `-keep-global-name` and `-keep-property-name` options in the resulting
325`obfuscate.txt`.
326* The options `-print-namecache` and `apply-namecache` will be omitted and will not appear in the resulting
327`obfuscate.txt`.
328