Skip to content

Commit c16f2f0

Browse files
Merge branch 'main' into lcartey/cds-extractor
2 parents 3701318 + aed7714 commit c16f2f0

File tree

3 files changed

+69
-24
lines changed

3 files changed

+69
-24
lines changed

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

Lines changed: 62 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,58 @@ import advanced_security.javascript.frameworks.cap.CDS
77

88
abstract class CdlObject extends JsonObject {
99
predicate hasLocationInfo(string path, int sl, int sc, int el, int ec) {
10-
exists(Location loc, JsonValue locValue |
11-
loc = this.getLocation() and
12-
locValue = this.getPropValue("$location") and
13-
path =
14-
any(File f |
15-
f.getAbsolutePath()
16-
.matches("%" + locValue.getPropValue("file").getStringValue() + ".json")
17-
).getAbsolutePath().regexpReplaceAll("\\.json$", "") and
18-
sl = locValue.getPropValue("line").getIntValue() and
19-
sc = locValue.getPropValue("col").getIntValue() and
20-
el = sl + 1 and
21-
ec = 1
22-
)
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+
if
23+
not exists(locValue.getPropValue("line")) and
24+
not exists(locValue.getPropValue("col"))
25+
then
26+
// We don't know where this entity starts, so mark the whole file
27+
sl = 0 and
28+
sc = 0 and
29+
el = 0 and
30+
ec = 0
31+
else (
32+
sl = locValue.getPropValue("line").getIntValue() and
33+
(
34+
if exists(locValue.getPropValue("col"))
35+
then sc = locValue.getPropValue("col").getIntValue()
36+
else
37+
// We don't know where this entity starts, so mark the start of the line
38+
sc = 0
39+
) and
40+
el = sl and
41+
(
42+
if exists(getObjectLocationName())
43+
then
44+
// Currently $locations does not provide an end location. However, we can
45+
// automatically deduce the end location from the length of the name.
46+
ec = sc + getObjectLocationName().length() - 1
47+
else
48+
// Mark a single character if we cannot predicate the length
49+
ec = sc + 1
50+
)
51+
)
52+
)
53+
else super.getLocation().hasLocationInfo(path, sl, sc, el, ec)
2354
}
55+
56+
/**
57+
* The name of the object that should be highlighted as the location.
58+
*
59+
* This is used to deduce the length of the location.
60+
*/
61+
string getObjectLocationName() { none() }
2462
}
2563

2664
private newtype CdlKind =
@@ -31,21 +69,26 @@ private newtype CdlKind =
3169
CdlFunctionKind(string value) { value = "function" }
3270

3371
/**
34-
* Any CDL element, including entities, event, actions, and more.
72+
* A list of CDL definitions, which can include entities, events, actions and more.
3573
*/
36-
class CdlDefinition extends CdlObject {
37-
CdlDefinition() { exists(JsonObject root | this = root.getPropValue("definitions")) }
74+
class CdlDefinitions extends CdlObject {
75+
CdlDefinitions() { exists(JsonObject root | this = root.getPropValue("definitions")) }
3876

3977
JsonObject getElement(string elementName) { result = this.getPropValue(elementName) }
4078

4179
JsonObject getAnElement() { result = this.getElement(_) }
4280
}
4381

82+
/**
83+
* A CDL definition element.
84+
*/
4485
abstract class CdlElement extends CdlObject {
4586
CdlKind kind;
4687
string name;
4788

48-
CdlElement() { exists(CdlDefinition definition | this = definition.getElement(name)) }
89+
CdlElement() { exists(CdlDefinitions definitions | this = definitions.getElement(name)) }
90+
91+
override string getObjectLocationName() { result = getUnqualifiedName() }
4992

5093
/**
5194
* Gets the name of this CDL element.
@@ -215,6 +258,8 @@ class CdlAttribute extends CdlObject {
215258
exists(CdlElement entity | this = entity.getPropValue("elements").getPropValue(name))
216259
}
217260

261+
override string getObjectLocationName() { result = getName() }
262+
218263
string getType() { result = this.getPropStringValue("type") }
219264

220265
int getLength() { result = this.getPropValue("length").(JsonPrimitiveValue).getIntValue() }
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
| srv/service1.cds:3:9:4:1 | {\\n ... }\\n } | The CDS service `Service1` is exposed without any authentication. |
2-
| srv/service1.cds:5:10:6:1 | {\\n ... }\\n } | The CDS entity `Service1.Service1Entity` is exposed without any authentication. |
3-
| srv/service1.cds:8:10:9:1 | {\\n ... }\\n } | The CDS action `Service1.send1` is exposed without any authentication. |
4-
| srv/service2.cds:3:9:4:1 | {\\n ... }\\n } | The CDS service `Service2` is exposed without any authentication. |
5-
| srv/service2.cds:5:10:6:1 | {\\n ... }\\n } | The CDS entity `Service2.Service2Entity` is exposed without any authentication. |
6-
| srv/service2.cds:8:10:9:1 | {\\n ... }\\n } | The CDS action `Service2.send2` is exposed without any authentication. |
1+
| srv/service1.cds:3:9:3:16 | {\\n ... }\\n } | The CDS service `Service1` is exposed without any authentication. |
2+
| srv/service1.cds:5:10:5:23 | {\\n ... }\\n } | The CDS entity `Service1.Service1Entity` is exposed without any authentication. |
3+
| srv/service1.cds:8:10:8:14 | {\\n ... }\\n } | The CDS action `Service1.send1` is exposed without any authentication. |
4+
| srv/service2.cds:3:9:3:16 | {\\n ... }\\n } | The CDS service `Service2` is exposed without any authentication. |
5+
| srv/service2.cds:5:10:5:23 | {\\n ... }\\n } | The CDS entity `Service2.Service2Entity` is exposed without any authentication. |
6+
| srv/service2.cds:8:10:8:14 | {\\n ... }\\n } | The CDS action `Service2.send2` is exposed without any authentication. |

javascript/frameworks/cap/test/queries/sensitive-exposure/sensitive-exposure.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ nodes
55
edges
66
| sensitive-exposure.js:9:32:9:42 | Sample.name | sensitive-exposure.js:9:32:9:42 | Sample.name |
77
#select
8-
| sensitive-exposure.js:9:32:9:42 | Sample.name | sensitive-exposure.js:9:32:9:42 | Sample.name | sensitive-exposure.js:9:32:9:42 | Sample.name | Log entry depends on the $@ field which is annotated as potentially sensitive. | sensitive-exposure.cds:4:5:5:1 | {\\n ... } | name |
8+
| sensitive-exposure.js:9:32:9:42 | Sample.name | sensitive-exposure.js:9:32:9:42 | Sample.name | sensitive-exposure.js:9:32:9:42 | Sample.name | Log entry depends on the $@ field which is annotated as potentially sensitive. | sensitive-exposure.cds:4:5:4:8 | {\\n ... } | name |

0 commit comments

Comments
 (0)