mirror of
https://github.com/github/codeql.git
synced 2026-05-01 03:35:13 +02:00
Merge pull request #2163 from tausbn/python-undefined-export-fp
Python: Modernise and fix FP in `py/undefined-export`
This commit is contained in:
@@ -14,39 +14,54 @@
|
||||
import python
|
||||
|
||||
/** Whether name is declared in the __all__ list of this module */
|
||||
predicate declaredInAll(Module m, StrConst name)
|
||||
{
|
||||
exists(Assign a, GlobalVariable all |
|
||||
a.defines(all) and a.getScope() = m and
|
||||
all.getId() = "__all__" and ((List)a.getValue()).getAnElt() = name
|
||||
predicate declaredInAll(Module m, StrConst name) {
|
||||
exists(Assign a, GlobalVariable all |
|
||||
a.defines(all) and
|
||||
a.getScope() = m and
|
||||
all.getId() = "__all__" and
|
||||
a.getValue().(List).getAnElt() = name
|
||||
)
|
||||
}
|
||||
|
||||
predicate mutates_globals(PythonModuleObject m) {
|
||||
predicate mutates_globals(ModuleValue m) {
|
||||
exists(CallNode globals |
|
||||
globals = Object::builtin("globals").(FunctionObject).getACall() and
|
||||
globals.getScope() = m.getModule() |
|
||||
globals = Value::named("globals").(FunctionValue).getACall() and
|
||||
globals.getScope() = m.getScope()
|
||||
|
|
||||
exists(AttrNode attr | attr.getObject() = globals)
|
||||
or
|
||||
exists(SubscriptNode sub | sub.getValue() = globals and sub.isStore())
|
||||
)
|
||||
or
|
||||
exists(Object enum_convert |
|
||||
enum_convert.hasLongName("enum.Enum._convert") and
|
||||
exists(CallNode call |
|
||||
call.getScope() = m.getModule()
|
||||
|
|
||||
enum_convert.(FunctionObject).getACall() = call or
|
||||
call.getFunction().refersTo(enum_convert)
|
||||
exists(Value enum_convert, ClassValue enum_class |
|
||||
enum_class.getASuperType() = Value::named("enum.Enum") and
|
||||
enum_convert = enum_class.attr("_convert") and
|
||||
exists(CallNode call | call.getScope() = m.getScope() |
|
||||
enum_convert.getACall() = call or
|
||||
call.getFunction().pointsTo(enum_convert)
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
from PythonModuleObject m, StrConst name, string exported_name
|
||||
where declaredInAll(m.getModule(), name) and
|
||||
exported_name = name.strValue() and
|
||||
not m.hasAttribute(exported_name) and
|
||||
not (m.getShortName() = "__init__" and exists(m.getPackage().getModule().getSubModule(exported_name))) and
|
||||
not exists(ImportStarNode imp | imp.getEnclosingModule() = m.getModule() | not imp.getModule().refersTo(_)) and
|
||||
not mutates_globals(m)
|
||||
select name, "The name '" + exported_name + "' is exported by __all__ but is not defined."
|
||||
predicate is_exported_submodule_name(ModuleValue m, string exported_name) {
|
||||
m.getScope().getShortName() = "__init__" and
|
||||
exists(m.getScope().getPackage().getSubModule(exported_name))
|
||||
}
|
||||
|
||||
predicate contains_unknown_import_star(ModuleValue m) {
|
||||
exists(ImportStarNode imp | imp.getEnclosingModule() = m.getScope() |
|
||||
imp.getModule().pointsTo().isAbsent()
|
||||
or
|
||||
not exists(imp.getModule().pointsTo())
|
||||
)
|
||||
}
|
||||
|
||||
from ModuleValue m, StrConst name, string exported_name
|
||||
where
|
||||
declaredInAll(m.getScope(), name) and
|
||||
exported_name = name.strValue() and
|
||||
not m.hasAttribute(exported_name) and
|
||||
not is_exported_submodule_name(m, exported_name) and
|
||||
not contains_unknown_import_star(m) and
|
||||
not mutates_globals(m)
|
||||
select name, "The name '" + exported_name + "' is exported by __all__ but is not defined."
|
||||
|
||||
@@ -52,6 +52,13 @@ class Module extends Module_, Scope, AstNode {
|
||||
result = moduleNameFromFile(this.getPath())
|
||||
}
|
||||
|
||||
/** Gets the short name of the module. For example the short name of module x.y.z is 'z' */
|
||||
string getShortName() {
|
||||
result = this.getName().suffix(this.getPackage().getName().length()+1)
|
||||
or
|
||||
result = this.getName() and not exists(this.getPackage())
|
||||
}
|
||||
|
||||
/** Gets this module */
|
||||
override Module getEnclosingModule() {
|
||||
result = this
|
||||
|
||||
@@ -99,6 +99,12 @@ class Value extends TObject {
|
||||
this.(ObjectInternal).hasAttribute(name)
|
||||
}
|
||||
|
||||
/** Whether this value is absent from the database, but has been inferred to likely exist */
|
||||
predicate isAbsent() {
|
||||
this instanceof AbsentModuleObjectInternal
|
||||
or
|
||||
this instanceof AbsentModuleAttributeObjectInternal
|
||||
}
|
||||
}
|
||||
|
||||
/** Class representing modules in the Python program
|
||||
|
||||
Reference in New Issue
Block a user