[JavaScript] Functions stored in nested properties or Map not detected using DataFlow #18746
Replies: 2 comments
-
In your original use-case, are they global variables or merely top-level variables in a module? Global variables are accessible outside the file they were declared in, whereas top-level variables are scoped to their file and are therefore tracked more precisely. In the example code you've provided, the analysis will interpret the top-level declarations as global variables and track them less precisely. But you were to add an import or I'm saying this mainly because, in the process of crafting a minimal reproduction example, it often happens that the example file stops looking like a module and is instead interpreted as a global script. And then we get a report about global variables even though the user's original problem had nothing to do with global variables. So I was just wondering if you could double check if they are global variables or not. You can use the class |
Beta Was this translation helpful? Give feedback.
-
Great answer! This was exactly the problem, just adding a module makes the above example work. It was an issue in minimizing my example too much. After some more debugging, the real problem seems to lie with storing functions and nested properties. The library has a With this handler function pattern, it seems like CodeQL also detects less features. Saving a handler in an object variable's property works, but saving it in a // Make it detected as a module
const { Client } = require('library');
function handler(input) {
sink(input);
}
function test1() {
const a = handler;
const obj = {};
obj.b = handler;
const map = new Map();
map.set("c", handler);
// These are detected:
handler(source());
a(source());
obj.b(source());
// This one is not:
map.get("c")(source());
}
function test2() {
const client = new Client();
client.a = handler;
client.obj = {};
client.obj.b = handler;
client.map = new Map();
client.map.set("c", handler);
// These are detected:
handler(source());
client.a(source());
// These are not:
client.obj.b(source());
client.map.get("c")(source());
} The query is still the same as in my original post, it should find all ways that |
Beta Was this translation helpful? Give feedback.
-
I'm having some trouble with getting CodeQL to detect a common pattern in a framework I am trying to model, where a global
Map
variable is accessed by functions. In my testing, this doesn't appear to be tracked byDataFlow
, while it does work in a local scope (also proven by this existing test).In the following source code example,
source()
is marked as user input andsink()
represents a dangerous function:I'm running the following query to track sources to sinks:
The global
obj2.key
andmap.get("key")
aren't detected, while they are in the local scope. Even theobj = { key: ... }
syntax works in a global variable. It seems like changes to a variable in a global scope aren't tracked?Is there any easy fix I could apply to allow it to track my example code correctly, or is it a deeper underlying issue of
DataFlow
?Beta Was this translation helpful? Give feedback.
All reactions