Skip to content

Commit 6ef9a2b

Browse files
committed
Python: Fix problem if import is used
I fixed it in both predicates... I think we might still be able to remove `newDirectAlias` -- but with it being better, it will allow us to better test if `newImportAlias` actually cover everything we need!
1 parent fcdc810 commit 6ef9a2b

File tree

2 files changed

+30
-25
lines changed

2 files changed

+30
-25
lines changed

python/ql/lib/semmle/python/frameworks/internal/SubclassFinder.qll

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -156,29 +156,28 @@ module NotExposed {
156156
* ```
157157
*/
158158
predicate newDirectAlias(
159-
FindSubclassesSpec spec, string newAliasFullyQualified, ImportMember importMember, Module mod,
160-
Location loc
159+
FindSubclassesSpec spec, string newAliasFullyQualified, Expr value, Module mod, Location loc
161160
) {
162-
importMember =
163-
newOrExistingModeling(spec).getASubclass*().getAValueReachableFromSource().asExpr() and
164-
importMember.getScope() = mod and
165-
loc = importMember.getLocation() and
166-
exists(Alias alias, string base |
167-
mod.isPackageInit() and base = mod.getPackageName()
168-
or
169-
not mod.isPackageInit() and base = mod.getName()
170-
|
171-
alias.getValue() = importMember and
172-
newAliasFullyQualified = base + "." + alias.getAsname()
173-
) and
174-
(
175-
not hasAllStatement(mod)
176-
or
177-
mod.declaredInAll(importMember.getName())
178-
) and
179-
not alreadyExplicitlyModeled(spec, newAliasFullyQualified) and
180-
not isTestCode(importMember) and
181-
isAllowedModule(mod)
161+
exists(Alias alias | value = alias.getValue() |
162+
value = newOrExistingModeling(spec).getASubclass*().getAValueReachableFromSource().asExpr() and
163+
value.getScope() = mod and
164+
loc = value.getLocation() and
165+
exists(string base |
166+
mod.isPackageInit() and base = mod.getPackageName()
167+
or
168+
not mod.isPackageInit() and base = mod.getName()
169+
|
170+
newAliasFullyQualified = base + "." + alias.getAsname().(Name).getId()
171+
) and
172+
(
173+
not hasAllStatement(mod)
174+
or
175+
mod.declaredInAll(alias.getAsname().(Name).getId())
176+
) and
177+
not alreadyExplicitlyModeled(spec, newAliasFullyQualified) and
178+
not isTestCode(value) and
179+
isAllowedModule(mod)
180+
)
182181
}
183182

184183
/**
@@ -191,11 +190,15 @@ module NotExposed {
191190
string relevantName, Location loc
192191
) {
193192
loc = mod.getLocation() and
194-
exists(API::Node relevantClass, Expr value |
193+
exists(API::Node relevantClass, ControlFlowNode value |
195194
relevantClass = newOrExistingModeling(spec).getASubclass*() and
196195
ImportResolution::module_export(mod, relevantName, def) and
197-
value = relevantClass.getAValueReachableFromSource().asExpr() and
198-
value = def.asVar().getDefinition().(AssignmentDefinition).getValue().getNode()
196+
value = relevantClass.getAValueReachableFromSource().asCfgNode() and
197+
(
198+
value = def.asVar().getDefinition().(AssignmentDefinition).getValue()
199+
or
200+
value = def.asCfgNode()
201+
)
199202
// value could be a ClassExpr if a new class is defined, or a Name if defining an alias
200203
) and
201204
(

python/ql/test/experimental/library-tests/FindSubclass/Find.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
| flask.View~Subclass | find_subclass_test | Member[B] |
77
| flask.View~Subclass | find_subclass_test | Member[ViewAliasInExcept] |
88
| flask.View~Subclass | find_subclass_test | Member[ViewAliasInTry] |
9+
| flask.View~Subclass | find_subclass_test | Member[ViewAlias] |
910
| flask.View~Subclass | find_subclass_test | Member[ViewAlias_no_use] |
1011
| flask.View~Subclass | find_subclass_test | Member[View] |
1112
| flask.View~Subclass | find_subclass_test | Member[clash2] |
1213
| flask.View~Subclass | find_subclass_test | Member[clash3] |
1314
| flask.View~Subclass | find_subclass_test | Member[clash] |
15+
| flask.View~Subclass | find_subclass_test | Member[complete_module_alias] |
1416
| flask.View~Subclass | find_subclass_test | Member[complete_module_alias_no_use] |

0 commit comments

Comments
 (0)