- 
                Notifications
    
You must be signed in to change notification settings  - Fork 366
 
[ruby] Add Support for ERB files #5447
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
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice
        
          
                ...s/rubysrc2cpg/src/main/scala/io/joern/rubysrc2cpg/astcreation/AstForExpressionsCreator.scala
              
                Outdated
          
            Show resolved
            Hide resolved
        
              
          
                ...s/rubysrc2cpg/src/main/scala/io/joern/rubysrc2cpg/astcreation/AstForExpressionsCreator.scala
              
                Outdated
          
            Show resolved
            Hide resolved
        
              
          
                ...s/rubysrc2cpg/src/main/scala/io/joern/rubysrc2cpg/astcreation/AstForExpressionsCreator.scala
              
                Outdated
          
            Show resolved
            Hide resolved
        
      | 
           I'm still missing a lot of .erb files. E.g. for https://github.com/chatwoot/chatwoot: The frontend spits out some warnings for the project, e.g.   | 
    
| 
           @maltek these are the new results on chatwoot with all of the changes made for ERB handling.  | 
    
        
          
                joern-cli/frontends/rubysrc2cpg/src/test/scala/io/joern/rubysrc2cpg/querying/ErbTests.scala
              
                Outdated
          
            Show resolved
            Hide resolved
        
              
          
                joern-cli/frontends/rubysrc2cpg/src/test/scala/io/joern/rubysrc2cpg/querying/ErbTests.scala
              
                Outdated
          
            Show resolved
            Hide resolved
        
      | import io.shiftleft.codepropertygraph.generated.nodes.{Block, Call, FieldIdentifier, Identifier, Literal, TypeRef} | ||
| import io.shiftleft.semanticcpg.language.* | ||
| 
               | 
          ||
| class ErbTests extends RubyCode2CpgFixture { | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are you writing tests that include the lowering of a ruby AST into CPG?
The ERB support takes input ERB files and emits ruby code that is then fed into rubyAstGen. For the later part we already have plenty of tests. Here it would be much better readable if you test the first step ( ERB -> ruby code) the added benefit would be much better readability.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ERB -> Ruby code is tested on the ruby_ast_gen project e.g., https://github.com/joernio/ruby_ast_gen/blob/main/spec/ruby_ast_gen_spec.rb#L155
Albeit, one could argue for more tests there perhaps, but we haven't designed the RubyAstGen hook to give us the lowered ERB code as-is, only via AST
| 
           all the ruby code in  | 
    
| 
           for the  and the   | 
    
| 
           the  
  | 
    
c408dfd    to
    233da30      
    Compare
  
    | 
           current problem: class UsersController < ActionController::Base
  def show
    respond_to do |format|
      format.json { render partial: "foo" }
    end
  end
endsome  cpg.identifier.name("self").filter(_.refsTo.size != 1).lIdentifiers should only ever have REF edges to the single variable of that name in the exact same method.  | 
    
| 
           @TNSelahle the problem with the   | 
    
83462f3    to
    47aa16b      
    Compare
  
    ea86534    to
    b834c49      
    Compare
  
            
          
                ...s/rubysrc2cpg/src/main/scala/io/joern/rubysrc2cpg/astcreation/AstForExpressionsCreator.scala
              
                Outdated
          
            Show resolved
            Hide resolved
        
      | val typeFullName = if (node.memberName == "joern__buffer" || node.memberName == "joern__inner_buffer") { | ||
| Constants.stringPrefix | ||
| } else { | ||
| Defines.Any | ||
| } | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we should handle this special case in the general astForFieldAccess method. Can we make the type an optional parameter that's handed down here from the surrounding context if it's known?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's sort of the same special case now, just one layer up. I thought we could do it from around here where we've already determined to be dealing with the ERB lowering. But I just read over the diff in the browser, so I could very well be mistaken.
Lines 200 to 201 in bfd14fc
| val ast = astForFieldAccess(MemberAccess(n.target, ".", n.methodName)(n.span), stripLeadingAt = true) | |
| (n.op, hasStringArgs, ast) | 
        
          
                ...nds/rubysrc2cpg/src/main/scala/io/joern/rubysrc2cpg/astcreation/AstForFunctionsCreator.scala
              
                Outdated
          
            Show resolved
            Hide resolved
        
              
          
                ...nds/rubysrc2cpg/src/main/scala/io/joern/rubysrc2cpg/astcreation/AstForFunctionsCreator.scala
              
                Outdated
          
            Show resolved
            Hide resolved
        
              
          
                ...ntends/rubysrc2cpg/src/main/scala/io/joern/rubysrc2cpg/astcreation/RubyIntermediateAst.scala
              
                Outdated
          
            Show resolved
            Hide resolved
        
              
          
                ...cli/frontends/rubysrc2cpg/src/main/scala/io/joern/rubysrc2cpg/datastructures/RubyScope.scala
              
                Outdated
          
            Show resolved
            Hide resolved
        
              
          
                joern-cli/frontends/rubysrc2cpg/src/test/scala/io/joern/rubysrc2cpg/querying/LiteralTests.scala
              
                Outdated
          
            Show resolved
            Hide resolved
        
              
          
                ...i/frontends/x2cpg/src/main/scala/io/joern/x2cpg/frontendspecific/rubysrc2cpg/Constants.scala
              
                Outdated
          
            Show resolved
            Hide resolved
        
      b834c49    to
    505352f      
    Compare
  
    | 
           @maltek thanks for the review! I've made and pushed up the changes. Integration tests on CS master are all green.  | 
    
        
          
                ...s/rubysrc2cpg/src/main/scala/io/joern/rubysrc2cpg/astcreation/AstForExpressionsCreator.scala
              
                Outdated
          
            Show resolved
            Hide resolved
        
      | val typeFullName = if (node.memberName == "joern__buffer" || node.memberName == "joern__inner_buffer") { | ||
| Constants.stringPrefix | ||
| } else { | ||
| Defines.Any | ||
| } | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's sort of the same special case now, just one layer up. I thought we could do it from around here where we've already determined to be dealing with the ERB lowering. But I just read over the diff in the browser, so I could very well be mistaken.
Lines 200 to 201 in bfd14fc
| val ast = astForFieldAccess(MemberAccess(n.target, ".", n.methodName)(n.span), stripLeadingAt = true) | |
| (n.op, hasStringArgs, ast) | 
| 
           @maltek the snippet you highlighted applies to  I'll rename   | 
    
This also fixes a bug where a lambda param IDENTIFIER was not referencing the param
39947b2    to
    f0fdf28      
    Compare
  
    
templateOutRaw(<%== %>) andtemplateOutEscape(<%= %>) operator callsRETURNfor thejoern__bufferwhich holds all of the appended strings from the ERB lowering.html.erbfiles as config files