mirror of
https://github.com/javascript-obfuscator/javascript-obfuscator
synced 2026-08-09 12:42:29 +00:00
Added improved domainDest option validation
This commit is contained in:
Vendored
+1
-1
File diff suppressed because one or more lines are too long
Vendored
+1
-1
File diff suppressed because one or more lines are too long
Vendored
+1
-1
File diff suppressed because one or more lines are too long
@@ -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()
|
||||
@@ -137,11 +138,7 @@ export class Options implements IOptions {
|
||||
/**
|
||||
* @type {string}
|
||||
*/
|
||||
@IsString()
|
||||
@IsAllowedForObfuscationTargets([
|
||||
ObfuscationTarget.Browser,
|
||||
ObfuscationTarget.BrowserNoEval,
|
||||
])
|
||||
@IsDomainDestUrl()
|
||||
public readonly domainDest!: string;
|
||||
|
||||
/**
|
||||
|
||||
@@ -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);
|
||||
};
|
||||
};
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -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(() => {
|
||||
|
||||
@@ -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';
|
||||
|
||||
Reference in New Issue
Block a user