Skip to content

Commit 1100b75

Browse files
committed
Ruby: handle routes with path/action pairs
1 parent 0473655 commit 1100b75

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

ruby/ql/lib/codeql/ruby/frameworks/actiondispatch/internal/Routing.qll

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,28 @@ module Routing {
177177
}
178178
}
179179

180+
private Expr getActionFromMethodCall(MethodCall methodCall) {
181+
result =
182+
[
183+
// e.g. `get "/comments", to: "comments#index"
184+
methodCall.getKeywordArgument("to"),
185+
// e.g. `get "/comments" => "comments#index"
186+
methodCall.getArgument(0).(Pair).getValue()
187+
]
188+
}
189+
190+
/**
191+
* Gets a string representation of the controller-action pair that is routed
192+
* to by this method call.
193+
*/
194+
private string getActionStringFromMethodCall(MethodCall methodCall) {
195+
getActionFromMethodCall(methodCall).getConstantValue().isStringlikeValue(result)
196+
or
197+
// TODO: use the redirect call argument to resolve the redirect target
198+
getActionFromMethodCall(methodCall).(MethodCall).getMethodName() = "redirect" and
199+
result = "<redirect>#<redirect>"
200+
}
201+
180202
/**
181203
* A route block defined by a call to `resources`.
182204
* ```rb
@@ -512,12 +534,7 @@ module Routing {
512534
)
513535
}
514536

515-
private string getActionString() {
516-
methodCall.getKeywordArgument("to").getConstantValue().isStringlikeValue(result)
517-
or
518-
methodCall.getKeywordArgument("to").(MethodCall).getMethodName() = "redirect" and
519-
result = "<redirect>#<redirect>"
520-
}
537+
private string getActionString() { result = getActionStringFromMethodCall(methodCall) }
521538

522539
override string getAction() {
523540
// get "/photos", action: "index"
@@ -670,11 +687,7 @@ module Routing {
670687
}
671688

672689
override string getLastControllerComponent() {
673-
result =
674-
extractController(methodCall
675-
.getKeywordArgument("to")
676-
.getConstantValue()
677-
.getStringlikeValue()) or
690+
result = extractController(getActionStringFromMethodCall(methodCall)) or
678691
methodCall.getKeywordArgument("controller").getConstantValue().isStringlikeValue(result) or
679692
result =
680693
extractController(methodCall
@@ -704,8 +717,7 @@ module Routing {
704717
}
705718

706719
override string getAction() {
707-
result =
708-
extractAction(methodCall.getKeywordArgument("to").getConstantValue().getStringlikeValue()) or
720+
result = extractAction(getActionStringFromMethodCall(methodCall)) or
709721
methodCall.getKeywordArgument("action").getConstantValue().isStringlikeValue(result) or
710722
result =
711723
extractAction(methodCall

ruby/ql/test/library-tests/frameworks/action_dispatch/ActionDispatch.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ actionDispatchRoutes
1212
| app/config/routes.rb:4:7:4:41 | call to resources | post | posts/:post_id/comments/:comment_id/replies | replies | create |
1313
| app/config/routes.rb:5:7:5:28 | call to post | post | posts/:post_id/comments/:comment_id/flag | comments | flag |
1414
| app/config/routes.rb:7:5:7:37 | call to post | post | posts/:post_id/upvote | posts | upvote |
15+
| app/config/routes.rb:8:5:8:39 | call to post | post | posts/:post_id | posts | downvote |
1516
| app/config/routes.rb:12:5:12:54 | call to post | post | destroy_all_posts | posts | destroy_alll |
1617
| app/config/routes.rb:16:5:16:46 | call to get | get | numbers/:number | numbers | show |
1718
| app/config/routes.rb:20:5:20:44 | call to get | get | admin/jobs | background_jobs | index |
@@ -39,6 +40,7 @@ actionDispatchControllerMethods
3940
| app/config/routes.rb:3:5:6:7 | call to resources | app/controllers/comments_controller.rb:2:3:39:5 | index |
4041
| app/config/routes.rb:3:5:6:7 | call to resources | app/controllers/comments_controller.rb:41:3:42:5 | show |
4142
| app/config/routes.rb:7:5:7:37 | call to post | app/controllers/posts_controller.rb:8:3:9:5 | upvote |
43+
| app/config/routes.rb:8:5:8:39 | call to post | app/controllers/posts_controller.rb:11:3:12:5 | downvote |
4244
| app/config/routes.rb:28:3:28:48 | call to match | app/controllers/photos_controller.rb:2:3:3:5 | show |
4345
| app/config/routes.rb:29:3:29:50 | call to match | app/controllers/photos_controller.rb:2:3:3:5 | show |
4446
| app/config/routes.rb:30:3:30:69 | call to match | app/controllers/photos_controller.rb:2:3:3:5 | show |

0 commit comments

Comments
 (0)