Skip to content

Commit 9eafee3

Browse files
committed
Some updates missed in earlier PRs
1 parent 5898615 commit 9eafee3

File tree

5 files changed

+120
-31
lines changed

5 files changed

+120
-31
lines changed

docs/codeql/codeql-language-guides/codeql-library-for-typescript.rst

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,6 @@ Select expressions that cast a value to a type parameter:
123123
where assertion.getTypeAnnotation() = param.getLocalTypeName().getAnAccess()
124124
select assertion, "Cast to type parameter."
125125
126-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/1505979606441/>`__.
127-
128126
Classes and interfaces
129127
~~~~~~~~~~~~~~~~~~~~~~
130128

@@ -179,7 +177,7 @@ Ambient nodes are mostly ignored by control flow and data flow analysis. The out
179177
Static type information
180178
-----------------------
181179

182-
Static type information and global name binding is available for projects with "full" TypeScript extraction enabled. This option is enabled by default for projects on LGTM.com and when you create databases with the :ref:`CodeQL CLI <codeql-cli>`.
180+
Static type information and global name binding is available for projects with "full" TypeScript extraction enabled. This option is enabled by default when you create databases with the :ref:`CodeQL CLI <codeql-cli>`.
183181

184182
Basic usage
185183
~~~~~~~~~~~
@@ -403,8 +401,6 @@ It is best to use `TypeName <https://codeql.github.com/codeql-standard-libraries
403401
and not access.hasTypeArguments()
404402
select access, "Type arguments are omitted"
405403
406-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/1505985316500/>`__.
407-
408404
Find imported names that are used as both a type and a value:
409405

410406
.. code-block:: ql
@@ -416,8 +412,6 @@ Find imported names that are used as both a type and a value:
416412
and exists (VarAccess access | access.getVariable().getADeclaration() = spec.getLocal())
417413
select spec, "Used as both variable and type"
418414
419-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/1505975787348/>`__.
420-
421415
Namespace names
422416
~~~~~~~~~~~~~~~
423417

docs/codeql/codeql-language-guides/navigating-the-call-graph.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ We can use the ``Callable`` class to write a query that finds methods that are n
8080
where not exists(Callable caller | caller.polyCalls(callee))
8181
select callee
8282
83-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/8376915232270534450/>`__. This simple query typically returns a large number of results.
83+
This simple query typically returns a large number of results.
8484

8585
.. pull-quote::
8686

@@ -99,7 +99,7 @@ Running this query on a typical Java project results in lots of hits in the Java
9999
callee.getCompilationUnit().fromSource()
100100
select callee, "Not called."
101101
102-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/8711624074465690976/>`__. This change reduces the number of results returned for most projects.
102+
This change reduces the number of results returned for most codebases.
103103

104104
We might also notice several unused methods with the somewhat strange name ``<clinit>``: these are class initializers; while they are not explicitly called anywhere in the code, they are called implicitly whenever the surrounding class is loaded. Hence it makes sense to exclude them from our query. While we are at it, we can also exclude finalizers, which are similarly invoked implicitly:
105105

@@ -113,7 +113,7 @@ We might also notice several unused methods with the somewhat strange name ``<cl
113113
not callee.hasName("<clinit>") and not callee.hasName("finalize")
114114
select callee, "Not called."
115115
116-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/925473733866047471/>`__. This also reduces the number of results returned by most projects.
116+
This also reduces the number of results returned by most codebases.
117117

118118
We may also want to exclude public methods from our query, since they may be external API entry points:
119119

@@ -128,7 +128,7 @@ We may also want to exclude public methods from our query, since they may be ext
128128
not callee.isPublic()
129129
select callee, "Not called."
130130
131-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/6284320987237954610/>`__. This should have a more noticeable effect on the number of results returned.
131+
This should have a more noticeable effect on the number of results returned.
132132

133133
A further special case is non-public default constructors: in the singleton pattern, for example, a class is provided with private empty default constructor to prevent it from being instantiated. Since the very purpose of such constructors is their not being called, they should not be flagged up:
134134

@@ -144,7 +144,7 @@ A further special case is non-public default constructors: in the singleton patt
144144
not callee.(Constructor).getNumberOfParameters() = 0
145145
select callee, "Not called."
146146
147-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/2625028545869146918/>`__. This change has a large effect on the results for some projects but little effect on the results for others. Use of this pattern varies widely between different projects.
147+
This change has a large effect on the results for some projects but little effect on the results for others. Use of this pattern varies widely between different projects.
148148

149149
Finally, on many Java projects there are methods that are invoked indirectly by reflection. So, while there are no calls invoking these methods, they are, in fact, used. It is in general very hard to identify such methods. A very common special case, however, is JUnit test methods, which are reflectively invoked by a test runner. The CodeQL library for Java has support for recognizing test classes of JUnit and other testing frameworks, which we can employ to filter out methods defined in such classes:
150150

@@ -161,7 +161,7 @@ Finally, on many Java projects there are methods that are invoked indirectly by
161161
not callee.getDeclaringType() instanceof TestClass
162162
select callee, "Not called."
163163
164-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/2055862421970264112/>`__. This should give a further reduction in the number of results returned.
164+
This should give a further reduction in the number of results returned.
165165

