JavaScriptObfuscatorRuntime.spec.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import { assert } from 'chai';
  2. import { TInputOptions } from '../../src/types/options/TInputOptions';
  3. import { StringArrayEncoding } from '../../src/enums/StringArrayEncoding';
  4. import { IdentifierNamesGenerator } from '../../src/enums/generators/identifier-names-generators/IdentifierNamesGenerator';
  5. import { readFileAsString } from '../helpers/readFileAsString';
  6. import { JavaScriptObfuscator } from '../../src/JavaScriptObfuscatorFacade';
  7. const getEnvironmentCode = () => `
  8. global.document = {
  9. domain: 'obfuscator.io'
  10. };
  11. `;
  12. describe('JavaScriptObfuscator runtime eval', function () {
  13. const baseOptions: TInputOptions = {
  14. controlFlowFlattening: true,
  15. controlFlowFlatteningThreshold: 1,
  16. deadCodeInjection: true,
  17. deadCodeInjectionThreshold: 1,
  18. debugProtection: true,
  19. disableConsoleOutput: true,
  20. domainLock: ['obfuscator.io'],
  21. reservedNames: ['generate', 'sha256'],
  22. rotateStringArray: true,
  23. selfDefending: true,
  24. splitStrings: true,
  25. splitStringsChunkLength: 5,
  26. stringArray: true,
  27. stringArrayEncoding: StringArrayEncoding.Rc4,
  28. stringArrayThreshold: 1,
  29. transformObjectKeys: true,
  30. unicodeEscapeSequence: true
  31. };
  32. this.timeout(100000);
  33. [
  34. {
  35. identifierNamesGenerator: IdentifierNamesGenerator.HexadecimalIdentifierNamesGenerator,
  36. renameGlobals: false
  37. },
  38. {
  39. identifierNamesGenerator: IdentifierNamesGenerator.HexadecimalIdentifierNamesGenerator,
  40. renameGlobals: true
  41. },
  42. {
  43. identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator,
  44. renameGlobals: false
  45. },
  46. {
  47. identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator,
  48. renameGlobals: true
  49. }
  50. ].forEach((options: Partial<TInputOptions>) => {
  51. const detailedDescription: string = `Identifier names generator: ${options.identifierNamesGenerator}, rename globals: ${options.renameGlobals?.toString()}`;
  52. describe(`Astring. ${detailedDescription}`, () => {
  53. it('should obfuscate code without any runtime errors after obfuscation: Variant #1 astring', () => {
  54. const code: string = readFileAsString(__dirname + '/fixtures/astring.js');
  55. const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
  56. code,
  57. {
  58. ...baseOptions,
  59. ...options
  60. }
  61. ).getObfuscatedCode();
  62. assert.equal(
  63. eval(`
  64. ${getEnvironmentCode()}
  65. ${obfuscatedCode}
  66. const code = generate({
  67. "type": "Program",
  68. "body": [
  69. {
  70. "type": "FunctionDeclaration",
  71. "id": {
  72. "type": "Identifier",
  73. "name": "test",
  74. "range": [
  75. 9,
  76. 13
  77. ]
  78. },
  79. "params": [],
  80. "body": {
  81. "type": "BlockStatement",
  82. "body": [
  83. {
  84. "type": "ReturnStatement",
  85. "argument": {
  86. "type": "Literal",
  87. "value": "foo",
  88. "raw": "'foo'",
  89. "range": [
  90. 30,
  91. 35
  92. ]
  93. },
  94. "range": [
  95. 23,
  96. 36
  97. ]
  98. }
  99. ],
  100. "range": [
  101. 17,
  102. 38
  103. ]
  104. },
  105. "generator": false,
  106. "expression": false,
  107. "async": false,
  108. "range": [
  109. 0,
  110. 38
  111. ]
  112. }
  113. ],
  114. "sourceType": "module",
  115. "range": [
  116. 0,
  117. 38
  118. ],
  119. "comments": []
  120. });
  121. eval(\`\${code} test();\`);
  122. `),
  123. 'foo'
  124. );
  125. });
  126. });
  127. describe(`Sha256. ${detailedDescription}`, () => {
  128. it('should obfuscate code without any runtime errors after obfuscation: Variant #2 sha256', () => {
  129. const code: string = readFileAsString(__dirname + '/fixtures/sha256.js');
  130. const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
  131. code,
  132. {
  133. ...baseOptions,
  134. ...options
  135. }
  136. ).getObfuscatedCode();
  137. assert.equal(
  138. eval(`
  139. ${getEnvironmentCode()}
  140. ${obfuscatedCode}
  141. sha256('test');
  142. `),
  143. '9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08'
  144. );
  145. });
  146. });
  147. });
  148. });