ソースを参照

Added tests for rc4 encoded value collision fix

sanex3339 5 年 前
コミット
ca4a907094

ファイルの差分が大きいため隠しています
+ 0 - 0
dist/index.browser.js


ファイルの差分が大きいため隠しています
+ 0 - 0
dist/index.cli.js


ファイルの差分が大きいため隠しています
+ 0 - 0
dist/index.js


+ 1 - 1
src/analyzers/string-array-storage-analyzer/StringArrayStorageAnalyzer.ts

@@ -50,7 +50,7 @@ export class StringArrayStorageAnalyzer implements IStringArrayStorageAnalyzer {
      * @param {IOptions} options
      * @param {IOptions} options
      */
      */
     public constructor (
     public constructor (
-        @inject(ServiceIdentifiers.TStringArrayStorage) stringArrayStorage: IStringArrayStorage,
+        @inject(ServiceIdentifiers.IStringArrayStorage) stringArrayStorage: IStringArrayStorage,
         @inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
         @inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
         @inject(ServiceIdentifiers.IOptions) options: IOptions,
         @inject(ServiceIdentifiers.IOptions) options: IOptions,
     ) {
     ) {

+ 2 - 2
src/container/ServiceIdentifiers.ts

@@ -42,11 +42,11 @@ export enum ServiceIdentifiers {
     ISourceCode = 'ISourceCode',
     ISourceCode = 'ISourceCode',
     ISourceMapCorrector = 'ISourceMapCorrector',
     ISourceMapCorrector = 'ISourceMapCorrector',
     IScopeAnalyzer = 'IScopeAnalyzer',
     IScopeAnalyzer = 'IScopeAnalyzer',
+    IStringArrayStorage = 'IStringArrayStorage',
     IStringArrayStorageAnalyzer = 'IStringArrayStorageAnalyzer',
     IStringArrayStorageAnalyzer = 'IStringArrayStorageAnalyzer',
     ITransformersRunner = 'ITransformersRunner',
     ITransformersRunner = 'ITransformersRunner',
     Newable__ICustomNode = 'Newable<ICustomNode>',
     Newable__ICustomNode = 'Newable<ICustomNode>',
     Newable__TControlFlowStorage = 'Newable<TControlFlowStorage>',
     Newable__TControlFlowStorage = 'Newable<TControlFlowStorage>',
     TCustomNodeGroupStorage = 'TCustomNodeGroupStorage',
     TCustomNodeGroupStorage = 'TCustomNodeGroupStorage',
-    TInputOptions = 'TInputOptions',
-    TStringArrayStorage = 'TStringArrayStorage'
+    TInputOptions = 'TInputOptions'
 }
 }

+ 1 - 1
src/container/modules/storages/StoragesModule.ts

@@ -18,7 +18,7 @@ export const storagesModule: interfaces.ContainerModule = new ContainerModule((b
         .to(CustomNodeGroupStorage)
         .to(CustomNodeGroupStorage)
         .inSingletonScope();
         .inSingletonScope();
 
 
-    bind<IStringArrayStorage>(ServiceIdentifiers.TStringArrayStorage)
+    bind<IStringArrayStorage>(ServiceIdentifiers.IStringArrayStorage)
         .to(StringArrayStorage)
         .to(StringArrayStorage)
         .inSingletonScope();
         .inSingletonScope();
 
 

+ 1 - 1
src/custom-nodes/string-array-nodes/group/StringArrayCustomNodeGroup.ts

@@ -55,7 +55,7 @@ export class StringArrayCustomNodeGroup extends AbstractCustomNodeGroup {
      */
      */
     public constructor (
     public constructor (
         @inject(ServiceIdentifiers.Factory__ICustomNode) customNodeFactory: TCustomNodeFactory,
         @inject(ServiceIdentifiers.Factory__ICustomNode) customNodeFactory: TCustomNodeFactory,
-        @inject(ServiceIdentifiers.TStringArrayStorage) stringArrayStorage: IStringArrayStorage,
+        @inject(ServiceIdentifiers.IStringArrayStorage) stringArrayStorage: IStringArrayStorage,
         @inject(ServiceIdentifiers.Factory__IIdentifierNamesGenerator)
         @inject(ServiceIdentifiers.Factory__IIdentifierNamesGenerator)
             identifierNamesGeneratorFactory: TIdentifierNamesGeneratorFactory,
             identifierNamesGeneratorFactory: TIdentifierNamesGeneratorFactory,
         @inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
         @inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,

+ 1 - 1
src/node-transformers/obfuscating-transformers/obfuscating-replacers/literal-obfuscating-replacers/StringLiteralObfuscatingReplacer.ts

@@ -48,7 +48,7 @@ export class StringLiteralObfuscatingReplacer extends AbstractObfuscatingReplace
      * @param {IOptions} options
      * @param {IOptions} options
      */
      */
     public constructor (
     public constructor (
-        @inject(ServiceIdentifiers.TStringArrayStorage) stringArrayStorage: IStringArrayStorage,
+        @inject(ServiceIdentifiers.IStringArrayStorage) stringArrayStorage: IStringArrayStorage,
         @inject(ServiceIdentifiers.IStringArrayStorageAnalyzer) stringArrayStorageAnalyzer: IStringArrayStorageAnalyzer,
         @inject(ServiceIdentifiers.IStringArrayStorageAnalyzer) stringArrayStorageAnalyzer: IStringArrayStorageAnalyzer,
         @inject(ServiceIdentifiers.IOptions) options: IOptions
         @inject(ServiceIdentifiers.IOptions) options: IOptions
     ) {
     ) {

+ 1 - 1
src/storages/string-array/StringArrayStorage.ts

@@ -262,7 +262,7 @@ export class StringArrayStorage extends MapStorage <string, IStringArrayStorageI
     private getEncodedValue (value: string): IEncodedValue {
     private getEncodedValue (value: string): IEncodedValue {
         switch (this.options.stringArrayEncoding) {
         switch (this.options.stringArrayEncoding) {
             /**
             /**
-             * For rc4 there is a possible collision between encoded values that were received from
+             * For rc4 there is a possible chance of a collision between encoded values that were received from
              * different source values with different keys
              * different source values with different keys
              *
              *
              * For example:
              * For example:

+ 1 - 0
test/index.spec.ts

@@ -39,6 +39,7 @@ import './unit-tests/options/ValidationErrorsFormatter.spec';
 import './unit-tests/source-code/ObfuscatedCode.spec';
 import './unit-tests/source-code/ObfuscatedCode.spec';
 import './unit-tests/storages/ArrayStorage.spec';
 import './unit-tests/storages/ArrayStorage.spec';
 import './unit-tests/storages/MapStorage.spec';
 import './unit-tests/storages/MapStorage.spec';
+import './unit-tests/storages/string-array/StringArrayStorage.spec';
 import './unit-tests/utils/ArrayUtils.spec';
 import './unit-tests/utils/ArrayUtils.spec';
 import './unit-tests/utils/CryptUtils.spec';
 import './unit-tests/utils/CryptUtils.spec';
 import './unit-tests/utils/EscapeSequenceEncoder.spec';
 import './unit-tests/utils/EscapeSequenceEncoder.spec';

+ 75 - 0
test/unit-tests/storages/string-array/StringArrayStorage.spec.ts

@@ -0,0 +1,75 @@
+import 'reflect-metadata';
+
+import { assert } from 'chai';
+
+import { ServiceIdentifiers } from '../../../../src/container/ServiceIdentifiers';
+
+import { TInputOptions } from '../../../../src/types/options/TInputOptions';
+
+import { IInversifyContainerFacade } from '../../../../src/interfaces/container/IInversifyContainerFacade';
+import { IStringArrayStorage } from '../../../../src/interfaces/storages/string-array-storage/IStringArrayStorage';
+
+import { StringArrayEncoding } from '../../../../src/enums/StringArrayEncoding';
+
+import { NO_ADDITIONAL_NODES_PRESET } from '../../../../src/options/presets/NoCustomNodes';
+
+import { InversifyContainerFacade } from '../../../../src/container/InversifyContainerFacade';
+
+/**
+ * @returns {IMapStorage<string, V>}
+ */
+const getStorageInstance = (options: TInputOptions = {}): IStringArrayStorage => {
+    const inversifyContainerFacade: IInversifyContainerFacade = new InversifyContainerFacade();
+
+    inversifyContainerFacade.load('', '', {
+        ...NO_ADDITIONAL_NODES_PRESET,
+        stringArray: true,
+        stringArrayThreshold: 1,
+        ...options
+    });
+
+    const storage: IStringArrayStorage = inversifyContainerFacade.get(ServiceIdentifiers.IStringArrayStorage);
+
+    storage.initialize();
+
+    return storage;
+};
+
+const getEncodedValue = (stringArrayStorage: IStringArrayStorage, value: string, decodeKey: string): string => {
+    (<any>stringArrayStorage).rc4Keys = [
+        'foo',
+        decodeKey
+    ];
+
+    return stringArrayStorage.get(value)?.encodedValue ?? '';
+};
+
+describe('StringArrayStorage', () => {
+    describe('rc4 encoded value collision fix', () => {
+        const samplesCount: number = 100;
+
+        let firstEncodedValue: string;
+        let secondEncodedValue: string;
+        let isCollisionHappened: boolean = false;
+
+        before(() => {
+            const stringArrayStorage: IStringArrayStorage = getStorageInstance({
+                stringArrayEncoding: StringArrayEncoding.Rc4
+            });
+
+            for (let i = 0; i < samplesCount; i++) {
+                firstEncodedValue = getEncodedValue(stringArrayStorage, '_15', 'CRDL');
+                secondEncodedValue = getEncodedValue(stringArrayStorage, '_12', 'q9mB');
+
+                if (firstEncodedValue === secondEncodedValue) {
+                    isCollisionHappened = true;
+                    break;
+                }
+            }
+        });
+
+        it('should not make a collision between different source values with different keys', () => {
+            assert.equal(isCollisionHappened, false);
+        });
+    });
+});

この差分においてかなりの量のファイルが変更されているため、一部のファイルを表示していません