@@ -403,20 +403,49 @@ private module Stdlib {
403
403
}
404
404
}
405
405
406
+ /** Gets a reference to an open file. */
407
+ private DataFlow:: LocalSourceNode openFile ( DataFlow:: TypeTracker t , FileSystemAccess openCall ) {
408
+ t .start ( ) and
409
+ result = openCall and
410
+ (
411
+ openCall instanceof OpenCall
412
+ or
413
+ openCall instanceof PathLibOpenCall
414
+ )
415
+ or
416
+ exists ( DataFlow:: TypeTracker t2 | result = openFile ( t2 , openCall ) .track ( t2 , t ) )
417
+ }
418
+
419
+ /** Gets a reference to an open file. */
420
+ private DataFlow:: Node openFile ( FileSystemAccess openCall ) {
421
+ openFile ( DataFlow:: TypeTracker:: end ( ) , openCall ) .flowsTo ( result )
422
+ }
423
+
424
+ /** Gets a reference to the `write` or `writelines` method on an open file. */
425
+ private DataFlow:: LocalSourceNode writeMethodOnOpenFile (
426
+ DataFlow:: TypeTracker t , FileSystemAccess openCall
427
+ ) {
428
+ t .startInAttr ( [ "write" , "writelines" ] ) and
429
+ result = openFile ( openCall )
430
+ or
431
+ exists ( DataFlow:: TypeTracker t2 | result = writeMethodOnOpenFile ( t2 , openCall ) .track ( t2 , t ) )
432
+ }
433
+
434
+ /** Gets a reference to the `write` or `writelines` method on an open file. */
435
+ private DataFlow:: Node writeMethodOnOpenFile ( FileSystemAccess openCall ) {
436
+ writeMethodOnOpenFile ( DataFlow:: TypeTracker:: end ( ) , openCall ) .flowsTo ( result )
437
+ }
438
+
406
439
/** A call to the `write` or `writelines` method on an opened file, such as `open("foo", "w").write(...)`. */
407
- private class WriteOnOpenFile extends FileSystemWriteAccess:: Range , DataFlow:: CallCfgNode {
408
- WriteOnOpenFile ( ) {
409
- this = getOpenFunctionRef ( ) . getReturn ( ) . getMember ( [ "write" , "writelines" ] ) . getACall ( )
410
- }
440
+ private class WriteCallOnOpenFile extends FileSystemWriteAccess:: Range , DataFlow:: CallCfgNode {
441
+ FileSystemAccess openCall ;
442
+
443
+ WriteCallOnOpenFile ( ) { this . getFunction ( ) = writeMethodOnOpenFile ( openCall ) }
411
444
412
445
override DataFlow:: Node getAPathArgument ( ) {
413
- // best effort attempt to give the path argument, that was initially given to the `open` call.
414
- exists ( OpenCall openCall , DataFlow:: AttrRead read |
415
- read .getAttributeName ( ) in [ "write" , "writelines" ] and
416
- openCall .flowsTo ( read .getObject ( ) ) and
417
- read .( DataFlow:: LocalSourceNode ) .flowsTo ( this .getFunction ( ) ) and
418
- result = openCall .getAPathArgument ( )
419
- )
446
+ // best effort attempt to give the path argument, that was initially given to the
447
+ // `open` call.
448
+ result = openCall .getAPathArgument ( )
420
449
}
421
450
422
451
override DataFlow:: Node getADataNode ( ) { result in [ this .getArg ( 0 ) , this .getArgByName ( "data" ) ] }
@@ -1051,6 +1080,11 @@ private module Stdlib {
1051
1080
override DataFlow:: Node getADataNode ( ) { result in [ this .getArg ( 0 ) , this .getArgByName ( "data" ) ] }
1052
1081
}
1053
1082
1083
+ /** A call to the `open` method on a `pathlib.Path` instance. */
1084
+ private class PathLibOpenCall extends PathlibFileAccess {
1085
+ PathLibOpenCall ( ) { attrbuteName = "open" }
1086
+ }
1087
+
1054
1088
/** An additional taint steps for objects of type `pathlib.Path` */
1055
1089
private class PathlibPathTaintStep extends TaintTracking:: AdditionalTaintStep {
1056
1090
override predicate step ( DataFlow:: Node nodeFrom , DataFlow:: Node nodeTo ) {
0 commit comments