Skip to content

Commit e54789d

Browse files
committed
JS: Recognise route.ts outside api folder
1 parent f52f5b6 commit e54789d

File tree

4 files changed

+22
-4
lines changed

4 files changed

+22
-4
lines changed

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ module NextJS {
3232

3333
private Folder apiRoot() { result = [pagesRoot(), appRoot()].getFolder("api") }
3434

35+
private Folder appFolder() {
36+
result = appRoot()
37+
or
38+
result = appFolder().getAFolder()
39+
}
40+
3541
/**
3642
* Gets a "pages" folder in a `Next.js` application.
3743
* JavaScript files inside these folders are mapped to routes.
@@ -300,8 +306,13 @@ module NextJS {
300306
class NextAppRouteHandler extends DataFlow::FunctionNode, Http::Servers::StandardRouteHandler {
301307
NextAppRouteHandler() {
302308
exists(Module mod |
303-
mod.getFile().getParentContainer() = apiFolder() or
304-
mod.getFile().getStem() = "middleware"
309+
(
310+
mod.getFile().getParentContainer() = apiFolder()
311+
or
312+
mod.getFile().getStem() = "middleware"
313+
or
314+
mod.getFile().getStem() = "route" and mod.getFile().getParentContainer() = appFolder()
315+
)
305316
|
306317
this =
307318
mod.getAnExportedValue([any(Http::RequestMethodName m), "middleware"]).getAFunctionValue()

javascript/ql/test/query-tests/Security/CWE-079/ReflectedXss/ReflectedXss.expected

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
| app/api/routeNextRequest.ts:15:20:15:23 | body | app/api/routeNextRequest.ts:4:22:4:31 | req.json() | app/api/routeNextRequest.ts:15:20:15:23 | body | Cross-site scripting vulnerability due to a $@. | app/api/routeNextRequest.ts:4:22:4:31 | req.json() | user-provided value |
3636
| app/api/routeNextRequest.ts:27:20:27:23 | body | app/api/routeNextRequest.ts:4:22:4:31 | req.json() | app/api/routeNextRequest.ts:27:20:27:23 | body | Cross-site scripting vulnerability due to a $@. | app/api/routeNextRequest.ts:4:22:4:31 | req.json() | user-provided value |
3737
| app/api/routeNextRequest.ts:31:27:31:30 | body | app/api/routeNextRequest.ts:4:22:4:31 | req.json() | app/api/routeNextRequest.ts:31:27:31:30 | body | Cross-site scripting vulnerability due to a $@. | app/api/routeNextRequest.ts:4:22:4:31 | req.json() | user-provided value |
38+
| app/blah/route.ts:3:25:3:27 | url | app/blah/route.ts:2:17:2:23 | req.url | app/blah/route.ts:3:25:3:27 | url | Cross-site scripting vulnerability due to a $@. | app/blah/route.ts:2:17:2:23 | req.url | user-provided value |
3839
| app/pages/Next2.jsx:8:13:8:19 | req.url | app/pages/Next2.jsx:8:13:8:19 | req.url | app/pages/Next2.jsx:8:13:8:19 | req.url | Cross-site scripting vulnerability due to a $@. | app/pages/Next2.jsx:8:13:8:19 | req.url | user-provided value |
3940
| app/pages/Next2.jsx:15:13:15:19 | req.url | app/pages/Next2.jsx:15:13:15:19 | req.url | app/pages/Next2.jsx:15:13:15:19 | req.url | Cross-site scripting vulnerability due to a $@. | app/pages/Next2.jsx:15:13:15:19 | req.url | user-provided value |
4041
| etherpad.js:11:12:11:19 | response | etherpad.js:9:16:9:30 | req.query.jsonp | etherpad.js:11:12:11:19 | response | Cross-site scripting vulnerability due to a $@. | etherpad.js:9:16:9:30 | req.query.jsonp | user-provided value |
@@ -154,6 +155,8 @@ edges
154155
| app/api/routeNextRequest.ts:4:9:4:12 | body | app/api/routeNextRequest.ts:31:27:31:30 | body | provenance | |
155156
| app/api/routeNextRequest.ts:4:16:4:31 | await req.json() | app/api/routeNextRequest.ts:4:9:4:12 | body | provenance | |
156157
| app/api/routeNextRequest.ts:4:22:4:31 | req.json() | app/api/routeNextRequest.ts:4:16:4:31 | await req.json() | provenance | |
158+
| app/blah/route.ts:2:11:2:13 | url | app/blah/route.ts:3:25:3:27 | url | provenance | |
159+
| app/blah/route.ts:2:17:2:23 | req.url | app/blah/route.ts:2:11:2:13 | url | provenance | |
157160
| etherpad.js:9:5:9:12 | response | etherpad.js:11:12:11:19 | response | provenance | |
158161
| etherpad.js:9:16:9:30 | req.query.jsonp | etherpad.js:9:5:9:12 | response | provenance | |
159162
| formatting.js:4:9:4:12 | evil | formatting.js:6:43:6:46 | evil | provenance | |
@@ -367,6 +370,9 @@ nodes
367370
| app/api/routeNextRequest.ts:15:20:15:23 | body | semmle.label | body |
368371
| app/api/routeNextRequest.ts:27:20:27:23 | body | semmle.label | body |
369372
| app/api/routeNextRequest.ts:31:27:31:30 | body | semmle.label | body |
373+
| app/blah/route.ts:2:11:2:13 | url | semmle.label | url |
374+
| app/blah/route.ts:2:17:2:23 | req.url | semmle.label | req.url |
375+
| app/blah/route.ts:3:25:3:27 | url | semmle.label | url |
370376
| app/pages/Next2.jsx:8:13:8:19 | req.url | semmle.label | req.url |
371377
| app/pages/Next2.jsx:15:13:15:19 | req.url | semmle.label | req.url |
372378
| etherpad.js:9:5:9:12 | response | semmle.label | response |

javascript/ql/test/query-tests/Security/CWE-079/ReflectedXss/ReflectedXssWithCustomSanitizer.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
| app/api/routeNextRequest.ts:15:20:15:23 | body | Cross-site scripting vulnerability due to $@. | app/api/routeNextRequest.ts:4:22:4:31 | req.json() | user-provided value |
3535
| app/api/routeNextRequest.ts:27:20:27:23 | body | Cross-site scripting vulnerability due to $@. | app/api/routeNextRequest.ts:4:22:4:31 | req.json() | user-provided value |
3636
| app/api/routeNextRequest.ts:31:27:31:30 | body | Cross-site scripting vulnerability due to $@. | app/api/routeNextRequest.ts:4:22:4:31 | req.json() | user-provided value |
37+
| app/blah/route.ts:3:25:3:27 | url | Cross-site scripting vulnerability due to $@. | app/blah/route.ts:2:17:2:23 | req.url | user-provided value |
3738
| app/pages/Next2.jsx:8:13:8:19 | req.url | Cross-site scripting vulnerability due to $@. | app/pages/Next2.jsx:8:13:8:19 | req.url | user-provided value |
3839
| app/pages/Next2.jsx:15:13:15:19 | req.url | Cross-site scripting vulnerability due to $@. | app/pages/Next2.jsx:15:13:15:19 | req.url | user-provided value |
3940
| formatting.js:6:14:6:47 | util.fo ... , evil) | Cross-site scripting vulnerability due to $@. | formatting.js:4:16:4:29 | req.query.evil | user-provided value |
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
export async function GET(req: Request) {
2-
const url = req.url; // $ MISSING: Source
3-
return new Response(url, { headers: { "Content-Type": "text/html" } }); // $ MISSING: Alert
2+
const url = req.url; // $ Source
3+
return new Response(url, { headers: { "Content-Type": "text/html" } }); // $ Alert
44
}

0 commit comments

Comments
 (0)