Skip to content

Commit b075103

Browse files
authored
Merge pull request #2163 from tausbn/python-undefined-export-fp
Python: Modernise and fix FP in `py/undefined-export`
2 parents 3e8b28a + d2f9850 commit b075103

File tree

3 files changed

+51
-23
lines changed

3 files changed

+51
-23
lines changed

python/ql/src/Variables/UndefinedExport.ql

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,39 +14,54 @@
1414
import python
1515

1616
/** Whether name is declared in the __all__ list of this module */
17-
predicate declaredInAll(Module m, StrConst name)
18-
{
19-
exists(Assign a, GlobalVariable all |
20-
a.defines(all) and a.getScope() = m and
21-
all.getId() = "__all__" and ((List)a.getValue()).getAnElt() = name
17+
predicate declaredInAll(Module m, StrConst name) {
18+
exists(Assign a, GlobalVariable all |
19+
a.defines(all) and
20+
a.getScope() = m and
21+
all.getId() = "__all__" and
22+
a.getValue().(List).getAnElt() = name
2223
)
2324
}
2425

25-
predicate mutates_globals(PythonModuleObject m) {
26+
predicate mutates_globals(ModuleValue m) {
2627
exists(CallNode globals |
27-
globals = Object::builtin("globals").(FunctionObject).getACall() and
28-
globals.getScope() = m.getModule() |
28+
globals = Value::named("globals").(FunctionValue).getACall() and
29+
globals.getScope() = m.getScope()
30+
|
2931
exists(AttrNode attr | attr.getObject() = globals)
3032
or
3133
exists(SubscriptNode sub | sub.getValue() = globals and sub.isStore())
3234
)
3335
or
34-
exists(Object enum_convert |
35-
enum_convert.hasLongName("enum.Enum._convert") and
36-
exists(CallNode call |
37-
call.getScope() = m.getModule()
38-
|
39-
enum_convert.(FunctionObject).getACall() = call or
40-
call.getFunction().refersTo(enum_convert)
36+
exists(Value enum_convert, ClassValue enum_class |
37+
enum_class.getASuperType() = Value::named("enum.Enum") and
38+
enum_convert = enum_class.attr("_convert") and
39+
exists(CallNode call | call.getScope() = m.getScope() |
40+
enum_convert.getACall() = call or
41+
call.getFunction().pointsTo(enum_convert)
4142
)
4243
)
4344
}
4445

45-
from PythonModuleObject m, StrConst name, string exported_name
46-
where declaredInAll(m.getModule(), name) and
47-
exported_name = name.strValue() and
48-
not m.hasAttribute(exported_name) and
49-
not (m.getShortName() = "__init__" and exists(m.getPackage().getModule().getSubModule(exported_name))) and
50-
not exists(ImportStarNode imp | imp.getEnclosingModule() = m.getModule() | not imp.getModule().refersTo(_)) and
51-
not mutates_globals(m)
52-
select name, "The name '" + exported_name + "' is exported by __all__ but is not defined."
46+
predicate is_exported_submodule_name(ModuleValue m, string exported_name) {
47+
m.getScope().getShortName() = "__init__" and
48+
exists(m.getScope().getPackage().getSubModule(exported_name))
49+
}
50+
51+
predicate contains_unknown_import_star(ModuleValue m) {
52+
exists(ImportStarNode imp | imp.getEnclosingModule() = m.getScope() |
53+
imp.getModule().pointsTo().isAbsent()
54+
or
55+
not exists(imp.getModule().pointsTo())
56+
)
57+
}
58+
59+
from ModuleValue m, StrConst name, string exported_name
60+
where
61+
declaredInAll(m.getScope(), name) and
62+
exported_name = name.strValue() and
63+
not m.hasAttribute(exported_name) and
64+
not is_exported_submodule_name(m, exported_name) and
65+
not contains_unknown_import_star(m) and
66+
not mutates_globals(m)
67+
select name, "The name '" + exported_name + "' is exported by __all__ but is not defined."

python/ql/src/semmle/python/Module.qll

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ class Module extends Module_, Scope, AstNode {
5252
result = moduleNameFromFile(this.getPath())
5353
}
5454

55+
/** Gets the short name of the module. For example the short name of module x.y.z is 'z' */
56+
string getShortName() {
57+
result = this.getName().suffix(this.getPackage().getName().length()+1)
58+
or
59+
result = this.getName() and not exists(this.getPackage())
60+
}
61+
5562
/** Gets this module */
5663
override Module getEnclosingModule() {
5764
result = this

python/ql/src/semmle/python/objects/ObjectAPI.qll

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ class Value extends TObject {
9999
this.(ObjectInternal).hasAttribute(name)
100100
}
101101

102+
/** Whether this value is absent from the database, but has been inferred to likely exist */
103+
predicate isAbsent() {
104+
this instanceof AbsentModuleObjectInternal
105+
or
106+
this instanceof AbsentModuleAttributeObjectInternal
107+
}
102108
}
103109

104110
/** Class representing modules in the Python program

0 commit comments

Comments
 (0)