Skip to content

Commit a99b3f2

Browse files
authored
Merge pull request #21459 from github/tausbn/python-fix-missing-relative-imports
Python: Fix resolution of relative imports from namespace packages
2 parents c24b43d + 92718a9 commit a99b3f2

File tree

8 files changed

+60
-0
lines changed

8 files changed

+60
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
category: fix
3+
---
4+
5+
- Fixed the resolution of relative imports such as `from . import helper` inside namespace packages (directories without an `__init__.py` file), which previously did not work correctly, leading to missing flow.

python/ql/lib/semmle/python/Import.qll

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ private predicate valid_module_name(string name) {
1717
exists(Module m | m.getName() = name)
1818
or
1919
exists(Builtin cmod | cmod.getClass() = Builtin::special("ModuleType") and cmod.getName() = name)
20+
or
21+
// Namespace packages may not have a corresponding Module entity,
22+
// but their names are still valid for the purpose of import resolution.
23+
name = moduleNameFromFile(any(Folder f))
2024
}
2125

2226
/** An artificial expression representing an import */
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from . import helper
2+
3+
def use_relative():
4+
tainted = source()
5+
helper.process(tainted)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
def process(value):
2+
sink(value) #$ flow=source
3+
4+
def process2(value):
5+
sink(value) #$ flow=source

python/ql/test/experimental/import-resolution-namespace-relative/pkg/sub/__init__.py

Whitespace-only changes.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from .. import helper
2+
3+
def use_multi_level_relative():
4+
tainted = source()
5+
helper.process2(tainted)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import python
2+
import semmle.python.dataflow.new.DataFlow
3+
import semmle.python.dataflow.new.TaintTracking
4+
import utils.test.InlineExpectationsTest
5+
6+
private module TestConfig implements DataFlow::ConfigSig {
7+
predicate isSource(DataFlow::Node node) {
8+
node.(DataFlow::CallCfgNode).getFunction().asCfgNode().(NameNode).getId() = "source"
9+
}
10+
11+
predicate isSink(DataFlow::Node node) {
12+
exists(DataFlow::CallCfgNode call |
13+
call.getFunction().asCfgNode().(NameNode).getId() = "sink" and
14+
node = call.getArg(0)
15+
)
16+
}
17+
}
18+
19+
private module TestFlow = TaintTracking::Global<TestConfig>;
20+
21+
module FlowTest implements TestSig {
22+
string getARelevantTag() { result = "flow" }
23+
24+
predicate hasActualResult(Location location, string element, string tag, string value) {
25+
exists(DataFlow::Node sink |
26+
TestFlow::flow(_, sink) and
27+
tag = "flow" and
28+
location = sink.getLocation() and
29+
value = "source" and
30+
element = sink.toString()
31+
)
32+
}
33+
}
34+
35+
import MakeTest<FlowTest>

0 commit comments

Comments
 (0)