Skip to content

Commit 366ebbc

Browse files
committed
JS: Address review comments
1 parent e68e84f commit 366ebbc

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

docs/language/learn-ql/javascript/type-tracking.rst

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,17 @@ using the JavaScript type tracking library.
66

77
The type tracking library makes it possible to track values through properties and function calls,
88
usually to recognize method calls and properties accessed on a specific type of object.
9-
This can act as a substitute for static type information, so for TypeScript analysis, it may be easier to use the
10-
`static type system <https://help.semmle.com/QL/learn-ql/javascript/introduce-libraries-ts.html#static-type-information>`__.
11-
In this article we'll be working with plain untyped JavaScript.
129

1310
This is an advanced topic and is intended for readers already familiar with the
1411
`SourceNode <https://help.semmle.com/QL/learn-ql/javascript/dataflow.html#source-nodes>`__ class as well as
1512
`taint tracking <https://help.semmle.com/QL/learn-ql/javascript/dataflow.html#using-global-taint-tracking>`__.
13+
For TypeScript analysis also consider reading about `static type information <https://help.semmle.com/QL/learn-ql/javascript/introduce-libraries-ts.html#static-type-information>`__ first.
1614

1715

1816
The problem of recognizing method calls
1917
---------------------------------------
2018

21-
We'll start with a simple model of the Firebase API and gradually build on it to use type tracking.
19+
We'll start with a simple model of the `Firebase API <https://firebase.google.com/docs/reference/js/firebase.database>`__ and gradually build on it to use type tracking.
2220
Knowledge of Firebase is not required.
2321

2422
Suppose we wish to find places where data is written to a Firebase database, as
@@ -129,7 +127,7 @@ Predicates that use type tracking usually conform to the following general patte
129127
130128
SourceNode myType(TypeTracker t) {
131129
t.start() and
132-
result = /* value to track */
130+
result = /* SourceNode to track */
133131
or
134132
exists(TypeTracker t2 |
135133
result = myType(t2).track(t2, t)
@@ -276,7 +274,7 @@ For reference, here's our simple Firebase model with type tracking on every pred
276274
result = firebaseRef().getAMethodCall("set")
277275
}
278276
279-
`Here <https://lgtm.com/query/1053770500827789481>`__ is a run of an example query using the model on one of the Firebase sample projects.
277+
`Here <https://lgtm.com/query/1053770500827789481>`__ is a run of an example query using the model to find `set` calls on one of the Firebase sample projects.
280278
It's been modified slightly to handle a bit more of the API, which is out of scope of this tutorial.
281279

282280
Tracking associated data
@@ -391,7 +389,7 @@ Based on that we can track the ``snapshot`` value and find the ``val()`` call it
391389
392390
With this addition, ``firebaseDatabaseRead("forecast")`` finds the call to ``snapshot.val()`` which contains the value of the forecast.
393391

394-
`Here <https://lgtm.com/query/8761360814276109092>`__ is a run of an example query using the model.
392+
`Here <https://lgtm.com/query/8761360814276109092>`__ is a run of an example query using the model to find `val` calls.
395393

396394
Summary
397395
-------
@@ -402,7 +400,7 @@ This covers the use of the type tracking library. To recap, use this template to
402400
403401
SourceNode myType(TypeTracker t) {
404402
t.start() and
405-
result = /* value to track */
403+
result = /* SourceNode to track */
406404
or
407405
exists(TypeTracker t2 |
408406
result = myType(t2).track(t2, t)
@@ -430,6 +428,13 @@ Use this template to define backward type tracking predicates:
430428
result = myType(TypeBackTracker::end())
431429
}
432430
431+
Note that these predicates all return ``SourceNode``,
432+
so attempts to track a non-source node, such as an identifier or string literal,
433+
will not work.
434+
435+
Also note that the predicates taking a ``TypeTracker`` or ``TypeBackTracker`` can often be made ``private``,
436+
as they are typically only used as an intermediate result to compute the other predicate.
437+
433438
Limitations
434439
-----------
435440

@@ -453,15 +458,15 @@ This is an example of where `data flow configurations <https://help.semmle.com/Q
453458
When to use type tracking
454459
-------------------------
455460

456-
Type tracking and data flow configurations are essentally competing solutions to the same
461+
Type tracking and data flow configurations are essentially competing solutions to the same
457462
problem, each with their own tradeoffs.
458463

459464
Type tracking can be used in any number of predicates, which may depend on each other
460465
in fairly unrestricted ways. The result of one predicate may be the starting
461466
point for another. Type tracking predicates may be mutually recursive.
462467
Type tracking predicates can have any number of extra parameters, making it possible, but optional,
463468
to construct source/sink pairs. Omitting source/sink pairs can be useful when there is a huge number
464-
of sources and the sinks are not known to the library model.
469+
of sources and sinks.
465470

466471
Data flow configurations have more restricted dependencies but are more powerful in other ways.
467472
For performance reasons,
@@ -487,6 +492,15 @@ Prefer data flow configurations when:
487492
- Tracking values through string manipulation.
488493
- Generating a path from source to sink -- see :doc:`constructing path queries <../writing-queries/path-queries>`.
489494

495+
Lastly, depending on the code base being analyzed, some alternatives to consider are:
496+
497+
- Using `static type information <https://help.semmle.com/QL/learn-ql/javascript/introduce-libraries-ts.html#static-type-information>`__,
498+
if analyzing TypeScript code.
499+
500+
- Relying on local data flow.
501+
502+
- Relying on syntactic heuristics such as the name of a method, property, or variable.
503+
490504
Type tracking in the standard libraries
491505
---------------------------------------
492506

0 commit comments

Comments
 (0)