Skip to content

Commit aa8878b

Browse files
authored
Merge pull request github#13356 from geoffw0/qualname
Swift: Add FieldDecl.getQualifiedName
2 parents b5bbe63 + 4c82257 commit aa8878b

File tree

7 files changed

+52
-49
lines changed

7 files changed

+52
-49
lines changed

swift/ql/lib/codeql/swift/elements/decl/VarDecl.qll

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,32 @@ class VarDecl extends Generated::VarDecl {
99
}
1010

1111
/**
12-
* A field declaration.
12+
* A field declaration. That is, a variable declaration that is a member of a
13+
* class, struct, enum or protocol.
1314
*/
1415
class FieldDecl extends VarDecl {
1516
FieldDecl() { this = any(Decl ctx).getAMember() }
17+
18+
/**
19+
* Holds if this field is called `fieldName` and is a member of a
20+
* class, struct, extension, enum or protocol called `typeName`.
21+
*/
22+
cached
23+
predicate hasQualifiedName(string typeName, string fieldName) {
24+
this.getName() = fieldName and
25+
exists(Decl d |
26+
d.asNominalTypeDecl().getFullName() = typeName and
27+
d.getAMember() = this
28+
)
29+
}
30+
31+
/**
32+
* Holds if this field is called `fieldName` and is a member of a
33+
* class, struct, extension, enum or protocol called `typeName` in a module
34+
* called `moduleName`.
35+
*/
36+
predicate hasQualifiedName(string moduleName, string typeName, string fieldName) {
37+
this.hasQualifiedName(typeName, fieldName) and
38+
this.getModule().getFullName() = moduleName
39+
}
1640
}

swift/ql/lib/codeql/swift/frameworks/StandardLibrary/Collection.qll

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,6 @@ private class CollectionFieldsInheritTaint extends TaintInheritingContent,
4747
DataFlow::Content::FieldContent
4848
{
4949
CollectionFieldsInheritTaint() {
50-
exists(FieldDecl f | this.getField() = f |
51-
f.getEnclosingDecl().asNominalTypeDecl().getName() = ["Collection", "BidirectionalCollection"] and
52-
f.getName() = ["first", "last"]
53-
)
50+
this.getField().hasQualifiedName(["Collection", "BidirectionalCollection"], ["first", "last"])
5451
}
5552
}

swift/ql/lib/codeql/swift/frameworks/StandardLibrary/NsString.qll

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -132,20 +132,18 @@ private class NsStringFieldsInheritTaint extends TaintInheritingContent,
132132
DataFlow::Content::FieldContent
133133
{
134134
NsStringFieldsInheritTaint() {
135-
exists(FieldDecl f | this.getField() = f |
136-
f.getEnclosingDecl().asNominalTypeDecl().getName() = "NSString" and
137-
f.getName() =
138-
[
139-
"utf8String", "lowercased", "localizedLowedCase", "uppercased", "localizedUppercase",
140-
"capitalized", "localizedCapitalized", "decomposedStringWithCanonicalMapping",
141-
"decomposedStringWithCompatibilityMapping", "precomposedStringWithCanonicalMapping",
142-
"precomposedStringWithCompatibilityMapping", "doubleValue", "floatValue", "intValue",
143-
"integerValue", "longLongValue", "boolValue", "description", "pathComponents",
144-
"fileSystemRepresentation", "lastPathComponent", "pathExtension",
145-
"abbreviatingWithTildeInPath", "deletingLastPathComponent", "deletingPathExtension",
146-
"expandingTildeInPath", "resolvingSymlinksInPath", "standardizingPath",
147-
"removingPercentEncoding"
148-
]
149-
)
135+
this.getField()
136+
.hasQualifiedName("NSString",
137+
[
138+
"utf8String", "lowercased", "localizedLowedCase", "uppercased", "localizedUppercase",
139+
"capitalized", "localizedCapitalized", "decomposedStringWithCanonicalMapping",
140+
"decomposedStringWithCompatibilityMapping", "precomposedStringWithCanonicalMapping",
141+
"precomposedStringWithCompatibilityMapping", "doubleValue", "floatValue", "intValue",
142+
"integerValue", "longLongValue", "boolValue", "description", "pathComponents",
143+
"fileSystemRepresentation", "lastPathComponent", "pathExtension",
144+
"abbreviatingWithTildeInPath", "deletingLastPathComponent", "deletingPathExtension",
145+
"expandingTildeInPath", "resolvingSymlinksInPath", "standardizingPath",
146+
"removingPercentEncoding"
147+
])
150148
}
151149
}

