• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import path from 'path';
17import {ApiExtractor} from './ApiExtractor';
18import {ListUtil} from '../utils/ListUtil';
19import type {IOptions} from '../configs/IOptions';
20import { stringPropsSet, structPropsSet, enumPropsSet } from '../utils/OhsUtil';
21import type { MergedConfig } from '../ArkObfuscator';
22
23// The interface of settings for collect while lists
24export interface ScanProjectConfig {
25  mPropertyObfuscation?: boolean,
26  mKeepStringProperty?: boolean,
27  mExportObfuscation?: boolean,
28  mkeepFilesAndDependencies?: Set<string>,
29  isHarCompiled?: boolean,
30  mStripSystemApiArgs?: boolean,
31  mEnableAtKeep: boolean,
32  scanDecorator?: boolean;
33}
34
35// Settings for collect white lists.
36export let scanProjectConfig: ScanProjectConfig = {
37  mEnableAtKeep: false
38};
39
40/**
41 * if rename property is not open, api read and extract can be skipped
42 *
43 * init plugin, read api info of openHarmony sdk and generate file of reserved name, property and string.
44 * @param sdkDir absolute path like D:\\HuaweiApp\\ohsdk
45 * @param outputDir
46 */
47export function initPlugin(sdkDir: string, outputDir: string): void {
48  // create sdk api file if not exist
49  const ohSdkPath: string = path.resolve(sdkDir);
50  if (!ohSdkPath) {
51    console.error('SDK path is not found.');
52  }
53
54  const apiVersions: string[] = [''];
55
56  apiVersions.forEach((versionString) => {
57    ApiExtractor.parseOhSdk(ohSdkPath, versionString, true, outputDir);
58  });
59}
60
61/**
62 * need read api info or not
63 * @param customProfiles
64 */
65export function needReadApiInfo(customProfiles: IOptions): boolean {
66  return isEnabledPropertyObfuscation(customProfiles) || customProfiles.mExportObfuscation;
67}
68
69export function isEnabledPropertyObfuscation(customProfiles: IOptions): boolean {
70  return (customProfiles.mNameObfuscation &&
71    customProfiles.mNameObfuscation.mEnable &&
72    customProfiles.mNameObfuscation.mRenameProperties);
73}
74
75export function initScanProjectConfig(
76  customProfiles: IOptions,
77  isHarCompiled?: boolean,
78  scanDecorator: boolean = false
79): void {
80  scanProjectConfig.mPropertyObfuscation = customProfiles.mNameObfuscation?.mRenameProperties;
81  scanProjectConfig.mKeepStringProperty = customProfiles.mNameObfuscation?.mKeepStringProperty;
82  scanProjectConfig.mExportObfuscation = customProfiles.mExportObfuscation;
83  scanProjectConfig.mkeepFilesAndDependencies = customProfiles.mKeepFileSourceCode?.mkeepFilesAndDependencies;
84  scanProjectConfig.isHarCompiled = isHarCompiled;
85  scanProjectConfig.mEnableAtKeep = customProfiles.mNameObfuscation?.mEnableAtKeep;
86  scanProjectConfig.scanDecorator = scanDecorator;
87}
88
89/**
90 * Initialize scanProjectConfig by MergeConfig
91 * @param mergedConfig
92 */
93export function initScanProjectConfigByMergeConfig(config: MergedConfig): void {
94  scanProjectConfig.mStripSystemApiArgs = config.options.stripSystemApiArgs;
95}
96
97/**
98 * Reset scanProjectConfig
99 */
100export function resetScanProjectConfig(): void {
101  scanProjectConfig.mStripSystemApiArgs = undefined;
102}
103
104export interface ReseverdSetForArkguard {
105  structPropertySet: Set<string> | undefined;
106  stringPropertySet: Set<string> | undefined;
107  exportNameAndPropSet: Set<string> | undefined;
108  exportNameSet: Set<string> | undefined;
109  enumPropertySet: Set<string> | undefined;
110}
111
112/**
113 * read project reserved properties by collected paths
114 * @param filesForCompilation set collection of files
115 * @param customProfiles
116 */
117export function readProjectPropertiesByCollectedPaths(filesForCompilation: Set<string>,
118  customProfiles: IOptions, isHarCompiled: boolean, scanDecorator: boolean = false): ReseverdSetForArkguard {
119  const apiType = ApiExtractor.ApiType;
120  let scanningCommonType = undefined;
121  if (needReadApiInfo(customProfiles)) {
122    scanningCommonType = apiType.PROJECT;
123  } else {
124    scanningCommonType = apiType.CONSTRUCTOR_PROPERTY;
125  }
126
127  initScanProjectConfig(customProfiles, isHarCompiled, scanDecorator);
128
129  stringPropsSet.clear();
130
131  const exportWhiteList = ApiExtractor.parseFileByPaths(filesForCompilation, scanningCommonType);
132  const exportNamesAndProperties: Set<string> | undefined = exportWhiteList.reservedExportPropertyAndName;
133  const exportNames: Set<string> | undefined = exportWhiteList.reservedExportNames;
134
135  // if -enable-property-obfuscation, collect structPropsSet, exportNamesAndProperties and
136  // stringPropsSet(if -enable-string-property-obufscation is not enabled) as whitelists.
137  let exportNameAndPropSet: Set<string>;
138  let structPropertySet: Set<string>;
139  let stringPropertySet: Set<string>;
140  let enumPropertySet: Set<string>;
141  if (isEnabledPropertyObfuscation(customProfiles)) {
142    exportNameAndPropSet = new Set(exportNamesAndProperties);
143    structPropertySet = new Set(structPropsSet);
144    enumPropertySet = new Set(enumPropsSet);
145    if (scanProjectConfig.mKeepStringProperty) {
146      stringPropertySet = new Set(stringPropsSet);
147    }
148  }
149  structPropsSet.clear();
150  stringPropsSet.clear();
151  enumPropsSet.clear();
152
153  let exportNameSet: Set<string>;
154  if (scanProjectConfig.mExportObfuscation) {
155    exportNameSet = new Set(exportNames);
156  }
157
158  // scanProjectConfig needs to be cleared to prevent affecting incremental compilation
159  scanProjectConfig = {
160    mEnableAtKeep: false
161  };
162
163  return {
164    structPropertySet: structPropertySet,
165    stringPropertySet: stringPropertySet,
166    exportNameAndPropSet: exportNameAndPropSet,
167    exportNameSet: exportNameSet,
168    enumPropertySet: enumPropertySet,
169  };
170}
171