barebox 0.10.1 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
4
4
 
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
6
6
 
7
+ ## [0.11.0] - 2026-11-11
8
+
9
+ ### Added
10
+
11
+ - Card numbers are found in `gzip` and `deflate` request bodies up to 64 KB of decoded output, and the ask preview shows such bodies decoded. The body as sent is always scanned too, so a false `Content-Encoding` cannot hide a plain card. `br`, `zstd`, raw deflate sent as `deflate`, a gzip member followed by extra bytes, and stacked encodings are not decoded; they are scanned as sent.
12
+
7
13
  ## [0.10.1] - 2026-11-11
8
14
 
9
15
  ### Changed
package/README.md CHANGED
@@ -157,7 +157,7 @@ barebox is a guardrail against mistakes and prompt-injected agents, not a securi
157
157
  - Data in a `GET` URL goes out free: reads are `r`.
158
158
  - The proxy resolves DNS before the gate decides, so data hidden in a hostname can leak.
159
159
  - Model API calls pass with any key, an attacker's included.
160
- - Bodies over 64 KB and compressed bodies are not scanned for card numbers.
160
+ - Bodies over 64 KB are not scanned for card numbers. `gzip` and `deflate` bodies are decoded for the scan (up to 64 KB of output); `br`, `zstd` and bodies that fail to decode are scanned as sent, so a card inside their compressed bytes is not found.
161
161
  - `~/.claude` stays readable: Claude Code's tokens refresh themselves.
162
162
  - Writes over local sockets (a Postgres, a Docker daemon) are not on the undo layer.
163
163
 
@@ -152,7 +152,7 @@ optional single space or dash, first digit 2-6, Luhn-valid) and `paymentPath`
152
152
  (`/checkout`, `/payment`, `/orders`, `/purchase`); both ask in every mode,
153
153
  `trust` included. Nothing is denied because of a card. One exception: a call
154
154
  to a model endpoint (see "Labels and model endpoints") is never card-scanned,
155
- in any mode. Bodies over 64 KB stream through unscanned. The body never enters the gate's action.
155
+ in any mode. `gzip` and `deflate` bodies are decoded for the scan and the ask preview (decoded output capped at 64 KB; the upstream still gets the original bytes); `br` and other encodings are scanned as sent. Bodies over 64 KB stream through unscanned. The body never enters the gate's action.
156
156
 
157
157
  barebox's hold queue is each gate's `humanChannel`: the ask files, `onAsk`
158
158
  and `sbx approve|deny` work as above, and the ask carries the gate's `rule`
