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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
Change Log

v5.3.1
---
* Fixed class expression name references inside class body being incorrectly resolved to an import binding with the same name, causing broken code at runtime. Fixes https://github.com/javascript-obfuscator/javascript-obfuscator/issues/1386

v5.3.0
---
* Add Pro API support to CLI
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "javascript-obfuscator",
"version": "5.3.0",
"version": "5.3.1",
"description": "JavaScript obfuscator",
"keywords": [
"obfuscator",
Expand Down
6 changes: 5 additions & 1 deletion src/analyzers/scope-analyzer/ScopeAnalyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,11 @@ export class ScopeAnalyzer implements IScopeAnalyzer {
(definition: eslintScope.Definition) => definition.type === 'ClassName'
);

return isValidClassNameVariable && variable.name === classNameVariable.name;
const isImportBinding: boolean = variable.defs.some(
(definition: eslintScope.Definition) => definition.type === 'ImportBinding'
);

return isValidClassNameVariable && variable.name === classNameVariable.name && !isImportBinding;
}
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { evalLocal } from '../../../helpers/evalLocal';
import { readFileAsString } from '../../../helpers/readFileAsString';

import { JavaScriptObfuscator } from '../../../../src/JavaScriptObfuscatorFacade';
import { NO_ADDITIONAL_NODES_PRESET } from '../../../../src/options/presets/NoCustomNodes';

describe('ScopeAnalyzer', () => {
describe('analyze', () => {
Expand Down Expand Up @@ -121,5 +122,64 @@ describe('ScopeAnalyzer', () => {
});
});
});

describe('Variant #3: class expression name shadowing import binding', () => {
const samplesCount: number = 50;

let obfuscatedCode: string;
let importBindingName: string | null;
let classExpressionName: string | null;

beforeEach(() => {
const code: string = readFileAsString(
__dirname + '/fixtures/class-expression-name-shadowing-import.js'
);

importBindingName = null;
classExpressionName = null;

for (let i = 0; i < samplesCount; i++) {
obfuscatedCode = JavaScriptObfuscator.obfuscate(code, {
...NO_ADDITIONAL_NODES_PRESET,
compact: false,
stringArray: false,
seed: i
}).getObfuscatedCode();

const importMatch = obfuscatedCode.match(/import\s*\{\s*i\s+as\s+(\w+)\s*\}/);
const classMatch = obfuscatedCode.match(/=\s*class\s+(\w+)\s*\{/);

if (importMatch && classMatch) {
importBindingName = importMatch[1];
classExpressionName = classMatch[1];

if (importBindingName !== classExpressionName) {
break;
}
}
}
});

it('should not rename class expression self-references to the import binding', () => {
assert.isNotNull(importBindingName, 'should find import binding name');
assert.isNotNull(classExpressionName, 'should find class expression name');

const getInstanceMatch = obfuscatedCode.match(/getInstance[^}]*\{([^}]*)\}/s);
assert.isNotNull(getInstanceMatch, 'should find getInstance body');

const getInstanceBody: string = getInstanceMatch![1];

assert.include(
getInstanceBody,
`new ${classExpressionName!}()`,
'new <class>() inside getInstance should reference the class expression name'
);
assert.notInclude(
getInstanceBody,
`new ${importBindingName!}()`,
'new <import>() inside getInstance should NOT reference the import binding'
);
});
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { i as e } from './runtime.js';

function factory() {
return 'factory-result';
}

var P = class e {
static instance;

static getInstance() {
return ((e.instance ||= new e()), e.instance);
}
};

var x = e(factory(), 1);

export { P, x };
Loading