Skip to content

Commit 18dac9a

Browse files
committed
Ruby: Handle GraphQL array types
1 parent 5706bc6 commit 18dac9a

File tree

3 files changed

+34
-10
lines changed

3 files changed

+34
-10
lines changed

ruby/ql/lib/codeql/ruby/frameworks/GraphQL.qll

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,16 @@ private class GraphqlFieldArgumentDefinitionMethodCall extends GraphqlSchemaObje
327327

328328
/** Gets the type of this argument */
329329
GraphqlType getArgumentType() { result = this.getArgument(1) }
330+
331+
/**
332+
* Gets the element type of this argument, if it is an array.
333+
* For example if the argument type is `[String]`, this predicate yields `String`.
334+
*/
335+
GraphqlType getArgumentElementType() {
336+
result =
337+
any(ArrayLiteral lit | lit = this.getArgument(1) and lit.getNumberOfElements() = 1)
338+
.getElement(0)
339+
}
330340
}
331341

332342
private class GraphqlType extends ConstantAccess {
@@ -446,7 +456,8 @@ class GraphqlFieldResolutionMethod extends Method, Http::Server::RequestHandler:
446456
override Parameter getARoutedParameter() {
447457
result = this.getAParameter() and
448458
exists(GraphqlFieldArgumentDefinitionMethodCall argDefn |
449-
argDefn = this.getDefinition().getAnArgumentCall()
459+
argDefn = this.getDefinition().getAnArgumentCall() and
460+
[argDefn.getArgumentType(), argDefn.getArgumentElementType()].isScalar()
450461
|
451462
result.(KeywordParameter).hasName(argDefn.getArgumentName())
452463
)
@@ -474,7 +485,7 @@ private DataFlow::CallNode parameterAccess(
474485
def = method.getDefinition().getAnArgumentCall() and
475486
(
476487
// Direct access to the params hash
477-
def.getArgumentType() = type and
488+
[def.getArgumentType(), def.getArgumentElementType()] = type and
478489
def.getArgumentName() = key and
479490
exists(DataFlow::Node paramRead |
480491
paramRead.asExpr().getExpr() = param.getVariable().getAnAccess().(VariableReadAccess) and

ruby/ql/test/library-tests/frameworks/graphql/GraphQL.expected

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,21 @@ graphqlSchemaObjectClass
22
| app/graphql/types/base_object.rb:2:3:4:5 | BaseObject |
33
| app/graphql/types/mutation_type.rb:2:3:4:5 | MutationType |
44
| app/graphql/types/post.rb:1:1:6:5 | Post |
5-
| app/graphql/types/query_type.rb:2:3:64:5 | QueryType |
5+
| app/graphql/types/query_type.rb:2:3:71:5 | QueryType |
66
graphqlSchemaObjectFieldDefinition
77
| app/graphql/types/mutation_type.rb:2:3:4:5 | MutationType | app/graphql/types/mutation_type.rb:3:5:3:44 | call to field |
88
| app/graphql/types/post.rb:1:1:6:5 | Post | app/graphql/types/post.rb:2:5:2:24 | call to field |
99
| app/graphql/types/post.rb:1:1:6:5 | Post | app/graphql/types/post.rb:3:5:3:36 | call to field |
1010
| app/graphql/types/post.rb:1:1:6:5 | Post | app/graphql/types/post.rb:4:5:4:60 | call to field |
1111
| app/graphql/types/post.rb:1:1:6:5 | Post | app/graphql/types/post.rb:5:5:5:51 | call to field |
12-
| app/graphql/types/query_type.rb:2:3:64:5 | QueryType | app/graphql/types/query_type.rb:3:5:5:40 | call to field |
13-
| app/graphql/types/query_type.rb:2:3:64:5 | QueryType | app/graphql/types/query_type.rb:7:5:9:7 | call to field |
14-
| app/graphql/types/query_type.rb:2:3:64:5 | QueryType | app/graphql/types/query_type.rb:15:5:17:7 | call to field |
15-
| app/graphql/types/query_type.rb:2:3:64:5 | QueryType | app/graphql/types/query_type.rb:24:5:26:7 | call to field |
16-
| app/graphql/types/query_type.rb:2:3:64:5 | QueryType | app/graphql/types/query_type.rb:32:5:35:7 | call to field |
17-
| app/graphql/types/query_type.rb:2:3:64:5 | QueryType | app/graphql/types/query_type.rb:46:5:49:7 | call to field |
18-
| app/graphql/types/query_type.rb:2:3:64:5 | QueryType | app/graphql/types/query_type.rb:55:5:57:7 | call to field |
12+
| app/graphql/types/query_type.rb:2:3:71:5 | QueryType | app/graphql/types/query_type.rb:3:5:5:40 | call to field |
13+
| app/graphql/types/query_type.rb:2:3:71:5 | QueryType | app/graphql/types/query_type.rb:7:5:9:7 | call to field |
14+
| app/graphql/types/query_type.rb:2:3:71:5 | QueryType | app/graphql/types/query_type.rb:15:5:17:7 | call to field |
15+
| app/graphql/types/query_type.rb:2:3:71:5 | QueryType | app/graphql/types/query_type.rb:24:5:26:7 | call to field |
16+
| app/graphql/types/query_type.rb:2:3:71:5 | QueryType | app/graphql/types/query_type.rb:32:5:35:7 | call to field |
17+
| app/graphql/types/query_type.rb:2:3:71:5 | QueryType | app/graphql/types/query_type.rb:46:5:49:7 | call to field |
18+
| app/graphql/types/query_type.rb:2:3:71:5 | QueryType | app/graphql/types/query_type.rb:55:5:57:7 | call to field |
19+
| app/graphql/types/query_type.rb:2:3:71:5 | QueryType | app/graphql/types/query_type.rb:65:5:67:7 | call to field |
1920
graphqlResolveMethod
2021
| app/graphql/mutations/dummy.rb:9:5:12:7 | resolve |
2122
| app/graphql/resolvers/dummy_resolver.rb:10:5:13:7 | resolve |
@@ -41,24 +42,28 @@ graphqlFieldDefinitionMethodCall
4142
| app/graphql/types/query_type.rb:32:5:35:7 | call to field |
4243
| app/graphql/types/query_type.rb:46:5:49:7 | call to field |
4344
| app/graphql/types/query_type.rb:55:5:57:7 | call to field |
45+
| app/graphql/types/query_type.rb:65:5:67:7 | call to field |
4446
graphqlFieldResolutionMethod
4547
| app/graphql/types/query_type.rb:10:5:13:7 | with_arg |
4648
| app/graphql/types/query_type.rb:18:5:22:7 | custom_method |
4749
| app/graphql/types/query_type.rb:27:5:30:7 | with_splat |
4850
| app/graphql/types/query_type.rb:36:5:40:7 | with_splat_and_named_arg |
4951
| app/graphql/types/query_type.rb:50:5:53:7 | with_enum |
5052
| app/graphql/types/query_type.rb:58:5:63:7 | with_nested_enum |
53+
| app/graphql/types/query_type.rb:68:5:70:7 | with_array |
5154
graphqlFieldResolutionRoutedParameter
5255
| app/graphql/types/query_type.rb:10:5:13:7 | with_arg | app/graphql/types/query_type.rb:10:18:10:23 | number |
5356
| app/graphql/types/query_type.rb:18:5:22:7 | custom_method | app/graphql/types/query_type.rb:18:23:18:33 | blah_number |
5457
| app/graphql/types/query_type.rb:36:5:40:7 | with_splat_and_named_arg | app/graphql/types/query_type.rb:36:34:36:37 | arg1 |
58+
| app/graphql/types/query_type.rb:68:5:70:7 | with_array | app/graphql/types/query_type.rb:68:20:68:23 | list |
5559
graphqlFieldResolutionDefinition
5660
| app/graphql/types/query_type.rb:10:5:13:7 | with_arg | app/graphql/types/query_type.rb:7:5:9:7 | call to field |
5761
| app/graphql/types/query_type.rb:18:5:22:7 | custom_method | app/graphql/types/query_type.rb:15:5:17:7 | call to field |
5862
| app/graphql/types/query_type.rb:27:5:30:7 | with_splat | app/graphql/types/query_type.rb:24:5:26:7 | call to field |
5963
| app/graphql/types/query_type.rb:36:5:40:7 | with_splat_and_named_arg | app/graphql/types/query_type.rb:32:5:35:7 | call to field |
6064
| app/graphql/types/query_type.rb:50:5:53:7 | with_enum | app/graphql/types/query_type.rb:46:5:49:7 | call to field |
6165
| app/graphql/types/query_type.rb:58:5:63:7 | with_nested_enum | app/graphql/types/query_type.rb:55:5:57:7 | call to field |
66+
| app/graphql/types/query_type.rb:68:5:70:7 | with_array | app/graphql/types/query_type.rb:65:5:67:7 | call to field |
6267
graphqlRemoteFlowSources
6368
| app/graphql/mutations/dummy.rb:5:24:5:25 | id |
6469
| app/graphql/mutations/dummy.rb:9:17:9:25 | something |
@@ -72,3 +77,4 @@ graphqlRemoteFlowSources
7277
| app/graphql/types/query_type.rb:38:22:38:32 | ...[...] |
7378
| app/graphql/types/query_type.rb:52:22:52:32 | ...[...] |
7479
| app/graphql/types/query_type.rb:60:22:60:41 | ...[...] |
80+
| app/graphql/types/query_type.rb:68:20:68:23 | list |

ruby/ql/test/library-tests/frameworks/graphql/app/graphql/types/query_type.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,12 @@ def with_nested_enum(**args)
6161
system("echo #{args[:inner][:media_category]}")
6262
system("echo #{args[:inner][:direction]}")
6363
end
64+
65+
field :with_array, String do
66+
argument :list, [String], "Names"
67+
end
68+
def with_array(list:)
69+
system("echo #{list[0]}")
70+
end
6471
end
6572
end

0 commit comments

Comments
 (0)