44 */
55
66import javascript
7- private import semmle.javascript.PackageExports as Exports
87
98/**
109 * Provides classes and predicates for working with serverless handlers.
10+ * In particular a `RemoteFlowSource` is added for AWS and Alibaba serverless.
1111 */
1212private module ServerLess {
1313 /**
@@ -47,11 +47,13 @@ private module ServerLess {
4747
4848 /**
4949 * Gets a path to a file from a `codeURI` property and a file name from a serverless configuration.
50+ *
51+ * For example if `codeURI` is "function/." and `file` is "index", then the result becomes "function/index.js".
5052 */
5153 bindingset [ codeURI, file]
5254 private string getPathFromHandlerProperties ( string codeURI , string file ) {
5355 exists ( string folder | folder = removeLeadingDotSlash ( removeTrailingDot ( codeURI ) ) |
54- if folder . regexpMatch ( ".*\\..+" ) then result = folder else result = folder + file + ".js"
56+ result = folder + file + ".js"
5557 )
5658 }
5759
@@ -61,7 +63,9 @@ private module ServerLess {
6163 private predicate hasServerlessHandler ( File file , string func ) {
6264 exists ( File ymlFile , string handler , string codeURI , string fileName |
6365 hasServerlessHandler ( ymlFile , handler , codeURI ) and
66+ // Captures everything right of the dot in `handler`. E.g. if `handler` is "index.foo" then `func` is "foo".
6467 func = handler .regexpCapture ( ".*\\.(.*)" , 1 ) and
68+ // Captures everything left of the dot in `handler`. E.g. if `handler` is "index.foo" then `fileName` is "index".
6569 fileName = handler .regexpCapture ( "([^.]+).*" , 1 )
6670 |
6771 file .getAbsolutePath ( ) =
@@ -72,6 +76,21 @@ private module ServerLess {
7276
7377 /**
7478 * Gets a function that is a serverless request handler.
79+ *
80+ * For example: if an AWS serverless resource contains the following properties (in the "template.yml" file):
81+ * ```
82+ * Handler: mylibrary.handler
83+ * Runtime: nodejs12.x
84+ * CodeUri: backend/src/
85+ * ```
86+ *
87+ * And a file "mylibrary.js" exists in the folder "backend/src" (relative to the "template.yml" file).
88+ * Then the result of this predicate is a function exported as "handler" from "mylibrary.js".
89+ * The "mylibrary.js" file could for example look like:
90+ *
91+ * ```JavaScript
92+ * module.exports.handler = function (event) { ... }
93+ * ```
7594 */
7695 private DataFlow:: FunctionNode getAServerlessHandler ( ) {
7796 exists ( File file , string handler , Module mod | hasServerlessHandler ( file , handler ) |
0 commit comments