|
| 1 | +import javascript |
| 2 | +import utils.test.InlineExpectationsTest |
| 3 | + |
| 4 | +module CapturedConfig implements DataFlow::ConfigSig { |
| 5 | + predicate isSource(DataFlow::Node source) { |
| 6 | + source.(DataFlow::InvokeNode).getCalleeName() = "SOURCE" |
| 7 | + } |
| 8 | + |
| 9 | + predicate isSink(DataFlow::Node sink) { |
| 10 | + exists(DataFlow::InvokeNode call | |
| 11 | + call.getCalleeName() = "SINK" and |
| 12 | + sink = call.getArgument(0) |
| 13 | + ) |
| 14 | + } |
| 15 | + |
| 16 | + predicate isBarrier(DataFlow::Node node) { |
| 17 | + node = DataFlow::MakeBarrierGuard<SimpleBarrierGuard>::getABarrierNode() |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +module CapturedFlow = DataFlow::Global<CapturedConfig>; |
| 22 | + |
| 23 | +class SimpleBarrierGuard extends DataFlow::InvokeNode { |
| 24 | + SimpleBarrierGuard() { this.getCalleeName() = "BARRIER" } |
| 25 | + |
| 26 | + predicate blocksExpr(boolean outcome, Expr expr) { |
| 27 | + outcome = true and |
| 28 | + expr = this.getArgument(0).asExpr() |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +class LegacySimpleBarrierGuard extends DataFlow::BarrierGuardNode, SimpleBarrierGuard { |
| 33 | + override predicate blocks(boolean outcome, Expr expr) { this.blocksExpr(outcome, expr) } |
| 34 | +} |
| 35 | + |
| 36 | +class LegacyCapturedConfig extends DataFlow::Configuration { |
| 37 | + LegacyCapturedConfig() { this = "LegacyCapturedConfig" } |
| 38 | + |
| 39 | + override predicate isSource(DataFlow::Node source) { CapturedConfig::isSource(source) } |
| 40 | + |
| 41 | + override predicate isSink(DataFlow::Node sink) { CapturedConfig::isSink(sink) } |
| 42 | + |
| 43 | + override predicate isBarrierGuard(DataFlow::BarrierGuardNode guard) { |
| 44 | + guard instanceof LegacySimpleBarrierGuard |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +module InlineTest implements TestSig { |
| 49 | + string getARelevantTag() { |
| 50 | + result = |
| 51 | + [ |
| 52 | + "capturedWrite", "noCapturedWrite", "ssaDefinition", "noSsaAstNode", "localCapturedFlow", |
| 53 | + "capturedFlow", "legacyCapturedFlow" |
| 54 | + ] |
| 55 | + } |
| 56 | + |
| 57 | + predicate hasActualResult(Location location, string element, string tag, string value) { |
| 58 | + element = "" and |
| 59 | + location.getFile().getBaseName() = "captured.js" and |
| 60 | + ( |
| 61 | + exists(VarRef reference | |
| 62 | + value = reference.getVariable().getName() and |
| 63 | + value = ["capturedWrite", "uncapturedWrite", "globalWrite", "capturedPattern"] and |
| 64 | + location = reference.getLocation() and |
| 65 | + ( |
| 66 | + tag = "capturedWrite" and |
| 67 | + exists(DataFlow::SsaDefinitionNode node | |
| 68 | + node.getSsaVariable().getDefinition().(SsaExplicitDefinition).getLhs() = reference and |
| 69 | + reference.getVariable().isCaptured() |
| 70 | + ) |
| 71 | + or |
| 72 | + tag = "noCapturedWrite" and |
| 73 | + not exists(DataFlow::SsaDefinitionNode node | |
| 74 | + node.getSsaVariable().getDefinition().(SsaExplicitDefinition).getLhs() = reference and |
| 75 | + reference.getVariable().isCaptured() |
| 76 | + ) |
| 77 | + ) |
| 78 | + ) |
| 79 | + or |
| 80 | + tag = "ssaDefinition" and |
| 81 | + exists(SsaExplicitDefinition definition, VarRef write, DataFlow::SsaDefinitionNode node | |
| 82 | + write = definition.getLhs() and |
| 83 | + write.getVariable().isCaptured() and |
| 84 | + node = DataFlow::lvalueNode(write) and |
| 85 | + node = DataFlow::ssaDefinitionNode(definition) and |
| 86 | + node.getSsaVariable() = definition.getVariable() and |
| 87 | + location = write.getLocation() and |
| 88 | + value = write.getVariable().getName() |
| 89 | + ) |
| 90 | + or |
| 91 | + tag = "noSsaAstNode" and |
| 92 | + exists(VarDecl write | |
| 93 | + write.getVariable().getName() = "capturedWrite" and |
| 94 | + not exists(DataFlow::lvalueNode(write).getAstNode()) and |
| 95 | + location = write.getLocation() and |
| 96 | + value = write.getVariable().getName() |
| 97 | + ) |
| 98 | + or |
| 99 | + tag = "localCapturedFlow" and |
| 100 | + exists(DataFlow::InvokeNode sink, VarUse read, SsaExplicitDefinition definition | |
| 101 | + sink.getCalleeName() = "SINK" and |
| 102 | + sink.getArgument(0).asExpr() = read and |
| 103 | + read.getSsaVariable().getDefinition() = definition and |
| 104 | + definition.getSourceVariable().isCaptured() and |
| 105 | + definition.getContainer() = sink.getContainer() and |
| 106 | + DataFlow::lvalueNode(definition.getLhs()).getASuccessor+() = sink.getArgument(0) and |
| 107 | + location = sink.getLocation() and |
| 108 | + value = read.getVariable().getName() |
| 109 | + ) |
| 110 | + or |
| 111 | + exists(DataFlow::InvokeNode source, DataFlow::InvokeNode sink | |
| 112 | + source.getFile().getBaseName() = "captured.js" and |
| 113 | + source.getCalleeName() = "SOURCE" and |
| 114 | + sink.getCalleeName() = "SINK" and |
| 115 | + value = source.getArgument(0).getStringValue() and |
| 116 | + location = sink.getLocation() and |
| 117 | + ( |
| 118 | + tag = "capturedFlow" and CapturedFlow::flow(source, sink.getArgument(0)) |
| 119 | + or |
| 120 | + tag = "legacyCapturedFlow" and |
| 121 | + any(LegacyCapturedConfig config).hasFlow(source, sink.getArgument(0)) |
| 122 | + ) |
| 123 | + ) |
| 124 | + ) |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +import MakeTest<InlineTest> |
0 commit comments