@@ -357,7 +357,7 @@ The overlay needs bubblewrap >= 0.11.0 for `--overlay` / `sbx diff|commit|rollba
357
357
  ## What this package does not do
358
358
 
359
359
  Per-API labels cover only the vendors whose rwxmap folders you list in
360
- `labels`; every other request is `http.<METHOD>`. The card-in-body floor checks bodies of 64 KB or less;
360
+ `labels`; every other request is `http.<METHOD>`. The card-in-body floor checks bodies of 64 KB or less (a `gzip` or `deflate` body is checked decoded, up to 64 KB of output; `br`, `zstd`, raw deflate sent as `deflate`, and a gzip member followed by extra bytes are checked as sent, so a card inside their compressed bytes is not found);
361
361
  larger bodies stream through unchecked for cards. The removed `hold.shouldHold`
362
362
  and `hold.match` are gone: pass `gateFor` instead. No Docker, no MCP server, no macOS/Windows —
363
363
  Linux only. POC harnesses live in `poc/` in the repo and never ship.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "barebox",
3
- "version": "0.10.1",
3
+ "version": "0.11.0",
4
4
  "description": "Lightweight local sandbox for AI agents: every write undoable, nothing leaves the machine without passing the doorman.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -0,0 +1,92 @@
1
+ // The body view: the one owner of "what a request body says". It gives the
2
+ // proxy a view of a body that fits the 64 KB working slot, decoded when it is
3
+ // gzip or deflate. The card scan and the ask preview read the view; forwarding
4
+ // never does (the upstream keeps the original compressed bytes).
5
+ //
6
+ // The rule: decoding only adds to what is scanned. The raw bytes as sent are
7
+ // always scanned by the caller; any decoded output (full, truncated, partial
8
+ // before an error) is scanned as well, and the
9
+ // card floor fires if either scan finds a card. No decode outcome can then
10
+ // check a labelled body less than an unlabelled one.
11
+ //
12
+ // Decoding goes through ONE proxy-wide queue (one decoder at a time):
13
+ // compressed request bodies are rare, so the queue costs almost no wait and
14
+ // bounds a burst of bombs to one live decoder (PRD section 9, "M10
15
+ // decisions"). Output stops at 64 KB with destroy(): zlib's maxOutputLength
16
+ // throws instead of truncating, and without destroy() a bomb keeps inflating.
17
+ // A body that does not decode is viewed as sent (partial output first); a gzip
18
+ // member followed by garbage is not decoded by Node's zlib, a known gap (PRD
19
+ // section 10). Raw deflate (no zlib header) sent as `deflate` is not decoded
20
+ // either: it is viewed as sent, a known gap.
21
+ import zlib from 'node:zlib';
22
+
23
+ const CAP = 65536;
24
+ // Looked up by name at call time, so a test can wrap the factory and count decoders.
25
+ const MAKE = { gzip: 'createGunzip', 'x-gzip': 'createGunzip', deflate: 'createInflate' };
26
+
27
+ /**
28
+ * @typedef {object} BodyView
29
+ * @property {Buffer} text the decoded output when the body was decoded, else the body as sent;
30
+ * the caller must also scan the raw bytes
31
+ * @property {'plain'|'decoded'|'truncated'|'unread'} state plain: sent as is; decoded: gzip/deflate fully
32
+ * decoded; truncated: decoding stopped at 64 KB of output; unread: not decoded or broken, `text` is the
33
+ * partial output decoded before the error, else the body as sent
34
+ */
35
+
36
+ /**
37
+ * Decode up to 64 KB of a gzip/deflate body. Resolves on the stream's close,
38
+ * so the queue never starts the next decoder while this one is still alive.
39
+ *
40
+ * @param {Buffer} buf compressed body
41
+ * @param {string} factory zlib factory name
42
+ * @returns {Promise<{ text: Buffer, state: 'decoded'|'truncated'|'unread' }>} decode result
43
+ */
44
+ function decodeRaw(buf, factory) {
45
+ return new Promise((resolve) => {
46
+ const z = /** @type {import('node:zlib').Gunzip} */ (zlib[/** @type {'createGunzip'} */ (factory)]());
47
+ /** @type {Buffer[]} */
48
+ const out = [];
49
+ let n = 0;
50
+ // on error: the partial output if any, else the body as sent
51
+ const partial = () => (out.length ? Buffer.concat(out) : buf);
52
+ /** @type {{ text: Buffer, state: 'decoded'|'truncated'|'unread' }|null} */
53
+ let result = null;
54
+ z.on('data', (/** @type {Buffer} */ c) => {
55
+ if (result) return;
56
+ out.push(c);
57
+ n += c.length;
58
+ if (n >= CAP) {
59
+ result = { text: Buffer.concat(out, CAP), state: 'truncated' };
60
+ z.destroy();
61
+ }
62
+ });
63
+ z.on('end', () => { result ??= { text: Buffer.concat(out), state: 'decoded' }; });
64
+ z.on('error', () => { result ??= { text: partial(), state: 'unread' }; });
65
+ z.on('close', () => resolve(result ?? { text: partial(), state: 'unread' }));
66
+ z.end(buf);
67
+ });
68
+ }
69
+
70
+ // One decoder at a time for the whole proxy; the tail never rejects.
71
+ let tail = Promise.resolve();
72
+
73
+ /**
74
+ * @param {Buffer} buf the body (at most 64 KB as sent)
75
+ * @param {Record<string, string | string[] | undefined>} headers request headers
76
+ * @returns {Promise<BodyView>} the view
77
+ */
78
+ export async function viewBody(buf, headers) {
79
+ const enc = String(headers['content-encoding'] ?? '').trim().toLowerCase();
80
+ let text = buf;
81
+ /** @type {BodyView['state']} */
82
+ let state = 'plain';
83
+ if (enc !== '' && enc !== 'identity') {
84
+ const factory = Object.hasOwn(MAKE, enc) ? MAKE[/** @type {keyof typeof MAKE} */ (enc)] : null;
85
+ if (factory) {
86
+ const p = tail.then(() => decodeRaw(buf, factory));
87
+ tail = p.then(() => {}, () => {});
88
+ ({ text, state } = await p);
89
+ } else state = 'unread';
90
+ }
91
+ return { text, state };
92
+ }
package/src/proxy.js CHANGED
@@ -20,6 +20,7 @@ import path from 'node:path';
20
20
  import { ensureCA, certFor, HOST_RE } from './certs.js';
21
21
  import { createCardScanner } from './cardscan.js';
22
22
  import { ASK_BODY_LIMIT, DEFAULT_HOLD_MAX_BODY, bodyPreview, createHolds } from './holds.js';
23
+ import { viewBody } from './bodyview.js';
23
24
  import { claimRuntime, ensurePrivateDir, runtimePaths } from './runtime.js';
24
25
  import { paymentPath, toolFor, buildTools, modelTools, modelList, emptyMatcher } from './policy.js';
25
26
  import { checkUpstream } from './wall.js';
@@ -502,6 +503,7 @@ export function createProxy(opts) {
502
503
  * @property {number|null} status HTTP status the channel wants for a denial (null: 403)
503
504
  * @property {string|null} heldId id of the held body file, once spooled
504
505
  * @property {((reason: string) => void)|null} endAsk end a not-yet-parked ask as a denial
506
+ * @property {Buffer|null} decodedHead copy of the first 4 KB of a gzip/deflate body, decoded (the ask preview)
505
507
  */
506
508
 
507
509
  /**
@@ -608,7 +610,7 @@ export function createProxy(opts) {
608
610
  // Parked: the tunnel stays open until the ask channel decides.
609
611
  // null: the reservation already expired and onExpire refused.
610
612
  parked = queue.add({ agent, method: ctx.method, url: ctx.url,
611
- preview: bodyPreview(ctx.tee.head), bodyBytes: ctx.tee.bytes,
613
+ preview: bodyPreview(ctx.decodedHead ?? ctx.tee.head), bodyBytes: ctx.tee.bytes,
612
614
  sock, note: event.rule ?? 'ask', letter: event.rwxLetter, finish,
613
615
  slot, arrivedAt: slot.arrivedAt }) !== null;
614
616
  };
@@ -747,14 +749,16 @@ export function createProxy(opts) {
747
749
  /** @type {RequestCtx} */
748
750
  const ctx = { agent, method, url: urlStr, headers: { ...creq.headers }, creq, cres, tee,
749
751
  hasBody, chunks: [], over: false, freeWork, asked: false, status: null, heldId: null,
750
- endAsk: null };
752
+ endAsk: null, decodedHead: null };
751
753
  // A model call is never card-scanned (user decision, Nov 2); the floor keeps every other request.
752
754
  const tool = toolFor(matcher, tools, method, urlStr, modelEndpoints);
753
755
  let cardLike = false;
754
756
  if (hasBody) {
755
757
  if (await readWorkBody(ctx)) return;
756
758
  // Past 64 KB the body streams on unscanned (known gap).
757
- if (!ctx.over && !models.has(tool)) cardLike = createCardScanner().push(Buffer.concat(ctx.chunks));
759
+ if (!ctx.over && !models.has(tool)) {
760
+ cardLike = await scanBody(ctx);
761
+ }
758
762
  }
759
763
  // The body never goes in the action: bareguard writes the action to its audit file.
760
764
  const action = { type: 'net', tool, url: urlStr, method, agent,
@@ -850,6 +854,23 @@ export function createProxy(opts) {
850
854
  return false;
851
855
  }
852
856
 
857
+ /**
858
+ * Scan the working slice of the body for card numbers and keep a copy of the
859
+ * decoded head as the ask preview. Its own frame, so no reference to the
860
+ * body or its decoded view outlives the scan (same reason as `readWorkBody`).
861
+ *
862
+ * @param {RequestCtx} ctx request context, chunks already read
863
+ * @returns {Promise<boolean>} true when the body looks like a card number
864
+ */
865
+ async function scanBody(ctx) {
866
+ const body = Buffer.concat(ctx.chunks);
867
+ const view = await viewBody(body, ctx.headers);
868
+ // A copy, never a subarray: a subarray would keep the whole decoded buffer alive.
869
+ if (view.state === 'decoded' || view.state === 'truncated') ctx.decodedHead = Buffer.from(view.text.subarray(0, ASK_BODY_LIMIT));
870
+ // The raw bytes are always scanned; decoded output is scanned as well (one scanner per buffer).
871
+ return createCardScanner().push(body) || (view.text !== body && createCardScanner().push(view.text));
872
+ }
873
+
853
874
  /**
854
875
  * Read a paused request's body until it ends or passes `limit` bytes
855
876
  * (then pause again, keeping everything read so far in `chunks`).
@@ -0,0 +1,19 @@
1
+ /**
2
+ * @param {Buffer} buf the body (at most 64 KB as sent)
3
+ * @param {Record<string, string | string[] | undefined>} headers request headers
4
+ * @returns {Promise<BodyView>} the view
5
+ */
6
+ export function viewBody(buf: Buffer, headers: Record<string, string | string[] | undefined>): Promise<BodyView>;
7
+ export type BodyView = {
8
+ /**
9
+ * the decoded output when the body was decoded, else the body as sent;
10
+ * the caller must also scan the raw bytes
11
+ */
12
+ text: Buffer;
13
+ /**
14
+ * plain: sent as is; decoded: gzip/deflate fully
15
+ * decoded; truncated: decoding stopped at 64 KB of output; unread: not decoded or broken, `text` is the
16
+ * partial output decoded before the error, else the body as sent
17
+ */
18
+ state: "plain" | "decoded" | "truncated" | "unread";
19
+ };