|
| 1 | +/** |
| 2 | + * Provides a model of `cheerio`, a server-side DOM manipulation library with a jQuery-like API. |
| 3 | + */ |
| 4 | + |
| 5 | +import javascript |
| 6 | +private import semmle.javascript.security.dataflow.Xss as Xss |
| 7 | + |
| 8 | +module Cheerio { |
| 9 | + /** |
| 10 | + * A reference to the `cheerio` function, possibly with a loaded DOM. |
| 11 | + */ |
| 12 | + private DataFlow::SourceNode cheerioRef(DataFlow::TypeTracker t) { |
| 13 | + t.start() and |
| 14 | + ( |
| 15 | + result = DataFlow::moduleImport("cheerio") |
| 16 | + or |
| 17 | + exists(string name | result = cheerioRef().getAMemberCall(name) | |
| 18 | + name = "load" or |
| 19 | + name = "parseHTML" |
| 20 | + ) |
| 21 | + ) |
| 22 | + or |
| 23 | + exists(DataFlow::TypeTracker t2 | result = cheerioRef(t2).track(t2, t)) |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * A reference to the `cheerio` function, possibly with a loaded DOM. |
| 28 | + */ |
| 29 | + DataFlow::SourceNode cheerioRef() { result = cheerioRef(DataFlow::TypeTracker::end()) } |
| 30 | + |
| 31 | + /** |
| 32 | + * Creation of `cheerio` object, a collection of virtual DOM elements |
| 33 | + * with an interface similar to that of a jQuery object. |
| 34 | + */ |
| 35 | + class CheerioObjectCreation extends DataFlow::SourceNode { |
| 36 | + CheerioObjectCreation::Range range; |
| 37 | + |
| 38 | + CheerioObjectCreation() { this = range } |
| 39 | + } |
| 40 | + |
| 41 | + module CheerioObjectCreation { |
| 42 | + /** |
| 43 | + * Creation of a `cheerio` object. |
| 44 | + */ |
| 45 | + abstract class Range extends DataFlow::SourceNode { } |
| 46 | + |
| 47 | + private class DefaultRange extends Range { |
| 48 | + DefaultRange() { |
| 49 | + this = cheerioRef().getACall() |
| 50 | + or |
| 51 | + this = cheerioRef().getAMethodCall() |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Gets a reference to a `cheerio` object, a collection of virtual DOM elements |
| 58 | + * with an interface similar to jQuery objects. |
| 59 | + */ |
| 60 | + private DataFlow::SourceNode cheerioObjectRef(DataFlow::TypeTracker t) { |
| 61 | + t.start() and |
| 62 | + result instanceof CheerioObjectCreation |
| 63 | + or |
| 64 | + // Chainable calls. |
| 65 | + t.start() and |
| 66 | + exists(DataFlow::MethodCallNode call, string name | |
| 67 | + call = cheerioObjectRef().getAMethodCall(name) and |
| 68 | + result = call |
| 69 | + | |
| 70 | + if name = "attr" or name = "data" or name = "prop" or name = "css" |
| 71 | + then call.getNumArgument() = 2 |
| 72 | + else |
| 73 | + if name = "val" or name = "html" or name = "text" |
| 74 | + then call.getNumArgument() = 1 |
| 75 | + else ( |
| 76 | + name != "toString" and |
| 77 | + name != "toArray" and |
| 78 | + name != "hasClass" |
| 79 | + ) |
| 80 | + ) |
| 81 | + or |
| 82 | + exists(DataFlow::TypeTracker t2 | result = cheerioObjectRef(t2).track(t2, t)) |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Gets a reference to a `cheerio` object, a collection of virtual DOM elements |
| 87 | + * with an interface similar to jQuery objects. |
| 88 | + */ |
| 89 | + DataFlow::SourceNode cheerioObjectRef() { |
| 90 | + result = cheerioObjectRef(DataFlow::TypeTracker::end()) |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Definition of a DOM attribute through `cheerio`. |
| 95 | + */ |
| 96 | + class AttributeDef extends DOM::AttributeDefinition { |
| 97 | + DataFlow::CallNode call; |
| 98 | + |
| 99 | + AttributeDef() { |
| 100 | + this = call.asExpr() and |
| 101 | + call = cheerioObjectRef().getAMethodCall("attr") and |
| 102 | + call.getNumArgument() >= 2 |
| 103 | + } |
| 104 | + |
| 105 | + override string getName() { call.getArgument(0).mayHaveStringValue(result) } |
| 106 | + |
| 107 | + override DataFlow::Node getValueNode() { result = call.getArgument(1) } |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * XSS sink through `cheerio`. |
| 112 | + */ |
| 113 | + class XssSink extends Xss::DomBasedXss::Sink { |
| 114 | + XssSink() { |
| 115 | + exists(string name | this = cheerioObjectRef().getAMethodCall(name).getAnArgument() | |
| 116 | + JQuery::isMethodArgumentInterpretedAsHtml(name) |
| 117 | + ) |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments