|
| 1 | +/** |
| 2 | + * @name Client-side clipboard-based cross-site scripting |
| 3 | + * @description Pasting clipboard input directly to the DOM without proper sanitization allows for |
| 4 | + * a cross-site scripting vulnerability. |
| 5 | + * @kind path-problem |
| 6 | + * @problem.severity error |
| 7 | + * @security-severity 3.1 |
| 8 | + * @precision high |
| 9 | + * @id js/clipboard-xss |
| 10 | + * @tags security |
| 11 | + * external/cwe/cwe-079 |
| 12 | + */ |
| 13 | + |
| 14 | +import javascript |
| 15 | +import DataFlow |
| 16 | +import semmle.javascript.security.dataflow.DomBasedXssQuery as DomBasedXss |
| 17 | +import DataFlow::PathGraph |
| 18 | + |
| 19 | +/* |
| 20 | + * Gets references to clipboardData DataTransfer objects |
| 21 | + * https://developer.mozilla.org/en-US/docs/Web/API/ClipboardEvent/clipboardData |
| 22 | + */ |
| 23 | + |
| 24 | +SourceNode clipboardDataTransferSource(TypeTracker t) { |
| 25 | + t.start() and |
| 26 | + exists(DataFlow::PropRead pr | pr.getPropertyName() = "clipboardData" and result = pr) |
| 27 | + or |
| 28 | + exists(TypeTracker t2 | result = clipboardDataTransferSource(t2).track(t2, t)) |
| 29 | +} |
| 30 | + |
| 31 | +SourceNode clipboardDataTransferSource() { |
| 32 | + result = clipboardDataTransferSource(TypeTracker::end()) |
| 33 | +} |
| 34 | + |
| 35 | +/* |
| 36 | + * Gets references to the result of a call to getData on a DataTransfer object |
| 37 | + * https://developer.mozilla.org/en-US/docs/Web/API/DataTransfer/getData |
| 38 | + */ |
| 39 | + |
| 40 | +SourceNode clipboardDataSource(TypeTracker t) { |
| 41 | + t.start() and |
| 42 | + result = clipboardDataTransferSource().getAMethodCall("getData") |
| 43 | + or |
| 44 | + exists(TypeTracker t2 | result = clipboardDataSource(t2).track(t2, t)) |
| 45 | +} |
| 46 | + |
| 47 | +SourceNode clipboardDataSource() { result = clipboardDataSource(TypeTracker::end()) } |
| 48 | + |
| 49 | +class ClipboardSource extends DataFlow::Node { |
| 50 | + ClipboardSource() { this = clipboardDataSource() } |
| 51 | +} |
| 52 | + |
| 53 | +class ClipboardHtmlInjectionConfiguration extends DomBasedXss::HtmlInjectionConfiguration { |
| 54 | + override predicate isSource(DataFlow::Node source) { source instanceof ClipboardSource } |
| 55 | +} |
| 56 | + |
| 57 | +class ClipboardJQueryHtmlOrSelectorInjectionConfiguration extends DomBasedXss::JQueryHtmlOrSelectorInjectionConfiguration { |
| 58 | + override predicate isSource(DataFlow::Node source, DataFlow::FlowLabel label) { |
| 59 | + // Reuse any source not derived from location |
| 60 | + source instanceof ClipboardSource and |
| 61 | + ( |
| 62 | + label.isTaint() |
| 63 | + or |
| 64 | + label.isData() // Require transformation before reaching sink |
| 65 | + ) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +from DataFlow::Configuration cfg, DataFlow::PathNode source, DataFlow::PathNode sink |
| 70 | +where |
| 71 | + ( |
| 72 | + cfg instanceof ClipboardHtmlInjectionConfiguration or |
| 73 | + cfg instanceof ClipboardJQueryHtmlOrSelectorInjectionConfiguration |
| 74 | + ) and |
| 75 | + cfg.hasFlowPath(source, sink) |
| 76 | +select sink.getNode(), source, sink, |
| 77 | + sink.getNode().(DomBasedXss::Sink).getVulnerabilityKind() + " vulnerability due to $@.", |
| 78 | + source.getNode(), "user-provided clipboard value" |
0 commit comments