diff --git a/arkguard/src/transformers/rename/RenameIdentifierTransformer.ts b/arkguard/src/transformers/rename/RenameIdentifierTransformer.ts index 420211daba8162b57e4e936de01f659aa0c8022c..b8475375c1d22cacc0a37b759f0ce81550d7cc88 100644 --- a/arkguard/src/transformers/rename/RenameIdentifierTransformer.ts +++ b/arkguard/src/transformers/rename/RenameIdentifierTransformer.ts @@ -14,6 +14,7 @@ */ import { + SyntaxKind, factory, forEachChild, isBreakOrContinueStatement, @@ -22,6 +23,7 @@ import { isIdentifier, isImportSpecifier, isLabeledStatement, + isMetaProperty, isSourceFile, isStructDeclaration, setParentRecursive, @@ -433,6 +435,10 @@ namespace secharmony { return node; } + if (NodeUtils.isNewTargetNode(node)) { + return node; + } + let sym: Symbol | undefined = checker.getSymbolAtLocation(shadowNode); let mangledPropertyNameOfNoSymbolImportExport = ''; if ((!sym || sym.name === 'default')) { diff --git a/arkguard/src/utils/NodeUtils.ts b/arkguard/src/utils/NodeUtils.ts index d3911af8c5dc1330c30cbf912f3ea3fa2b15e5a3..0bf806841c74312b843acdd9a95b8a9e86955a39 100644 --- a/arkguard/src/utils/NodeUtils.ts +++ b/arkguard/src/utils/NodeUtils.ts @@ -13,7 +13,7 @@ * limitations under the License. */ -import type {ClassElement, Expression, Node, ObjectBindingPattern, SourceFile, StructDeclaration} from 'typescript'; +import type {ClassElement, Expression, Identifier, Node, ObjectBindingPattern, SourceFile, StructDeclaration} from 'typescript'; import { SyntaxKind, factory, @@ -29,6 +29,7 @@ import { isEnumMember, isGetAccessor, isIdentifier, + isMetaProperty, isMethodDeclaration, isMethodSignature, isParameter, @@ -211,4 +212,11 @@ export class NodeUtils { public static isInETSFile(node: Node | undefined): boolean { return !!node && NodeUtils.getSourceFileOfNode(node).fileName.endsWith('.ets'); } + + public static isNewTargetNode(node: Identifier): boolean { + if (isMetaProperty(node.parent) && node.parent.keywordToken === SyntaxKind.NewKeyword && node.escapedText === 'target') { + return true; + } + return false; + } } diff --git a/arkguard/test/grammar/target/newTargetConstructor_1.js b/arkguard/test/grammar/target/newTargetConstructor_1.js new file mode 100644 index 0000000000000000000000000000000000000000..e77c08826e151a7e83bf61b74bc71a56a20ea4f1 --- /dev/null +++ b/arkguard/test/grammar/target/newTargetConstructor_1.js @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed 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. + */ + +class foo { + constructor() { + console.log(new.target.name); + } +} + +class Foo extends foo { + constructor() { + super(); + } +} + +var f = new foo(); +var F = new Foo(); + diff --git a/arkguard/test/grammar/target/newTargetConstructor_1_expected.txt b/arkguard/test/grammar/target/newTargetConstructor_1_expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..fe62aaff8891ea4ac41ce6d3127ea3665491d07b --- /dev/null +++ b/arkguard/test/grammar/target/newTargetConstructor_1_expected.txt @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed 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. + */ +class g { + constructor() { + console.log(new.target.name); + } +} +class h extends g { + constructor() { + super(); + } +} +var f = new g(); +var i = new h(); diff --git a/arkguard/test/grammar/target/newTargetConstructor_2.js b/arkguard/test/grammar/target/newTargetConstructor_2.js new file mode 100644 index 0000000000000000000000000000000000000000..3a4f97d27c2aaa49f8a406dc3dc4cfd4a21fe300 --- /dev/null +++ b/arkguard/test/grammar/target/newTargetConstructor_2.js @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed 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. + */ + +class Human { + constructor() { + console.log(new.target); + } +} + +class Woman extends Human { + constructor() { + super(); + } +} + +new Human(); +new Woman(); \ No newline at end of file diff --git a/arkguard/test/grammar/target/newTargetConstructor_2_expected.txt b/arkguard/test/grammar/target/newTargetConstructor_2_expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..61fb97dd556c2af6298da8d4aa5734fd25e7d496 --- /dev/null +++ b/arkguard/test/grammar/target/newTargetConstructor_2_expected.txt @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed 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. + */ +class j { + constructor() { + console.log(new.target); + } +} +class k extends j { + constructor() { + super(); + } +} +new j(); +new k(); diff --git a/arkguard/test/grammar/target/newTargetFunction_1.js b/arkguard/test/grammar/target/newTargetFunction_1.js new file mode 100644 index 0000000000000000000000000000000000000000..3054b5d7fab24b7b1912e0ac69bd146168a0d41e --- /dev/null +++ b/arkguard/test/grammar/target/newTargetFunction_1.js @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed 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. + */ + +function P() { + this.name = 'John'; + console.log(new.target); +} + +function F() { + if(!new.target){ + console.log("Not new"); + } +} + +P(); +new P(); +F(); +new F(); \ No newline at end of file diff --git a/arkguard/test/grammar/target/newTargetFunction_1_expected.txt b/arkguard/test/grammar/target/newTargetFunction_1_expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..2de673e865be09eec006e4b2f2250673d2e769c5 --- /dev/null +++ b/arkguard/test/grammar/target/newTargetFunction_1_expected.txt @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed 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. + */ +function l() { + this.name = 'John'; + console.log(new.target); +} +function i() { + if (!new.target) { + console.log("Not new"); + } +} +l(); +new l(); +i(); +new i(); diff --git a/arkguard/test/grammar/target/newTargetFunction_2.js b/arkguard/test/grammar/target/newTargetFunction_2.js new file mode 100644 index 0000000000000000000000000000000000000000..4110c368bdff6a320e49b26b12210114c47dd14d --- /dev/null +++ b/arkguard/test/grammar/target/newTargetFunction_2.js @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed 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. + */ + +export var kmp = function(_) { + class Exception { + } + + class KMossException extends Exception { + constructor(message, cause) { + super(); + return new.target.new_kntr_k$(message, cause); + } + + static new_kntr_k$(message, cause) { + return new KMossException(); + } + } +} \ No newline at end of file diff --git a/arkguard/test/grammar/target/newTargetFunction_2_expected.txt b/arkguard/test/grammar/target/newTargetFunction_2_expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..7282b2289bdba71daec014dad6270cd5e961910c --- /dev/null +++ b/arkguard/test/grammar/target/newTargetFunction_2_expected.txt @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed 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. + */ +export var kmp = function (n) { + class o { + } + class p extends o { + constructor(s, t) { + super(); + return new.target.m(s, t); + } + static m(q, r) { + return new p(); + } + } +};