Skip to content

fix(jq): bound object-key/index error previews with capped rendering - #2697

Merged
chaliy merged 1 commit into
mainfrom
yolop/security-jq-object-key-errors
Oct 12, 2026
Merged

chaliy merged 1 commit into
mainfrom
yolop/security-jq-object-key-errors

Conversation

@chaliy

@chaliy chaliy commented Oct 12, 2026

Copy link
Copy Markdown
Contributor

What

Error-preview rendering in crates/bashkit/src/builtins/jq/jaq_json/jqmsg.rs materialized huge values before truncating:

  • dump_trunc called v.to_string() (full render, outside the memory meter) and truncated after.
  • index interpolated the raw key string (format!("string {}", r)) into cannot-index errors.

A 200 KB string key produced a ~200 KB error string; object keys built by filters (e.g. {([range(0;200000)]): 1}) transiently rendered megabytes.

Fix

  • dump_trunc renders through a 15-byte capped fmt::Write sink, then applies the legacy 11-bytes+... cut. Small-value output is byte-identical.
  • The cannot-index string branch previews via dump_trunc.
  • Regression tests: huge object-key and huge string-index errors stay under 4 KiB (index case failed pre-fix at 200,037 bytes).
  • Knowledge: noted the capped-preview rule in knowledge/runtimes/jaq-json-vendor.md.

Proof

  • cargo test -p bashkit --lib --features jq builtins::jq: 350 passed.
  • cargo fmt --check, cargo clippy --features jq -- -D warnings: clean.
  • E2E unchanged for short keys (Cannot index array with string "a", Cannot use array ([0,1,2,3,4,...) as object key); huge key error is 74 bytes.
  • just check-okf: conformant.

@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
bashkit 68a1bc6 Commit Preview URL

Branch Preview URL
Oct 12 2026, 04:13 AM

@chaliy
chaliy merged commit 47f8666 into main Oct 12, 2026
46 checks passed
@chaliy
chaliy deleted the yolop/security-jq-object-key-errors branch October 12, 2026 05:05
chaliy added a commit that referenced this pull request Oct 12, 2026
…ostile values (#2699)

## What changed

Bounds the CPU cost of jq math/index error previews, not just their
allocation.

## Why

Follow-up to #2697: that PR capped preview *allocation* with the
`Capped` sink, but the sink swallowed over-budget writes with `Ok(())`,
so `Val`'s `Display` (the `?`-laden `format_val!`/`write_seq!`
machinery) kept expanding the full hostile value just to discard the
output. For the finding's shared-structure input (`reduce range(0; 40)
as $i (1; [., .]) | . + 1`, a ~2^40-element expansion on type-mismatch),
the error path still walked the whole expansion.

## Before / After (production entry point, `timeout 120`, direct child
timing)

Before (by source inspection + failing test
`preview_writer_aborts_once_over_budget`): `write!` over a 200 KB value
returned `Ok` after walking all of it.

After:

```
$ time ./target/debug/bashkit -c 'jq -n '\''reduce range(0; 40) as $i (1; [., .]) | . + 1'\'''
jq: error (at <unknown>): array ([[[[[[[[[[[...) and number (1) cannot be added
exit=5, 80-byte stderr, 0.012s wall
```

Exit code 5 matches real jq's runtime-error exit; small-value wording is
unchanged (`math_errors_use_jq_wording` passes).

Also fixed: over-budget previews that aborted before filling the buffer
(multibyte char straddling the cut) previously returned without the
`...` marker; the marker is now unconditional on over-budget output.
Small-value output is byte-identical.

## Validation

- `cargo test -p bashkit --lib --features jq builtins::jq`: 357 passed,
0 failed (6 new tests: early-abort, exact-fit, just-over, multibyte,
recovery, huge-operand session integration)
- `cargo fmt --check`, `cargo clippy --all-targets --features jq`: clean
- `just check-okf`: conforms; knowledge note updated in
`knowledge/runtimes/jaq-json-vendor.md`

## Checklist
- [x] Tests added or updated
- [x] Backward compatibility considered (internal code; error-preview
bytes for small values unchanged)
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.

1 participant