|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { Bash } from "../../Bash.js"; |
| 3 | + |
| 4 | +// Real jq accepts JSON containing literal control characters inside strings, |
| 5 | +// even though RFC 8259 forbids them. Real-world inputs (webhooks, copy-paste |
| 6 | +// from terminals, log lines piped through `jq`) frequently contain unescaped |
| 7 | +// tabs and newlines, so matching jq's permissive behavior here avoids |
| 8 | +// surprising failures when our jq is dropped into existing pipelines. |
| 9 | +describe("jq permissive control chars in JSON strings", () => { |
| 10 | + it("accepts a literal newline inside a string value", async () => { |
| 11 | + const env = new Bash({ |
| 12 | + files: { "/payload.json": '{"body":"first\nsecond"}\n' }, |
| 13 | + }); |
| 14 | + |
| 15 | + const result = await env.exec("jq -r '.body' /payload.json"); |
| 16 | + |
| 17 | + expect(result.stderr).toBe(""); |
| 18 | + expect(result.stdout).toBe("first\nsecond\n"); |
| 19 | + expect(result.exitCode).toBe(0); |
| 20 | + }); |
| 21 | + |
| 22 | + it("accepts a literal tab inside a string value", async () => { |
| 23 | + const env = new Bash({ |
| 24 | + files: { "/payload.json": '{"body":"col1\tcol2"}\n' }, |
| 25 | + }); |
| 26 | + |
| 27 | + const result = await env.exec("jq -r '.body' /payload.json"); |
| 28 | + |
| 29 | + expect(result.stderr).toBe(""); |
| 30 | + expect(result.stdout).toBe("col1\tcol2\n"); |
| 31 | + expect(result.exitCode).toBe(0); |
| 32 | + }); |
| 33 | + |
| 34 | + it("accepts a literal carriage return inside a string value", async () => { |
| 35 | + const env = new Bash({ |
| 36 | + files: { "/payload.json": '{"body":"a\rb"}\n' }, |
| 37 | + }); |
| 38 | + |
| 39 | + const result = await env.exec("jq -r '.body' /payload.json"); |
| 40 | + |
| 41 | + expect(result.stderr).toBe(""); |
| 42 | + expect(result.stdout).toBe("a\rb\n"); |
| 43 | + expect(result.exitCode).toBe(0); |
| 44 | + }); |
| 45 | + |
| 46 | + it("preserves a real escape sequence next to a literal control char", async () => { |
| 47 | + const env = new Bash({ |
| 48 | + files: { "/payload.json": '{"body":"line1\\nline2\nline3"}\n' }, |
| 49 | + }); |
| 50 | + |
| 51 | + const result = await env.exec("jq -r '.body' /payload.json"); |
| 52 | + |
| 53 | + expect(result.stderr).toBe(""); |
| 54 | + expect(result.stdout).toBe("line1\nline2\nline3\n"); |
| 55 | + expect(result.exitCode).toBe(0); |
| 56 | + }); |
| 57 | + |
| 58 | + it("does not rewrite control characters that appear outside strings", async () => { |
| 59 | + const env = new Bash({ |
| 60 | + files: { "/payload.json": '{\n "a": 1,\n "b": 2\n}\n' }, |
| 61 | + }); |
| 62 | + |
| 63 | + const result = await env.exec("jq -c '.' /payload.json"); |
| 64 | + |
| 65 | + expect(result.stderr).toBe(""); |
| 66 | + expect(result.stdout).toBe('{"a":1,"b":2}\n'); |
| 67 | + expect(result.exitCode).toBe(0); |
| 68 | + }); |
| 69 | + |
| 70 | + it("handles concatenated JSON values where one contains a literal newline", async () => { |
| 71 | + const env = new Bash({ |
| 72 | + files: { |
| 73 | + "/stream.json": '{"a":"x\ny"}{"a":"z"}\n', |
| 74 | + }, |
| 75 | + }); |
| 76 | + |
| 77 | + const result = await env.exec("jq -r '.a' /stream.json"); |
| 78 | + |
| 79 | + expect(result.stderr).toBe(""); |
| 80 | + expect(result.stdout).toBe("x\ny\nz\n"); |
| 81 | + expect(result.exitCode).toBe(0); |
| 82 | + }); |
| 83 | +}); |
0 commit comments