java-topology/defects/nestjs/patch/nestjs-0002-get-injection-providers-set.patch

37 lines
1.6 KiB
Diff

diff --git a/packages/common/module-utils/utils/get-injection-providers.util.ts b/packages/common/module-utils/utils/get-injection-providers.util.ts
index xxxxxxx..xxxxxxx 100644
--- a/packages/common/module-utils/utils/get-injection-providers.util.ts
+++ b/packages/common/module-utils/utils/get-injection-providers.util.ts
@@ -32,13 +32,19 @@ export function getInjectionProviders(
providers: Provider[],
tokens: FactoryProvider['inject'],
): Provider[] {
const result: Provider[] = [];
+ // CWE-407 fix: companion Set for O(1) result-membership checks
+ const resultSet = new Set<Provider>();
+
let search: InjectionToken[] = tokens!.map(mapInjectToTokens);
+ // CWE-407 fix: companion Set for O(1) search-membership checks
+ let searchSet = new Set<InjectionToken>(search);
+
while (search.length > 0) {
const match = (providers ?? []).filter(
p =>
- !result.includes(p) && // this prevents circular loops and duplication
- (search.includes(p as any) || search.includes((p as any)?.provide)),
+ // CWE-407 fix: was Array.includes() = O(n); now Set.has() = O(1)
+ !resultSet.has(p) &&
+ (searchSet.has(p as any) || searchSet.has((p as any)?.provide)),
);
- result.push(...match);
+ for (const p of match) { result.push(p); resultSet.add(p); }
+
// get injection tokens of the matched providers, if any
search = match
.filter(p => (p as any)?.inject)
.flatMap(p => (p as FactoryProvider).inject!)
.map(mapInjectToTokens);
+ searchSet = new Set<InjectionToken>(search);
}
return result;
}