mirror of
https://github.com/github/codeql.git
synced 2026-04-27 17:55:19 +02:00
Enhance Next.js API endpoint handling for compatibility with both Pages and App Router structures.
This commit is contained in:
@@ -213,10 +213,12 @@ module NextJS {
|
||||
/**
|
||||
* Gets a folder that contains API endpoints for a Next.js application.
|
||||
* These API endpoints act as Express-like route-handlers.
|
||||
* It matches both the Pages Router (`pages/api/`) Next.js 12 or earlier and
|
||||
* the App Router (`app/api/`) Next.js 13+ structures.
|
||||
*/
|
||||
Folder apiFolder() {
|
||||
result = getANextPackage().getFile().getParentContainer().getFolder("pages").getFolder("api")
|
||||
or
|
||||
result =
|
||||
getANextPackage().getFile().getParentContainer().getFolder(["pages", "app"]).getFolder("api") or
|
||||
result = apiFolder().getAFolder()
|
||||
}
|
||||
|
||||
@@ -271,4 +273,56 @@ module NextJS {
|
||||
override string getCredentialsKind() { result = "jwt key" }
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A route handler for Next.js 13+ App Router API endpoints, which are defined by exporting
|
||||
* HTTP method functions (like `GET`, `POST`, `PUT`, `DELETE`) from route.js files inside
|
||||
* the `app/api/` directory.
|
||||
*/
|
||||
class NextAppRouteHandler extends DataFlow::FunctionNode, Http::Servers::StandardRouteHandler {
|
||||
NextAppRouteHandler() {
|
||||
exists(Module mod | mod.getFile().getParentContainer() = apiFolder() |
|
||||
this = mod.getAnExportedValue(any(Http::RequestMethodName m)).getAFunctionValue() and
|
||||
(
|
||||
this.getParameter(0).hasUnderlyingType("next/server", "NextRequest")
|
||||
or
|
||||
this.getParameter(0).hasUnderlyingType("Request")
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the request parameter, which is either a `NextRequest` object (from `next/server`) or a standard web `Request` object.
|
||||
*/
|
||||
DataFlow::SourceNode getRequest() { result = this.getParameter(0) }
|
||||
}
|
||||
|
||||
/**
|
||||
* A source of user-controlled data from a `NextRequest` object (from `next/server`) or a standard web `Request` object
|
||||
* in a Next.js App Router route handler.
|
||||
*/
|
||||
class NextAppRequestSource extends Http::RequestInputAccess {
|
||||
NextAppRouteHandler handler;
|
||||
string kind;
|
||||
|
||||
NextAppRequestSource() {
|
||||
(
|
||||
this =
|
||||
handler.getRequest().getAMethodCall(["json", "formData", "blob", "arrayBuffer", "text"])
|
||||
or
|
||||
this = handler.getRequest().getAPropertyRead("body")
|
||||
) and
|
||||
kind = "body"
|
||||
or
|
||||
this = handler.getRequest().getAPropertyRead(["url", "nextUrl"]) and kind = "url"
|
||||
or
|
||||
this = handler.getRequest().getAPropertyRead("headers") and kind = "headers"
|
||||
}
|
||||
|
||||
override string getKind() { result = kind }
|
||||
|
||||
override Http::RouteHandler getRouteHandler() { result = handler }
|
||||
|
||||
override string getSourceType() { result = "Next.js App Router request" }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user