JavaScriptObfuscatorRuntime.spec.ts 5.0 KB

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