|
| 1 | +/** |
| 2 | + * Provides classes for working with typeahead.js code (https://www.npmjs.com/package/typeahead.js). |
| 3 | + */ |
| 4 | + |
| 5 | +import javascript |
| 6 | + |
| 7 | +module Typeahead { |
| 8 | + /** |
| 9 | + * A reference to the Bloodhound class, which is a utility-class for generating auto-complete suggestions. |
| 10 | + */ |
| 11 | + private class Bloodhound extends DataFlow::SourceNode { |
| 12 | + Bloodhound() { |
| 13 | + this = DataFlow::moduleImport("typeahead.js/dist/bloodhound.js") |
| 14 | + or |
| 15 | + this = DataFlow::moduleImport("bloodhound-js") |
| 16 | + or |
| 17 | + this.accessesGlobal("Bloodhound") |
| 18 | + } |
| 19 | + } |
| 20 | + |
| 21 | + /** |
| 22 | + * An instance of the Bloodhound class. |
| 23 | + */ |
| 24 | + private class BloodhoundInstance extends DataFlow::NewNode { |
| 25 | + BloodhoundInstance() { this = any(Bloodhound b).getAnInstantiation() } |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * An instance of of the Bloodhound class that is used to fetch data from a remote server. |
| 30 | + */ |
| 31 | + private class RemoteBloodhoundClientRequest extends ClientRequest::Range, BloodhoundInstance { |
| 32 | + DataFlow::ValueNode option; |
| 33 | + |
| 34 | + RemoteBloodhoundClientRequest() { |
| 35 | + exists(string optionName | optionName = "remote" or optionName = "prefetch" | |
| 36 | + option = this.getOptionArgument(0, optionName) |
| 37 | + ) |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Gets the URL for this Bloodhound instance. |
| 42 | + * The Bloodhound API specifies that the "remote" and "prefetch" options are either strings, |
| 43 | + * or an object containing an "url" property. |
| 44 | + */ |
| 45 | + override DataFlow::Node getUrl() { |
| 46 | + result = option.getALocalSource().getAPropertyWrite("url").getRhs() |
| 47 | + or |
| 48 | + result = option |
| 49 | + } |
| 50 | + |
| 51 | + override DataFlow::Node getHost() { none() } |
| 52 | + |
| 53 | + override DataFlow::Node getADataNode() { none() } |
| 54 | + |
| 55 | + /** Gets a Bloodhound instance that fetches remote server data. */ |
| 56 | + private DataFlow::SourceNode ref(DataFlow::TypeTracker t) { |
| 57 | + t.start() and result = this |
| 58 | + or |
| 59 | + exists(DataFlow::TypeTracker t2 | result = ref(t2).track(t2, t)) |
| 60 | + } |
| 61 | + |
| 62 | + /** Gets a Bloodhound instance that fetches remote server data. */ |
| 63 | + private DataFlow::SourceNode ref() { result = ref(DataFlow::TypeTracker::end()) } |
| 64 | + |
| 65 | + override DataFlow::Node getAResponseDataNode(string responseType, boolean promise) { |
| 66 | + responseType = "json" and |
| 67 | + promise = false and |
| 68 | + exists(TypeaheadSource source | |
| 69 | + ref() = source.getALocalSource() or ref().getAMethodCall("ttAdapter") = source |
| 70 | + | |
| 71 | + result = source.getASuggestion() |
| 72 | + ) |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * An invocation of the `typeahead.js` library. |
| 78 | + */ |
| 79 | + private class TypeaheadCall extends DataFlow::CallNode { |
| 80 | + TypeaheadCall() { |
| 81 | + // Matches `$(...).typeahead(..)` |
| 82 | + this = JQuery::objectRef().getAMethodCall("typeahead") |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * A function that generates suggestions to typeahead.js. |
| 88 | + */ |
| 89 | + class TypeaheadSuggestionFunction extends DataFlow::FunctionNode { |
| 90 | + TypeaheadCall typeaheadCall; |
| 91 | + |
| 92 | + TypeaheadSuggestionFunction() { |
| 93 | + // Matches `$(...).typeahead({..}, { templates: { suggestion: <this> } })`. |
| 94 | + this = typeaheadCall |
| 95 | + .getOptionArgument(1, "templates") |
| 96 | + .getALocalSource() |
| 97 | + .getAPropertyWrite("suggestion") |
| 98 | + .getRhs() |
| 99 | + .getAFunctionValue() |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Gets the call to typeahead.js where this suggestion function is used. |
| 104 | + */ |
| 105 | + TypeaheadCall getTypeaheadCall() { result = typeaheadCall } |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * A `source` option for a typeahead.js plugin instance. |
| 110 | + */ |
| 111 | + private class TypeaheadSource extends DataFlow::ValueNode { |
| 112 | + TypeaheadCall typeaheadCall; |
| 113 | + |
| 114 | + TypeaheadSource() { this = typeaheadCall.getOptionArgument(1, "source") } |
| 115 | + |
| 116 | + /** Gets a node for a suggestion that this source motivates. */ |
| 117 | + DataFlow::Node getASuggestion() { |
| 118 | + exists(TypeaheadSuggestionFunction suggestionCallback | |
| 119 | + suggestionCallback.getTypeaheadCall() = typeaheadCall and |
| 120 | + result = suggestionCallback.getParameter(0) |
| 121 | + ) |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * A taint step that models that a function in the `source` of typeahead.js is used to determine the input to the suggestion function. |
| 127 | + */ |
| 128 | + private class TypeaheadSourceTaintStep extends TypeaheadSource, TaintTracking::AdditionalTaintStep { |
| 129 | + override predicate step(DataFlow::Node pred, DataFlow::Node succ) { |
| 130 | + // Matches `$(...).typeahead({..}, {source: function(q, cb) {..;cb(<pred>);..}, templates: { suggestion: function(<succ>) {} } })`. |
| 131 | + pred = this.getAFunctionValue().getParameter([1 .. 2]).getACall().getAnArgument() and |
| 132 | + succ = this.getASuggestion() |
| 133 | + } |
| 134 | + } |
| 135 | +} |
0 commit comments