Skip to content

Commit 227100d

Browse files
committed
Ruby: make old class names available as deprecated aliases
1 parent dd1b302 commit 227100d

File tree

5 files changed

+89
-59
lines changed

5 files changed

+89
-59
lines changed

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ private import codeql.ruby.frameworks.ActionView
1313
private import codeql.ruby.frameworks.Rails
1414
private import codeql.ruby.frameworks.internal.Rails
1515

16+
/**
17+
* DEPRECATED: Import `codeql.ruby.frameworks.Rails` and use `Rails::ParamsCall` instead.
18+
*/
19+
deprecated class ParamsCall = Rails::ParamsCall;
20+
21+
/**
22+
* DEPRECATED: Import `codeql.ruby.frameworks.Rails` and use `Rails::CookiesCall` instead.
23+
*/
24+
deprecated class CookiesCall = Rails::CookiesCall;
25+
1626
/**
1727
* A `ClassDeclaration` for a class that extends `ActionController::Base`.
1828
* For example,
@@ -74,7 +84,7 @@ class ActionControllerActionMethod extends Method, Http::Server::RequestHandler:
7484
override string getFramework() { result = "ActionController" }
7585

7686
/** Gets a call to render from within this method. */
77-
RenderCall getARenderCall() { result.getParent+() = this }
87+
Rails::RenderCall getARenderCall() { result.getParent+() = this }
7888

