diff --git a/ql/src/codeql_ruby/ast/internal/Module.qll b/ql/src/codeql_ruby/ast/internal/Module.qll index 2d2c904b0d6..a1f128d0a08 100644 --- a/ql/src/codeql_ruby/ast/internal/Module.qll +++ b/ql/src/codeql_ruby/ast/internal/Module.qll @@ -63,7 +63,13 @@ private TResolved resolveScopeExpr(ConstantReadAccess r) { qname = min(string qn, int p | isDefinedConstant(qn) and - qn = resolveScopeExpr(r, p) + qn = resolveScopeExpr(r, p) and + // prevent classes/modules that contain/extend themselves + not exists(ConstantWriteAccess w | qn = constantDefinition0(w) | + r = w.getScopeExpr() + or + r = w.(ClassDeclaration).getSuperclassExpr() + ) | qn order by p ) @@ -100,18 +106,20 @@ private string resolveScopeExpr(ConstantReadAccess c, int priority) { or not exists(c.getScopeExpr()) and not c.hasGlobalScope() and - exists(Namespace n | - result = qualifiedModuleName(constantDefinition0(n), c.getName()) and - n = enclosing(c.getEnclosingModule(), priority) + ( + exists(Namespace n | + result = qualifiedModuleName(constantDefinition0(n), c.getName()) and + n = enclosing(c.getEnclosingModule(), priority) + ) + or + result = + qualifiedModuleName(ancestors(qualifiedModuleName(c.getEnclosingModule()), + priority - maxDepth()), c.getName()) + or + result = c.getName() and + priority = maxDepth() + 4 and + qualifiedModuleName(c.getEnclosingModule()) != "BasicObject" ) - or - result = - qualifiedModuleName(ancestors(qualifiedModuleName(c.getEnclosingModule()), priority - maxDepth()), - c.getName()) - or - result = c.getName() and - priority = maxDepth() + 4 and - qualifiedModuleName(c.getEnclosingModule()) != "BasicObject" } bindingset[qualifier, name] diff --git a/ql/test/library-tests/ast/modules/modules.expected b/ql/test/library-tests/ast/modules/modules.expected index b3fe57cfc02..f7f52051674 100644 --- a/ql/test/library-tests/ast/modules/modules.expected +++ b/ql/test/library-tests/ast/modules/modules.expected @@ -141,10 +141,9 @@ moduleTypes | modules.rb:101:1:105:3 | PrependTest | modules.rb:101:1:105:3 | PrependTest | | modules.rb:103:3:104:5 | Y | modules.rb:103:3:104:5 | Test::Foo2::Y | | modules.rb:107:1:110:3 | MM | modules.rb:107:1:110:3 | MM | -| modules.rb:108:3:109:5 | MM | modules.rb:108:3:109:5 | ...::MM | +| modules.rb:108:3:109:5 | MM | modules.rb:108:3:109:5 | MM::MM | | modules.rb:112:1:113:3 | YY | modules.rb:112:1:113:3 | YY | | modules.rb:115:1:118:3 | XX | modules.rb:115:1:118:3 | XX | | modules.rb:116:7:117:9 | YY | modules.rb:116:7:117:9 | XX::YY | -| modules.rb:120:1:121:3 | Baz | modules.rb:120:1:121:3 | Bar::Baz | | modules.rb:120:1:121:3 | Baz | modules.rb:120:1:121:3 | Test::Foo1::Bar::Baz | | toplevel.rb:1:1:5:23 | toplevel.rb | file://:0:0:0:0 | Object |