Skip to content

Commit b3e1f57

Browse files
committed
PS: Add intermediate api graph nodes for 'New-Object' calls similar to what we have for type name expressions.
1 parent 2aa84f4 commit b3e1f57

2 files changed

Lines changed: 26 additions & 3 deletions

File tree

powershell/ql/lib/semmle/code/powershell/ApiGraphs.qll

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ module API {
369369

370370
final predicate isImplicit() { not this.isExplicit(_) }
371371

372-
predicate isExplicit(DataFlow::TypeNameNode typeName) { none() }
372+
predicate isExplicit(DataFlow::Node node) { none() }
373373
}
374374

375375
final class TypeNameNode = AbstractTypeNameNode;
@@ -392,8 +392,8 @@ module API {
392392
)
393393
}
394394

395-
final override predicate isExplicit(DataFlow::TypeNameNode typeName) {
396-
Specific::needsExplicitTypeNameNode(typeName, prefix)
395+
final override predicate isExplicit(DataFlow::Node node) {
396+
Specific::needsExplicitTypeNameNode(node, prefix)
397397
}
398398
}
399399

@@ -424,6 +424,18 @@ module API {
424424
}
425425
}
426426

427+
class NewObjectTypeNameNode extends AbstractTypeNameNode, Impl::MkNewObjectTypeNameNode {
428+
NewObjectTypeNameNode() { this = Impl::MkNewObjectTypeNameNode(prefix) }
429+
430+
final override Node getSuccessor(string name) {
431+
result = Impl::MkNewObjectTypeNameNode(prefix + "." + name)
432+
}
433+
434+
final override predicate isExplicit(DataFlow::Node node) {
435+
Specific::needsNewObjectTypeNameNode(node, prefix)
436+
}
437+
}
438+
427439
/**
428440
* An API entry point.
429441
*
@@ -517,6 +529,7 @@ module API {
517529
MkMethodAccessNode(DataFlow::CallNode call) or
518530
MkExplicitTypeNameNode(string prefix) { Specific::needsExplicitTypeNameNode(_, prefix) } or
519531
MkImplicitTypeNameNode(string prefix) { Specific::needsImplicitTypeNameNode(prefix) } or
532+
MkNewObjectTypeNameNode(string prefix) { Specific::needsNewObjectTypeNameNode(_, prefix) } or
520533
MkForwardNode(DataFlow::LocalSourceNode node, TypeTracker t) { isReachable(node, t) } or
521534
/** Intermediate node for following backward data flow. */
522535
MkBackwardNode(DataFlow::LocalSourceNode node, TypeTracker t) { isReachable(node, t) } or

powershell/ql/lib/semmle/code/powershell/frameworks/data/internal/ApiGraphModelsSpecific.qll

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,16 @@ predicate needsImplicitTypeNameNode(string component) {
101101
)
102102
}
103103

104+
predicate needsNewObjectTypeNameNode(DataFlow::ObjectCreationNode creation, string component) {
105+
creation.asExpr().getExpr() instanceof DotNetObjectCreation and
106+
exists(string type, int index |
107+
type = creation.getLowerCaseConstructedTypeName() and
108+
index = [0 .. strictcount(type.indexOf("."))] and
109+
component =
110+
strictconcat(int i, string s | s = type.splitAt(".", i) and i <= index | s, "." order by i)
111+
)
112+
}
113+
104114
/** Gets a Powershell-specific interpretation of the given `type`. */
105115
API::Node getExtraNodeFromType(string rawType) {
106116
exists(string type, string suffix, DataFlow::TypeNameNode typeName |

0 commit comments

Comments
 (0)