Skip to content

Commit 71e3041

Browse files
committed
JS: Fewer spurious reflected xss sinks
1 parent 4f53a1a commit 71e3041

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

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

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ module NestJS {
5151
getAFunctionDecorator(this) = nestjs().getMember("Redirect").getACall()
5252
}
5353

54+
/**
55+
* Holds if the return value is sent back in the response.
56+
*/
57+
predicate isReturnValueReflected() {
58+
getAFunctionDecorator(this) = nestjs().getMember(["Get", "Post"]).getACall() and
59+
not hasRedirectDecorator() and
60+
not getAFunctionDecorator(this) = nestjs().getMember("Render").getACall()
61+
}
62+
5463
/** Gets a pipe applied to the inputs of this route handler, not including global pipes. */
5564
DataFlow::Node getAPipe() {
5665
exists(DataFlow::CallNode decorator |
@@ -317,6 +326,14 @@ module NestJS {
317326
}
318327
}
319328

329+
private predicate isStringType(Type type) {
330+
type instanceof StringType
331+
or
332+
type instanceof AnyType
333+
or
334+
isStringType(type.(PromiseType).getElementType().unfold())
335+
}
336+
320337
/**
321338
* A return value from a route handler, seen as an argument to `res.send()`.
322339
*
@@ -333,8 +350,13 @@ module NestJS {
333350
NestJSRouteHandler handler;
334351

335352
ReturnValueAsResponseSend() {
336-
not handler.hasRedirectDecorator() and
337-
this = handler.getAReturn().asExpr()
353+
handler.isReturnValueReflected() and
354+
this = handler.getAReturn().asExpr() and
355+
// Only returned strings are sinks
356+
not exists(Type type |
357+
type = getType() and
358+
not isStringType(type.unfold())
359+
)
338360
}
339361

340362
override HTTP::RouteHandler getRouteHandler() { result = handler }

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,6 @@ responseSendArgument
6767
| local/customPipe.ts:37:16:37:31 | '' + unsanitized |
6868
| local/customPipe.ts:42:16:42:31 | '' + unsanitized |
6969
| local/customPipe.ts:48:16:48:31 | '' + unsanitized |
70-
| local/routes.ts:7:12:7:16 | 'foo' |
71-
| local/routes.ts:12:12:12:16 | 'foo' |
72-
| local/routes.ts:17:12:17:16 | 'foo' |
73-
| local/routes.ts:22:12:22:16 | 'bar' |
7470
| local/routes.ts:32:31:32:31 | x |
7571
| local/routes.ts:33:31:33:38 | queryObj |
7672
| local/routes.ts:34:31:34:34 | name |

0 commit comments

Comments
 (0)