Detect circular module imports to prevent stack overflow

jq used to recurse without bound on mutual or self-referential
`import` declarations, exhausting the stack. Track each library's
load state with a `loading` flag set before its dependencies are
processed; a recursive reference to an in-progress library now
reports "circular import of X".

Fixes CVE-2026-44777.
This commit is contained in:
itchyny committed 2026-05-11 20:41:38 +09:00
1 parent 9d223f153c
commit f58787c418
6 files changed
+70 -20

No files matched your search

+2
View File
@@ -0,0 +1,2 @@
import "cycle_b" as b;
def f: null;
+2
View File
@@ -0,0 +1,2 @@
import "cycle_a" as a;
def f: null;
+2
View File
@@ -0,0 +1,2 @@
import "cycle_self" as s;
def f: null;
+23
View File
@@ -382,17 +382,40 @@ if ! HOME="$mods/home2" $VALGRIND $Q $JQ -n 'include "g"; empty'; then
exit 1
fi
(
cd "$JQBASEDIR" # so that relative library paths are guaranteed correct
if ! $VALGRIND $Q $JQ -L ./tests/modules -ne 'import "test_bind_order" as check; check::check==true'; then
echo "Issue #817 regression?" 1>&2
exit 1
fi
)
(
cd "$JQBASEDIR"
if ! $VALGRIND $Q $JQ -L tests/modules -ne 'import "test_bind_order" as check; check::check==true'; then
echo "Issue #817 regression?" 1>&2
exit 1
fi
)
# CVE-2026-44777: Circular imports should be detected
if $VALGRIND $JQ -L "$mods" -ne 'import "cycle_a" as a; null' 2> $d/out; then
echo "Mutual import should be rejected" 1>&2
exit 1
fi
if ! grep -q "circular import" $d/out; then
echo "Expected circular import error" 1>&2
exit 1
fi
if $VALGRIND $JQ -L "$mods" -ne 'import "cycle_self" as s; null' 2> $d/out; then
echo "Self import should be rejected" 1>&2
exit 1
fi
if ! grep -q "circular import" $d/out; then
echo "Expected circular import error" 1>&2
exit 1
fi
## Halt