Skip to content

Commit 477f32c

Browse files
committed
NestJS dependency injection support useValue provider
1 parent 2b143c8 commit 477f32c

File tree

6 files changed

+25
-5
lines changed

6 files changed

+25
-5
lines changed

javascript/ql/lib/semmle/javascript/frameworks/Nest.qll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,9 @@ module NestJS {
526526
f = tuple.getAPropertyWrite("useFactory").getRhs().getALocalSource() and
527527
result.getAstNode() = f.getFunction().getAReturnedExpr().getType().(ClassType).getClass()
528528
)
529+
or
530+
result.getAstNode() =
531+
tuple.getAPropertyWrite("useValue").getRhs().asExpr().getType().(ClassType).getClass()
529532
}
530533

531534
private predicate providerPair(DataFlow::Node interface, DataFlow::Node concreteClass) {

javascript/ql/test/library-tests/frameworks/Nest/global/app.module.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { Module } from '@nestjs/common';
22
import { Controller } from './validation';
3-
import { Foo, Foo2 } from './foo.interface';
4-
import { FooImpl, Foo2Impl } from './foo.impl';
3+
import { Foo, Foo2, Foo3 } from './foo.interface';
4+
import { FooImpl, Foo2Impl, Foo3Impl } from './foo.impl';
5+
6+
const foo3 = new Foo3Impl()
57

68
@Module({
79
controllers: [Controller],
@@ -13,6 +15,10 @@ import { FooImpl, Foo2Impl } from './foo.impl';
1315
{
1416
provide: Foo2,
1517
useFactory: () => new Foo2Impl()
18+
},
19+
{
20+
provide: Foo3,
21+
useValue: foo3
1622
}
1723
],
1824
})

javascript/ql/test/library-tests/frameworks/Nest/global/foo.impl.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,10 @@ export class Foo2Impl extends Foo2 {
1010
fooMethod(x: string) {
1111
sink(x); // $ hasValueFlow=x
1212
}
13+
}
14+
15+
export class Foo3Impl extends Foo2 {
16+
fooMethod(x: string) {
17+
sink(x); // $ hasValueFlow=x
18+
}
1319
}

javascript/ql/test/library-tests/frameworks/Nest/global/foo.interface.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,7 @@ export abstract class Foo {
55
export abstract class Foo2 {
66
abstract fooMethod(x: string): void;
77
}
8+
9+
export abstract class Foo3 {
10+
abstract fooMethod(x: string): void;
11+
}

javascript/ql/test/library-tests/frameworks/Nest/global/validation.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { Get, Query } from '@nestjs/common';
22
import { IsIn } from 'class-validator';
3-
import { Foo, Foo2 } from './foo.interface';
3+
import { Foo, Foo2, Foo3 } from './foo.interface';
44

55
export class Controller {
66
constructor(
7-
private readonly foo: Foo, private readonly foo2: Foo2
7+
private readonly foo: Foo, private readonly foo2: Foo2, private readonly foo3: Foo3
88
) { }
99

1010
@Get()
@@ -17,6 +17,7 @@ export class Controller {
1717
route2(@Query('x') x: string) {
1818
this.foo.fooMethod(x);
1919
this.foo2.fooMethod(x);
20+
this.foo3.fooMethod(x);
2021
}
2122
}
2223

javascript/ql/test/library-tests/frameworks/Nest/test.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
testFailures
22
routeHandler
33
| global/validation.ts:11:3:14:3 | route1( ... OK\\n } |
4-
| global/validation.ts:17:3:20:3 | route2( ... x);\\n } |
4+
| global/validation.ts:17:3:21:3 | route2( ... x);\\n } |
55
| local/customDecorator.ts:18:3:20:3 | sneaky( ... OK\\n } |
66
| local/customDecorator.ts:23:3:25:3 | safe(@S ... OK\\n } |
77
| local/customPipe.ts:20:5:22:5 | sanitiz ... K\\n } |

0 commit comments

Comments
 (0)