|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { BashEnv } from "../../BashEnv.js"; |
| 3 | + |
| 4 | +/** |
| 5 | + * AWK Execution Limits Tests |
| 6 | + * |
| 7 | + * These tests verify that awk commands cannot cause runaway compute. |
| 8 | + * AWK programs should complete in bounded time regardless of input. |
| 9 | + * |
| 10 | + * IMPORTANT: All tests should complete quickly (<1s each). |
| 11 | + */ |
| 12 | + |
| 13 | +describe("AWK Execution Limits", () => { |
| 14 | + describe("infinite loop protection", () => { |
| 15 | + it("should protect against while(1) infinite loop", async () => { |
| 16 | + const env = new BashEnv(); |
| 17 | + const result = await env.exec( |
| 18 | + `echo "test" | awk 'BEGIN { while(1) print "x" }'`, |
| 19 | + ); |
| 20 | + |
| 21 | + // Should not hang - awk should have iteration limits |
| 22 | + expect(result.stderr.length).toBeGreaterThan(0); |
| 23 | + expect(result.exitCode).not.toBe(0); |
| 24 | + }); |
| 25 | + |
| 26 | + it("should protect against for loop with always-true condition", async () => { |
| 27 | + const env = new BashEnv(); |
| 28 | + const result = await env.exec( |
| 29 | + `echo "test" | awk 'BEGIN { for(i=0; 1; i++) print "x" }'`, |
| 30 | + ); |
| 31 | + |
| 32 | + expect(result.stderr.length).toBeGreaterThan(0); |
| 33 | + expect(result.exitCode).not.toBe(0); |
| 34 | + }); |
| 35 | + |
| 36 | + // TODO: for(;;) requires special parsing - skip for now |
| 37 | + it.skip("should protect against for(;;) infinite loop", async () => { |
| 38 | + const env = new BashEnv(); |
| 39 | + const result = await env.exec( |
| 40 | + `echo "test" | awk 'BEGIN { for(;;) print "x" }'`, |
| 41 | + ); |
| 42 | + |
| 43 | + expect(result.stderr.length).toBeGreaterThan(0); |
| 44 | + expect(result.exitCode).not.toBe(0); |
| 45 | + }); |
| 46 | + |
| 47 | + // TODO: do-while not implemented in our awk |
| 48 | + it.skip("should protect against do-while infinite loop", async () => { |
| 49 | + const env = new BashEnv(); |
| 50 | + const result = await env.exec( |
| 51 | + `echo "test" | awk 'BEGIN { do { print "x" } while(1) }'`, |
| 52 | + ); |
| 53 | + |
| 54 | + expect(result.stderr.length).toBeGreaterThan(0); |
| 55 | + expect(result.exitCode).not.toBe(0); |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + // TODO: User-defined functions not implemented in our awk |
| 60 | + describe.skip("recursion protection", () => { |
| 61 | + it("should protect against recursive function calls", async () => { |
| 62 | + const env = new BashEnv(); |
| 63 | + const result = await env.exec( |
| 64 | + `echo "test" | awk 'function f() { f() } BEGIN { f() }'`, |
| 65 | + ); |
| 66 | + |
| 67 | + expect(result.stderr.length).toBeGreaterThan(0); |
| 68 | + expect(result.exitCode).not.toBe(0); |
| 69 | + }); |
| 70 | + |
| 71 | + it("should protect against mutual recursion", async () => { |
| 72 | + const env = new BashEnv(); |
| 73 | + const result = await env.exec( |
| 74 | + `echo "test" | awk 'function a() { b() } function b() { a() } BEGIN { a() }'`, |
| 75 | + ); |
| 76 | + |
| 77 | + expect(result.stderr.length).toBeGreaterThan(0); |
| 78 | + expect(result.exitCode).not.toBe(0); |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + describe("output size limits", () => { |
| 83 | + it("should limit output from print in loop", async () => { |
| 84 | + const env = new BashEnv(); |
| 85 | + const result = await env.exec( |
| 86 | + `echo "test" | awk 'BEGIN { for(i=0; i<1000000; i++) print "x" }'`, |
| 87 | + ); |
| 88 | + |
| 89 | + // Should either error or limit output |
| 90 | + if (result.exitCode === 0) { |
| 91 | + expect(result.stdout.length).toBeLessThan(10_000_000); |
| 92 | + } |
| 93 | + }); |
| 94 | + |
| 95 | + it("should limit string concatenation growth", async () => { |
| 96 | + const env = new BashEnv(); |
| 97 | + const result = await env.exec( |
| 98 | + `echo "test" | awk 'BEGIN { s="x"; for(i=0; i<30; i++) s=s s; print length(s) }'`, |
| 99 | + ); |
| 100 | + |
| 101 | + // Should either error or limit string size |
| 102 | + expect(result.exitCode).toBeDefined(); |
| 103 | + }); |
| 104 | + }); |
| 105 | + |
| 106 | + describe("array limits", () => { |
| 107 | + it("should handle large array creation", async () => { |
| 108 | + const env = new BashEnv(); |
| 109 | + const result = await env.exec( |
| 110 | + `echo "test" | awk 'BEGIN { for(i=0; i<100000; i++) a[i]=i; print length(a) }'`, |
| 111 | + ); |
| 112 | + |
| 113 | + // Should complete without hanging |
| 114 | + expect(result.exitCode).toBeDefined(); |
| 115 | + }); |
| 116 | + }); |
| 117 | + |
| 118 | + // TODO: ReDoS protection requires a different regex engine or timeout |
| 119 | + // JavaScript's regex engine is vulnerable to pathological patterns |
| 120 | + describe.skip("regex limits", () => { |
| 121 | + it("should handle pathological regex patterns", async () => { |
| 122 | + const env = new BashEnv(); |
| 123 | + // ReDoS-style pattern |
| 124 | + const result = await env.exec( |
| 125 | + `echo "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaab" | awk '/^(a+)+$/'`, |
| 126 | + ); |
| 127 | + |
| 128 | + // Should complete quickly |
| 129 | + expect(result.exitCode).toBeDefined(); |
| 130 | + }); |
| 131 | + }); |
| 132 | + |
| 133 | + describe("getline limits", () => { |
| 134 | + it("should not hang on getline in loop", async () => { |
| 135 | + const env = new BashEnv(); |
| 136 | + const result = await env.exec( |
| 137 | + `echo "test" | awk '{ while((getline line < "/dev/zero") > 0) print line }'`, |
| 138 | + ); |
| 139 | + |
| 140 | + // Should either error or be handled safely |
| 141 | + expect(result.exitCode).toBeDefined(); |
| 142 | + }); |
| 143 | + }); |
| 144 | +}); |
0 commit comments