Browse Source

Added improved `domainDest` option validation

sanex 3 năm trước cách đây
mục cha
commit
4eca2178fd

Những thai đổi đã bị hủy bỏ vì nó quá lớn
+ 0 - 0
dist/index.browser.js


Những thai đổi đã bị hủy bỏ vì nó quá lớn
+ 0 - 0
dist/index.cli.js


Những thai đổi đã bị hủy bỏ vì nó quá lớn
+ 0 - 0
dist/index.js


+ 2 - 5
src/options/Options.ts

@@ -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;
 
     /**

+ 30 - 0
src/options/validators/IsDomainDestUrl.ts

@@ -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 - 0
test/functional-tests/options/domain-dest/Validation.spec.ts

@@ -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);
+                });
+            });
+        });
+    });
+});

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

@@ -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(() => {

+ 1 - 0
test/index.spec.ts

@@ -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';

Một số tệp đã không được hiển thị bởi vì quá nhiều tập tin thay đổi trong này khác