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
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.

7 changes: 2 additions & 5 deletions src/options/Options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import { HIGH_OBFUSCATION_PRESET } from './presets/HighObfuscation';

import { ValidationErrorsFormatter } from './ValidationErrorsFormatter';
import { IsAllowedForObfuscationTargets } from './validators/IsAllowedForObfuscationTargets';
import { IsDomainDestUrl } from './validators/IsDomainDestUrl';
import { IsIdentifierNamesCache } from './validators/IsIdentifierNamesCache';

@injectable()
Expand Down Expand Up @@ -137,11 +138,7 @@ export class Options implements IOptions {
/**
* @type {string}
*/
@IsString()
@IsAllowedForObfuscationTargets([
ObfuscationTarget.Browser,
ObfuscationTarget.BrowserNoEval,
])
@IsDomainDestUrl()
public readonly domainDest!: string;

/**
Expand Down
30 changes: 30 additions & 0 deletions src/options/validators/IsDomainDestUrl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { IsUrl, ValidateIf } from 'class-validator';

import { TInputOptions } from '../../types/options/TInputOptions';

import { IOptions } from '../../interfaces/options/IOptions';

import { ObfuscationTarget } from '../../enums/ObfuscationTarget';

import { DEFAULT_PRESET } from '../presets/Default';

import { IsAllowedForObfuscationTargets } from './IsAllowedForObfuscationTargets';

/**
* @returns {PropertyDecorator}
*/
export const IsDomainDestUrl = (): PropertyDecorator => {
return (target: any, key: string | symbol): void => {
ValidateIf(({domainDest}: TInputOptions) => {
return domainDest !== DEFAULT_PRESET.domainDest;
})(target, key);
IsUrl({
require_protocol: false,
require_host: false
})(target, key);
IsAllowedForObfuscationTargets([
ObfuscationTarget.Browser,
ObfuscationTarget.BrowserNoEval,
])(target, <keyof IOptions>key);
};
};
104 changes: 104 additions & 0 deletions test/functional-tests/options/domain-dest/Validation.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import { assert } from 'chai';

import { JavaScriptObfuscator } from '../../../../src/JavaScriptObfuscatorFacade';

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

describe('`domainDest` validation', () => {
describe('IsDomainDestUrl', () => {
describe('Variant #1: positive validation', () => {
describe('Variant #1: string with url containing protocol, host and some path', () => {
let testFunc: () => string;

beforeEach(() => {
testFunc = () => JavaScriptObfuscator.obfuscate(
'',
{
...NO_ADDITIONAL_NODES_PRESET,
domainDest: 'https://example.com/path'
}
).getObfuscatedCode();
});

it('should pass validation', () => {
assert.doesNotThrow(testFunc);
});
});

describe('Variant #2: string with url containing host and some path', () => {
let testFunc: () => string;

beforeEach(() => {
testFunc = () => JavaScriptObfuscator.obfuscate(
'',
{
...NO_ADDITIONAL_NODES_PRESET,
domainDest: 'example.com/path'
}
).getObfuscatedCode();
});

it('should pass validation', () => {
assert.doesNotThrow(testFunc);
});
});

describe('Variant #3: string with url containing host and some path', () => {
let testFunc: () => string;

beforeEach(() => {
testFunc = () => JavaScriptObfuscator.obfuscate(
'',
{
...NO_ADDITIONAL_NODES_PRESET,
domainDest: '/path'
}
).getObfuscatedCode();
});

it('should pass validation', () => {
assert.doesNotThrow(testFunc);
});
});

describe('Variant #4: `about:blank` string', () => {
let testFunc: () => string;

beforeEach(() => {
testFunc = () => JavaScriptObfuscator.obfuscate(
'',
{
...NO_ADDITIONAL_NODES_PRESET,
domainDest: 'about:blank'
}
).getObfuscatedCode();
});

it('should pass validation', () => {
assert.doesNotThrow(testFunc);
});
});
});

describe('Variant #2: negative validation', () => {
describe('Variant #1: some non-url string', () => {
const expectedError: string = 'must be an URL address';
let testFunc: () => string;

beforeEach(() => {
testFunc = () => JavaScriptObfuscator.obfuscate(
'',
{
...NO_ADDITIONAL_NODES_PRESET,
domainDest: 'foo'
}
).getObfuscatedCode();
});

it('should not pass validation', () => {
assert.throws(testFunc, expectedError);
});
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ describe('`identifierNamesCache` validation', () => {
});
});

describe('Variant #4: cache with nullable dictionary fields', () => {
describe('Variant #5: cache with nullable dictionary fields', () => {
let testFunc: () => string;

beforeEach(() => {
Expand Down
1 change: 1 addition & 0 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ import './functional-tests/node-transformers/string-array-transformers/string-ar
import './functional-tests/node-transformers/string-array-transformers/string-array-scope-calls-wrapper-transformer/StringArrayScopeCallsWrapperTransformer.spec';
import './functional-tests/node-transformers/string-array-transformers/string-array-transformer/StringArrayTransformer.spec';
import './functional-tests/options/OptionsNormalizer.spec';
import './functional-tests/options/domain-dest/Validation.spec';
import './functional-tests/options/domain-lock/Validation.spec';
import './functional-tests/options/identifier-names-cache/Validation.spec';
import './functional-tests/storages/string-array-transformers/string-array-storage/StringArrayStorage.spec';
Expand Down