Skip to content

Commit e5480e4

Browse files
authored
Merge pull request github#3591 from RasmusWL/python-taintkind-fixup
Python: Fix some problems in TaintKind useage
2 parents e4e51b5 + 5514204 commit e5480e4

File tree

6 files changed

+42
-16
lines changed

6 files changed

+42
-16
lines changed

change-notes/1.25/analysis-python.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Improvements to Python analysis
2+
3+
The following changes in version 1.25 affect Python analysis in all applications.
4+
5+
## General improvements
6+
7+
8+
## New queries
9+
10+
| **Query** | **Tags** | **Purpose** |
11+
|-----------------------------|-----------|--------------------------------------------------------------------|
12+
13+
14+
## Changes to existing queries
15+
16+
| **Query** | **Expected impact** | **Change** |
17+
|----------------------------|------------------------|------------------------------------------------------------------|
18+
19+
20+
## Changes to libraries
21+
22+
* Importing `semmle.python.web.HttpRequest` will no longer import `UntrustedStringKind` transitively. `UntrustedStringKind` is the most commonly used non-abstract subclass of `ExternalStringKind`. If not imported (by one mean or another), taint-tracking queries that concern `ExternalStringKind` will not produce any results. Please ensure such queries contain an explicit import (`import semmle.python.security.strings.Untrusted`).

python/ql/src/semmle/python/security/strings/Basic.qll

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,11 @@ private predicate os_path_join(ControlFlowNode fromnode, CallNode tonode) {
107107
tonode.getAnArg() = fromnode
108108
}
109109

110-
/** A kind of "taint", representing a dictionary mapping str->"taint" */
111-
class StringDictKind extends DictKind {
110+
/**
111+
* A kind of "taint", representing a dictionary mapping str->"taint"
112+
*
113+
* DEPRECATED: Use `ExternalStringDictKind` instead.
114+
*/
115+
deprecated class StringDictKind extends DictKind {
112116
StringDictKind() { this.getValue() instanceof StringKind }
113117
}

python/ql/src/semmle/python/security/strings/External.qll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,14 @@ class ExternalJsonKind extends TaintKind {
6060
}
6161
}
6262

63-
/** A kind of "taint", representing a dictionary mapping str->"taint" */
63+
/** A kind of "taint", representing a dictionary mapping keys to tainted strings. */
6464
class ExternalStringDictKind extends DictKind {
6565
ExternalStringDictKind() { this.getValue() instanceof ExternalStringKind }
6666
}
6767

6868
/**
69-
* A kind of "taint", representing a dictionary mapping strings to sequences of
70-
* tainted strings
69+
* A kind of "taint", representing a dictionary mapping keys to sequences of
70+
* tainted strings.
7171
*/
7272
class ExternalStringSequenceDictKind extends DictKind {
7373
ExternalStringSequenceDictKind() { this.getValue() instanceof ExternalStringSequenceKind }

python/ql/src/semmle/python/web/bottle/Request.qll

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import python
22
import semmle.python.dataflow.TaintTracking
3-
import semmle.python.security.strings.Untrusted
3+
import semmle.python.security.strings.External
44
import semmle.python.web.Http
55
import semmle.python.web.bottle.General
66

@@ -13,7 +13,7 @@ class BottleRequestKind extends TaintKind {
1313
result instanceof BottleFormsDict and
1414
(name = "cookies" or name = "query" or name = "form")
1515
or
16-
result instanceof UntrustedStringKind and
16+
result instanceof ExternalStringKind and
1717
(name = "query_string" or name = "url_args")
1818
or
1919
result.(DictKind).getValue() instanceof FileUpload and
@@ -34,27 +34,27 @@ class BottleFormsDict extends TaintKind {
3434
/* Cannot use `getTaintOfAttribute(name)` as it wouldn't bind `name` */
3535
exists(string name |
3636
fromnode = tonode.(AttrNode).getObject(name) and
37-
result instanceof UntrustedStringKind
37+
result instanceof ExternalStringKind
3838
|
3939
name != "get" and name != "getunicode" and name != "getall"
4040
)
4141
}
4242

4343
override TaintKind getTaintOfMethodResult(string name) {
4444
(name = "get" or name = "getunicode") and
45-
result instanceof UntrustedStringKind
45+
result instanceof ExternalStringKind
4646
or
47-
name = "getall" and result.(SequenceKind).getItem() instanceof UntrustedStringKind
47+
name = "getall" and result.(SequenceKind).getItem() instanceof ExternalStringKind
4848
}
4949
}
5050

5151
class FileUpload extends TaintKind {
5252
FileUpload() { this = "bottle.FileUpload" }
5353

5454
override TaintKind getTaintOfAttribute(string name) {
55-
name = "filename" and result instanceof UntrustedStringKind
55+
name = "filename" and result instanceof ExternalStringKind
5656
or
57-
name = "raw_filename" and result instanceof UntrustedStringKind
57+
name = "raw_filename" and result instanceof ExternalStringKind
5858
or
5959
name = "file" and result instanceof UntrustedFile
6060
}
@@ -74,7 +74,7 @@ class BottleRequestParameter extends HttpRequestTaintSource {
7474
exists(BottleRoute route | route.getANamedArgument() = this.(ControlFlowNode).getNode())
7575
}
7676

77-
override predicate isSourceOf(TaintKind kind) { kind instanceof UntrustedStringKind }
77+
override predicate isSourceOf(TaintKind kind) { kind instanceof ExternalStringKind }
7878

7979
override string toString() { result = "bottle handler function argument" }
8080
}

python/ql/src/semmle/python/web/turbogears/Request.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import python
2-
import semmle.python.security.strings.Untrusted
2+
import semmle.python.security.strings.External
33
import semmle.python.web.Http
44
import TurboGears
55

@@ -22,5 +22,5 @@ class UnvalidatedControllerMethodParameter extends HttpRequestTaintSource {
2222
)
2323
}
2424

25-
override predicate isSourceOf(TaintKind kind) { kind instanceof UntrustedStringKind }
25+
override predicate isSourceOf(TaintKind kind) { kind instanceof ExternalStringKind }
2626
}

python/ql/src/semmle/python/web/turbogears/Response.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@ class ControllerMethodTemplatedReturnValue extends HttpResponseTaintSink {
2727
)
2828
}
2929

30-
override predicate sinks(TaintKind kind) { kind instanceof StringDictKind }
30+
override predicate sinks(TaintKind kind) { kind instanceof ExternalStringDictKind }
3131
}

0 commit comments

Comments
 (0)