Skip to content

Add dangerously-overwrite option to CLI and API #1305

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,13 @@ javascript-obfuscator ./dist [options]

javascript-obfuscator ./dist --output ./dist/obfuscated [options]
// creates a folder structure with obfuscated files under `./dist/obfuscated` path

#### Obfuscate directory recursively and overwrite original files

Usage:
```sh
javascript-obfuscator ./dist --dangerously-overwrite true [options]
// obfuscates all `.js` files under `./dist` directory and overwrites the original files with obfuscated content
```

Obfuscation of all `.js` files under input directory. If this directory contains already obfuscated files with `-obfuscated` postfix - these files will ignored.
Expand Down Expand Up @@ -457,6 +464,7 @@ Following options are available for the JS Obfuscator:
--target <string> [browser, browser-no-eval, node]
--transform-object-keys <boolean>
--unicode-escape-sequence <boolean>
--dangerously-overwrite <boolean>
```

<!-- ##options-start## -->
Expand Down
9 changes: 8 additions & 1 deletion src/cli/JavaScriptObfuscatorCLI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,11 @@ export class JavaScriptObfuscatorCLI implements IInitializable {
'Allows to enable/disable string conversion to unicode escape sequence',
BooleanSanitizer
)
.option(
'--dangerously-overwrite <boolean>',
'Enables overwriting the original files with obfuscated content',
BooleanSanitizer
)
.parse(this.rawArguments);
}

Expand All @@ -463,7 +468,9 @@ export class JavaScriptObfuscatorCLI implements IInitializable {
*/
private processSourceCodeData (sourceCodeData: IFileData[]): void {
sourceCodeData.forEach(({ filePath, content }: IFileData, index: number) => {
const outputCodePath: string = this.obfuscatedCodeFileUtils.getOutputCodePath(filePath);
const outputCodePath: string = this.inputCLIOptions.dangerouslyOverwrite
? filePath
: this.obfuscatedCodeFileUtils.getOutputCodePath(filePath);

try {
Logger.log(
Expand Down
12 changes: 12 additions & 0 deletions src/cli/utils/ObfuscatedCodeFileUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ export class ObfuscatedCodeFileUtils {
* @returns {string}
*/
public getOutputCodePath (filePath: string): string {
if (this.options.dangerouslyOverwrite) {
return this.getOutputCodePathForOverwrite(filePath);
}

const normalizedFilePath: string = path.normalize(filePath);
const normalizedRawOutputPath: string | null = this.options.output
? path.normalize(this.options.output)
Expand Down Expand Up @@ -146,4 +150,12 @@ export class ObfuscatedCodeFileUtils {
encoding: JavaScriptObfuscatorCLI.encoding
});
}
/**
* @param {string} filePath
* @returns {string}
*/
private getOutputCodePathForOverwrite (filePath: string): string {
return path.normalize(filePath);
}

}
1 change: 1 addition & 0 deletions src/interfaces/options/ICLIOptions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { IOptions } from './IOptions';

export interface ICLIOptions extends IOptions {
readonly dangerouslyOverwrite: boolean;
readonly config: string;
readonly exclude: string[];
readonly identifierNamesCachePath: string;
Expand Down
68 changes: 68 additions & 0 deletions test/functional-tests/cli/JavaScriptObfuscatorCLI.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,74 @@ describe('JavaScriptObfuscatorCLI', function (): void {
});
});
});

describe('Variant #5: obfuscation of directory with `--dangerously-overwrite` option', () => {
const directoryPath: string = path.join(fixturesDirName, 'directory-obfuscation');
const outputFileName1: string = 'foo.js';
const outputFileName2: string = 'bar.js';
const outputFileName3: string = 'baz.js';
const readFileEncoding = 'utf8';
const regExp1: RegExp = /^var a1_0x(\w){4,6} *= *0x1;$/;
const regExp2: RegExp = /^var a0_0x(\w){4,6} *= *0x2;$/;

let outputFixturesFilePath1: string,
outputFixturesFilePath2: string,
outputFixturesFilePath3: string,
isFileExist1: boolean,
isFileExist2: boolean,
isFileExist3: boolean,
fileContent1: string,
fileContent2: string;

before(() => {
outputFixturesFilePath1 = path.join(directoryPath, outputFileName1);
outputFixturesFilePath2 = path.join(directoryPath, outputFileName2);
outputFixturesFilePath3 = path.join(directoryPath, outputFileName3);

JavaScriptObfuscatorCLI.obfuscate([
'node',
'javascript-obfuscator',
directoryPath,
'--dangerously-overwrite',
'true',
'--rename-globals',
'true'
]);

isFileExist1 = fs.existsSync(outputFixturesFilePath1);
isFileExist2 = fs.existsSync(outputFixturesFilePath2);
isFileExist3 = fs.existsSync(outputFixturesFilePath3);

fileContent1 = fs.readFileSync(outputFixturesFilePath1, readFileEncoding);
fileContent2 = fs.readFileSync(outputFixturesFilePath2, readFileEncoding);
});

it(`should overwrite file \`${outputFileName1}\` with obfuscated code in \`${fixturesDirName}\` directory`, () => {
assert.equal(isFileExist1, true);
});

it(`should overwrite file \`${outputFileName2}\` with obfuscated code in \`${fixturesDirName}\` directory`, () => {
assert.equal(isFileExist2, true);
});

it(`shouldn't create file \`${outputFileName3}\` in \`${fixturesDirName}\` directory`, () => {
assert.equal(isFileExist3, false);
});

it(`match #1: should overwrite file with obfuscated code with prefixed identifier`, () => {
assert.match(fileContent1, regExp1);
});

it(`match #2: should overwrite file with obfuscated code with prefixed identifier`, () => {
assert.match(fileContent2, regExp2);
});

after(() => {
// instead recreate foo and bar files exactly as they were instead of deleting them
fs.writeFileSync(outputFixturesFilePath1, 'var foo = 1;');
fs.writeFileSync(outputFixturesFilePath2, 'var bar = 2;');
});
});
});

describe('`--sourceMap` option is set', () => {
Expand Down