JavaScriptObfuscatorRuntime.spec.ts 5.3 KB

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