|
| 1 | +import java |
| 2 | +import semmle.code.java.dataflow.DataFlow |
| 3 | + |
| 4 | +abstract class CLIFlowSource extends DataFlow::Node { } |
| 5 | + |
| 6 | +Field childFiled(RefType rt) { |
| 7 | + not result.getType() instanceof PrimitiveType and |
| 8 | + not result.getType() instanceof BoxedType and |
| 9 | + ( |
| 10 | + result.getDeclaringType() = rt or |
| 11 | + result.getDeclaringType() = childFiled(result.getDeclaringType()).getType() |
| 12 | + ) |
| 13 | +} |
| 14 | + |
| 15 | +module CommonsCLI { |
| 16 | + class TypeCommandLine extends RefType { |
| 17 | + TypeCommandLine() { |
| 18 | + this.getAStrictAncestor*().hasQualifiedName("org.apache.commons.cli", "CommandLine") |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + class TypeOption extends RefType { |
| 23 | + TypeOption() { this.getAStrictAncestor*().hasQualifiedName("org.apache.commons.cli", "Option") } |
| 24 | + } |
| 25 | + |
| 26 | + class OptionValue extends CLIFlowSource { |
| 27 | + OptionValue() { |
| 28 | + exists(MethodAccess ma | |
| 29 | + ma.getCallee().getDeclaringType() instanceof TypeOption and |
| 30 | + ma.getCallee().hasName(["getValue", "getValues", "getValuesList"]) and |
| 31 | + this.asExpr() = ma |
| 32 | + ) |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + class CommandLine extends CLIFlowSource { |
| 37 | + CommandLine() { |
| 38 | + exists(MethodAccess ma | |
| 39 | + ma.getCallee().getDeclaringType() instanceof TypeCommandLine and |
| 40 | + ma.getCallee() |
| 41 | + .hasName(["getArgs", "getArgList", "getOptionValue", "getOptionValues", "getArgs"]) and |
| 42 | + this.asExpr() = ma |
| 43 | + ) |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +module Args4j { |
| 49 | + class TypeCmdLineParser extends RefType { |
| 50 | + TypeCmdLineParser() { |
| 51 | + // classes of ArgumentParser interface like `SubParser` |
| 52 | + this.getAStrictAncestor*().hasQualifiedName("org.kohsuke.args4j", "CmdLineParser") |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + class CommandLine extends CLIFlowSource { |
| 57 | + CommandLine() { |
| 58 | + exists(Call c, ClassInstanceExpr cie, Field f | |
| 59 | + c.getCallee().getDeclaringType() instanceof TypeCmdLineParser and |
| 60 | + DataFlow::localExprFlow(cie, c.getArgument(0)) and |
| 61 | + f.getDeclaringType() = cie.getType() and |
| 62 | + ( |
| 63 | + this.asExpr() = f.getAnAccess() |
| 64 | + or |
| 65 | + this.asExpr() = childFiled(f.getType()).getAnAccess() |
| 66 | + ) |
| 67 | + ) |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +module Jcommander { |
| 73 | + class TypeBuilder extends RefType { |
| 74 | + TypeBuilder() { |
| 75 | + // classes of ArgumentParser interface like `SubParser` |
| 76 | + this.getAStrictAncestor*().hasQualifiedName("com.beust.jcommander", "JCommander$Builder") |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + class CommandLine extends CLIFlowSource { |
| 81 | + CommandLine() { |
| 82 | + exists(Call c, ClassInstanceExpr cie, Field f | |
| 83 | + c.getCallee().getDeclaringType() instanceof TypeBuilder and |
| 84 | + DataFlow::localExprFlow(cie, c.getArgument(0)) and |
| 85 | + f.getDeclaringType() = cie.getType() and |
| 86 | + ( |
| 87 | + this.asExpr() = f.getAnAccess() |
| 88 | + or |
| 89 | + this.asExpr() = childFiled(f.getType()).getAnAccess() |
| 90 | + ) |
| 91 | + ) |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +module Picocli { |
| 97 | + class TypeCommandLine extends RefType { |
| 98 | + TypeCommandLine() { |
| 99 | + // classes of ArgumentParser interface like `SubParser` |
| 100 | + this.getAStrictAncestor*().hasQualifiedName("picocli", "CommandLine") |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + class GetCallResult extends CLIFlowSource { |
| 105 | + GetCallResult() { |
| 106 | + exists(MethodAccess ma | |
| 107 | + ma.getCallee().getDeclaringType() instanceof TypeCommandLine and |
| 108 | + ma.getCallee().hasName("getExecutionResult") and |
| 109 | + this.asExpr() = ma |
| 110 | + ) |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + class Execute extends CLIFlowSource { |
| 115 | + Execute() { |
| 116 | + exists(MethodAccess ma, ClassInstanceExpr cie, Field f | |
| 117 | + ma.getCallee().getDeclaringType() instanceof TypeCommandLine and |
| 118 | + ma.getCallee().hasName("populateCommand") and |
| 119 | + DataFlow::localExprFlow(cie, ma.getArgument(0)) and |
| 120 | + f.getDeclaringType() = cie.getType() and |
| 121 | + ( |
| 122 | + this.asExpr() = f.getAnAccess() |
| 123 | + or |
| 124 | + this.asExpr() = childFiled(f.getType()).getAnAccess() |
| 125 | + ) |
| 126 | + ) |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + class CommandLine extends CLIFlowSource { |
| 131 | + CommandLine() { |
| 132 | + exists(Call c, ClassInstanceExpr cie, Field f | |
| 133 | + c.getCallee().getDeclaringType() instanceof TypeCommandLine and |
| 134 | + DataFlow::localExprFlow(cie, c.getArgument(0)) and |
| 135 | + f.getDeclaringType() = cie.getType() and |
| 136 | + ( |
| 137 | + this.asExpr() = f.getAnAccess() |
| 138 | + or |
| 139 | + this.asExpr() = childFiled(f.getType()).getAnAccess() |
| 140 | + ) |
| 141 | + ) |
| 142 | + } |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +module ArgParse4j { |
| 147 | + class TypeArgumentParser extends RefType { |
| 148 | + TypeArgumentParser() { |
| 149 | + // classes of ArgumentParser interface like `SubParser` |
| 150 | + this.getAStrictAncestor*() |
| 151 | + .hasQualifiedName("net.sourceforge.argparse4j.inf", "ArgumentParser") |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + class TypeNamespace extends RefType { |
| 156 | + TypeNamespace() { |
| 157 | + this.getAStrictAncestor*().hasQualifiedName("net.sourceforge.argparse4j.inf", "Namespace") |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + class ParseArgsReturnValue extends CLIFlowSource { |
| 162 | + ParseArgsReturnValue() { |
| 163 | + exists(MethodAccess ma | |
| 164 | + ma.getReceiverType() instanceof TypeNamespace and |
| 165 | + ma.getCallee().hasName(["getString", "getList", "toString", "getByte", "get", "getAttrs"]) and |
| 166 | + this.asExpr() = ma |
| 167 | + ) |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + class ParseArgsSecondArg extends CLIFlowSource { |
| 172 | + ParseArgsSecondArg() { |
| 173 | + exists(MethodAccess ma, ClassInstanceExpr cie, Field f | |
| 174 | + ma.getReceiverType() instanceof TypeArgumentParser and |
| 175 | + ma.getCallee().hasName(["parseArgs"]) and |
| 176 | + ma.getNumArgument() = 2 and |
| 177 | + DataFlow::localExprFlow(cie, ma.getArgument(1)) and |
| 178 | + f.getDeclaringType() = cie.getType() and |
| 179 | + ( |
| 180 | + this.asExpr() = f.getAnAccess() |
| 181 | + or |
| 182 | + this.asExpr() = childFiled(f.getType()).getAnAccess() |
| 183 | + ) |
| 184 | + ) |
| 185 | + } |
| 186 | + } |
| 187 | +} |
0 commit comments