Skip to content

Commit 0094e7c

Browse files
committed
Add force convert of unicode control characters to the unicode escape sequence
Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues
1 parent a5ad91e commit 0094e7c

File tree

7 files changed

+58
-45
lines changed

7 files changed

+58
-45
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ v2.2.0
1616
v2.1.0
1717
---
1818
* **New API:** `getOptionsByPreset` allows to get options for the passed options preset name
19+
* Add force convert of unicode control characters to the unicode escape sequence. Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues
1920

2021
v2.0.0
2122
---

dist/index.browser.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.cli.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/utils/EscapeSequenceEncoder.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,21 @@ import { IEscapeSequenceEncoder } from '../interfaces/utils/IEscapeSequenceEncod
44

55
@injectable()
66
export class EscapeSequenceEncoder implements IEscapeSequenceEncoder {
7+
/**
8+
* https://bytefreaks.net/gnulinux/regular-expression-to-match-any-ascii-character
9+
*
10+
* @type {RegExp}
11+
*/
12+
private static readonly ASCIICharactersRegExp: RegExp = /[\x00-\x7F]/;
13+
14+
/**
15+
* https://en.wikipedia.org/wiki/List_of_Unicode_characters
16+
* \x00-\x1F\x7F-\x9F are the control unicode characters
17+
*
18+
* @type {RegExp}
19+
*/
20+
private static readonly forceEscapeCharactersRegExp: RegExp = /[\x00-\x1F\x7F-\x9F'"\\\s]/;
21+
722
/**
823
* @type {Map<string, string>}
924
*/
@@ -23,18 +38,19 @@ export class EscapeSequenceEncoder implements IEscapeSequenceEncoder {
2338

2439
const radix: number = 16;
2540
const replaceRegExp: RegExp = new RegExp('[\\s\\S]', 'g');
26-
const escapeSequenceRegExp: RegExp = new RegExp('[\'\"\\\\\\s]');
27-
const regExp: RegExp = new RegExp('[\\x00-\\x7F]');
2841

2942
let prefix: string;
3043
let template: string;
3144

3245
const result: string = string.replace(replaceRegExp, (character: string): string => {
33-
if (!encodeAllSymbols && !escapeSequenceRegExp.exec(character)) {
46+
const shouldEncodeCharacter: boolean = encodeAllSymbols
47+
|| EscapeSequenceEncoder.forceEscapeCharactersRegExp.test(character);
48+
49+
if (!shouldEncodeCharacter) {
3450
return character;
3551
}
3652

37-
if (regExp.exec(character)) {
53+
if (EscapeSequenceEncoder.ASCIICharactersRegExp.test(character)) {
3854
prefix = '\\x';
3955
template = '00';
4056
} else {

test/dev/dev.ts

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,17 @@
11
'use strict';
22

33
import { NO_ADDITIONAL_NODES_PRESET } from '../../src/options/presets/NoCustomNodes';
4-
import { IdentifierNamesGenerator } from '../../src/enums/generators/identifier-names-generators/IdentifierNamesGenerator';
5-
import { StringArrayWrappersType } from '../../src/enums/node-transformers/string-array-transformers/StringArrayWrappersType';
6-
import { StringArrayEncoding } from '../../src/enums/node-transformers/string-array-transformers/StringArrayEncoding';
74

85
(function () {
96
const JavaScriptObfuscator: any = require('../../index');
107

118
let obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
129
`
13-
const foo = 'aaa';
14-
15-
function test (a, b) {
16-
const bar = 'bbb';
17-
const baz = 'ccc';
18-
19-
function test1 (a, b) {
20-
const bark = 'ddd';
21-
const hawk = 'eee';
22-
23-
function test2 (a, b) {
24-
const bark = 'ddd';
25-
const hawk = 'eee';
26-
27-
return bark + hawk;
28-
}
29-
30-
return bark + hawk;
31-
}
32-
33-
return bar + baz + test1();
34-
}
35-
36-
foo + test();
10+
const s = '\0ab cd';
3711
`,
3812
{
3913
...NO_ADDITIONAL_NODES_PRESET,
40-
compact: false,
41-
identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator,
42-
stringArray: true,
43-
stringArrayThreshold: 1,
44-
stringArrayEncoding: [
45-
StringArrayEncoding.None,
46-
StringArrayEncoding.Rc4
47-
],
48-
stringArrayWrappersChainedCalls: true,
49-
stringArrayWrappersCount: 5,
50-
stringArrayWrappersType: StringArrayWrappersType.Function
14+
compact: false
5115
}
5216
).getObfuscatedCode();
5317

test/unit-tests/utils/EscapeSequenceEncoder.spec.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'reflect-metadata';
2+
13
import { assert } from 'chai';
24

35
import { InversifyContainerFacade } from '../../../src/container/InversifyContainerFacade';
@@ -48,5 +50,35 @@ describe('EscapeSequenceEncoder', () => {
4850
assert.equal(actualString, expectedString);
4951
});
5052
});
53+
54+
describe('Variant #3: non-ascii character`', () => {
55+
const string: string = 'тест';
56+
const expectedString: string = '\\u0442\\u0435\\u0441\\u0442';
57+
58+
let actualString: string;
59+
60+
before(() => {
61+
actualString = escapeSequenceEncoder.encode(string, true);
62+
});
63+
64+
it('should return a string where all non-ascii characters are encoded', () => {
65+
assert.equal(actualString, expectedString);
66+
});
67+
});
68+
69+
describe('Variant #4: unicode control character`', () => {
70+
const string: string = '\x00\x1F\x7F\x9F';
71+
const expectedString: string = '\\x00\\x1f\\x7f\\u009f';
72+
73+
let actualString: string;
74+
75+
before(() => {
76+
actualString = escapeSequenceEncoder.encode(string, false);
77+
});
78+
79+
it('should return a string where all unicode control characters are encoded', () => {
80+
assert.equal(actualString, expectedString);
81+
});
82+
});
5183
});
5284
});

0 commit comments

Comments
 (0)