Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ruby: Model editor improvements #15048

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

hmac
Copy link
Contributor

@hmac hmac commented Dec 8, 2023

This PR does a few things:

  • Excludes endpoints whose path has examples in it. This removes a lot of example code that otherwise gets picked up when modeling libraries.
  • Changes the FrameworkModeEndpoints query to produce not only methods, but also classes. These are not technically endpoints, but it allows the model editor to learn about them and thus handle the new results from GenerateModel (see below). This involves some refactoring of the Endpoint class, because we want to include both DataFlow::MethodNodes and DataFlow::ModuleNodes, which don't have a common ancestor.
  • Updates the GenerateModel query to produce typeModel rows for subclass relationships. For example:
class A
  def foo(x)
    # ...
  end
end

class B < A
end

If we mark A#foo as a sink, we also want to recognise calls to B#foo as a sink as well. Instead of duplicating the models for every subclass, we instead emit a type row:

A,B,

which states that B can be considered to have type A. This means any models that target A will target B as well.

The same applies for include:

module C
  def bar(x)
    # ...
  end
end

class D
  include C
end

but not for extend, because its behaviour is a bit odd (instance methods on the included module become class methods on the including class). I haven't worked out how to deal with them yet.

@github-actions github-actions bot added the Ruby label Dec 8, 2023
@hmac hmac force-pushed the hmac-model-editor-ruby-modules branch from c255d95 to ec24b25 Compare December 8, 2023 14:17
We don't actually generate type models for `extend` relationships yet,
because they are more complex than `include`.
@hmac hmac marked this pull request as ready for review December 8, 2023 15:15
@hmac hmac requested a review from a team as a code owner December 8, 2023 15:15
@hmac hmac added the no-change-note-required This PR does not need a change note label Dec 8, 2023
Copy link
Contributor

@aibaars aibaars left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me overall.


File getFile() { result = this.getLocation().getFile() }
abstract class Endpoint instanceof AstNode {
string getNamespace() { result = getNamespace(this.(AstNode).getLocation().getFile()) }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should work:

Suggested change
string getNamespace() { result = getNamespace(this.(AstNode).getLocation().getFile()) }
string getNamespace() { result = getNamespace(super.getLocation().getFile()) }


string getName() { result = this.getMethodName() }
string getFileName() { result = this.(AstNode).getLocation().getFile().getBaseName() }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
string getFileName() { result = this.(AstNode).getLocation().getFile().getBaseName() }
string getFileName() { result = super.getLocation().getFile().getBaseName() }

Comment on lines 46 to 48
string toString() { result = this.(AstNode).toString() }

Location getLocation() { result = this.(AstNode).getLocation() }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
string toString() { result = this.(AstNode).toString() }
Location getLocation() { result = this.(AstNode).getLocation() }
string toString() { result = super.toString() }
Location getLocation() { result = super.getLocation() }


File getFile() { result = this.getLocation().getFile() }
abstract class Endpoint instanceof AstNode {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not

Suggested change
abstract class Endpoint instanceof AstNode {
abstract class Endpoint instanceof DataFlow::Node {

An AstNode may have multiple associated DataFlow Nodes, so I think we best avoid switching between AST and Dataflow layers.

@hmac hmac requested a review from aibaars December 11, 2023 10:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
no-change-note-required This PR does not need a change note Ruby
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants