|
| 1 | +/** |
| 2 | + * Provides classes for modeling the `github.com/rs/cors` package. |
| 3 | + */ |
| 4 | + |
| 5 | +import go |
| 6 | + |
| 7 | +/** |
| 8 | + * An abstract class for modeling the Go CORS handler model origin write. |
| 9 | + */ |
| 10 | +abstract class UniversalOriginWrite extends DataFlow::ExprNode { |
| 11 | + /** |
| 12 | + * Get config variable holding header values |
| 13 | + */ |
| 14 | + abstract DataFlow::Node getBase(); |
| 15 | + |
| 16 | + /** |
| 17 | + * Get config variable holding header values |
| 18 | + */ |
| 19 | + abstract Variable getConfig(); |
| 20 | +} |
| 21 | + |
| 22 | +/** |
| 23 | + * An abstract class for modeling the Go CORS handler model allow all origins write. |
| 24 | + */ |
| 25 | +abstract class UniversalAllowAllOriginsWrite extends DataFlow::ExprNode { |
| 26 | + /** |
| 27 | + * Get config variable holding header values |
| 28 | + */ |
| 29 | + abstract DataFlow::Node getBase(); |
| 30 | + |
| 31 | + /** |
| 32 | + * Get config variable holding header values |
| 33 | + */ |
| 34 | + abstract Variable getConfig(); |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * An abstract class for modeling the Go CORS handler model allow credentials write. |
| 39 | + */ |
| 40 | +abstract class UniversalAllowCredentialsWrite extends DataFlow::ExprNode { |
| 41 | + /** |
| 42 | + * Get config struct holding header values |
| 43 | + */ |
| 44 | + abstract DataFlow::Node getBase(); |
| 45 | + |
| 46 | + /** |
| 47 | + * Get config variable holding header values |
| 48 | + */ |
| 49 | + abstract Variable getConfig(); |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | + * Provides classes for modeling the `github.com/rs/cors` package. |
| 54 | + */ |
| 55 | +module RsCors { |
| 56 | + /** Gets the package name `github.com/gin-gonic/gin`. */ |
| 57 | + string packagePath() { result = package("github.com/rs/cors", "") } |
| 58 | + |
| 59 | + /** |
| 60 | + * A new function create a new rs Handler |
| 61 | + */ |
| 62 | + class New extends Function { |
| 63 | + New() { exists(Function f | f.hasQualifiedName(packagePath(), "New") | this = f) } |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * A write to the value of Access-Control-Allow-Credentials header |
| 68 | + */ |
| 69 | + class AllowCredentialsWrite extends UniversalAllowCredentialsWrite { |
| 70 | + DataFlow::Node base; |
| 71 | + |
| 72 | + AllowCredentialsWrite() { |
| 73 | + exists(Field f, Write w | |
| 74 | + f.hasQualifiedName(packagePath(), "Options", "AllowCredentials") and |
| 75 | + w.writesField(base, f, this) and |
| 76 | + this.getType() instanceof BoolType |
| 77 | + ) |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Get options struct holding header values |
| 82 | + */ |
| 83 | + override DataFlow::Node getBase() { result = base } |
| 84 | + |
| 85 | + /** |
| 86 | + * Get options variable holding header values |
| 87 | + */ |
| 88 | + override RsOptions getConfig() { |
| 89 | + exists(RsOptions gc | |
| 90 | + ( |
| 91 | + gc.getV().getBaseVariable().getDefinition().(SsaExplicitDefinition).getRhs() = |
| 92 | + base.asInstruction() or |
| 93 | + gc.getV().getAUse() = base |
| 94 | + ) and |
| 95 | + result = gc |
| 96 | + ) |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * A write to the value of Access-Control-Allow-Origins header |
| 102 | + */ |
| 103 | + class AllowOriginsWrite extends UniversalOriginWrite { |
| 104 | + DataFlow::Node base; |
| 105 | + |
| 106 | + AllowOriginsWrite() { |
| 107 | + exists(Field f, Write w | |
| 108 | + f.hasQualifiedName(packagePath(), "Options", "AllowedOrigins") and |
| 109 | + w.writesField(base, f, this) and |
| 110 | + this.asExpr() instanceof SliceLit |
| 111 | + ) |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Get options struct holding header values |
| 116 | + */ |
| 117 | + override DataFlow::Node getBase() { result = base } |
| 118 | + |
| 119 | + /** |
| 120 | + * Get options variable holding header values |
| 121 | + */ |
| 122 | + override RsOptions getConfig() { |
| 123 | + exists(RsOptions gc | |
| 124 | + ( |
| 125 | + gc.getV().getBaseVariable().getDefinition().(SsaExplicitDefinition).getRhs() = |
| 126 | + base.asInstruction() or |
| 127 | + gc.getV().getAUse() = base |
| 128 | + ) and |
| 129 | + result = gc |
| 130 | + ) |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * A write to the value of Access-Control-Allow-Origins of value "*", overriding AllowOrigins |
| 136 | + */ |
| 137 | + class AllowAllOriginsWrite extends UniversalAllowAllOriginsWrite { |
| 138 | + DataFlow::Node base; |
| 139 | + |
| 140 | + AllowAllOriginsWrite() { |
| 141 | + exists(Field f, Write w | |
| 142 | + f.hasQualifiedName(packagePath(), "Options", "AllowAllOrigins") and |
| 143 | + w.writesField(base, f, this) and |
| 144 | + this.getType() instanceof BoolType |
| 145 | + ) |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Get options struct holding header values |
| 150 | + */ |
| 151 | + override DataFlow::Node getBase() { result = base } |
| 152 | + |
| 153 | + /** |
| 154 | + * Get options variable holding header values |
| 155 | + */ |
| 156 | + override RsOptions getConfig() { |
| 157 | + exists(RsOptions gc | |
| 158 | + ( |
| 159 | + gc.getV().getBaseVariable().getDefinition().(SsaExplicitDefinition).getRhs() = |
| 160 | + base.asInstruction() or |
| 161 | + gc.getV().getAUse() = base |
| 162 | + ) and |
| 163 | + result = gc |
| 164 | + ) |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * A variable of type Options that holds the headers to be set. |
| 170 | + */ |
| 171 | + class RsOptions extends Variable { |
| 172 | + SsaWithFields v; |
| 173 | + |
| 174 | + RsOptions() { |
| 175 | + this = v.getBaseVariable().getSourceVariable() and |
| 176 | + exists(Type t | t.hasQualifiedName(packagePath(), "Options") | v.getType() = t) |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * Get variable declaration of Options |
| 181 | + */ |
| 182 | + SsaWithFields getV() { result = v } |
| 183 | + } |
| 184 | +} |
0 commit comments