Skip to content

Commit 714465b

Browse files
committed
Python: Refactor SaxParserSetFeatureCall
Originally made by @erik-krogh in https://github.com/github/codeql/pull/8693/files#diff-9627c1fb9a1cc77fb93e6b7e31af1a4fa908f2a60362cfb34377d24debb97398 Could not be applied directly to this PR, since this PR deletes the file.
1 parent 5f01fc2 commit 714465b

File tree

1 file changed

+7
-31
lines changed

1 file changed

+7
-31
lines changed

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

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3408,7 +3408,7 @@ private module StdlibPrivate {
34083408
*
34093409
* See https://docs.python.org/3.10/library/xml.sax.reader.html#xml.sax.xmlreader.XMLReader.setFeature
34103410
*/
3411-
private class SaxParserSetFeatureCall extends DataFlow::MethodCallNode {
3411+
private class SaxParserSetFeatureCall extends API::CallNode, DataFlow::MethodCallNode {
34123412
SaxParserSetFeatureCall() {
34133413
this =
34143414
API::moduleImport("xml")
@@ -3421,27 +3421,9 @@ private module StdlibPrivate {
34213421

34223422
// The keyword argument names does not match documentation. I checked (with Python
34233423
// 3.9.5) that the names used here actually works.
3424-
DataFlow::Node getFeatureArg() { result in [this.getArg(0), this.getArgByName("name")] }
3424+
API::Node getFeatureArg() { result = this.getParameter(0, "name") }
34253425

3426-
DataFlow::Node getStateArg() { result in [this.getArg(1), this.getArgByName("state")] }
3427-
}
3428-
3429-
/** Gets a back-reference to the `setFeature` state argument `arg`. */
3430-
private DataFlow::TypeTrackingNode saxParserSetFeatureStateArgBacktracker(
3431-
DataFlow::TypeBackTracker t, DataFlow::Node arg
3432-
) {
3433-
t.start() and
3434-
arg = any(SaxParserSetFeatureCall c).getStateArg() and
3435-
result = arg.getALocalSource()
3436-
or
3437-
exists(DataFlow::TypeBackTracker t2 |
3438-
result = saxParserSetFeatureStateArgBacktracker(t2, arg).backtrack(t2, t)
3439-
)
3440-
}
3441-
3442-
/** Gets a back-reference to the `setFeature` state argument `arg`. */
3443-
DataFlow::LocalSourceNode saxParserSetFeatureStateArgBacktracker(DataFlow::Node arg) {
3444-
result = saxParserSetFeatureStateArgBacktracker(DataFlow::TypeBackTracker::end(), arg)
3426+
API::Node getStateArg() { result = this.getParameter(1, "state") }
34453427
}
34463428

34473429
/**
@@ -3452,16 +3434,13 @@ private module StdlibPrivate {
34523434
private DataFlow::Node saxParserWithFeatureExternalGesTurnedOn(DataFlow::TypeTracker t) {
34533435
t.start() and
34543436
exists(SaxParserSetFeatureCall call |
3455-
call.getFeatureArg() =
3437+
call.getFeatureArg().getARhs() =
34563438
API::moduleImport("xml")
34573439
.getMember("sax")
34583440
.getMember("handler")
34593441
.getMember("feature_external_ges")
34603442
.getAUse() and
3461-
saxParserSetFeatureStateArgBacktracker(call.getStateArg())
3462-
.asExpr()
3463-
.(BooleanLiteral)
3464-
.booleanValue() = true and
3443+
call.getStateArg().getAValueReachingRhs().asExpr().(BooleanLiteral).booleanValue() = true and
34653444
result = call.getObject()
34663445
)
34673446
or
@@ -3471,16 +3450,13 @@ private module StdlibPrivate {
34713450
// take account of that we can set the feature to False, which makes the parser safe again
34723451
not exists(SaxParserSetFeatureCall call |
34733452
call.getObject() = result and
3474-
call.getFeatureArg() =
3453+
call.getFeatureArg().getARhs() =
34753454
API::moduleImport("xml")
34763455
.getMember("sax")
34773456
.getMember("handler")
34783457
.getMember("feature_external_ges")
34793458
.getAUse() and
3480-
saxParserSetFeatureStateArgBacktracker(call.getStateArg())
3481-
.asExpr()
3482-
.(BooleanLiteral)
3483-
.booleanValue() = false
3459+
call.getStateArg().getAValueReachingRhs().asExpr().(BooleanLiteral).booleanValue() = false
34843460
)
34853461
}
34863462

0 commit comments

Comments
 (0)