166166
Further reading
167167
---------------

docs/codeql/codeql-language-guides/using-flow-labels-for-precise-data-flow-analysis.rst

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -338,10 +338,9 @@ step by step in the UI:
338338
where cfg.hasFlowPath(source, sink)
339339
select sink, source, sink, "Property access on JSON value originating $@.", source, "here"
340340
341-
`Here <https://lgtm.com/query/5347702611074820306>`_ is a run of this query on the `plexus-interop
342-
<https://lgtm.com/projects/g/finos-plexus/plexus-interop/>`_ project on LGTM.com. Many of the 19
343-
results are false positives since we currently do not model many ways in which a value can be
344-
checked for nullness. In particular, after a property reference ``x.p`` we implicitly know that
341+
We ran this query on the https://github.com/finos/plexus-interop repository. Many of the
342+
results were false positives since the query does not currently model many ways in which we can check
343+
a value for nullness. In particular, after a property reference ``x.p`` we implicitly know that
345344
``x`` cannot be null anymore, since otherwise the reference would have thrown an exception.
346345
Modeling this would allow us to get rid of most of the false positives, but is beyond the scope of
347346
this tutorial.
@@ -391,10 +390,10 @@ Some of our standard security queries use flow labels. You can look at their imp
391390
to get a feeling for how to use flow labels in practice.
392391

393392
In particular, both of the examples mentioned in the section on limitations of basic data flow above
394-
are from standard security queries that use flow labels. The `Prototype pollution
395-
<https://lgtm.com/rules/1508857356317>`_ query uses two flow labels to distinguish completely
393+
are from standard security queries that use flow labels. The `Prototype-pollutiing merge call
394+
<https://codeql.github.com/codeql-query-help/javascript/js-prototype-pollution/>`_ query uses two flow labels to distinguish completely
396395
tainted objects from partially tainted objects. The `Uncontrolled data used in path expression
397-
<https://lgtm.com/rules/1971530250>`_ query uses four flow labels to track whether a user-controlled
396+
<https://codeql.github.com/codeql-query-help/javascript/js-path-injection/>`_ query uses four flow labels to track whether a user-controlled
398397
string may be an absolute path and whether it may contain ``..`` components.
399398

400399
Further reading

docs/codeql/codeql-language-guides/using-type-tracking-for-api-modeling.rst

Lines changed: 105 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -228,15 +228,23 @@ Here's see an example of what this can handle now:
228228
229229
Tracking in the whole model
230230
---------------------------
231-
We applied this pattern to ``firebaseDatabase()`` in the previous section, and it
232-
can just as easily apply to the other predicates.
233-
For reference, here's our simple Firebase model with type tracking on every predicate:
231+
We applied this pattern to ``firebaseDatabase()`` in the previous section, and we can
232+
apply the model just as easily to other predicates.
233+
This example query uses the model to find `set` calls.
234+
It's been modified slightly to handle a bit more of the API, which is beyond the scope of this tutorial.
234235

