|
| 1 | +/** |
| 2 | + * Provides classes for working with [Sails](https://sailsjs.com/) applications. |
| 3 | + */ |
| 4 | + |
| 5 | +private import javascript |
| 6 | +private import semmle.javascript.frameworks.HTTP |
| 7 | +private import DataFlow |
| 8 | + |
| 9 | +/** |
| 10 | + * Provides classes for working with [Sails](https://sailsjs.com/) applications. |
| 11 | + */ |
| 12 | +module Sails { |
| 13 | + /** |
| 14 | + * A Sails Action2 action object exported from a module. |
| 15 | + * |
| 16 | + * For example: |
| 17 | + * |
| 18 | + * ```javascript |
| 19 | + * module.exports = { |
| 20 | + * inputs: { filename: { type: "string" } }, |
| 21 | + * fn(inputs, exits) { ... } |
| 22 | + * }; |
| 23 | + * ``` |
| 24 | + */ |
| 25 | + private class Action2Action extends ObjectExpr { |
| 26 | + Action2Action() { |
| 27 | + exists(Module mod, DataFlow::FunctionNode fn | |
| 28 | + mod.getABulkExportedNode().getALocalSource().asExpr() = this and |
| 29 | + this.getFile().getRelativePath().regexpMatch("(^|.*/)api/controllers/.*") and |
| 30 | + this.getPropertyByName("inputs").getInit() instanceof ObjectExpr and |
| 31 | + fn = DataFlow::valueNode(this.getPropertyByName("fn").getInit()).getAFunctionValue() |
| 32 | + ) |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Gets the `fn` function that handles the action. |
| 37 | + */ |
| 38 | + DataFlow::FunctionNode getFunction() { |
| 39 | + result = DataFlow::valueNode(this.getPropertyByName("fn").getInit()).getAFunctionValue() |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Gets the name of an input declared by this action. |
| 44 | + */ |
| 45 | + string getADeclaredInputName() { |
| 46 | + result = this.getPropertyByName("inputs").getInit().(ObjectExpr).getAProperty().getName() |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * A Sails Action2 handler function. |
| 52 | + */ |
| 53 | + private class Action2RouteHandler extends Http::Servers::StandardRouteHandler, |
| 54 | + DataFlow::FunctionNode |
| 55 | + { |
| 56 | + Action2Action action; |
| 57 | + |
| 58 | + Action2RouteHandler() { this = action.getFunction() } |
| 59 | + |
| 60 | + /** |
| 61 | + * Gets the parameter of the action handler that contains the validated action inputs. |
| 62 | + */ |
| 63 | + DataFlow::ParameterNode getInputsParameter() { result = this.getParameter(0) } |
| 64 | + |
| 65 | + /** |
| 66 | + * Gets the name of an input declared by this action. |
| 67 | + */ |
| 68 | + string getADeclaredInputName() { result = action.getADeclaredInputName() } |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * An access to a user-controlled Sails Action2 input. |
| 73 | + */ |
| 74 | + private class Action2InputAccess extends Http::RequestInputAccess { |
| 75 | + Action2RouteHandler rh; |
| 76 | + |
| 77 | + Action2InputAccess() { |
| 78 | + this = rh.getInputsParameter().getAPropertyRead(rh.getADeclaredInputName()) |
| 79 | + } |
| 80 | + |
| 81 | + override Action2RouteHandler getRouteHandler() { result = rh } |
| 82 | + |
| 83 | + override string getKind() { result = "parameter" } |
| 84 | + } |
| 85 | +} |
0 commit comments