Skip to content

Commit 6b35098

Browse files
committed
C#: Replace more uses of getQualifiedName/0.
1 parent 0a3295e commit 6b35098

File tree

11 files changed

+93
-35
lines changed

11 files changed

+93
-35
lines changed

csharp/ql/lib/semmle/code/csharp/security/dataflow/ExternalAPIsQuery.qll

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55

66
import csharp
7+
private import semmle.code.csharp.commons.QualifiedName
78
private import semmle.code.csharp.dataflow.flowsources.Remote
89
private import semmle.code.csharp.frameworks.System
910
private import semmle.code.csharp.dataflow.FlowSummary
@@ -70,8 +71,21 @@ class ExternalApiDataNode extends DataFlow::Node {
7071
/** Gets the index which is passed untrusted data (where -1 indicates the qualifier). */
7172
int getIndex() { result = i }
7273

73-
/** Gets the description of the callable being called. */
74-
string getCallableDescription() { result = this.getCallable().getQualifiedName() }
74+
/** Holds if the callable being use has name `name` and is defined in namespace `namespace`. */
75+
predicate hasQualifiedName(string namespace, string name) {
76+
this.getCallable().hasQualifiedName(namespace, name)
77+
}
78+
79+
/**
80+
* DEPRECATED: Use hasQualifiedName/2 instead.
81+
*
82+
* Gets the description of the callable being called.
83+
*/
84+
deprecated string getCallableDescription() {
85+
exists(string namespace, string name |
86+
this.hasQualifiedName(namespace, name) and result = printQualifiedName(namespace, name)
87+
)
88+
}
7589
}
7690

7791
/** DEPRECATED: Alias for ExternalApiDataNode */

csharp/ql/src/Security Features/CWE-020/UntrustedDataToExternalAPI.ql

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,16 @@
1010
*/
1111

1212
import csharp
13+
import semmle.code.csharp.commons.QualifiedName
1314
import semmle.code.csharp.security.dataflow.ExternalAPIsQuery
1415
import DataFlow::PathGraph
1516

16-
from UntrustedDataToExternalApiConfig config, DataFlow::PathNode source, DataFlow::PathNode sink
17-
where config.hasFlowPath(source, sink)
17+
from
18+
UntrustedDataToExternalApiConfig config, DataFlow::PathNode source, DataFlow::PathNode sink,
19+
string namespace, string name
20+
where
21+
config.hasFlowPath(source, sink) and
22+
sink.getNode().(ExternalApiDataNode).hasQualifiedName(namespace, name)
1823
select sink, source, sink,
19-
"Call to " + sink.getNode().(ExternalApiDataNode).getCallableDescription() +
20-
" with untrusted data from $@.", source, source.toString()
24+
"Call to " + printQualifiedName(namespace, name) + " with untrusted data from $@.", source,
25+
source.toString()

csharp/ql/test/library-tests/cil/typeAnnotations/typeAnnotations.ql

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,42 @@
11
import cil
2+
import semmle.code.csharp.commons.QualifiedName
23
import semmle.code.cil.Type
34

