Skip to content

Commit fa28d94

Browse files
Added a sanitizer for replacing newlines.
1 parent dbbc944 commit fa28d94

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

python/ql/lib/semmle/python/security/dataflow/HttpHeaderInjectionCustomizations.qll

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,14 @@ module HttpHeaderInjection {
7171
Http::Server::ResponseHeaderBulkWrite.super.valueAllowsNewline()
7272
}
7373
}
74+
75+
/**
76+
* A call to replace line breaks, considered as a sanitizer.
77+
*/
78+
class ReplaceLineBreaksSanitizer extends Sanitizer, DataFlow::CallCfgNode {
79+
ReplaceLineBreaksSanitizer() {
80+
this.getFunction().(DataFlow::AttrRead).getAttributeName() = "replace" and
81+
this.getArg(0).asExpr().(StrConst).getText() = "\n"
82+
}
83+
}
7484
}

python/ql/test/query-tests/Security/CWE-113-HeaderInjection/flask_tests.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,13 @@ def flask_make_response_header_arg3():
5858
def flask_make_response_header_arg2():
5959
rfs_header = request.args["rfs_header"]
6060
resp = make_response("hello", {request.args["rfs_header"]: "HeaderValue"}) # BAD
61-
return resp
61+
return resp
62+
63+
@app.route("/flask_escaped")
64+
def flask_escaped():
65+
rfs_header = request.args["rfs_header"]
66+
resp = make_response("hello", {rfs_header.replace("\n", ""): "HeaderValue"}) # GOOD - Newlines are removed from the input.
67+
return resp
68+
69+
# if __name__ == "__main__":
70+
# app.run(debug=True)

0 commit comments

Comments
 (0)