Added tests for rc4 encoded value collision fix

This commit is contained in:
sanex3339
2020-02-01 22:15:48 +03:00
parent 740b84d598
commit ca4a907094
11 changed files with 87 additions and 11 deletions
+2 -2
View File
File diff suppressed because one or more lines are too long
+1 -1
View File
File diff suppressed because one or more lines are too long
+1 -1
View File
File diff suppressed because one or more lines are too long
@@ -50,7 +50,7 @@ export class StringArrayStorageAnalyzer implements IStringArrayStorageAnalyzer {
* @param {IOptions} options
*/
public constructor (
@inject(ServiceIdentifiers.TStringArrayStorage) stringArrayStorage: IStringArrayStorage,
@inject(ServiceIdentifiers.IStringArrayStorage) stringArrayStorage: IStringArrayStorage,
@inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
@inject(ServiceIdentifiers.IOptions) options: IOptions,
) {
+2 -2
View File
@@ -42,11 +42,11 @@ export enum ServiceIdentifiers {
ISourceCode = 'ISourceCode',
ISourceMapCorrector = 'ISourceMapCorrector',
IScopeAnalyzer = 'IScopeAnalyzer',
IStringArrayStorage = 'IStringArrayStorage',
IStringArrayStorageAnalyzer = 'IStringArrayStorageAnalyzer',
ITransformersRunner = 'ITransformersRunner',
Newable__ICustomNode = 'Newable<ICustomNode>',
Newable__TControlFlowStorage = 'Newable<TControlFlowStorage>',
TCustomNodeGroupStorage = 'TCustomNodeGroupStorage',
TInputOptions = 'TInputOptions',
TStringArrayStorage = 'TStringArrayStorage'
TInputOptions = 'TInputOptions'
}
@@ -18,7 +18,7 @@ export const storagesModule: interfaces.ContainerModule = new ContainerModule((b
.to(CustomNodeGroupStorage)
.inSingletonScope();
bind<IStringArrayStorage>(ServiceIdentifiers.TStringArrayStorage)
bind<IStringArrayStorage>(ServiceIdentifiers.IStringArrayStorage)
.to(StringArrayStorage)
.inSingletonScope();
@@ -55,7 +55,7 @@ export class StringArrayCustomNodeGroup extends AbstractCustomNodeGroup {
*/
public constructor (
@inject(ServiceIdentifiers.Factory__ICustomNode) customNodeFactory: TCustomNodeFactory,
@inject(ServiceIdentifiers.TStringArrayStorage) stringArrayStorage: IStringArrayStorage,
@inject(ServiceIdentifiers.IStringArrayStorage) stringArrayStorage: IStringArrayStorage,
@inject(ServiceIdentifiers.Factory__IIdentifierNamesGenerator)
identifierNamesGeneratorFactory: TIdentifierNamesGeneratorFactory,
@inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
@@ -48,7 +48,7 @@ export class StringLiteralObfuscatingReplacer extends AbstractObfuscatingReplace
* @param {IOptions} options
*/
public constructor (
@inject(ServiceIdentifiers.TStringArrayStorage) stringArrayStorage: IStringArrayStorage,
@inject(ServiceIdentifiers.IStringArrayStorage) stringArrayStorage: IStringArrayStorage,
@inject(ServiceIdentifiers.IStringArrayStorageAnalyzer) stringArrayStorageAnalyzer: IStringArrayStorageAnalyzer,
@inject(ServiceIdentifiers.IOptions) options: IOptions
) {
@@ -262,7 +262,7 @@ export class StringArrayStorage extends MapStorage <string, IStringArrayStorageI
private getEncodedValue (value: string): IEncodedValue {
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
*
* For example:
+1
View File
@@ -39,6 +39,7 @@ import './unit-tests/options/ValidationErrorsFormatter.spec';
import './unit-tests/source-code/ObfuscatedCode.spec';
import './unit-tests/storages/ArrayStorage.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/CryptUtils.spec';
import './unit-tests/utils/EscapeSequenceEncoder.spec';
@@ -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);
});
});
});