7989
/**
8090
* Gets the controller class containing this method.
@@ -126,7 +136,7 @@ private class ActionControllerContextCall extends MethodCall {
126136
* ActionController parameters available via the `params` method.
127137
*/
128138
class ParamsSource extends Http::Server::RequestInputAccess::Range {
129-
ParamsSource() { this.asExpr().getExpr() instanceof ParamsCall }
139+
ParamsSource() { this.asExpr().getExpr() instanceof Rails::ParamsCall }
130140

131141
override string getSourceType() { result = "ActionController::Metal#params" }
132142
}
@@ -136,7 +146,7 @@ class ParamsSource extends Http::Server::RequestInputAccess::Range {
136146
* ActionController parameters available via the `cookies` method.
137147
*/
138148
class CookiesSource extends Http::Server::RequestInputAccess::Range {
139-
CookiesSource() { this.asExpr().getExpr() instanceof CookiesCall }
149+
CookiesSource() { this.asExpr().getExpr() instanceof Rails::CookiesCall }
140150

141151
override string getSourceType() { result = "ActionController::Metal#cookies" }
142152
}
@@ -290,7 +300,7 @@ ActionControllerControllerClass getAssociatedControllerClass(ErbFile f) {
290300
// template file, `fp`. In this case, `f` inherits the associated
291301
// controller classes from `fp`.
292302
f.isPartial() and
293-
exists(RenderCall r, ErbFile fp |
303+
exists(Rails::RenderCall r, ErbFile fp |
294304
r.getLocation().getFile() = fp and
295305
r.getTemplateFile() = f and
296306
result = getAssociatedControllerClass(fp)

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

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,26 @@ private import codeql.ruby.dataflow.RemoteFlowSources
1111
private import codeql.ruby.frameworks.internal.Rails
1212
private import codeql.ruby.frameworks.Rails
1313

14+
/**
15+
* DEPRECATED: Import `codeql.ruby.frameworks.Rails` and use `Rails::HtmlSafeCall` instead.
16+
*/
17+
deprecated class HtmlSafeCall = Rails::HtmlSafeCall;
18+
19+
/**
20+
* DEPRECATED: Import `codeql.ruby.frameworks.Rails` and use `Rails::HtmlEscapeCall` instead.
21+
*/
22+
deprecated class HtmlEscapeCall = Rails::HtmlEscapeCall;
23+
24+
/**
25+
* DEPRECATED: Import `codeql.ruby.frameworks.Rails` and use `Rails::RenderCall` instead.
26+
*/
27+
deprecated class RenderCall = Rails::RenderCall;
28+
29+
/**
30+
* DEPRECATED: Import `codeql.ruby.frameworks.Rails` and use `Rails::RenderToCall` instead.
31+
*/
32+
deprecated class RenderToCall = Rails::RenderToCall;
33+
1434
/**
1535
* Holds if this AST node is in a context where `ActionView` methods are available.
1636
*/
@@ -28,7 +48,7 @@ private class ActionViewHtmlSafeCall extends HtmlSafeCallImpl {
2848
* A call to a Rails method that escapes HTML.
2949
*/
3050
class RailsHtmlEscaping extends Escaping::Range, DataFlow::CallNode {
31-
RailsHtmlEscaping() { this.asExpr().getExpr() instanceof HtmlEscapeCall }
51+
RailsHtmlEscaping() { this.asExpr().getExpr() instanceof Rails::HtmlEscapeCall }
3252

3353
override DataFlow::Node getAnInput() { result = this.getArgument(0) }
3454

@@ -80,8 +100,8 @@ private class ActionViewCookiesCall extends ActionViewContextCall, CookiesCallIm
80100
*/
81101
private class RenderCallAsHttpResponse extends DataFlow::CallNode, Http::Server::HttpResponse::Range {
82102
RenderCallAsHttpResponse() {
83-
this.asExpr().getExpr() instanceof RenderCall or
84-
this.asExpr().getExpr() instanceof RenderToCall
103+
this.asExpr().getExpr() instanceof Rails::RenderCall or
104+
this.asExpr().getExpr() instanceof Rails::RenderToCall
85105
}
86106

87107
// `render` is a very polymorphic method - all of these are valid calls:

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

Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -5,71 +5,71 @@
55
private import codeql.ruby.AST
66
private import codeql.ruby.Concepts
77
private import codeql.ruby.DataFlow
8-
private import codeql.ruby.frameworks.ActionController
9-
private import codeql.ruby.frameworks.ActionView
108
private import codeql.ruby.frameworks.ActiveRecord
119
private import codeql.ruby.frameworks.ActiveStorage
1210
private import codeql.ruby.frameworks.internal.Rails
1311
private import codeql.ruby.ApiGraphs
1412
private import codeql.ruby.security.OpenSSL
1513

16-
/**
17-
* A method call on a string to mark it as HTML safe for Rails. Strings marked
18-
* as such will not be automatically escaped when inserted into HTML.
19-
*/
20-
class HtmlSafeCall extends MethodCall instanceof HtmlSafeCallImpl { }
14+
module Rails {
15+
/**
16+
* A method call on a string to mark it as HTML safe for Rails. Strings marked
17+
* as such will not be automatically escaped when inserted into HTML.
18+
*/
19+
class HtmlSafeCall extends MethodCall instanceof HtmlSafeCallImpl { }
2120

22-
/** A call to a Rails method to escape HTML. */
23-
class HtmlEscapeCall extends MethodCall instanceof HtmlEscapeCallImpl { }
21+
/** A call to a Rails method to escape HTML. */
22+
class HtmlEscapeCall extends MethodCall instanceof HtmlEscapeCallImpl { }
2423

25-
/** A call to fetch the request parameters in a Rails app. */
26-
class ParamsCall extends MethodCall instanceof ParamsCallImpl { }
24+
/** A call to fetch the request parameters in a Rails app. */
25+
class ParamsCall extends MethodCall instanceof ParamsCallImpl { }
2726

28-
/** A call to fetch the request cookies in a Rails app. */
29-
class CookiesCall extends MethodCall instanceof CookiesCallImpl { }
27+
/** A call to fetch the request cookies in a Rails app. */
28+
class CookiesCall extends MethodCall instanceof CookiesCallImpl { }
3029

31-
/**
32-
* A call to a render method that will populate the response body with the
33-
* rendered content.
34-
*/
35-
class RenderCall extends MethodCall instanceof RenderCallImpl {
36-
private Expr getTemplatePathArgument() {
37-
// TODO: support other ways of specifying paths (e.g. `file`)
38-
result = [this.getKeywordArgument(["partial", "template", "action"]), this.getArgument(0)]
39-
}
30+
/**
31+
* A call to a render method that will populate the response body with the
32+
* rendered content.
33+
*/
34+
class RenderCall extends MethodCall instanceof RenderCallImpl {
35+
private Expr getTemplatePathArgument() {
36+
// TODO: support other ways of specifying paths (e.g. `file`)
37+
result = [this.getKeywordArgument(["partial", "template", "action"]), this.getArgument(0)]
38+
}
4039

41-
private string getTemplatePathValue() {
42-
result = this.getTemplatePathArgument().getConstantValue().getStringlikeValue()
43-
}
40+
private string getTemplatePathValue() {
41+
result = this.getTemplatePathArgument().getConstantValue().getStringlikeValue()
42+
}
4443

45-
// everything up to and including the final slash, but ignoring any leading slash
46-
private string getSubPath() {
47-
result = this.getTemplatePathValue().regexpCapture("^/?(.*/)?(?:[^/]*?)$", 1)
48-
}
44+
// everything up to and including the final slash, but ignoring any leading slash
45+
private string getSubPath() {
46+
result = this.getTemplatePathValue().regexpCapture("^/?(.*/)?(?:[^/]*?)$", 1)
47+
}
4948

50-
// everything after the final slash, or the whole string if there is no slash
51-
private string getBaseName() {
52-
result = this.getTemplatePathValue().regexpCapture("^/?(?:.*/)?([^/]*?)$", 1)
53-
}
49+
// everything after the final slash, or the whole string if there is no slash
50+
private string getBaseName() {
51+
result = this.getTemplatePathValue().regexpCapture("^/?(?:.*/)?([^/]*?)$", 1)
52+
}
5453

55-
/**
56-
* Gets the template file to be rendered by this call, if any.
57-
*/
58-
ErbFile getTemplateFile() {
59-
result.getTemplateName() = this.getBaseName() and
60-
result.getRelativePath().matches("%app/views/" + this.getSubPath() + "%")
54+
/**
55+
* Gets the template file to be rendered by this call, if any.
56+
*/
57+
ErbFile getTemplateFile() {
58+
result.getTemplateName() = this.getBaseName() and
59+
result.getRelativePath().matches("%app/views/" + this.getSubPath() + "%")
60+
}
61+
62+
/**
63+
* Get the local variables passed as context to the renderer
64+
*/
65+
HashLiteral getLocals() { result = this.getKeywordArgument("locals") }
66+
// TODO: implicit renders in controller actions
6167
}
6268

63-
/**
64-
* Get the local variables passed as context to the renderer
65-
*/
66-
HashLiteral getLocals() { result = this.getKeywordArgument("locals") }
67-
// TODO: implicit renders in controller actions
69+
/** A render call that does not automatically set the HTTP response body. */
70+
class RenderToCall extends MethodCall instanceof RenderToCallImpl { }
6871
}
6972

70-
/** A render call that does not automatically set the HTTP response body. */
71-
class RenderToCall extends MethodCall instanceof RenderToCallImpl { }
72-
7373
/**
7474
* A reference to either `Rails::Railtie`, `Rails::Engine`, or `Rails::Application`.
7575
* `Engine` and `Application` extend `Railtie`, but may not have definitions present in the database.

ruby/ql/test/library-tests/frameworks/ActionController.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ query predicate actionControllerControllerClasses(ActionControllerControllerClas
66

77
query predicate actionControllerActionMethods(ActionControllerActionMethod m) { any() }
88

9-
query predicate paramsCalls(ParamsCall c) { any() }
9+
query predicate paramsCalls(Rails::ParamsCall c) { any() }
1010

1111
query predicate paramsSources(ParamsSource src) { any() }
1212

13-
query predicate cookiesCalls(CookiesCall c) { any() }
13+
query predicate cookiesCalls(Rails::CookiesCall c) { any() }
1414

1515
query predicate cookiesSources(CookiesSource src) { any() }
1616

ruby/ql/test/library-tests/frameworks/ActionView.ql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ private import codeql.ruby.frameworks.ActionView
44
private import codeql.ruby.frameworks.Rails
55
private import codeql.ruby.Concepts
66

7-
query predicate htmlSafeCalls(HtmlSafeCall c) { any() }
7+
query predicate htmlSafeCalls(Rails::HtmlSafeCall c) { any() }
88

99
query predicate rawCalls(RawCall c) { any() }
1010

11-
query predicate renderCalls(RenderCall c) { any() }
11+
query predicate renderCalls(Rails::RenderCall c) { any() }
1212

13-
query predicate renderToCalls(RenderToCall c) { any() }
13+
query predicate renderToCalls(Rails::RenderToCall c) { any() }
1414

1515
query predicate linkToCalls(LinkToCall c) { any() }
1616

0 commit comments

Comments
 (0)