Skip to content

Commit f8253f5

Browse files
committed
Python: Fully disallow API::moduleImport of module with dots
Inspired by discussion about this for MaD in github#8883 (comment)
1 parent 597a841 commit f8253f5

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

python/ql/lib/semmle/python/ApiGraphs.qll

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,13 @@ module API {
280280
* you should use `.getMember` on the parent module. For example, for nodes corresponding to the module `foo.bar`,
281281
* use `moduleImport("foo").getMember("bar")`.
282282
*/
283-
Node moduleImport(string m) { result = Impl::MkModuleImport(m) }
283+
Node moduleImport(string m) {
284+
result = Impl::MkModuleImport(m) and
285+
// restrict `moduleImport` so it will never give results for a dotted name. Note
286+
// that we cannot move this logic to the `MkModuleImport` construction, since we
287+
// need the intermediate API graph nodes for the prefixes in `import foo.bar.baz`.
288+
not m.matches("%.%")
289+
}
284290

285291
/** Gets a node corresponding to the built-in with the given name, if any. */
286292
Node builtin(string n) { result = moduleImport("builtins").getMember(n) }
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
moduleImportWithDots
2-
| file://:0:0:0:0 | ModuleImport moduleImport("a").getMember("b").getMember("c").getMember("d") |
32
doesntFullyWork
4-
| test.py:28:10:28:17 | ControlFlowNode for method() |
53
works
64
| test.py:25:6:25:18 | ControlFlowNode for Attribute() |
75
| test.py:28:10:28:17 | ControlFlowNode for method() |

0 commit comments

Comments
 (0)