Skip to content

Commit 66f389a

Browse files
committed
Python: Model stdin thread-model
1 parent e1801f3 commit 66f389a

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

python/ql/lib/semmle/python/frameworks/Stdlib.model.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ extensions:
1212
- ['sys', 'Member[argv]', 'commandargs']
1313
- ['sys', 'Member[orig_argv]', 'commandargs']
1414

15+
- ['sys', 'Member[stdin]', 'stdin']
16+
- ['builtins', 'Member[input].ReturnValue', 'stdin']
17+
1518
# if no argument is given, the default is to use sys.argv[1:]
1619
- ['argparse.ArgumentParser', 'Member[parse_args,parse_known_args].WithArity[0].ReturnValue', 'commandargs']
1720
- addsTo:
@@ -20,5 +23,3 @@ extensions:
2023
data:
2124
- ['argparse.ArgumentParser', 'Member[parse_args,parse_known_args]', 'Argument[0,args:]', 'ReturnValue', 'taint']
2225
# note: taint of attribute lookups is handled in QL
23-
24-
# TODO: input / read from stdin

python/ql/lib/semmle/python/frameworks/Stdlib.qll

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5009,6 +5009,19 @@ module StdlibPrivate {
50095009
nodeTo.(DataFlow::AttrRead).getObject() = nodeFrom
50105010
}
50115011
}
5012+
5013+
// ---------------------------------------------------------------------------
5014+
// sys
5015+
// ---------------------------------------------------------------------------
5016+
/**
5017+
* An access of `sys.stdin`/`sys.stdout`/`sys.stderr`, to get additional FileLike
5018+
* modeling.
5019+
*/
5020+
private class SysStandardStreams extends Stdlib::FileLikeObject::InstanceSource, DataFlow::Node {
5021+
SysStandardStreams() {
5022+
this = API::moduleImport("sys").getMember(["stdin", "stdout", "stderr"]).asSource()
5023+
}
5024+
}
50125025
}
50135026

50145027
// ---------------------------------------------------------------------------

python/ql/test/library-tests/frameworks/stdlib/threat_models.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@
4444
########################################
4545

4646
ensure_tainted(
47-
sys.stdin.readline(), # $ MISSING: tainted threatModelSource
48-
input(), # $ MISSING: tainted threatModelSource
47+
sys.stdin.readline(), # $ tainted threatModelSource[stdin]=sys.stdin
48+
input(), # $ tainted threatModelSource[stdin]=input()
4949
)
5050

5151
########################################

0 commit comments

Comments
 (0)