Skip to content

Commit fc66aac

Browse files
authored
Merge pull request github#12922 from hvitved/ruby/controller-template-file-join
Ruby: Fix bad join in `controllerTemplateFile`
2 parents 1ae116c + e5f2b90 commit fc66aac

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

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

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,23 @@ ActionControllerClass getAssociatedControllerClass(ErbFile f) {
511511
)
512512
}
513513

514+
pragma[nomagic]
515+
private string getActionControllerClassRelativePath(ActionControllerClass cls) {
516+
result = cls.getLocation().getFile().getRelativePath()
517+
}
518+
519+
pragma[nomagic]
520+
private string getErbFileRelativePath(ErbFile templateFile) {
521+
result = templateFile.getRelativePath() and
522+
result.matches("%app/views/layouts/%")
523+
}
524+
525+
bindingset[result]
526+
pragma[inline_late]
527+
private string getErbFileRelativePathInlineLate(ErbFile templateFile) {
528+
result = getErbFileRelativePath(templateFile)
529+
}
530+
514531
// TODO: improve layout support, e.g. for `layout` method
515532
// https://guides.rubyonrails.org/layouts_and_rendering.html
516533
/**
@@ -522,15 +539,18 @@ ActionControllerClass getAssociatedControllerClass(ErbFile f) {
522539
*/
523540
predicate controllerTemplateFile(ActionControllerClass cls, ErbFile templateFile) {
524541
exists(string sourcePrefix, string subPath, string controllerPath |
525-
controllerPath = cls.getLocation().getFile().getRelativePath() and
542+
controllerPath = getActionControllerClassRelativePath(cls) and
526543
// `sourcePrefix` is either a prefix path ending in a slash, or empty if
527544
// the rails app is at the source root
528545
sourcePrefix = [controllerPath.regexpCapture("^(.*/)app/controllers/(?:.*?)/(?:[^/]*)$", 1), ""] and
529-
controllerPath = sourcePrefix + "app/controllers/" + subPath + "_controller.rb" and
530-
(
531-
sourcePrefix + "app/views/" + subPath = templateFile.getParentContainer().getRelativePath()
532-
or
533-
templateFile.getRelativePath().matches(sourcePrefix + "app/views/layouts/" + subPath + "%")
546+
controllerPath = sourcePrefix + "app/controllers/" + subPath + "_controller.rb"
547+
|
548+
sourcePrefix + "app/views/" + subPath = templateFile.getParentContainer().getRelativePath()
549+
or
550+
exists(string path |
551+
path = getErbFileRelativePath(_) and
552+
path.matches(sourcePrefix + "app/views/layouts/" + subPath + "%") and
553+
path = getErbFileRelativePathInlineLate(templateFile)
534554
)
535555
)
536556
}

0 commit comments

Comments
 (0)