Skip to content

fix: replace abort-on-invalid-label with graceful bounds check#1416

Closed
santhsecurity wants to merge 1 commit intoquickjs-ng:masterfrom
santhsecurity:fix/update-label-assert-to-bounds-check
Closed

fix: replace abort-on-invalid-label with graceful bounds check#1416
santhsecurity wants to merge 1 commit intoquickjs-ng:masterfrom
santhsecurity:fix/update-label-assert-to-bounds-check

Conversation

@santhsecurity
Copy link
Copy Markdown

The update_label function uses assert() to validate the label index, which calls abort() on failure. In embedding contexts where QuickJS executes untrusted JavaScript (sandboxes, URL detonation engines, server-side JS), crafted scripts can trigger the assertion through pathological code patterns that produce invalid label indices during bytecode optimization.

This turns a logic error into a process-killing SIGABRT, which is a denial-of-service vector for any application embedding QuickJS.

The fix replaces the fatal assert with a bounds check that returns -1. Callers that check the return value (e.g., the if (update_label(...) > 0) pattern in resolve_labels) already handle -1 correctly. Callers that ignore the return value are unaffected. An a -1 return on an invalid label is strictly better than a process abort.

The second assert (ref_count >= 0) is similarly replaced with a clamp to zero, preventing underflow from cascading into further undefined behavior.

Discovered during adversarial testing of the Sear URL detonation engine (https://santh.io/sear), which executes untrusted JavaScript from phishing pages in a QuickJS sandbox.

The update_label function uses assert() to validate the label index,
which calls abort() on failure. In embedding contexts where QuickJS
executes untrusted JavaScript (sandboxes, URL detonation engines,
server-side JS), crafted scripts can trigger the assertion through
pathological code patterns that produce invalid label indices during
bytecode optimization.

This turns a logic error into a process-killing SIGABRT — a
denial-of-service vector for any application embedding QuickJS.

The fix replaces the fatal assert with a bounds check that returns -1.
Callers that check the return value (e.g., the if (update_label(...) > 0)
pattern in resolve_labels) already handle -1 correctly. Callers that
ignore the return value are unaffected — a -1 return on an invalid
label is strictly better than a process abort.

The second assert (ref_count >= 0) is similarly replaced with a clamp
to zero, preventing underflow from cascading into further undefined
behavior.

Discovered during adversarial testing of the Sear URL detonation engine
(https://santh.io/sear), which executes untrusted JavaScript from
phishing pages in a QuickJS sandbox.
@saghul
Copy link
Copy Markdown
Contributor

saghul commented Mar 20, 2026

I'm not sure about this one. The whole bytecode reader was not designed for untrusted input. You can patch some calls but I think there are way more that would surface with specially crafter payloads.

Why not evaluate JS code rather than bytecode?

@bnoordhuis
Copy link
Copy Markdown
Contributor

crafted scripts can trigger the assertion through pathological code patterns that produce invalid label indices during bytecode optimization

Assuming that's actually possible, then that's a bug and it needs to be fixed, not worked around.

@bnoordhuis
Copy link
Copy Markdown
Contributor

I don't think we'll be merging this so I'm going to close it. Thanks anyway for the pull request.

@bnoordhuis bnoordhuis closed this Mar 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants