Python: Disallow invalid path component

This commit is contained in:
Rasmus Wriedt Larsen
2023-11-02 18:18:40 +01:00
parent 6ce8cd38d8
commit 004bb50ef2
2 changed files with 7 additions and 2 deletions

View File

@@ -177,7 +177,7 @@ private predicate legalDottedName(string name) {
}
bindingset[name]
private predicate legalShortName(string name) { name.regexpMatch("(\\p{L}|_)(\\p{L}|\\d|_)*") }
predicate legalShortName(string name) { name.regexpMatch("(\\p{L}|_)(\\p{L}|\\d|_)*") }
private string moduleNameFromBase(Container file) {
// We used to also require `isPotentialPackage(f)` to hold in this case,

View File

@@ -10,6 +10,7 @@ private import semmle.python.dataflow.new.DataFlow
private import semmle.python.dataflow.new.internal.ImportResolution
private import semmle.python.ApiGraphs
private import semmle.python.filters.Tests
private import semmle.python.Module
// very much inspired by the draft at https://github.com/github/codeql/pull/5632
module NotExposed {
@@ -114,7 +115,11 @@ module NotExposed {
predicate isAllowedModule(Module mod) {
// don't include anything found in site-packages
exists(mod.getFile().getRelativePath()) and
not mod.getFile().getRelativePath().regexpMatch("(?i)(^|/)examples?/.*")
not mod.getFile().getRelativePath().regexpMatch("(?i)(^|/)examples?/.*") and
// to counter things like `my-example/app/foo.py` being allowed under `app.foo`
forall(string part | part = mod.getFile().getParent().getRelativePath().splitAt("/") |
legalShortName(part)
)
}
predicate isTestCode(AstNode ast) {