Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ v2.16.0
* `stringArrayWrappersType: 'function'` now generates different indexes between each wrapper inside the same lexical scope
* `stringArrayWrappersType: 'function'` now generates different parameters order between each wrapper inside the same lexical scope
* `stringArrayWrappersType: 'function'` now appends `FunctionDeclaration` functions instead of `FunctionExpression` functions. This allows to append these wrappers at random positions inside each scope
* `renameProperties` option now won't generate duplicated property names in some cases

v2.15.6
---
Expand Down
2 changes: 1 addition & 1 deletion dist/index.browser.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.cli.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ export class RenamePropertiesReplacer implements IRenamePropertiesReplacer {
*/
private replacePropertyName (propertyName: string): string {
if (this.isReservedName(propertyName)) {
this.identifierNamesGenerator.preserveName(propertyName);

return propertyName;
}

Expand Down
40 changes: 8 additions & 32 deletions test/dev/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,19 @@ import { NO_ADDITIONAL_NODES_PRESET } from '../../src/options/presets/NoCustomNo

let obfuscationResult = JavaScriptObfuscator.obfuscate(
`
function foo () {
console.log('foo', 1);
console.log('foo', 2);
console.log('foo', 3);

function bar() {
console.log('bar', 1);
console.log('bar', 2);
console.log('bar', 3);

function baz() {
console.log('baz', 1);
console.log('baz', 2);
console.log('baz', 3);
}

baz();
}

bar();
}

foo();
var object = {
b: 'field',
bar: 'value'
};
`,
{
...NO_ADDITIONAL_NODES_PRESET,
seed: 1,
compact: false,
simplify: false,
stringArray: true,
stringArrayThreshold: 1,
stringArrayIndexShift: true,
stringArrayWrappersChainedCalls: true,
stringArrayWrappersCount: 3,
stringArrayWrappersType: 'function',
stringArrayWrappersParametersMaxCount: 5,
identifierNamesGenerator: 'mangled'
renameProperties: true,
renamePropertiesMode: 'safe',
identifierNamesGenerator: 'mangled',
reservedNames: ['^a$']
}
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { JavaScriptObfuscator } from '../../../../../src/JavaScriptObfuscatorFac
describe('RenamePropertiesTransformer', () => {
describe('transformNode', () => {
describe('Mode: `unsafe`', () => {
describe('Hexadecimal identifier names generator', () => {
describe('Variant #1: Hexadecimal identifier names generator', () => {
describe('Variant #1: base properties rename', () => {
const property1RegExp: RegExp = /'(_0x[a-f0-9]{4,6})': *0x1/;
const property2RegExp: RegExp = /'(_0x[a-f0-9]{4,6})': *0x2/;
Expand Down Expand Up @@ -54,7 +54,7 @@ describe('RenamePropertiesTransformer', () => {
});
});

describe('Mangled identifier names generator', () => {
describe('Variant #2: Mangled identifier names generator', () => {
describe('Variant #1: base properties mangle', () => {
const property1RegExp: RegExp = /'a': *0x1/;
const property2RegExp: RegExp = /'b': *0x2/;
Expand Down Expand Up @@ -335,7 +335,7 @@ describe('RenamePropertiesTransformer', () => {
});
});

describe('Ignored literal node type', () => {
describe('Variant #3: Ignored literal node type', () => {
describe('Variant #1: boolean literal node', () => {
const regExp: RegExp = /var obj *= *{}; *obj\[!!\[]] *= *0x1;/;

Expand All @@ -359,6 +359,31 @@ describe('RenamePropertiesTransformer', () => {
});
});
});

describe('Variant #4: Prevent generation of the property names that are equal to the existing object property names', () => {
const regExp: RegExp = /var object *= *{'b': *'field', *'c': *'value'};/;

let obfuscatedCode: string;

before(() => {
const code: string = readFileAsString(__dirname + '/fixtures/duplicated-generated-names-1.js');

obfuscatedCode = JavaScriptObfuscator.obfuscate(
code,
{
...NO_ADDITIONAL_NODES_PRESET,
renameProperties: true,
renamePropertiesMode: RenamePropertiesMode.Unsafe,
identifierNamesGenerator: 'mangled',
reservedNames: ['^a$']
}
).getObfuscatedCode();
});

it('Match #1: should skip literal property with invalid type', () => {
assert.match(obfuscatedCode, regExp);
});
});
});

describe('Mode: `safe`', () => {
Expand Down Expand Up @@ -406,6 +431,31 @@ describe('RenamePropertiesTransformer', () => {
assert.match(obfuscatedCode, referencesRegExp);
});
});

describe('Variant #2: Prevent generation of the property names that are equal to the existing object property names', () => {
const regExp: RegExp = /var object *= *{'b': *'field', *'c': *'value'};/;

let obfuscatedCode: string;

before(() => {
const code: string = readFileAsString(__dirname + '/fixtures/duplicated-generated-names-1.js');

obfuscatedCode = JavaScriptObfuscator.obfuscate(
code,
{
...NO_ADDITIONAL_NODES_PRESET,
renameProperties: true,
renamePropertiesMode: RenamePropertiesMode.Safe,
identifierNamesGenerator: 'mangled',
reservedNames: ['^a$']
}
).getObfuscatedCode();
});

it('Match #1: should skip literal property with invalid type', () => {
assert.match(obfuscatedCode, regExp);
});
});
});

describe('Property identifier names from property identifier names cache', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
var object = {
b: 'field',
bar: 'value'
};