Skip to content

Commit 82fbae3

Browse files
Handle standalone extraction case in which generated files list absolute paths
1 parent 26c048a commit 82fbae3

File tree

1 file changed

+20
-4
lines changed
  • csharp/ql/lib/semmle/code/csharp/frameworks

1 file changed

+20
-4
lines changed

csharp/ql/lib/semmle/code/csharp/frameworks/Razor.qll

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,28 @@ class RazorPageClass extends Class {
7878
}
7979

8080
/**
81-
* Gets the filepath of the source file that this class was generated from,
82-
* relative to the application root.
81+
* Gets the filepath of the source file that this class was generated from.
82+
*
83+
* This is an absolute path if the database was extracted in standalone mode,
84+
* and is relative to to application root (the directory containing the .csproj file) otherwise.
8385
*/
8486
string getSourceFilepath() { result = attr.getArgument(2).(StringLiteral).getValue() }
8587
}
8688

89+
/**
90+
* Gets a possible prefix to be applied to view search paths to locate a Razor page.
91+
* This may be empty (for the case that the generated Razor page files contain paths relative to the application root),
92+
* or the absolute path of the directory containing the .csproj file (for the case that standalone extraction is used and the generated files contain absolute paths).
93+
*/
94+
private string getARazorPathPrefix() {
95+
result = ""
96+
or
97+
exists(File csproj |
98+
csproj.getExtension() = "csproj" and
99+
result = csproj.getParentContainer().getAbsolutePath()
100+
)
101+
}
102+
87103
private class ViewCallJumpNode extends DataFlow::NonLocalJumpNode {
88104
RazorPageClass rp;
89105

@@ -113,7 +129,7 @@ bindingset[path]
113129
private string stripTilde(string path) { result = path.regexpReplaceAll("^~/", "/") }
114130

115131
private predicate viewCallRefersToPageAbsolute(ViewCall vc, RazorPageClass rp) {
116-
["/", ""] + stripTilde(vc.getNameArgument()) = rp.getSourceFilepath()
132+
getARazorPathPrefix() + ["/", ""] + stripTilde(vc.getNameArgument()) = rp.getSourceFilepath()
117133
}
118134

119135
private predicate viewCallRefersToPageRelative(ViewCall vc, RazorPageClass rp) {
@@ -123,7 +139,7 @@ private predicate viewCallRefersToPageRelative(ViewCall vc, RazorPageClass rp) {
123139
private predicate matchesViewCallWithIndex(ViewCall vc, RazorPageClass rp, int i) {
124140
exists(RelativeViewCallFilepath fp |
125141
fp.hasViewCallWithIndex(vc, i) and
126-
fp.getNormalizedPath() = rp.getSourceFilepath()
142+
getARazorPathPrefix() + fp.getNormalizedPath() = rp.getSourceFilepath()
127143
)
128144
}
129145

0 commit comments

Comments
 (0)