Skip to content

Commit

Permalink
fix: improve security by adding Object.freeze to global objects and l…
Browse files Browse the repository at this point in the history
…ocal singletons

fix #72
  • Loading branch information
vitoke committed May 14, 2022
1 parent 33dd7ca commit 426277d
Show file tree
Hide file tree
Showing 130 changed files with 1,215 additions and 997 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ yarn-error.log
.eslintcache
lerna-debug.log

.DS_Store
**/.DS_Store
14 changes: 8 additions & 6 deletions deno_dist/bimap/custom/implementation/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,21 @@ export class BiMapContext<UK, UV, Tp extends BiMap.Types = BiMap.Types>
return undefined as any;
}

readonly _empty: BiMap<any, any> = new BiMapEmpty<any, any>(this);
readonly _empty: BiMap<any, any> = Object.freeze(
new BiMapEmpty<any, any>(this)
);

empty = <K extends UK, V extends UV>(): BiMap<K, V> => {
readonly empty = <K extends UK, V extends UV>(): BiMap<K, V> => {
return this._empty;
};

of: any = <K, V>(
readonly of: any = <K, V>(
...entries: ArrayNonEmpty<readonly [K, V]>
): [K, V] extends [UK, UV] ? BiMap.NonEmpty<K, V> : never => {
return this.from(entries);
};

from = <K, V>(
readonly from = <K, V>(
...sources: ArrayNonEmpty<StreamSource<readonly [K, V]>>
): [K, V] extends [UK, UV] ? BiMap<K, V> | any : never => {
if (sources.length === 1) {
Expand Down Expand Up @@ -72,11 +74,11 @@ export class BiMapContext<UK, UV, Tp extends BiMap.Types = BiMap.Types>
return builder.build() as any;
};

builder = <K extends UK, V extends UV>(): BiMap.Builder<K, V> => {
readonly builder = <K extends UK, V extends UV>(): BiMap.Builder<K, V> => {
return new BiMapBuilder(this as unknown as BiMapContext<K, V>);
};

reducer = <K extends UK, V extends UV>(
readonly reducer = <K extends UK, V extends UV>(
source?: StreamSource<readonly [K, V]>
): Reducer<readonly [K, V], BiMap<K, V>> => {
return Reducer.create(
Expand Down
12 changes: 7 additions & 5 deletions deno_dist/bimap/main/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -773,18 +773,20 @@ function createContext<UK, UV>(options?: {
keyValueContext?: RMap.Context<UK>;
valueKeyContext?: RMap.Context<UV>;
}): BiMap.Context<UK, UV> {
return new BiMapContext(
options?.keyValueContext ?? HashMap.defaultContext(),
options?.valueKeyContext ?? HashMap.defaultContext()
return Object.freeze(
new BiMapContext<UK, UV>(
options?.keyValueContext ?? HashMap.defaultContext(),
options?.valueKeyContext ?? HashMap.defaultContext()
)
);
}

const _defaultContext: BiMap.Context<any, any> = createContext();

export const BiMap: BiMapCreators = {
export const BiMap: BiMapCreators = Object.freeze({
..._defaultContext,
createContext,
defaultContext<UK, UV>(): BiMap.Context<UK, UV> {
return _defaultContext;
},
};
});
22 changes: 12 additions & 10 deletions deno_dist/bimultimap/custom/implementation/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,25 @@ export class BiMultiMapContext<
return undefined as any;
}

readonly _empty = new BiMultiMapEmpty<UK, UV, Tp>(this) as WithKeyValue<
Tp,
UK,
UV
>['normal'];
readonly _empty = Object.freeze(
new BiMultiMapEmpty<UK, UV, Tp>(this)
) as WithKeyValue<Tp, UK, UV>['normal'];

empty = <K extends UK, V extends UV>(): WithKeyValue<Tp, K, V>['normal'] => {
readonly empty = <K extends UK, V extends UV>(): WithKeyValue<
Tp,
K,
V
>['normal'] => {
return this._empty;
};

of: any = <K extends UK, V extends UV>(
readonly of: any = <K extends UK, V extends UV>(
...entries: ArrayNonEmpty<readonly [K, V]>
): [K, V] extends [UK, UV] ? WithKeyValue<Tp, K, V>['nonEmpty'] : never => {
return this.from(entries);
};

from = <K extends UK, V extends UV>(
readonly from = <K extends UK, V extends UV>(
...sources: ArrayNonEmpty<StreamSource<readonly [K, V]>>
): [K, V] extends [UK, UV]
? WithKeyValue<Tp, K, V>['normal'] | any
Expand Down Expand Up @@ -93,7 +95,7 @@ export class BiMultiMapContext<
return builder.build() as any;
};

builder = <K extends UK, V extends UV>(): WithKeyValue<
readonly builder = <K extends UK, V extends UV>(): WithKeyValue<
Tp,
K,
V
Expand All @@ -105,7 +107,7 @@ export class BiMultiMapContext<
>['builder'];
};

reducer = <K extends UK, V extends UV>(
readonly reducer = <K extends UK, V extends UV>(
source?: StreamSource<readonly [K, V]>
): Reducer<[K, V], WithKeyValue<Tp, K, V>['normal']> => {
return Reducer.create(
Expand Down
14 changes: 8 additions & 6 deletions deno_dist/bimultimap/main/interface/generic/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,17 @@ export namespace BiMultiMap {
}
}

export const BiMultiMap: BiMultiMapGeneric.Creators = {
export const BiMultiMap: BiMultiMapGeneric.Creators = Object.freeze({
createContext<UK, UV>(options: {
keyValueMultiMapContext: MultiMap.Context<UK, UV>;
valueKeyMultiMapContext: MultiMap.Context<UV, UK>;
}): BiMultiMap.Context<UK, UV> {
return new BiMultiMapContext<UK, UV, 'BiMultiMap', any>(
'BiMultiMap',
options.keyValueMultiMapContext,
options.valueKeyMultiMapContext
return Object.freeze(
new BiMultiMapContext<UK, UV, 'BiMultiMap', any>(
'BiMultiMap',
options.keyValueMultiMapContext,
options.valueKeyMultiMapContext
)
);
},
};
});
15 changes: 9 additions & 6 deletions deno_dist/bimultimap/main/interface/hashed/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,22 @@ function createContext<UK, UV>(options?: {
keyValueMultiMapContext?: HashMultiMapHashValue.Context<UK, UV>;
valueKeyMultiMapContext?: HashMultiMapHashValue.Context<UV, UK>;
}): HashBiMultiMap.Context<UK, UV> {
return new BiMultiMapContext<UK, UV, 'HashBiMultiMap', any>(
'HashBiMultiMap',
options?.keyValueMultiMapContext ?? HashMultiMapHashValue.defaultContext(),
options?.valueKeyMultiMapContext ?? HashMultiMapHashValue.defaultContext()
return Object.freeze(
new BiMultiMapContext<UK, UV, 'HashBiMultiMap', any>(
'HashBiMultiMap',
options?.keyValueMultiMapContext ??
HashMultiMapHashValue.defaultContext(),
options?.valueKeyMultiMapContext ?? HashMultiMapHashValue.defaultContext()
)
);
}

const _defaultContext: HashBiMultiMap.Context<any, any> = createContext();

export const HashBiMultiMap: BiMultiMapHashed.Creators = {
export const HashBiMultiMap: BiMultiMapHashed.Creators = Object.freeze({
..._defaultContext,
createContext,
defaultContext<UK, UV>(): HashBiMultiMap.Context<UK, UV> {
return _defaultContext;
},
};
});
18 changes: 10 additions & 8 deletions deno_dist/bimultimap/main/interface/sorted/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,21 +98,23 @@ function createContext<K, V>(options?: {
keyValueMultiMapContext?: SortedMultiMapSortedValue.Context<K, V>;
valueKeyMultiMapContext?: SortedMultiMapSortedValue.Context<V, K>;
}): SortedBiMultiMap.Context<K, V> {
return new BiMultiMapContext<K, V, 'SortedBiMultiMap', any>(
'SortedBiMultiMap',
options?.keyValueMultiMapContext ??
SortedMultiMapSortedValue.defaultContext(),
options?.valueKeyMultiMapContext ??
SortedMultiMapSortedValue.defaultContext()
return Object.freeze(
new BiMultiMapContext<K, V, 'SortedBiMultiMap', any>(
'SortedBiMultiMap',
options?.keyValueMultiMapContext ??
SortedMultiMapSortedValue.defaultContext(),
options?.valueKeyMultiMapContext ??
SortedMultiMapSortedValue.defaultContext()
)
);
}

const _defaultContext: SortedBiMultiMap.Context<any, any> = createContext();

export const SortedBiMultiMap: BiMultiMapSorted.Creators = {
export const SortedBiMultiMap: BiMultiMapSorted.Creators = Object.freeze({
..._defaultContext,
createContext,
defaultContext<UK, UV>(): SortedBiMultiMap.Context<UK, UV> {
return _defaultContext;
},
};
});
20 changes: 12 additions & 8 deletions deno_dist/collection-types/map-custom/interface/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -974,11 +974,11 @@ export namespace RMapBase {
return undefined as any;
}

empty = <K extends UK, V>(): WithKeyValue<Tp, K, V>['normal'] => {
readonly empty = <K extends UK, V>(): WithKeyValue<Tp, K, V>['normal'] => {
return this._empty;
};

from: any = <K extends UK, V>(
readonly from: any = <K extends UK, V>(
...sources: ArrayNonEmpty<StreamSource<readonly [K, V]>>
): WithKeyValue<Tp, K, V>['normal'] => {
let builder = this.builder<K, V>();
Expand Down Expand Up @@ -1007,13 +1007,13 @@ export namespace RMapBase {
return builder.build();
};

of = <K extends UK, V>(
readonly of = <K extends UK, V>(
...values: ArrayNonEmpty<readonly [K, V]>
): K extends UK ? WithKeyValue<Tp, K, V>['nonEmpty'] : never => {
return this.from(values);
};

reducer = <K extends UK, V>(
readonly reducer = <K extends UK, V>(
source?: StreamSource<readonly [K, V]>
): Reducer<readonly [K, V], WithKeyValue<Tp, K, V>['normal']> => {
return Reducer.create(
Expand All @@ -1031,7 +1031,7 @@ export namespace RMapBase {
);
};

mergeAllWith: any = <
readonly mergeAllWith: any = <
K,
I extends readonly [unknown, unknown, ...unknown[]]
>(
Expand Down Expand Up @@ -1079,7 +1079,11 @@ export namespace RMapBase {
};
};

mergeAll: any = <O, K, I extends readonly [unknown, unknown, ...unknown[]]>(
readonly mergeAll: any = <
O,
K,
I extends readonly [unknown, unknown, ...unknown[]]
>(
fillValue: O,
...sources: { [KT in keyof I]: StreamSource<readonly [K, I[KT]]> }
): any => {
Expand All @@ -1089,7 +1093,7 @@ export namespace RMapBase {
);
};

mergeWith: any = <I extends readonly [unknown, ...unknown[]], K>(
readonly mergeWith: any = <I extends readonly [unknown, ...unknown[]], K>(
...sources: { [KT in keyof I]: StreamSource<readonly [K, I[KT]]> }
): any => {
return <R>(mergeFun: (key: K, ...values: I) => R): any => {
Expand Down Expand Up @@ -1154,7 +1158,7 @@ export namespace RMapBase {
};
};

merge: any = <K, I extends readonly [unknown, ...unknown[]]>(
readonly merge: any = <K, I extends readonly [unknown, ...unknown[]]>(
...sources: { [KT in keyof I]: StreamSource<readonly [K, I[KT]]> }
): any => {
return this.mergeWith(...sources)(
Expand Down
8 changes: 4 additions & 4 deletions deno_dist/collection-types/set-custom/interface/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -578,11 +578,11 @@ export namespace RSetBase {
return undefined as any;
}

empty = <T extends UT>(): WithElem<Tp, T>['normal'] => {
readonly empty = <T extends UT>(): WithElem<Tp, T>['normal'] => {
return this._empty;
};

from: any = <T extends UT>(
readonly from: any = <T extends UT>(
...sources: ArrayNonEmpty<StreamSource<T>>
): WithElem<Tp, T>['normal'] => {
let builder = this.builder<T>();
Expand Down Expand Up @@ -611,13 +611,13 @@ export namespace RSetBase {
return builder.build();
};

of = <T extends UT>(
readonly of = <T extends UT>(
...values: ArrayNonEmpty<T>
): T extends UT ? WithElem<Tp, T>['nonEmpty'] : never => {
return this.from(values);
};

reducer = <T extends UT>(
readonly reducer = <T extends UT>(
source?: StreamSource<T>
): Reducer<T, WithElem<Tp, T>['normal']> => {
return Reducer.create(
Expand Down
2 changes: 1 addition & 1 deletion deno_dist/common/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ export namespace Reducer {
};

/**
* Returns a `yaReducer` that joins the given input values into a string using the given options.
* Returns a `Reducer` that joins the given input values into a string using the given options.
* @param options - an object containing:<br/>
* - sep: (optional) a seperator string value between values in the output<br/>
* - start: (optional) a start string to prepend to the output<br/>
Expand Down
2 changes: 1 addition & 1 deletion deno_dist/common/traverse-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class TraverseStateImpl implements TraverseState {
return this.currentIndex++;
}

halt = (): void => {
readonly halt = (): void => {
this.halted = true;
};

Expand Down
10 changes: 5 additions & 5 deletions deno_dist/graph/custom/non-valued/implementation/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class GraphContext<
any
>['linkConnectionsContext']
) {
this._empty = new GraphEmpty(isDirected, this) as any;
this._empty = Object.freeze(new GraphEmpty(isDirected, this)) as any;
}

isNonEmptyInstance(
Expand All @@ -42,11 +42,11 @@ export class GraphContext<
return source instanceof GraphNonEmpty;
}

empty = <N extends UN>(): any => {
readonly empty = <N extends UN>(): any => {
return this._empty;
};

from: any = <N extends UN>(
readonly from: any = <N extends UN>(
...sources: ArrayNonEmpty<StreamSource<GraphElement<N>>>
): any => {
let builder = this.builder();
Expand Down Expand Up @@ -74,11 +74,11 @@ export class GraphContext<
return builder.build();
};

of = <N>(...values: ArrayNonEmpty<GraphElement<N>>): any => {
readonly of = <N>(...values: ArrayNonEmpty<GraphElement<N>>): any => {
return this.from(values).assumeNonEmpty();
};

builder = (): any => {
readonly builder = (): any => {
return new GraphBuilder(this.isDirected, this);
};

Expand Down
Loading

0 comments on commit 426277d

Please sign in to comment.