|
| 1 | +private import codeql.Locations |
| 2 | +private import codeql.ruby.AST |
| 3 | +private import internal.Erb |
| 4 | +private import internal.TreeSitter |
| 5 | + |
| 6 | +/** |
| 7 | + * A node in the ERB abstract syntax tree. This class is the base class for all |
| 8 | + * ERB elements. |
| 9 | + */ |
| 10 | +class ErbAstNode extends TAstNode { |
| 11 | + /** Gets a textual representation of this node. */ |
| 12 | + cached |
| 13 | + string toString() { none() } |
| 14 | + |
| 15 | + /** Gets the location of this node. */ |
| 16 | + Location getLocation() { result = getLocation(this) } |
| 17 | + |
| 18 | + /** |
| 19 | + * Gets the name of a primary CodeQL class to which this node belongs. |
| 20 | + * |
| 21 | + * This predicate always has a result. If no primary class can be |
| 22 | + * determined, the result is `"???"`. If multiple primary classes match, |
| 23 | + * this predicate can have multiple results. |
| 24 | + */ |
| 25 | + string getAPrimaryQlClass() { result = "???" } |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * An ERB template. This can contain multiple directives to be executed when |
| 30 | + * the template is compiled. |
| 31 | + */ |
| 32 | +class ErbTemplate extends TTemplate, ErbAstNode { |
| 33 | + private Erb::Template g; |
| 34 | + |
| 35 | + ErbTemplate() { this = TTemplate(g) } |
| 36 | + |
| 37 | + override string toString() { result = "erb template" } |
| 38 | + |
| 39 | + final override string getAPrimaryQlClass() { result = "ErbTemplate" } |
| 40 | + |
| 41 | + ErbAstNode getAChildNode() { toGenerated(result) = g.getChild(_) } |
| 42 | +} |
| 43 | + |
| 44 | +// Truncate the token string value to 32 char max |
| 45 | +bindingset[val] |
| 46 | +private string displayToken(string val) { |
| 47 | + val.length() <= 32 and result = val |
| 48 | + or |
| 49 | + val.length() > 32 and result = val.prefix(29) + "..." |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | + * An ERB token. This could be embedded code, a comment, or arbitrary text. |
| 54 | + */ |
| 55 | +class ErbToken extends TTokenNode, ErbAstNode { |
| 56 | + override string toString() { result = displayToken(this.getValue()) } |
| 57 | + |
| 58 | + /** Gets the string value of this token. */ |
| 59 | + string getValue() { exists(Erb::Token g | this = fromGenerated(g) | result = g.getValue()) } |
| 60 | + |
| 61 | + override string getAPrimaryQlClass() { result = "ErbToken" } |
| 62 | +} |
| 63 | + |
| 64 | +/** |
| 65 | + * An ERB token appearing within a comment directive. |
| 66 | + */ |
| 67 | +class ErbComment extends ErbToken { |
| 68 | + private Erb::Comment g; |
| 69 | + |
| 70 | + ErbComment() { this = TComment(g) } |
| 71 | + |
| 72 | + override string getValue() { result = g.getValue() } |
| 73 | + |
| 74 | + final override string getAPrimaryQlClass() { result = "ErbComment" } |
| 75 | +} |
| 76 | + |
| 77 | +/** |
| 78 | + * An ERB token appearing within a code directive. This will typically be |
| 79 | + * interpreted as Ruby code or a GraphQL query, depending on context. |
| 80 | + */ |
| 81 | +class ErbCode extends ErbToken { |
| 82 | + private Erb::Code g; |
| 83 | + |
| 84 | + ErbCode() { this = TCode(g) } |
| 85 | + |
| 86 | + override string getValue() { result = g.getValue() } |
| 87 | + |
| 88 | + final override string getAPrimaryQlClass() { result = "ErbCode" } |
| 89 | +} |
| 90 | + |
| 91 | +bindingset[line, col] |
| 92 | +private predicate locationIncludesPosition(Location loc, int line, int col) { |
| 93 | + // position between start and end line, exclusive |
| 94 | + line > loc.getStartLine() and |
| 95 | + line < loc.getEndLine() |
| 96 | + or |
| 97 | + // position on start line, multi line location |
| 98 | + line = loc.getStartLine() and |
| 99 | + not loc.getStartLine() = loc.getEndLine() and |
| 100 | + col >= loc.getStartColumn() |
| 101 | + or |
| 102 | + // position on end line, multi line location |
| 103 | + line = loc.getEndLine() and |
| 104 | + not loc.getStartLine() = loc.getEndLine() and |
| 105 | + col <= loc.getEndColumn() |
| 106 | + or |
| 107 | + // single line location, position between start and end column |
| 108 | + line = loc.getStartLine() and |
| 109 | + loc.getStartLine() = loc.getEndLine() and |
| 110 | + col >= loc.getStartColumn() and |
| 111 | + col <= loc.getEndColumn() |
| 112 | +} |
| 113 | + |
| 114 | +/** |
| 115 | + * A directive in an ERB template. |
| 116 | + */ |
| 117 | +class ErbDirective extends TDirectiveNode, ErbAstNode { |
| 118 | + private predicate containsStartOf(Location loc) { |
| 119 | + loc.getFile() = this.getLocation().getFile() and |
| 120 | + locationIncludesPosition(this.getLocation(), loc.getStartLine(), loc.getStartColumn()) |
| 121 | + } |
| 122 | + |
| 123 | + private predicate containsStmtStart(Stmt s) { |
| 124 | + this.containsStartOf(s.getLocation()) and |
| 125 | + // `Toplevel` statements are not contained within individual directives, |
| 126 | + // though their start location may appear within a directive location |
| 127 | + not s instanceof Toplevel |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Gets a statement that starts in directive that is not a child of any other |
| 132 | + * statement starting in this directive. |
| 133 | + */ |
| 134 | + Stmt getAChildStmt() { |
| 135 | + this.containsStmtStart(result) and |
| 136 | + not this.containsStmtStart(result.getParent()) |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Gets the last child statement in this directive. |
| 141 | + * See `getAChildStmt` for more details. |
| 142 | + */ |
| 143 | + Stmt getTerminalStmt() { |
| 144 | + result = this.getAChildStmt() and |
| 145 | + forall(Stmt s | s = this.getAChildStmt() and not s = result | |
| 146 | + s.getLocation().strictlyBefore(result.getLocation()) |
| 147 | + ) |
| 148 | + } |
| 149 | + |
| 150 | + /** Gets the child token of this directive. */ |
| 151 | + ErbToken getToken() { |
| 152 | + exists(Erb::Directive g | this = fromGenerated(g) | toGenerated(result) = g.getChild()) |
| 153 | + } |
| 154 | + |
| 155 | + override string toString() { result = "erb directive" } |
| 156 | + |
| 157 | + override string getAPrimaryQlClass() { result = "ErbDirective" } |
| 158 | +} |
| 159 | + |
| 160 | +/** |
| 161 | + * A comment directive in an ERB template. |
| 162 | + * ```erb |
| 163 | + * <%#= 2 + 2 %> |
| 164 | + * <%# for x in xs do %> |
| 165 | + * ``` |
| 166 | + */ |
| 167 | +class ErbCommentDirective extends ErbDirective { |
| 168 | + private Erb::CommentDirective g; |
| 169 | + |
| 170 | + ErbCommentDirective() { this = TCommentDirective(g) } |
| 171 | + |
| 172 | + override ErbComment getToken() { toGenerated(result) = g.getChild() } |
| 173 | + |
| 174 | + final override string toString() { result = "<%#" + this.getToken().toString() + "%>" } |
| 175 | + |
| 176 | + final override string getAPrimaryQlClass() { result = "ErbCommentDirective" } |
| 177 | +} |
| 178 | + |
| 179 | +/** |
| 180 | + * A GraphQL directive in an ERB template. |
| 181 | + * ```erb |
| 182 | + * <%graphql |
| 183 | + * fragment Foo on Bar { |
| 184 | + * some { |
| 185 | + * queryText |
| 186 | + * moreProperties |
| 187 | + * } |
| 188 | + * } |
| 189 | + * %> |
| 190 | + * ``` |
| 191 | + */ |
| 192 | +class ErbGraphqlDirective extends ErbDirective { |
| 193 | + private Erb::GraphqlDirective g; |
| 194 | + |
| 195 | + ErbGraphqlDirective() { this = TGraphqlDirective(g) } |
| 196 | + |
| 197 | + override ErbCode getToken() { toGenerated(result) = g.getChild() } |
| 198 | + |
| 199 | + final override string toString() { result = "<%graphql" + this.getToken().toString() + "%>" } |
| 200 | + |
| 201 | + final override string getAPrimaryQlClass() { result = "ErbGraphqlDirective" } |
| 202 | +} |
| 203 | + |
| 204 | +/** |
| 205 | + * An output directive in an ERB template. |
| 206 | + * ```erb |
| 207 | + * <%= |
| 208 | + * fragment Foo on Bar { |
| 209 | + * some { |
| 210 | + * queryText |
| 211 | + * moreProperties |
| 212 | + * } |
| 213 | + * } |
| 214 | + * %> |
| 215 | + * ``` |
| 216 | + */ |
| 217 | +class ErbOutputDirective extends ErbDirective { |
| 218 | + private Erb::OutputDirective g; |
| 219 | + |
| 220 | + ErbOutputDirective() { this = TOutputDirective(g) } |
| 221 | + |
| 222 | + override ErbCode getToken() { toGenerated(result) = g.getChild() } |
| 223 | + |
| 224 | + final override string toString() { result = "<%=" + this.getToken().toString() + "%>" } |
| 225 | + |
| 226 | + final override string getAPrimaryQlClass() { result = "ErbOutputDirective" } |
| 227 | +} |
| 228 | + |
| 229 | +/** |
| 230 | + * An execution directive in an ERB template. |
| 231 | + * This code will be executed as Ruby, but not rendered. |
| 232 | + * ```erb |
| 233 | + * <% books = author.books |
| 234 | + * for book in books do %> |
| 235 | + * ``` |
| 236 | + */ |
| 237 | +class ErbExecutionDirective extends ErbDirective { |
| 238 | + private Erb::Directive g; |
| 239 | + |
| 240 | + ErbExecutionDirective() { this = TDirective(g) } |
| 241 | + |
| 242 | + final override string toString() { result = "<%" + this.getToken().toString() + "%>" } |
| 243 | + |
| 244 | + final override string getAPrimaryQlClass() { result = "ErbExecutionDirective" } |
| 245 | +} |
| 246 | + |
| 247 | +/** |
| 248 | + * A `File` containing an Embedded Ruby template. |
| 249 | + * This is typically a file containing snippets of Ruby code that can be |
| 250 | + * evaluated to create a compiled version of the file. |
| 251 | + */ |
| 252 | +class ErbFile extends File { |
| 253 | + private ErbTemplate template; |
| 254 | + |
| 255 | + ErbFile() { this = template.getLocation().getFile() } |
| 256 | + |
| 257 | + /** |
| 258 | + * Holds if the file represents a partial to be rendered in the context of |
| 259 | + * another template. |
| 260 | + */ |
| 261 | + predicate isPartial() { this.getStem().charAt(0) = "_" } |
| 262 | + |
| 263 | + /** |
| 264 | + * Gets the erb template contained within this file. |
| 265 | + */ |
| 266 | + ErbTemplate getTemplate() { result = template } |
| 267 | +} |
0 commit comments