|
| 1 | +/** |
| 2 | + * Provides predicates for analyzing string concatenations and their operands. |
| 3 | + */ |
| 4 | +import javascript |
| 5 | + |
| 6 | +module StringConcatenation { |
| 7 | + /** Gets a data flow node referring to the result of the given concatenation. */ |
| 8 | + private DataFlow::Node getAssignAddResult(AssignAddExpr expr) { |
| 9 | + result = expr.flow() |
| 10 | + or |
| 11 | + exists (SsaExplicitDefinition def | def.getDef() = expr | |
| 12 | + result = DataFlow::valueNode(def.getVariable().getAUse())) |
| 13 | + } |
| 14 | + |
| 15 | + /** Gets the `n`th operand to the string concatenation defining `node`. */ |
| 16 | + DataFlow::Node getOperand(DataFlow::Node node, int n) { |
| 17 | + exists (AddExpr add | node = add.flow() | |
| 18 | + n = 0 and result = add.getLeftOperand().flow() |
| 19 | + or |
| 20 | + n = 1 and result = add.getRightOperand().flow()) |
| 21 | + or |
| 22 | + exists (TemplateLiteral template | node = template.flow() | |
| 23 | + result = template.getElement(n).flow() and |
| 24 | + not exists (TaggedTemplateExpr tag | template = tag.getTemplate())) |
| 25 | + or |
| 26 | + exists (AssignAddExpr assign | node = getAssignAddResult(assign) | |
| 27 | + n = 0 and result = assign.getLhs().flow() |
| 28 | + or |
| 29 | + n = 1 and result = assign.getRhs().flow()) |
| 30 | + or |
| 31 | + exists (DataFlow::ArrayCreationNode array, DataFlow::MethodCallNode call | |
| 32 | + call = array.getAMethodCall("join") and |
| 33 | + call.getArgument(0).mayHaveStringValue("") and |
| 34 | + ( |
| 35 | + // step from array element to array |
| 36 | + result = array.getElement(n) and |
| 37 | + node = array |
| 38 | + or |
| 39 | + // step from array to join call |
| 40 | + node = call and |
| 41 | + result = array and |
| 42 | + n = 0 |
| 43 | + )) |
| 44 | + } |
| 45 | + |
| 46 | + /** Gets an operand to the string concatenation defining `node`. */ |
| 47 | + DataFlow::Node getAnOperand(DataFlow::Node node) { |
| 48 | + result = getOperand(node, _) |
| 49 | + } |
| 50 | + |
| 51 | + /** Gets the number of operands to the given concatenation. */ |
| 52 | + int getNumOperand(DataFlow::Node node) { |
| 53 | + result = strictcount(getAnOperand(node)) |
| 54 | + } |
| 55 | + |
| 56 | + /** Gets the first operand to the string concatenation defining `node`. */ |
| 57 | + DataFlow::Node getFirstOperand(DataFlow::Node node) { |
| 58 | + result = getOperand(node, 0) |
| 59 | + } |
| 60 | + |
| 61 | + /** Gets the last operand to the string concatenation defining `node`. */ |
| 62 | + DataFlow::Node getLastOperand(DataFlow::Node node) { |
| 63 | + result = getOperand(node, getNumOperand(node) - 1) |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Holds if `src` flows to `dst` through the `n`th operand of the given concatenation operator. |
| 68 | + */ |
| 69 | + predicate taintStep(DataFlow::Node src, DataFlow::Node dst, DataFlow::Node operator, int n) { |
| 70 | + src = getOperand(dst, n) and |
| 71 | + operator = dst |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Holds if there is a taint step from `src` to `dst` through string concatenation. |
| 76 | + */ |
| 77 | + predicate taintStep(DataFlow::Node src, DataFlow::Node dst) { |
| 78 | + taintStep(src, dst, _, _) |
| 79 | + } |
| 80 | +} |
0 commit comments