|
15 | 15 | package policydevel |
16 | 16 |
|
17 | 17 | import ( |
| 18 | + "bytes" |
18 | 19 | "encoding/json" |
19 | 20 | "os" |
20 | 21 | "path/filepath" |
21 | 22 | "testing" |
22 | 23 |
|
| 24 | + v12 "github.com/chainloop-dev/chainloop/pkg/attestation/crafter/api/attestation/v1" |
23 | 25 | "github.com/rs/zerolog" |
24 | 26 | "github.com/stretchr/testify/assert" |
25 | 27 | "github.com/stretchr/testify/require" |
@@ -230,3 +232,117 @@ func TestEvaluateSimplifiedPolicies(t *testing.T) { |
230 | 232 | assert.Contains(t, string(result.Result.Violations[0]), "too few components") |
231 | 233 | }) |
232 | 234 | } |
| 235 | + |
| 236 | +// fixtureGitHubPAT is assembled from fragments so that this file does not itself |
| 237 | +// carry a credential-shaped literal for secret scanners to flag. |
| 238 | +const fixtureGitHubPAT = "ghp_erOZlZv0B1e3amrQ" + "ugdwZ8Ro2W4kDql9WPTf" |
| 239 | + |
| 240 | +// writeSessionFixture materialises an AI coding session fixture with its |
| 241 | +// credential placeholder resolved, so that the crafter sees a real secret on disk. |
| 242 | +func writeSessionFixture(t *testing.T) string { |
| 243 | + t.Helper() |
| 244 | + |
| 245 | + content, err := os.ReadFile("testdata/ai-coding-session-with-secret.json") |
| 246 | + require.NoError(t, err) |
| 247 | + content = bytes.ReplaceAll(content, []byte("__GITHUB_PAT__"), []byte(fixtureGitHubPAT)) |
| 248 | + |
| 249 | + path := filepath.Join(t.TempDir(), "ai-coding-session.json") |
| 250 | + require.NoError(t, os.WriteFile(path, content, 0600)) |
| 251 | + |
| 252 | + return path |
| 253 | +} |
| 254 | + |
| 255 | +// Policies must be evaluated against the material as it sits on disk. The crafter |
| 256 | +// redacts the copy it stages for upload, and a dry run always stages inline, so |
| 257 | +// evaluating the staged bytes would hide from a policy the very secret it exists |
| 258 | +// to catch. |
| 259 | +func TestEvaluateReadsUnredactedMaterialFromDisk(t *testing.T) { |
| 260 | + testCases := []struct { |
| 261 | + name string |
| 262 | + annotations map[string]string |
| 263 | + }{ |
| 264 | + // The bug this pins: with no --annotation flags the crafter's annotation |
| 265 | + // map was replaced by an empty one, so the marker that selects the file on |
| 266 | + // disk was lost and policies evaluated the sanitized copy. |
| 267 | + {name: "without user annotations", annotations: nil}, |
| 268 | + {name: "with user annotations", annotations: map[string]string{"custom": "value"}}, |
| 269 | + { |
| 270 | + name: "with an attempt to override the redaction marker", |
| 271 | + annotations: map[string]string{v12.AnnotationMaterialRedacted: "false"}, |
| 272 | + }, |
| 273 | + } |
| 274 | + |
| 275 | + for _, tc := range testCases { |
| 276 | + t.Run(tc.name, func(t *testing.T) { |
| 277 | + opts := &EvalOptions{ |
| 278 | + PolicyPath: "testdata/ai-coding-session-no-secrets-policy.yaml", |
| 279 | + MaterialKind: "CHAINLOOP_AI_CODING_SESSION", |
| 280 | + MaterialPath: writeSessionFixture(t), |
| 281 | + Annotations: tc.annotations, |
| 282 | + } |
| 283 | + |
| 284 | + result, err := Evaluate(opts, zerolog.New(os.Stderr)) |
| 285 | + require.NoError(t, err) |
| 286 | + require.NotNil(t, result) |
| 287 | + |
| 288 | + assert.False(t, result.Result.Skipped) |
| 289 | + require.Len(t, result.Result.Violations, 1) |
| 290 | + assert.Contains(t, result.Result.Violations[0], "GitHub token found") |
| 291 | + }) |
| 292 | + } |
| 293 | +} |
| 294 | + |
| 295 | +func TestMergeAnnotations(t *testing.T) { |
| 296 | + testCases := []struct { |
| 297 | + name string |
| 298 | + existing map[string]string |
| 299 | + user map[string]string |
| 300 | + want map[string]string |
| 301 | + }{ |
| 302 | + { |
| 303 | + name: "crafter annotations survive when the user supplies none", |
| 304 | + existing: map[string]string{v12.AnnotationMaterialRedacted: v12.AnnotationValueTrue}, |
| 305 | + user: nil, |
| 306 | + want: map[string]string{v12.AnnotationMaterialRedacted: v12.AnnotationValueTrue}, |
| 307 | + }, |
| 308 | + { |
| 309 | + name: "user annotations are added alongside the crafter's", |
| 310 | + existing: map[string]string{v12.AnnotationMaterialRedacted: v12.AnnotationValueTrue}, |
| 311 | + user: map[string]string{"custom": "value"}, |
| 312 | + want: map[string]string{v12.AnnotationMaterialRedacted: v12.AnnotationValueTrue, "custom": "value"}, |
| 313 | + }, |
| 314 | + { |
| 315 | + name: "user annotations win on conflict outside the reserved namespace", |
| 316 | + existing: map[string]string{"custom": "crafted"}, |
| 317 | + user: map[string]string{"custom": "user"}, |
| 318 | + want: map[string]string{"custom": "user"}, |
| 319 | + }, |
| 320 | + { |
| 321 | + name: "the reserved chainloop namespace can not be overridden", |
| 322 | + existing: map[string]string{v12.AnnotationMaterialRedacted: v12.AnnotationValueTrue}, |
| 323 | + user: map[string]string{v12.AnnotationMaterialRedacted: "false", "custom": "value"}, |
| 324 | + want: map[string]string{v12.AnnotationMaterialRedacted: v12.AnnotationValueTrue, "custom": "value"}, |
| 325 | + }, |
| 326 | + { |
| 327 | + name: "a material with no annotations gets the user's", |
| 328 | + existing: nil, |
| 329 | + user: map[string]string{"custom": "value"}, |
| 330 | + want: map[string]string{"custom": "value"}, |
| 331 | + }, |
| 332 | + { |
| 333 | + name: "nothing to merge leaves the material untouched", |
| 334 | + existing: nil, |
| 335 | + user: nil, |
| 336 | + want: nil, |
| 337 | + }, |
| 338 | + } |
| 339 | + |
| 340 | + logger := zerolog.New(os.Stderr) |
| 341 | + for _, tc := range testCases { |
| 342 | + t.Run(tc.name, func(t *testing.T) { |
| 343 | + material := &v12.Attestation_Material{Annotations: tc.existing} |
| 344 | + mergeAnnotations(material, tc.user, &logger) |
| 345 | + assert.Equal(t, tc.want, material.GetAnnotations()) |
| 346 | + }) |
| 347 | + } |
| 348 | +} |
0 commit comments