Skip to content

Commit f503dcd

Browse files
committed
Move the use of CDL locations back to CdlObject
CdlAttributes extend CdlObject, not CdlElement, but still require location sniffing.
1 parent c2b4716 commit f503dcd

File tree

1 file changed

+41
-23
lines changed
  • javascript/frameworks/cap/lib/advanced_security/javascript/frameworks/cap

1 file changed

+41
-23
lines changed

javascript/frameworks/cap/lib/advanced_security/javascript/frameworks/cap/CDL.qll

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,44 @@
55
import javascript
66
import advanced_security.javascript.frameworks.cap.CDS
77

8-
abstract class CdlObject extends JsonObject { }
8+
abstract class CdlObject extends JsonObject {
9+
predicate hasLocationInfo(string path, int sl, int sc, int el, int ec) {
10+
// If the cds.json file has a $location property, then use that,
11+
// otherwise fall back to the cds.json file itself
12+
if exists(this.getPropValue("$location"))
13+
then
14+
exists(Location loc, JsonValue locValue |
15+
loc = this.getLocation() and
16+
locValue = this.getPropValue("$location") and
17+
path =
18+
any(File f |
19+
f.getAbsolutePath()
20+
.matches("%" + locValue.getPropValue("file").getStringValue() + ".json")
21+
).getAbsolutePath().regexpReplaceAll("\\.json$", "") and
22+
sl = locValue.getPropValue("line").getIntValue() and
23+
sc = locValue.getPropValue("col").getIntValue() and
24+
if exists(getObjectLocationName())
25+
then
26+
// Currently $locations does not provide an end location. However, we can
27+
// automatically deduce the end location from the length of the name.
28+
el = sl and
29+
ec = sc + getObjectLocationName().length() - 1
30+
else (
31+
// We don't know where this entity ends, so mark the whole line
32+
el = sl + 1 and
33+
ec = 1
34+
)
35+
)
36+
else super.getLocation().hasLocationInfo(path, sl, sc, el, ec)
37+
}
38+
39+
/**
40+
* The name of the object that should be highlighted as the location.
41+
*
42+
* This is used to deduce the length of the location.
43+
*/
44+
string getObjectLocationName() { none() }
45+
}
946

1047
private newtype CdlKind =
1148
CdlServiceKind(string value) { value = "service" } or
@@ -34,28 +71,7 @@ abstract class CdlElement extends CdlObject {
3471

3572
CdlElement() { exists(CdlDefinitions definitions | this = definitions.getElement(name)) }
3673

37-
predicate hasLocationInfo(string path, int sl, int sc, int el, int ec) {
38-
// If the cds.json file has a $location property, then use that,
39-
// otherwise fall back to the cds.json file itself
40-
if exists(this.getPropValue("$location"))
41-
then
42-
exists(Location loc, JsonValue locValue |
43-
loc = this.getLocation() and
44-
locValue = this.getPropValue("$location") and
45-
path =
46-
any(File f |
47-
f.getAbsolutePath()
48-
.matches("%" + locValue.getPropValue("file").getStringValue() + ".json")
49-
).getAbsolutePath().regexpReplaceAll("\\.json$", "") and
50-
sl = locValue.getPropValue("line").getIntValue() and
51-
sc = locValue.getPropValue("col").getIntValue() and
52-
el = sl and
53-
// Currently $locations does not provide an end location. However, we can
54-
// automatically deduce the end location from the length of the name.
55-
ec = sc + getUnqualifiedName().length() - 1
56-
)
57-
else super.getLocation().hasLocationInfo(path, sl, sc, el, ec)
58-
}
74+
override string getObjectLocationName() { result = getUnqualifiedName() }
5975

6076
/**
6177
* Gets the name of this CDL element.
@@ -225,6 +241,8 @@ class CdlAttribute extends CdlObject {
225241
exists(CdlElement entity | this = entity.getPropValue("elements").getPropValue(name))
226242
}
227243

244+
override string getObjectLocationName() { result = getName() }
245+
228246
string getType() { result = this.getPropStringValue("type") }
229247

230248
int getLength() { result = this.getPropValue("length").(JsonPrimitiveValue).getIntValue() }

0 commit comments

Comments
 (0)