Skip to content

Commit 004bb50

Browse files
committed
Python: Disallow invalid path component
1 parent 6ce8cd3 commit 004bb50

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

python/ql/lib/semmle/python/Module.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ private predicate legalDottedName(string name) {
177177
}
178178

179179
bindingset[name]
180-
private predicate legalShortName(string name) { name.regexpMatch("(\\p{L}|_)(\\p{L}|\\d|_)*") }
180+
predicate legalShortName(string name) { name.regexpMatch("(\\p{L}|_)(\\p{L}|\\d|_)*") }
181181

182182
private string moduleNameFromBase(Container file) {
183183
// We used to also require `isPotentialPackage(f)` to hold in this case,

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ private import semmle.python.dataflow.new.DataFlow
1010
private import semmle.python.dataflow.new.internal.ImportResolution
1111
private import semmle.python.ApiGraphs
1212
private import semmle.python.filters.Tests
13+
private import semmle.python.Module
1314

1415
// very much inspired by the draft at https://github.com/github/codeql/pull/5632
1516
module NotExposed {
@@ -114,7 +115,11 @@ module NotExposed {
114115
predicate isAllowedModule(Module mod) {
115116
// don't include anything found in site-packages
116117
exists(mod.getFile().getRelativePath()) and
117-
not mod.getFile().getRelativePath().regexpMatch("(?i)(^|/)examples?/.*")
118+
not mod.getFile().getRelativePath().regexpMatch("(?i)(^|/)examples?/.*") and
119+
// to counter things like `my-example/app/foo.py` being allowed under `app.foo`
120+
forall(string part | part = mod.getFile().getParent().getRelativePath().splitAt("/") |
121+
legalShortName(part)
122+
)
118123
}
119124

120125
predicate isTestCode(AstNode ast) {

0 commit comments

Comments
 (0)