package renderer import ( "strings" "testing" "github.com/ungluedlabs/go-jdenticon/internal/engine" ) // TestSVGRenderer_SecurityValidation tests defense-in-depth color validation // This test addresses SEC-06 from the security report by verifying that // the SVG renderer properly validates color inputs and prevents injection attacks. func TestSVGRenderer_SecurityValidation(t *testing.T) { tests := []struct { name string color string expectInSVG bool description string }{ { name: "valid_hex_color_3_digit", color: "#f00", expectInSVG: true, description: "Valid 3-digit hex color should be rendered", }, { name: "valid_hex_color_6_digit", color: "#ff0000", expectInSVG: true, description: "Valid 6-digit hex color should be rendered", }, { name: "valid_hex_color_8_digit", color: "#ff0000ff", expectInSVG: true, description: "Valid 8-digit hex color with alpha should be rendered", }, { name: "injection_attempt_script", color: "\">") { t.Errorf("SVG output should end with tag") } }) } } // TestSVGRenderer_BackgroundColorValidation tests background color validation func TestSVGRenderer_BackgroundColorValidation(t *testing.T) { tests := []struct { name string bgColor string opacity float64 expectInSVG bool description string }{ { name: "valid_background_color", bgColor: "#ffffff", opacity: 1.0, expectInSVG: true, description: "Valid background color should be rendered", }, { name: "invalid_background_injection", bgColor: "#fff\"/>", } // Try to add shapes with all malicious colors for _, color := range maliciousColors { renderer.BeginShape(color) points := []engine.Point{{X: 0, Y: 0}, {X: 50, Y: 50}} renderer.AddPolygon(points) renderer.EndShape() } svgOutput := renderer.ToSVG() // Verify none of the malicious colors appear in the output for _, color := range maliciousColors { if strings.Contains(svgOutput, color) { t.Errorf("Malicious color %s should not appear in SVG output, but was found: %s", color, svgOutput) } } // Verify the SVG is still valid and doesn't contain path elements for rejected colors pathCount := strings.Count(svgOutput, " 0 { t.Errorf("Expected no path elements for invalid colors, but found %d", pathCount) } // Ensure SVG structure is intact if !strings.Contains(svgOutput, `