Skip to content

Commit 0096f16

Browse files
authored
Merge pull request javascript-obfuscator#935 from javascript-obfuscator/improved-domain-dest-validation
Added improved `domainDest` option validation
2 parents 36a6c22 + 4eca217 commit 0096f16

8 files changed

Lines changed: 141 additions & 9 deletions

File tree

dist/index.browser.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.cli.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/options/Options.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import { HIGH_OBFUSCATION_PRESET } from './presets/HighObfuscation';
4646

4747
import { ValidationErrorsFormatter } from './ValidationErrorsFormatter';
4848
import { IsAllowedForObfuscationTargets } from './validators/IsAllowedForObfuscationTargets';
49+
import { IsDomainDestUrl } from './validators/IsDomainDestUrl';
4950
import { IsIdentifierNamesCache } from './validators/IsIdentifierNamesCache';
5051

5152
@injectable()
@@ -137,11 +138,7 @@ export class Options implements IOptions {
137138
/**
138139
* @type {string}
139140
*/
140-
@IsString()
141-
@IsAllowedForObfuscationTargets([
142-
ObfuscationTarget.Browser,
143-
ObfuscationTarget.BrowserNoEval,
144-
])
141+
@IsDomainDestUrl()
145142
public readonly domainDest!: string;
146143

147144
/**
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { IsUrl, ValidateIf } from 'class-validator';
2+
3+
import { TInputOptions } from '../../types/options/TInputOptions';
4+
5+
import { IOptions } from '../../interfaces/options/IOptions';
6+
7+
import { ObfuscationTarget } from '../../enums/ObfuscationTarget';
8+
9+
import { DEFAULT_PRESET } from '../presets/Default';
10+
11+
import { IsAllowedForObfuscationTargets } from './IsAllowedForObfuscationTargets';
12+
13+
/**
14+
* @returns {PropertyDecorator}
15+
*/
16+
export const IsDomainDestUrl = (): PropertyDecorator => {
17+
return (target: any, key: string | symbol): void => {
18+
ValidateIf(({domainDest}: TInputOptions) => {
19+
return domainDest !== DEFAULT_PRESET.domainDest;
20+
})(target, key);
21+
IsUrl({
22+
require_protocol: false,
23+
require_host: false
24+
})(target, key);
25+
IsAllowedForObfuscationTargets([
26+
ObfuscationTarget.Browser,
27+
ObfuscationTarget.BrowserNoEval,
28+
])(target, <keyof IOptions>key);
29+
};
30+
};
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import { assert } from 'chai';
2+
3+
import { JavaScriptObfuscator } from '../../../../src/JavaScriptObfuscatorFacade';
4+
5+
import { NO_ADDITIONAL_NODES_PRESET } from '../../../../src/options/presets/NoCustomNodes';
6+
7+
describe('`domainDest` validation', () => {
8+
describe('IsDomainDestUrl', () => {
9+
describe('Variant #1: positive validation', () => {
10+
describe('Variant #1: string with url containing protocol, host and some path', () => {
11+
let testFunc: () => string;
12+
13+
beforeEach(() => {
14+
testFunc = () => JavaScriptObfuscator.obfuscate(
15+
'',
16+
{
17+
...NO_ADDITIONAL_NODES_PRESET,
18+
domainDest: 'https://example.com/path'
19+
}
20+
).getObfuscatedCode();
21+
});
22+
23+
it('should pass validation', () => {
24+
assert.doesNotThrow(testFunc);
25+
});
26+
});
27+
28+
describe('Variant #2: string with url containing host and some path', () => {
29+
let testFunc: () => string;
30+
31+
beforeEach(() => {
32+
testFunc = () => JavaScriptObfuscator.obfuscate(
33+
'',
34+
{
35+
...NO_ADDITIONAL_NODES_PRESET,
36+
domainDest: 'example.com/path'
37+
}
38+
).getObfuscatedCode();
39+
});
40+
41+
it('should pass validation', () => {
42+
assert.doesNotThrow(testFunc);
43+
});
44+
});
45+
46+
describe('Variant #3: string with url containing host and some path', () => {
47+
let testFunc: () => string;
48+
49+
beforeEach(() => {
50+
testFunc = () => JavaScriptObfuscator.obfuscate(
51+
'',
52+
{
53+
...NO_ADDITIONAL_NODES_PRESET,
54+
domainDest: '/path'
55+
}
56+
).getObfuscatedCode();
57+
});
58+
59+
it('should pass validation', () => {
60+
assert.doesNotThrow(testFunc);
61+
});
62+
});
63+
64+
describe('Variant #4: `about:blank` string', () => {
65+
let testFunc: () => string;
66+
67+
beforeEach(() => {
68+
testFunc = () => JavaScriptObfuscator.obfuscate(
69+
'',
70+
{
71+
...NO_ADDITIONAL_NODES_PRESET,
72+
domainDest: 'about:blank'
73+
}
74+
).getObfuscatedCode();
75+
});
76+
77+
it('should pass validation', () => {
78+
assert.doesNotThrow(testFunc);
79+
});
80+
});
81+
});
82+
83+
describe('Variant #2: negative validation', () => {
84+
describe('Variant #1: some non-url string', () => {
85+
const expectedError: string = 'must be an URL address';
86+
let testFunc: () => string;
87+
88+
beforeEach(() => {
89+
testFunc = () => JavaScriptObfuscator.obfuscate(
90+
'',
91+
{
92+
...NO_ADDITIONAL_NODES_PRESET,
93+
domainDest: 'foo'
94+
}
95+
).getObfuscatedCode();
96+
});
97+
98+
it('should not pass validation', () => {
99+
assert.throws(testFunc, expectedError);
100+
});
101+
});
102+
});
103+
});
104+
});

test/functional-tests/options/identifier-names-cache/Validation.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ describe('`identifierNamesCache` validation', () => {
233233
});
234234
});
235235

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

239239
beforeEach(() => {

test/index.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ import './functional-tests/node-transformers/string-array-transformers/string-ar
133133
import './functional-tests/node-transformers/string-array-transformers/string-array-scope-calls-wrapper-transformer/StringArrayScopeCallsWrapperTransformer.spec';
134134
import './functional-tests/node-transformers/string-array-transformers/string-array-transformer/StringArrayTransformer.spec';
135135
import './functional-tests/options/OptionsNormalizer.spec';
136+
import './functional-tests/options/domain-dest/Validation.spec';
136137
import './functional-tests/options/domain-lock/Validation.spec';
137138
import './functional-tests/options/identifier-names-cache/Validation.spec';
138139
import './functional-tests/storages/string-array-transformers/string-array-storage/StringArrayStorage.spec';

0 commit comments

Comments
 (0)