| 1 | package agent |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "testing" |
| 6 | |
| 7 | "reasonix/internal/jobs" |
| 8 | "reasonix/internal/runtimepolicy" |
| 9 | "reasonix/internal/sessiontemp" |
| 10 | ) |
| 11 | |
| 12 | // CodeQL's generic context model propagates the raw input through any Value |
| 13 | // lookup. These distinct private key types must never alias privileged values. |
| 14 | func TestRawInputCannotReplacePathOwningContextValues(t *testing.T) { |
| 15 | manager := &jobs.Manager{} |
| 16 | temp := sessiontemp.NewWithRoot(t.TempDir()) |
| 17 | t.Cleanup(temp.Release) |
| 18 | trusted := func(ctx context.Context) context.Context { |
| 19 | ctx = WithParentSession(ctx, "trusted-session") |
| 20 | ctx = jobs.WithManager(ctx, manager) |
| 21 | ctx = jobs.WithSession(ctx, "trusted-session") |
| 22 | return sessiontemp.WithManager(ctx, temp) |
| 23 | } |
| 24 | for _, raw := range []string{"../../outside", `/absolute/path`, `C:\outside`, "trusted-session", ""} { |
| 25 | for _, ctx := range []context.Context{ |
| 26 | WithRawUserInput(trusted(context.Background()), raw), |
| 27 | trusted(WithRawUserInput(context.Background(), raw)), |
| 28 | } { |
| 29 | ctx = WithResponseFormat(ctx, raw) |
| 30 | ctx = runtimepolicy.WithContext(ctx, runtimepolicy.Constraints{Notes: []string{raw}, AllowedChecks: []string{raw}}) |
| 31 | if RawUserInput(ctx, "fallback") != raw || ParentSession(ctx) != "trusted-session" || jobs.SessionFromContext(ctx) != "trusted-session" { |
| 32 | t.Fatal("raw input crossed the parent-session key boundary") |
| 33 | } |
| 34 | if got, ok := jobs.FromContext(ctx); !ok || got != manager { |
| 35 | t.Fatal("raw input replaced the jobs manager") |
| 36 | } |
| 37 | if sessiontemp.FromContext(ctx) != temp { |
| 38 | t.Fatal("raw input replaced the temporary-directory manager") |
| 39 | } |
| 40 | } |
| 41 | ctx := WithRawUserInput(context.Background(), raw) |
| 42 | if ParentSession(ctx) != "" || jobs.SessionFromContext(ctx) != "" || sessiontemp.FromContext(ctx) != nil { |
| 43 | t.Fatal("raw input fabricated a privileged context value") |
| 44 | } |
| 45 | if _, ok := jobs.FromContext(ctx); ok { |
| 46 | t.Fatal("raw input fabricated a jobs manager") |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 |