mirror of
https://github.com/javascript-obfuscator/javascript-obfuscator
synced 2026-08-09 12:42:29 +00:00
selfDefending test implementation
This commit is contained in:
@@ -27,6 +27,7 @@ export class OptionsNormalizer {
|
||||
let normalizedOptions: IOptions = Object.assign({}, options);
|
||||
|
||||
normalizedOptions = OptionsNormalizer.unicodeArrayRule(normalizedOptions);
|
||||
normalizedOptions = OptionsNormalizer.unicodeArrayThresholdRule(normalizedOptions);
|
||||
normalizedOptions = OptionsNormalizer.selfDefendingRule(normalizedOptions);
|
||||
|
||||
return normalizedOptions;
|
||||
@@ -55,4 +56,23 @@ export class OptionsNormalizer {
|
||||
|
||||
return options;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param options
|
||||
* @returns {IOptions}
|
||||
*/
|
||||
private static unicodeArrayThresholdRule (options: IOptions): IOptions {
|
||||
const minValue: number = 0,
|
||||
maxValue: number = 1;
|
||||
|
||||
options['unicodeArrayThreshold'] = Math.min(
|
||||
Math.max(
|
||||
options['unicodeArrayThreshold'],
|
||||
minValue
|
||||
),
|
||||
maxValue
|
||||
);
|
||||
|
||||
return options;
|
||||
}
|
||||
}
|
||||
|
||||
Vendored
+1
@@ -8,5 +8,6 @@ export interface IOptions {
|
||||
rotateUnicodeArray?: boolean;
|
||||
selfDefending?: boolean;
|
||||
unicodeArray?: boolean;
|
||||
unicodeArrayThreshold?: number;
|
||||
wrapUnicodeArrayCalls?: boolean;
|
||||
}
|
||||
|
||||
@@ -97,19 +97,20 @@ export abstract class NodeObfuscator implements INodeObfuscator {
|
||||
* @returns {string}
|
||||
*/
|
||||
protected replaceLiteralValueByUnicodeValue (nodeValue: string): string {
|
||||
let value: string = nodeValue;
|
||||
let value: string = nodeValue,
|
||||
replaceByUnicodeArrayFlag: boolean = Math.random() > this.options['unicodeArrayThreshold'];
|
||||
|
||||
if (this.options['encodeUnicodeLiterals']) {
|
||||
if (this.options['encodeUnicodeLiterals'] && replaceByUnicodeArrayFlag) {
|
||||
value = new Buffer(encodeURI(value)).toString('base64');
|
||||
}
|
||||
|
||||
value = Utils.stringToUnicode(value);
|
||||
|
||||
if (!this.options['unicodeArray']) {
|
||||
if (!this.options['unicodeArray'] || !replaceByUnicodeArrayFlag) {
|
||||
return value;
|
||||
}
|
||||
|
||||
return this.replaceLiteralValueByUnicodeArrayCall(value)
|
||||
return this.replaceLiteralValueByUnicodeArrayCall(value);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -10,5 +10,6 @@ export const DEFAULT_PRESET: IOptions = Object.freeze({
|
||||
rotateUnicodeArray: true,
|
||||
selfDefending: true,
|
||||
unicodeArray: true,
|
||||
unicodeArrayThreshold: 0.8,
|
||||
wrapUnicodeArrayCalls: true
|
||||
});
|
||||
|
||||
@@ -10,5 +10,6 @@ export const NO_CUSTOM_NODES_PRESET: IOptions = Object.freeze({
|
||||
rotateUnicodeArray: false,
|
||||
selfDefending: false,
|
||||
unicodeArray: false,
|
||||
unicodeArrayThreshold: 0,
|
||||
wrapUnicodeArrayCalls: false
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user