@@ -26,6 +26,7 @@ import (
2626 "math/rand"
2727 "os"
2828 "os/exec"
29+ "regexp"
2930 "strings"
3031 "testing"
3132 "time"
@@ -241,16 +242,33 @@ func printDebugging(t *testing.T, obj objects.TektonObject) {
241242 t .Helper ()
242243 kind := obj .GetObjectKind ().GroupVersionKind ().Kind
243244
245+ // Validate and sanitize inputs to prevent potential command injection
246+ if ! isValidKubernetesKind (kind ) {
247+ t .Logf ("Invalid kind: %s, skipping logs" , kind )
248+ return
249+ }
250+
251+ objNamespace := obj .GetNamespace ()
252+ objName := obj .GetName ()
253+
254+ if ! isValidKubernetesName (objNamespace ) || ! isValidKubernetesName (objName ) {
255+ t .Logf ("Invalid namespace or name: %s/%s, skipping logs" , objNamespace , objName )
256+ return
257+ }
258+
244259 t .Logf ("============================== %s logs ==============================" , obj .GetGVK ())
245- output , _ := exec .Command ("tkn" , strings .ToLower (kind ), "logs" , "-n" , obj .GetNamespace (), obj .GetName ()).CombinedOutput ()
260+ // #nosec G204 -- objNamespace and objName are validated by isValidKubernetesName to prevent command injection
261+ output , _ := exec .Command ("tkn" , strings .ToLower (kind ), "logs" , "--namespace" , objNamespace , objName ).CombinedOutput ()
246262 t .Log (string (output ))
247263
248264 t .Logf ("============================== %s describe ==============================" , obj .GetGVK ())
249- output , _ = exec .Command ("tkn" , strings .ToLower (kind ), "describe" , "-n" , obj .GetNamespace (), obj .GetName ()).CombinedOutput ()
265+ // #nosec G204 -- objNamespace and objName are validated by isValidKubernetesName to prevent command injection
266+ output , _ = exec .Command ("tkn" , strings .ToLower (kind ), "describe" , "--namespace" , objNamespace , objName ).CombinedOutput ()
250267 t .Log (string (output ))
251268
252269 t .Log ("============================== chains controller logs ==============================" )
253- output , _ = exec .Command ("kubectl" , "logs" , "deploy/tekton-chains-controller" , "-n" , namespace ).CombinedOutput ()
270+ // #nosec G204 -- namespace is a package-level constant, not user input
271+ output , _ = exec .Command ("kubectl" , "logs" , "deploy/tekton-chains-controller" , "--namespace" , namespace ).CombinedOutput ()
254272 t .Log (string (output ))
255273}
256274
@@ -348,3 +366,26 @@ func restartControllerPod(ctx context.Context, c kubernetes.Interface, timeout t
348366 return false , nil
349367 })
350368}
369+
370+ // isValidKubernetesKind validates that a string is a valid Kubernetes kind
371+ func isValidKubernetesKind (kind string ) bool {
372+ // Allow common Tekton kinds
373+ validKinds := []string {"TaskRun" , "PipelineRun" , "Task" , "Pipeline" }
374+ for _ , validKind := range validKinds {
375+ if strings .EqualFold (kind , validKind ) {
376+ return true
377+ }
378+ }
379+ return false
380+ }
381+
382+ // isValidKubernetesName validates that a string follows Kubernetes naming rules
383+ // DNS-1123 subdomain rules: lowercase alphanumeric characters, '-' or '.'
384+ func isValidKubernetesName (name string ) bool {
385+ if len (name ) == 0 || len (name ) > 253 {
386+ return false
387+ }
388+ // Kubernetes names should match DNS-1123 subdomain rules
389+ matched , _ := regexp .MatchString (`^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$` , name )
390+ return matched
391+ }
0 commit comments