|
| 1 | +/** |
| 2 | + * Provides classes modeling security-relevant aspects of the `gradio` PyPI package. |
| 3 | + * See https://pypi.org/project/gradio/. |
| 4 | + */ |
| 5 | + |
| 6 | +import python |
| 7 | +import semmle.python.dataflow.new.RemoteFlowSources |
| 8 | +import semmle.python.dataflow.new.TaintTracking |
| 9 | +import semmle.python.ApiGraphs |
| 10 | + |
| 11 | + |
| 12 | +module Gradio { |
| 13 | + /** |
| 14 | + * Event handlers in Gradio, which are sources of untrusted data. |
| 15 | + */ |
| 16 | + |
| 17 | + class GradioInput extends API::CallNode { |
| 18 | + GradioInput() { this = API::moduleImport("gradio") |
| 19 | + .getMember([ |
| 20 | + "Button", "Textbox", "UploadButton", "Slider", |
| 21 | + "JSON", "HTML", "Markdown", "File", |
| 22 | + "AnnotatedImage", "Audio", "BarPlot", "Chatbot", "Checkbox", |
| 23 | + "CheckboxGroup", "ClearButton", "Code", "ColorPicker", "Dataframe", |
| 24 | + "Dataset", "DownloadButton", "Dropdown", "DuplicateButton", "FileExplorer", |
| 25 | + "Gallery", "HighlightedText", "Image", "ImageEditor", "Label", "LinePlot", |
| 26 | + "LoginButton", "LogoutButton", "Model3D", "Number", "ParamViewer", |
| 27 | + "Plot", "Radio", "ScatterPlot", "SimpleImage", "State", "Video"]) |
| 28 | + .getReturn() |
| 29 | + .getMember([ |
| 30 | + "change", "input", "click", "submit", |
| 31 | + "edit", "clear", "play", "pause", "stop", "end", "start_recording", |
| 32 | + "pause_recording", "stop_recording", "focus", "blur", |
| 33 | + "upload", "release", "select", "stream", "like", "load", |
| 34 | + "like", "key_up",]) |
| 35 | + .getACall() |
| 36 | + } |
| 37 | + |
| 38 | + } |
| 39 | + |
| 40 | + class GradioInterface extends API::CallNode { |
| 41 | + GradioInterface() { this = API::moduleImport("gradio").getMember(["Interface", "ChatInterface"]).getACall() } |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Track `inputs` parameters in Gradio event handlers, that are lists, back to source, f.ex. `gr.Textbox(...)`. Handle keyword and positional parameters. |
| 46 | + */ |
| 47 | + class GradioInputList extends RemoteFlowSource::Range { |
| 48 | + GradioInputList() { |
| 49 | + exists(API::CallNode call | |
| 50 | + (call instanceof GradioInput |
| 51 | + or |
| 52 | + call instanceof GradioInterface) |
| 53 | + and |
| 54 | + // limit only to lists of parameters given to `inputs`. |
| 55 | + ((call.getKeywordParameter("inputs").asSink().asCfgNode() instanceof ListNode |
| 56 | + or |
| 57 | + call.getParameter(1).asSink().asCfgNode() instanceof ListNode) |
| 58 | + and |
| 59 | + (this = call.getKeywordParameter("inputs").getASuccessor().getAValueReachingSink() |
| 60 | + or |
| 61 | + this = call.getParameter(1).getASuccessor().getAValueReachingSink())) |
| 62 | + ) |
| 63 | + } |
| 64 | + override string getSourceType() { result = "Gradio untrusted input" } |
| 65 | + |
| 66 | + |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Track `inputs` parameters in Gradio event handlers, that are not lists. Handle keyword and positional parameters. |
| 71 | + */ |
| 72 | + class GradioInputParameter extends RemoteFlowSource::Range { |
| 73 | + GradioInputParameter() { |
| 74 | + exists(API::CallNode call | |
| 75 | + (call instanceof GradioInput |
| 76 | + or |
| 77 | + call instanceof GradioInterface) |
| 78 | + |
| 79 | + and |
| 80 | + (this = call.getKeywordParameter("fn").getParameter(_).asSource() |
| 81 | + or |
| 82 | + this = call.getParameter(0).getParameter(_).asSource()) |
| 83 | + |
| 84 | + and |
| 85 | + // exclude lists of parameters given to `inputs` |
| 86 | + not call.getKeywordParameter("inputs").asSink().asCfgNode() instanceof ListNode |
| 87 | + and |
| 88 | + not call.getParameter(1).asSink().asCfgNode() instanceof ListNode |
| 89 | + ) |
| 90 | + } |
| 91 | + override string getSourceType() { result = "Gradio untrusted input" } |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Track `inputs` parameters in Gradio decorators to event handlers. |
| 96 | + */ |
| 97 | + class GradioInputDecorator extends RemoteFlowSource::Range { |
| 98 | + GradioInputDecorator() { |
| 99 | + exists(API::CallNode call | |
| 100 | + (call instanceof GradioInput or call instanceof GradioInterface) |
| 101 | + and |
| 102 | + this = call.getReturn().getACall().getParameter(0).getParameter(_).asSource() |
| 103 | + ) |
| 104 | + } |
| 105 | + override string getSourceType() { result = "Gradio untrusted input" } |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Extra taint propagation for tracking `inputs` parameters in Gradio event handlers, that are lists. |
| 110 | + */ |
| 111 | + private class ListTaintStep extends TaintTracking::AdditionalTaintStep { |
| 112 | + override predicate step(DataFlow::Node nodeFrom, DataFlow::Node nodeTo) { |
| 113 | + exists(API::CallNode node | |
| 114 | + (node instanceof GradioInput |
| 115 | + or |
| 116 | + node instanceof GradioInterface) |
| 117 | + and |
| 118 | + // handle cases where there are multiple arguments passed as a list to `inputs` |
| 119 | + (( |
| 120 | + (node.getKeywordParameter("inputs").asSink().asCfgNode() instanceof ListNode |
| 121 | + or |
| 122 | + node.getParameter(1).asSink().asCfgNode() instanceof ListNode) |
| 123 | + and |
| 124 | + exists(int i | |
| 125 | + (nodeTo = node.getParameter(0).getParameter(i).asSource() |
| 126 | + or |
| 127 | + nodeTo = node.getKeywordParameter("fn").getParameter(i).asSource()) |
| 128 | + and |
| 129 | + (nodeFrom.asCfgNode() = node.getKeywordParameter("inputs").asSink().asCfgNode().(ListNode).getElement(i) |
| 130 | + or |
| 131 | + nodeFrom.asCfgNode() = node.getParameter(1).asSink().asCfgNode().(ListNode).getElement(i)))) |
| 132 | + ) |
| 133 | + ) |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | +} |
0 commit comments