|
| 1 | +/** |
| 2 | + * Provides a taint-tracking configuration for reasoning about hard-coded data |
| 3 | + * being interpreted as code. |
| 4 | + */ |
| 5 | + |
| 6 | +import javascript |
| 7 | +private import semmle.javascript.security.dataflow.CodeInjection |
| 8 | + |
| 9 | +module HardcodedDataInterpretedAsCode { |
| 10 | + /** |
| 11 | + * A data flow source for hard-coded data. |
| 12 | + */ |
| 13 | + abstract class Source extends DataFlow::Node { |
| 14 | + /** Gets a flow label for which this is a source. */ |
| 15 | + DataFlow::FlowLabel getLabel() { |
| 16 | + result = DataFlow::FlowLabel::data() |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + /** |
| 21 | + * A data flow sink for code injection. |
| 22 | + */ |
| 23 | + abstract class Sink extends DataFlow::Node { |
| 24 | + /** Gets a flow label for which this is a sink. */ |
| 25 | + abstract DataFlow::FlowLabel getLabel(); |
| 26 | + |
| 27 | + /** Gets a description of what kind of sink this is. */ |
| 28 | + abstract string getKind(); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * A sanitizer for hard-coded data. |
| 33 | + */ |
| 34 | + abstract class Sanitizer extends DataFlow::Node {} |
| 35 | + |
| 36 | + /** |
| 37 | + * A taint-tracking configuration for reasoning about hard-coded data |
| 38 | + * being interpreted as code |
| 39 | + */ |
| 40 | + class Configuration extends TaintTracking::Configuration { |
| 41 | + Configuration() { |
| 42 | + this = "HardcodedDataInterpretedAsCode" |
| 43 | + } |
| 44 | + |
| 45 | + override predicate isSource(DataFlow::Node source, DataFlow::FlowLabel lbl) { |
| 46 | + source.(Source).getLabel() = lbl |
| 47 | + } |
| 48 | + |
| 49 | + override predicate isSink(DataFlow::Node nd, DataFlow::FlowLabel lbl) { |
| 50 | + nd.(Sink).getLabel() = lbl |
| 51 | + } |
| 52 | + |
| 53 | + override predicate isSanitizer(DataFlow::Node node) { |
| 54 | + node instanceof Sanitizer |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * A constant string consisting of eight or more hexadecimal characters (including at |
| 60 | + * least one digit), viewed as a source of hard-coded data that should not be |
| 61 | + * interpreted as code. |
| 62 | + */ |
| 63 | + private class DefaultSource extends Source, DataFlow::ValueNode { |
| 64 | + DefaultSource() { |
| 65 | + exists (string val | val = astNode.(Expr).getStringValue() | |
| 66 | + val.regexpMatch("[0-9a-fA-F]{8,}") and |
| 67 | + val.regexpMatch(".*[0-9].*") |
| 68 | + ) |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * A code injection sink; hard-coded data should not flow here. |
| 74 | + */ |
| 75 | + private class DefaultCodeInjectionSink extends Sink { |
| 76 | + DefaultCodeInjectionSink() { this instanceof CodeInjection::Sink } |
| 77 | + override DataFlow::FlowLabel getLabel() { result = DataFlow::FlowLabel::taint() } |
| 78 | + override string getKind() { result = "code" } |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * An argument to `require` path; hard-coded data should not flow here. |
| 83 | + */ |
| 84 | + private class RequireArgumentSink extends Sink { |
| 85 | + RequireArgumentSink() { |
| 86 | + this = any(Require r).getAnArgument().flow() |
| 87 | + } |
| 88 | + |
| 89 | + override DataFlow::FlowLabel getLabel() { |
| 90 | + result = DataFlow::FlowLabel::data() |
| 91 | + or |
| 92 | + result = DataFlow::FlowLabel::taint() |
| 93 | + } |
| 94 | + |
| 95 | + override string getKind() { result = "an import path" } |
| 96 | + } |
| 97 | +} |
0 commit comments