235236
.. code-block:: ql
236237
238+
import javascript
239+
import DataFlow
240+
237241
SourceNode firebase(TypeTracker t) {
238242
t.start() and
239-
result = globalVarRef("firebase")
243+
(
244+
result = globalVarRef("firebase")
245+
or
246+
result = moduleImport("firebase/app")
247+
)
240248
or
241249
exists(TypeTracker t2 |
242250
result = firebase(t2).track(t2, t)
@@ -277,8 +285,7 @@ For reference, here's our simple Firebase model with type tracking on every pred
277285
result = firebaseRef().getAMethodCall("set")
278286
}
279287
280-
`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.
281-
It's been modified slightly to handle a bit more of the API, which is beyond the scope of this tutorial.
288+
select firebaseSetterCall()
282289
283290
Tracking associated data
284291
------------------------
@@ -392,7 +399,98 @@ Based on that we can track the ``snapshot`` value and find the ``val()`` call it
392399
393400
With this addition, ``firebaseDatabaseRead("forecast")`` finds the call to ``snapshot.val()`` that contains the value of the forecast.
394401

395-
`Here <https://lgtm.com/query/8761360814276109092>`__ is a run of an example query using the model to find `val` calls.
402+
.. code-block:: ql
403+
404+
import javascript
405+
import DataFlow
406+
407+
SourceNode firebase(TypeTracker t) {
408+
t.start() and
409+
(
410+
result = globalVarRef("firebase")
411+
or
412+
result = moduleImport("firebase/app")
413+
)
414+
or
415+
exists(TypeTracker t2 |
416+
result = firebase(t2).track(t2, t)
417+
)
418+
}
419+
420+
SourceNode firebase() {
421+
result = firebase(TypeTracker::end())
422+
}
423+
424+
SourceNode firebaseDatabase(TypeTracker t) {
425+
t.start() and
426+
result = firebase().getAMethodCall("database")
427+
or
428+
exists(TypeTracker t2 |
429+
result = firebaseDatabase(t2).track(t2, t)
430+
)
431+
}
432+
433+
SourceNode firebaseDatabase() {
434+
result = firebaseDatabase(TypeTracker::end())
435+
}
436+
437+
SourceNode firebaseRef(Node name, TypeTracker t) {
438+
t.start() and
439+
exists(CallNode call |
440+
call = firebaseDatabase().getAMethodCall("ref") and
441+
name = call.getArgument(0) and
442+
result = call
443+
)
444+
or
445+
exists(TypeTracker t2 |
446+
result = firebaseRef(name, t2).track(t2, t)
447+
)
448+
}
449+
450+
SourceNode firebaseRef(Node name) {
451+
result = firebaseRef(name, TypeTracker::end())
452+
}
453+
454+
MethodCallNode firebaseSetterCall(Node name) {
455+
result = firebaseRef(name).getAMethodCall("set")
456+
}
457+
458+
SourceNode firebaseSnapshotCallback(Node refName, TypeBackTracker t) {
459+
t.start() and
460+
(
461+
result = firebaseRef(refName).getAMethodCall("once").getArgument(1).getALocalSource()
462+
or
463+
result = firebaseRef(refName).getAMethodCall("once").getAMethodCall("then").getArgument(0).getALocalSource()
464+
)
465+
or
466+
exists(TypeBackTracker t2 |
467+
result = firebaseSnapshotCallback(refName, t2).backtrack(t2, t)
468+
)
469+
}
470+
471+
FunctionNode firebaseSnapshotCallback(Node refName) {
472+
result = firebaseSnapshotCallback(refName, TypeBackTracker::end())
473+
}
474+
475+
SourceNode firebaseSnapshot(Node refName, TypeTracker t) {
476+
t.start() and
477+
result = firebaseSnapshotCallback(refName).getParameter(0)
478+
or
479+
exists(TypeTracker t2 |
480+
result = firebaseSnapshot(refName, t2).track(t2, t)
481+
)
482+
}
483+
484+
SourceNode firebaseSnapshot(Node refName) {
485+
result = firebaseSnapshot(refName, TypeTracker::end())
486+
}
487+
488+
MethodCallNode firebaseDatabaseRead(Node refName) {
489+
result = firebaseSnapshot(refName).getAMethodCall("val")
490+
}
491+
492+
from Node name
493+
select name, firebaseDatabaseRead(name)
396494
397495
Summary
398496
-------

docs/codeql/codeql-language-guides/working-with-source-locations.rst

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ Here's a first version of our query:
112112
wsinner > wsouter
113113
select outer, "Whitespace around nested operators contradicts precedence."
114114
115-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/8141155897270480914/>`__. This query is likely to find results on most projects.
115+
This query is likely to find results on most codebases.
116116

117117
The first conjunct of the ``where`` clause restricts ``inner`` to be an operand of ``outer``, the second conjunct binds ``wsinner`` and ``wsouter``, while the last conjunct selects the suspicious cases.
118118

@@ -143,7 +143,7 @@ Note that our predicate ``operatorWS`` computes the **total** amount of white sp
143143
wsinner > wsouter
144144
select outer, "Whitespace around nested operators contradicts precedence."
145145
146-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/3151720037708691205/>`__. Any results will be refined by our changes to the query.
146+
Any results will be refined by our changes to the query.
147147

148148
Another source of false positives are associative operators: in an expression of the form ``x + y+z``, the first plus is syntactically nested inside the second, since + in Java associates to the left; hence the expression is flagged as suspicious. But since + is associative to begin with, it does not matter which way around the operators are nested, so this is a false positive. To exclude these cases, let us define a new class identifying binary expressions with an associative operator:
149149

@@ -175,8 +175,6 @@ Now we can extend our query to discard results where the outer and the inner exp
175175
wsinner > wsouter
176176
select outer, "Whitespace around nested operators contradicts precedence."
177177
178-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/5714614966569401039/>`__.
179-
180178
Notice that we again use ``getOp``, this time to determine whether two binary expressions have the same operator. Running our improved query now finds the Java standard library bug described in the Overview. It also flags up the following suspicious code in `Hadoop HBase <https://hbase.apache.org/>`__:
181179

182180
.. code-block:: java

0 commit comments

Comments
 (0)