@@ -0,0 +1,317 @@ |
| 1 | +// SPDX-License-Identifier: AGPL-3.0-or-later |
| 2 | + |
| 3 | +package expr_test |
| 4 | + |
| 5 | +import ( |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/tenseleyFlow/shithub/internal/actions/expr" |
| 10 | +) |
| 11 | + |
| 12 | +// evalString is the test helper that lex + parse + eval in one shot. |
| 13 | +func evalString(t *testing.T, src string, ctx *expr.Context) (expr.Value, error) { |
| 14 | + t.Helper() |
| 15 | + toks, err := expr.Lex(src) |
| 16 | + if err != nil { |
| 17 | + return expr.Value{}, err |
| 18 | + } |
| 19 | + ast, err := expr.Parse(toks) |
| 20 | + if err != nil { |
| 21 | + return expr.Value{}, err |
| 22 | + } |
| 23 | + return expr.Eval(ast, ctx) |
| 24 | +} |
| 25 | + |
| 26 | +func defaultContext() *expr.Context { |
| 27 | + return &expr.Context{ |
| 28 | + Secrets: map[string]string{"MY_SECRET": "shh"}, |
| 29 | + Vars: map[string]string{"REGION": "us-east-1"}, |
| 30 | + Env: map[string]string{"GREETING": "hello"}, |
| 31 | + Shithub: expr.ShithubContext{ |
| 32 | + RunID: "42", |
| 33 | + SHA: "deadbeef", |
| 34 | + Ref: "refs/heads/trunk", |
| 35 | + Actor: "alice", |
| 36 | + Event: map[string]any{ |
| 37 | + "pull_request": map[string]any{ |
| 38 | + "title": "feat: add foo", |
| 39 | + "head": map[string]any{ |
| 40 | + "ref": "feat/foo", |
| 41 | + }, |
| 42 | + }, |
| 43 | + "head_commit": map[string]any{ |
| 44 | + "message": "WIP: testing", |
| 45 | + }, |
| 46 | + }, |
| 47 | + }, |
| 48 | + Untrusted: expr.DefaultUntrusted(), |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +func TestEval_LiteralAndRefs(t *testing.T) { |
| 53 | + t.Parallel() |
| 54 | + ctx := defaultContext() |
| 55 | + cases := []struct { |
| 56 | + src string |
| 57 | + want string |
| 58 | + }{ |
| 59 | + {`'hello'`, "hello"}, |
| 60 | + {`secrets.MY_SECRET`, "shh"}, |
| 61 | + {`vars.REGION`, "us-east-1"}, |
| 62 | + {`env.GREETING`, "hello"}, |
| 63 | + {`shithub.run_id`, "42"}, |
| 64 | + {`shithub.sha`, "deadbeef"}, |
| 65 | + {`shithub.actor`, "alice"}, |
| 66 | + } |
| 67 | + for _, tc := range cases { |
| 68 | + t.Run(tc.src, func(t *testing.T) { |
| 69 | + t.Parallel() |
| 70 | + v, err := evalString(t, tc.src, ctx) |
| 71 | + if err != nil { |
| 72 | + t.Fatalf("eval: %v", err) |
| 73 | + } |
| 74 | + if v.S != tc.want { |
| 75 | + t.Errorf("got %q, want %q", v.S, tc.want) |
| 76 | + } |
| 77 | + }) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +func TestEval_TaintFromEvent(t *testing.T) { |
| 82 | + t.Parallel() |
| 83 | + ctx := defaultContext() |
| 84 | + v, err := evalString(t, `shithub.event.pull_request.title`, ctx) |
| 85 | + if err != nil { |
| 86 | + t.Fatalf("eval: %v", err) |
| 87 | + } |
| 88 | + if v.S != "feat: add foo" { |
| 89 | + t.Errorf("got %q", v.S) |
| 90 | + } |
| 91 | + if !v.Tainted { |
| 92 | + t.Fatal("expected Tainted=true on shithub.event.* reference (load-bearing for S41d injection prevention)") |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +func TestEval_TaintNotFromTrustedSources(t *testing.T) { |
| 97 | + t.Parallel() |
| 98 | + ctx := defaultContext() |
| 99 | + for _, src := range []string{ |
| 100 | + `secrets.MY_SECRET`, |
| 101 | + `vars.REGION`, |
| 102 | + `env.GREETING`, |
| 103 | + `shithub.run_id`, |
| 104 | + `shithub.sha`, |
| 105 | + `shithub.actor`, |
| 106 | + } { |
| 107 | + t.Run(src, func(t *testing.T) { |
| 108 | + t.Parallel() |
| 109 | + v, err := evalString(t, src, ctx) |
| 110 | + if err != nil { |
| 111 | + t.Fatalf("eval: %v", err) |
| 112 | + } |
| 113 | + if v.Tainted { |
| 114 | + t.Fatalf("expected Tainted=false on %s; got Tainted=true (would falsely trip S41d guard)", src) |
| 115 | + } |
| 116 | + }) |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +func TestEval_TaintPropagatesThroughBinary(t *testing.T) { |
| 121 | + t.Parallel() |
| 122 | + ctx := defaultContext() |
| 123 | + // 'WIP' compared to a tainted value → result tainted. |
| 124 | + v, err := evalString(t, `shithub.event.pull_request.title == 'WIP'`, ctx) |
| 125 | + if err != nil { |
| 126 | + t.Fatalf("eval: %v", err) |
| 127 | + } |
| 128 | + if !v.Tainted { |
| 129 | + t.Fatal("equality with a tainted operand must be tainted") |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +func TestEval_TaintPropagatesThroughFunction(t *testing.T) { |
| 134 | + t.Parallel() |
| 135 | + ctx := defaultContext() |
| 136 | + v, err := evalString(t, `contains(shithub.event.head_commit.message, 'WIP')`, ctx) |
| 137 | + if err != nil { |
| 138 | + t.Fatalf("eval: %v", err) |
| 139 | + } |
| 140 | + if !v.B { |
| 141 | + t.Errorf("expected contains() to return true; got false") |
| 142 | + } |
| 143 | + if !v.Tainted { |
| 144 | + t.Fatal("contains() with a tainted operand must be tainted") |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +func TestEval_AllowedFunctions(t *testing.T) { |
| 149 | + t.Parallel() |
| 150 | + ctx := defaultContext() |
| 151 | + cases := []struct { |
| 152 | + src string |
| 153 | + want bool |
| 154 | + }{ |
| 155 | + {`contains('hello world', 'world')`, true}, |
| 156 | + {`contains('hello', 'WORLD')`, false}, |
| 157 | + {`startsWith('refs/heads/release/v1', 'refs/heads/release/')`, true}, |
| 158 | + {`endsWith('foo.tar.gz', '.gz')`, true}, |
| 159 | + {`success()`, true}, // JobStatus zero-value: not failed, not cancelled |
| 160 | + {`failure()`, false}, |
| 161 | + {`always()`, true}, |
| 162 | + {`cancelled()`, false}, |
| 163 | + {`!success()`, false}, |
| 164 | + {`true && false`, false}, |
| 165 | + {`true || false`, true}, |
| 166 | + {`'a' == 'a'`, true}, |
| 167 | + {`'a' != 'b'`, true}, |
| 168 | + } |
| 169 | + for _, tc := range cases { |
| 170 | + t.Run(tc.src, func(t *testing.T) { |
| 171 | + t.Parallel() |
| 172 | + v, err := evalString(t, tc.src, ctx) |
| 173 | + if err != nil { |
| 174 | + t.Fatalf("eval: %v", err) |
| 175 | + } |
| 176 | + if v.B != tc.want { |
| 177 | + t.Errorf("got %v, want %v", v.B, tc.want) |
| 178 | + } |
| 179 | + }) |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +func TestEval_DisallowedFunctionFails(t *testing.T) { |
| 184 | + t.Parallel() |
| 185 | + ctx := defaultContext() |
| 186 | + cases := []string{ |
| 187 | + `fromJSON('{}')`, |
| 188 | + `hashFiles('**/*.go')`, |
| 189 | + `toJSON('foo')`, |
| 190 | + `format('hello {0}', 'world')`, |
| 191 | + } |
| 192 | + for _, src := range cases { |
| 193 | + t.Run(src, func(t *testing.T) { |
| 194 | + t.Parallel() |
| 195 | + _, err := evalString(t, src, ctx) |
| 196 | + if err == nil { |
| 197 | + t.Fatal("expected eval error for disallowed function") |
| 198 | + } |
| 199 | + if !strings.Contains(err.Error(), "unknown function") { |
| 200 | + t.Errorf("expected 'unknown function' error, got: %v", err) |
| 201 | + } |
| 202 | + }) |
| 203 | + } |
| 204 | +} |
| 205 | + |
| 206 | +func TestEval_DisallowedNamespaceFails(t *testing.T) { |
| 207 | + t.Parallel() |
| 208 | + ctx := defaultContext() |
| 209 | + // Each of these would let workflows reach into a namespace we |
| 210 | + // don't want to support in v1. Some fail at lex (when the |
| 211 | + // identifier shape isn't lex-valid Go-ish, e.g. `go-version`), |
| 212 | + // some at eval. Both are correct rejections — neither lets |
| 213 | + // the workflow author smuggle a value through. |
| 214 | + cases := []string{ |
| 215 | + `runner.os`, |
| 216 | + `steps.foo.outputs.bar`, |
| 217 | + `needs.lint.result`, |
| 218 | + `matrix.versions`, |
| 219 | + `inputs.foo`, |
| 220 | + } |
| 221 | + for _, src := range cases { |
| 222 | + t.Run(src, func(t *testing.T) { |
| 223 | + t.Parallel() |
| 224 | + _, err := evalString(t, src, ctx) |
| 225 | + if err == nil { |
| 226 | + t.Fatal("expected eval error for disallowed namespace") |
| 227 | + } |
| 228 | + if !strings.Contains(err.Error(), "unknown namespace") { |
| 229 | + t.Errorf("expected 'unknown namespace' error, got: %v", err) |
| 230 | + } |
| 231 | + }) |
| 232 | + } |
| 233 | +} |
| 234 | + |
| 235 | +func TestEval_MissingSecretIsError(t *testing.T) { |
| 236 | + t.Parallel() |
| 237 | + ctx := defaultContext() |
| 238 | + _, err := evalString(t, `secrets.NOT_BOUND`, ctx) |
| 239 | + if err == nil { |
| 240 | + t.Fatal("expected error for unbound secret") |
| 241 | + } |
| 242 | + if !strings.Contains(err.Error(), "not bound") { |
| 243 | + t.Errorf("expected 'not bound', got: %v", err) |
| 244 | + } |
| 245 | +} |
| 246 | + |
| 247 | +func TestEval_MissingVarIsEmpty(t *testing.T) { |
| 248 | + t.Parallel() |
| 249 | + // vars (and env) match GHA semantics: missing → empty string, not error. |
| 250 | + ctx := defaultContext() |
| 251 | + v, err := evalString(t, `vars.MISSING`, ctx) |
| 252 | + if err != nil { |
| 253 | + t.Fatalf("eval: %v", err) |
| 254 | + } |
| 255 | + if v.S != "" { |
| 256 | + t.Errorf("got %q", v.S) |
| 257 | + } |
| 258 | +} |
| 259 | + |
| 260 | +func TestEval_MissingEventPathReturnsNull(t *testing.T) { |
| 261 | + t.Parallel() |
| 262 | + ctx := defaultContext() |
| 263 | + v, err := evalString(t, `shithub.event.deeply.nested.missing`, ctx) |
| 264 | + if err != nil { |
| 265 | + t.Fatalf("eval: %v", err) |
| 266 | + } |
| 267 | + if v.Kind != expr.KindNull { |
| 268 | + t.Errorf("got Kind=%v, want KindNull", v.Kind) |
| 269 | + } |
| 270 | + if !v.Tainted { |
| 271 | + t.Errorf("missing-key result from event.* must still be tainted (it derives from the event payload)") |
| 272 | + } |
| 273 | +} |
| 274 | + |
| 275 | +func TestEval_StringEscapeQuote(t *testing.T) { |
| 276 | + t.Parallel() |
| 277 | + ctx := defaultContext() |
| 278 | + v, err := evalString(t, `'it''s ok'`, ctx) |
| 279 | + if err != nil { |
| 280 | + t.Fatalf("eval: %v", err) |
| 281 | + } |
| 282 | + if v.S != "it's ok" { |
| 283 | + t.Errorf("got %q", v.S) |
| 284 | + } |
| 285 | +} |
| 286 | + |
| 287 | +func TestEval_JobStatusFunctions(t *testing.T) { |
| 288 | + t.Parallel() |
| 289 | + cases := []struct { |
| 290 | + failed, cancelled bool |
| 291 | + src string |
| 292 | + want bool |
| 293 | + }{ |
| 294 | + {false, false, `success()`, true}, |
| 295 | + {true, false, `success()`, false}, |
| 296 | + {true, false, `failure()`, true}, |
| 297 | + {false, true, `cancelled()`, true}, |
| 298 | + {false, true, `failure()`, false}, // cancelled overrides failure |
| 299 | + {true, true, `failure()`, false}, |
| 300 | + {true, true, `always()`, true}, |
| 301 | + } |
| 302 | + for _, tc := range cases { |
| 303 | + t.Run(tc.src, func(t *testing.T) { |
| 304 | + t.Parallel() |
| 305 | + ctx := defaultContext() |
| 306 | + ctx.JobStatus = expr.JobStatus{Failed: tc.failed, Cancelled: tc.cancelled} |
| 307 | + v, err := evalString(t, tc.src, ctx) |
| 308 | + if err != nil { |
| 309 | + t.Fatalf("eval: %v", err) |
| 310 | + } |
| 311 | + if v.B != tc.want { |
| 312 | + t.Errorf("failed=%v cancelled=%v %s = %v, want %v", |
| 313 | + tc.failed, tc.cancelled, tc.src, v.B, tc.want) |
| 314 | + } |
| 315 | + }) |
| 316 | + } |
| 317 | +} |