Merge pull request #244 from javascript-obfuscator/tagged-template-literal-fix

Disabled obfuscation of tagged template literals.
This commit is contained in:
Timofey Kachalov
2018-04-16 22:19:26 +03:00
committed by GitHub
7 changed files with 48 additions and 3 deletions
+1
View File
@@ -3,6 +3,7 @@ Change Log
v0.16.0
---
* Correct obfuscation of object rest and spread properties
* Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/243
v0.15.0
---
+1 -1
View File
File diff suppressed because one or more lines are too long
+1
View File
@@ -36,6 +36,7 @@ export enum NodeType {
Super = 'Super',
SwitchCase = 'SwitchCase',
SwitchStatement = 'SwitchStatement',
TaggedTemplateExpression = 'TaggedTemplateExpression',
TemplateLiteral = 'TemplateLiteral',
TryStatement = 'TryStatement',
UnaryExpression = 'UnaryExpression',
@@ -38,6 +38,15 @@ export class TemplateLiteralTransformer extends AbstractNodeTransformer {
return node && NodeGuards.isLiteralNode(node) && typeof node.value === 'string';
}
/**
* @param {Node} node
* @param {Node | null} parentNode
* @returns {boolean}
*/
private static isValidTemplateLiteralNode (node: ESTree.Node, parentNode: ESTree.Node): node is ESTree.TemplateLiteral {
return NodeGuards.isTemplateLiteralNode(node) && !NodeGuards.isTaggedTemplateExpressionNode(parentNode);
}
/**
* @param {TransformationStage} transformationStage
* @returns {IVisitor | null}
@@ -47,7 +56,7 @@ export class TemplateLiteralTransformer extends AbstractNodeTransformer {
case TransformationStage.Converting:
return {
leave: (node: ESTree.Node, parentNode: ESTree.Node | null) => {
if (parentNode && NodeGuards.isTemplateLiteralNode(node)) {
if (parentNode && TemplateLiteralTransformer.isValidTemplateLiteralNode(node, parentNode)) {
return this.transformNode(node, parentNode);
}
}
+8
View File
@@ -351,6 +351,14 @@ export class NodeGuards {
return node.type === NodeType.SwitchCase;
}
/**
* @param {Node} node
* @returns {boolean}
*/
public static isTaggedTemplateExpressionNode (node: ESTree.Node): node is ESTree.TaggedTemplateExpression {
return node.type === NodeType.TaggedTemplateExpression;
}
/**
* @param {Node} node
* @returns {boolean}
@@ -132,7 +132,28 @@ describe('TemplateLiteralTransformer', () => {
}
);
assert.match(obfuscationResult.getObfuscatedCode(), /^var *test *= *0x1 *\+ *0x1 *\+ *'\\x20abc\\x20' *\+ *\(0x1 *\+ *0x1\);$/);
assert.match(
obfuscationResult.getObfuscatedCode(),
/^var *test *= *0x1 *\+ *0x1 *\+ *'\\x20abc\\x20' *\+ *\(0x1 *\+ *0x1\);$/
);
});
});
describe('Variant #6: tagged template literal', () => {
it('shouldn\'t transform es6 tagged template literal to es5', () => {
const code: string = readFileAsString(__dirname + '/fixtures/tagged-template-literal.js');
const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
code,
{
...NO_ADDITIONAL_NODES_PRESET,
unicodeEscapeSequence: false
}
);
assert.match(
obfuscationResult.getObfuscatedCode(),
/tag`foo *\${0x1 *\+ *0x1} *bar`;/
);
});
});
});
@@ -0,0 +1,5 @@
function tag(strings) {
console.log(strings.raw[0]);
}
tag`foo ${1 + 1} bar`;