Skip to content

Commit a8c4455

Browse files
author
Max Schaefer
committed
Factor out an auxiliary predicate.
1 parent bc91f66 commit a8c4455

File tree

1 file changed

+38
-24
lines changed

1 file changed

+38
-24
lines changed

javascript/ql/lib/semmle/javascript/ApiGraphs.qll

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,32 @@ module API {
556556
nd = MkDef(rhs)
557557
}
558558

559+
/**
560+
* Holds if `ref` is a read of a property described by `lbl` on `pred`, and
561+
* `propDesc` is compatible with that property, meaning it is either the
562+
* name of the property itself or the empty string.
563+
*/
564+
pragma[noinline]
565+
private predicate propertyRead(
566+
DataFlow::SourceNode pred, string propDesc, string lbl, DataFlow::Node ref
567+
) {
568+
ref = pred.getAPropertyRead() and
569+
lbl = Label::memberFromRef(ref) and
570+
(
571+
lbl = Label::member(propDesc)
572+
or
573+
propDesc = ""
574+
)
575+
or
576+
PromiseFlow::loadStep(pred.getALocalUse(), ref, Promises::valueProp()) and
577+
lbl = Label::promised() and
578+
(propDesc = Promises::valueProp() or propDesc = "")
579+
or
580+
PromiseFlow::loadStep(pred.getALocalUse(), ref, Promises::errorProp()) and
581+
lbl = Label::promisedError() and
582+
(propDesc = Promises::errorProp() or propDesc = "")
583+
}
584+
559585
/**
560586
* Holds if `ref` is a use of a node that should have an incoming edge from `base` labeled
561587
* `lbl` in the API graph.
@@ -567,37 +593,25 @@ module API {
567593
base = MkRoot() and
568594
ref = lbl.(EntryPoint).getAUse()
569595
or
570-
exists(DataFlow::SourceNode src, DataFlow::SourceNode pred, string prop |
571-
use(base, src) and pred = trackUseNode(src, false, 0, prop)
572-
|
596+
// property reads
597+
exists(DataFlow::SourceNode src, DataFlow::SourceNode pred, string propDesc |
598+
use(base, src) and
599+
pred = trackUseNode(src, false, 0, propDesc) and
600+
propertyRead(pred, propDesc, lbl, ref) and
573601
// `module.exports` is special: it is a use of a def-node, not a use-node,
574602
// so we want to exclude it here
575-
(base instanceof TNonModuleDef or base instanceof TUse) and
576-
lbl = Label::memberFromRef(ref) and
577-
(
578-
lbl = Label::member(prop)
579-
or
580-
prop = ""
581-
) and
582-
ref = pred.getAPropertyRead()
583-
or
603+
(base instanceof TNonModuleDef or base instanceof TUse)
604+
)
605+
or
606+
// invocations
607+
exists(DataFlow::SourceNode src, DataFlow::SourceNode pred |
608+
use(base, src) and pred = trackUseNode(src)
609+
|
584610
lbl = Label::instance() and
585-
prop = "" and
586611
ref = pred.getAnInstantiation()
587612
or
588613
lbl = Label::return() and
589-
prop = "" and
590614
ref = pred.getAnInvocation()
591-
or
592-
(
593-
lbl = Label::promised() and
594-
(prop = Promises::valueProp() or prop = "") and
595-
PromiseFlow::loadStep(pred.getALocalUse(), ref, Promises::valueProp())
596-
or
597-
lbl = Label::promisedError() and
598-
(prop = Promises::errorProp() or prop = "") and
599-
PromiseFlow::loadStep(pred.getALocalUse(), ref, Promises::errorProp())
600-
)
601615
)
602616
or
603617
exists(DataFlow::Node def, DataFlow::FunctionNode fn |

0 commit comments

Comments
 (0)