JavaScriptObfuscatorRuntime.spec.ts 4.9 KB

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