swift/ql/lib/codeql/swift/frameworks/StandardLibrary/Sequence.qll

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,5 @@ private class SequenceSummaries extends SummaryModelCsv {
3636
private class SequenceFieldsInheritTaint extends TaintInheritingContent,
3737
DataFlow::Content::FieldContent
3838
{
39-
SequenceFieldsInheritTaint() {
40-
exists(FieldDecl f | this.getField() = f |
41-
f.getEnclosingDecl().asNominalTypeDecl().getName() = "Sequence" and
42-
f.getName() = "lazy"
43-
)
44-
}
39+
SequenceFieldsInheritTaint() { this.getField().hasQualifiedName("Sequence", "lazy") }
4540
}

swift/ql/lib/codeql/swift/frameworks/StandardLibrary/String.qll

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -124,16 +124,15 @@ private class StringFieldsInheritTaint extends TaintInheritingContent,
124124
DataFlow::Content::FieldContent
125125
{
126126
StringFieldsInheritTaint() {
127-
exists(FieldDecl f | this.getField() = f |
128-
f.getEnclosingDecl().asNominalTypeDecl().getName() = ["String", "StringProtocol"] and
129-
f.getName() =
130-
[
131-
"unicodeScalars", "utf8", "utf16", "lazy", "utf8CString", "description",
132-
"debugDescription", "dataValue", "identifierValue", "capitalized", "localizedCapitalized",
133-
"localizedLowercase", "localizedUppercase", "decomposedStringWithCanonicalMapping",
134-
"decomposedStringWithCompatibilityMapping", "precomposedStringWithCanonicalMapping",
135-
"precomposedStringWithCompatibilityMapping", "removingPercentEncoding"
136-
]
137-
)
127+
this.getField()
128+
.hasQualifiedName(["String", "StringProtocol"],
129+
[
130+
"unicodeScalars", "utf8", "utf16", "lazy", "utf8CString", "description",
131+
"debugDescription", "dataValue", "identifierValue", "capitalized",
132+
"localizedCapitalized", "localizedLowercase", "localizedUppercase",
133+
"decomposedStringWithCanonicalMapping", "decomposedStringWithCompatibilityMapping",
134+
"precomposedStringWithCanonicalMapping", "precomposedStringWithCompatibilityMapping",
135+
"removingPercentEncoding"
136+
])
138137
}
139138
}

swift/ql/lib/codeql/swift/frameworks/StandardLibrary/WebView.qll

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,5 @@ private class WKUserScriptSummaries extends SummaryModelCsv {
208208
private class WKUserScriptInheritsTaint extends TaintInheritingContent,
209209
DataFlow::Content::FieldContent
210210
{
211-
WKUserScriptInheritsTaint() {
212-
exists(FieldDecl f | this.getField() = f |
213-
f.getEnclosingDecl().asNominalTypeDecl().getName() = "WKUserScript" and
214-
f.getName() = "source"
215-
)
216-
}
211+
WKUserScriptInheritsTaint() { this.getField().hasQualifiedName("WKUserScript", "source") }
217212
}

swift/ql/lib/codeql/swift/security/CleartextLoggingExtensions.qll

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,7 @@ private class OsLogNonRedactedType extends Type {
7474
private class OsLogPrivacyRef extends MemberRefExpr {
7575
string optionName;
7676

77-
OsLogPrivacyRef() {
78-
exists(FieldDecl f | this.getMember() = f |
79-
f.getEnclosingDecl().asNominalTypeDecl().getName() = "OSLogPrivacy" and
80-
optionName = f.getName()
81-
)
82-
}
77+
OsLogPrivacyRef() { this.getMember().(FieldDecl).hasQualifiedName("OSLogPrivacy", optionName) }
8378

8479
/** Holds if this is a safe privacy option (private or sensitive). */
8580
predicate isSafe() { optionName = ["private", "sensitive"] }

0 commit comments

Comments
 (0)