45
private string elementType(Element e, string toString) {
5-
toString = e.(Method).getQualifiedName() and result = "method"
6-
or
7-
toString = e.(Property).getQualifiedName() and result = "property"
6+
exists(string namespace, string type, string name |
7+
toString = printQualifiedName(namespace, type, name)
8+
|
9+
e.(Method).hasQualifiedName(namespace, type, name) and result = "method"
10+
or
11+
e.(Property).hasQualifiedName(namespace, type, name) and result = "property"
12+
)
813
or
914
e =
1015
any(Parameter p |
11-
toString = "Parameter " + p.getIndex() + " of " + p.getDeclaringElement().getQualifiedName()
16+
exists(string namespace, string name |
17+
p.getDeclaringElement().hasQualifiedName(namespace, name)
18+
|
19+
toString = "Parameter " + p.getIndex() + " of " + printQualifiedName(namespace, name)
20+
)
1221
) and
1322
result = "parameter"
1423
or
1524
e =
1625
any(LocalVariable v |
17-
toString =
18-
"Local variable " + v.getIndex() + " of method " +
19-
v.getImplementation().getMethod().getQualifiedName()
26+
exists(string namespace, string type, string name |
27+
v.getImplementation().getMethod().hasQualifiedName(namespace, type, name)
28+
|
29+
toString =
30+
"Local variable " + v.getIndex() + " of method " +
31+
printQualifiedName(namespace, type, name)
32+
)
2033
) and
2134
result = "local"
2235
or
23-
toString = e.(FunctionPointerType).getQualifiedName() and result = "fnptr"
36+
exists(string namespace, string name | e.(FunctionPointerType).hasQualifiedName(namespace, name) |
37+
toString = printQualifiedName(namespace, name)
38+
) and
39+
result = "fnptr"
2440
or
2541
not e instanceof Method and
2642
not e instanceof Property and
@@ -53,7 +69,9 @@ where
5369
(
5470
not e instanceof Parameter
5571
or
56-
e.(Parameter).getDeclaringElement().(Method).getDeclaringType().getQualifiedName() !=
57-
"System.Environment" // There are OS specific methods in this class
72+
not exists(Type t |
73+
t = e.(Parameter).getDeclaringElement().(Method).getDeclaringType() and
74+
t.hasQualifiedName("System", "Environment")
75+
) // There are OS specific methods in this class
5876
)
5977
select toString, type, i
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import cil
22
import semmle.code.csharp.commons.Disposal
3+
import semmle.code.csharp.commons.QualifiedName
34

4-
from CIL::Field field
5+
from CIL::Field field, string namespace, string name
56
where
67
mayBeDisposed(field) and
7-
field.getDeclaringType().hasQualifiedName("DisposalTests", "Class1")
8-
select field.getQualifiedName()
8+
field.getDeclaringType().hasQualifiedName("DisposalTests", "Class1") and
9+
field.hasQualifiedName(namespace, name)
10+
select printQualifiedName(namespace, name)

csharp/ql/test/library-tests/commons/Disposal/DisposedParameter.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ from DotNet::Callable c, DotNet::Parameter param, int p
55
where
66
mayBeDisposed(param) and
77
param = c.getParameter(p) and
8-
c.getDeclaringType().getQualifiedName() = "DisposalTests.Class1"
8+
c.getDeclaringType().hasQualifiedName("DisposalTests", "Class1")
99
select c.toStringWithTypes(), p

csharp/ql/test/library-tests/commons/Disposal/UndisposedParameter.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ from DotNet::Callable c, DotNet::Parameter param, int p
66
where
77
not mayBeDisposed(param) and
88
param = c.getParameter(p) and
9-
c.getDeclaringType().getQualifiedName() = "DisposalTests.Class1"
9+
c.getDeclaringType().hasQualifiedName("DisposalTests", "Class1")
1010
select c.toStringWithTypes(), p

csharp/ql/test/library-tests/constructors/Destructors1.ql

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
*/
44

55
import csharp
6+
import semmle.code.csharp.commons.QualifiedName
67

7-
from Destructor c
8+
from Destructor c, string namespace, string name
89
where
9-
c.getDeclaringType().getName() = "Class" and
10-
c.getDeclaringType().getNamespace().getQualifiedName() = "Constructors"
10+
c.getDeclaringType().hasQualifiedName(namespace, name) and
11+
namespace = "Constructors" and
12+
name = "Class"
1113
select c, c.getDeclaringType().getQualifiedName()
Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
import csharp
2+
import semmle.code.csharp.commons.QualifiedName
23

3-
from Method m, Method overrider
4+
from
5+
Method m, Method overrider, string mnamespace, string mtype, string mname, string onamespace,
6+
string otype, string oname
47
where
58
m.getAnOverrider() = overrider and
6-
m.getFile().getStem() = "CovariantReturn"
7-
select m.getQualifiedName(), m.getReturnType().toString(), overrider.getQualifiedName(),
8-
overrider.getReturnType().toString()
9+
m.getFile().getStem() = "CovariantReturn" and
10+
m.hasQualifiedName(mnamespace, mtype, mname) and
11+
overrider.hasQualifiedName(onamespace, otype, oname)
12+
select printQualifiedName(mnamespace, mtype, mname), m.getReturnType().toString(),
13+
printQualifiedName(onamespace, otype, oname), overrider.getReturnType().toString()
Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import csharp
2+
import semmle.code.csharp.commons.QualifiedName
23

34
private string getLocation(Member m) {
45
if m.fromSource() then result = m.getALocation().(SourceLocation).toString() else result = "-"
@@ -8,8 +9,13 @@ private string getIsAsync(ForeachStmt f) {
89
if f.isAsync() then result = "async" else result = "sync"
910
}
1011

11-
from ForeachStmt f
12-
select f, f.getElementType().toString(), getIsAsync(f),
13-
f.getGetEnumerator().getDeclaringType().getQualifiedName(), getLocation(f.getGetEnumerator()),
14-
f.getCurrent().getDeclaringType().getQualifiedName(), getLocation(f.getCurrent()),
15-
f.getMoveNext().getDeclaringType().getQualifiedName(), getLocation(f.getMoveNext())
12+
from
13+
ForeachStmt f, string namespace1, string type1, string namespace2, string type2,
14+
string namespace3, string type3
15+
where
16+
f.getGetEnumerator().getDeclaringType().hasQualifiedName(namespace1, type1) and
17+
f.getCurrent().getDeclaringType().hasQualifiedName(namespace2, type2) and
18+
f.getMoveNext().getDeclaringType().hasQualifiedName(namespace3, type3)
19+
select f, f.getElementType().toString(), getIsAsync(f), printQualifiedName(namespace1, type1),
20+
getLocation(f.getGetEnumerator()), printQualifiedName(namespace2, type2),
21+
getLocation(f.getCurrent()), printQualifiedName(namespace3, type3), getLocation(f.getMoveNext())

csharp/ql/test/library-tests/csharp9/record.ql

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import csharp
2+
import semmle.code.csharp.commons.QualifiedName
23

34
query predicate records(RecordClass t, string i, RecordCloneMethod clone) {
45
t.getABaseInterface().toStringWithTypes() = i and
@@ -7,7 +8,9 @@ query predicate records(RecordClass t, string i, RecordCloneMethod clone) {
78
}
89

910
private string getMemberName(Member m) {
10-
result = m.getDeclaringType().getQualifiedName() + "." + m.toStringWithTypes()
11+
exists(string namespace, string name | m.getDeclaringType().hasQualifiedName(namespace, name) |
12+
result = printQualifiedName(namespace, name) + "." + m.toStringWithTypes()
13+
)
1114
}
1215

1316
query predicate members(RecordClass t, string ms, string l) {

0 commit comments

Comments
 (0)