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