Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b66dd23

Browse files
committed
Python: Streamline what modules to allow for now
1 parent ba0a5b1 commit b66dd23

2 files changed

Lines changed: 15 additions & 4 deletions

File tree

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,15 @@ module NotExposed {
109109
fullyQualifiedToApiGraphPath(fullyQualifiedName) = spec.getAlreadyModeledClass().getPath()
110110
}
111111

112+
predicate isAllowedModule(Module mod) {
113+
// for now, we only want to model things in site-packages (since we know these are
114+
// libraries used by real code)
115+
mod.getFile().getAbsolutePath().matches("%/site-packages/%")
116+
or
117+
// for CI testing
118+
mod.getFile().getRelativePath().matches("%/find_subclass_test.py")
119+
}
120+
112121
predicate isTestCode(AstNode ast) {
113122
ast.getScope*() instanceof TestScope
114123
or
@@ -164,7 +173,8 @@ module NotExposed {
164173
mod.declaredInAll(importMember.getName())
165174
) and
166175
not alreadyExplicitlyModeled(spec, newAliasFullyQualified) and
167-
not isTestCode(importMember)
176+
not isTestCode(importMember) and
177+
isAllowedModule(mod)
168178
}
169179

170180
/** same as `newDirectAlias` predicate, but handling `from <module> import *`, considering all `<member>`, where `<module>.<member>` belongs to `spec`. */
@@ -192,7 +202,8 @@ module NotExposed {
192202
mod.declaredInAll(relevantName)
193203
) and
194204
not alreadyExplicitlyModeled(spec, newAliasFullyQualified) and
195-
not isTestCode(importStar)
205+
not isTestCode(importStar) and
206+
isAllowedModule(mod)
196207
}
197208

198209
/** Holds if `classExpr` defines a new subclass that belongs to `spec`, which has the fully qualified name `newSubclassQualified`. */
@@ -205,6 +216,7 @@ module NotExposed {
205216
newSubclassQualified = mod.getName() + "." + classExpr.getName() and
206217
loc = classExpr.getLocation() and
207218
not alreadyExplicitlyModeled(spec, newSubclassQualified) and
208-
not isTestCode(classExpr)
219+
not isTestCode(classExpr) and
220+
isAllowedModule(mod)
209221
}
210222
}

python/ql/src/meta/ClassHierarchy/Find.ql

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ where
7676
not exists(FindSubclassesSpec subclass | subclass.getSuperClass() = spec |
7777
newModel(subclass, newModelFullyQualified, _, mod, _)
7878
) and
79-
not exists(mod.getLocation().getFile().getRelativePath()) and
8079
fullyQualifiedToYamlFormat(newModelFullyQualified, type2, path) and
8180
not Extensions::typeModel(spec, type2, path)
8281
select spec.(string), type2, path

0 commit comments

Comments
 (0)