|
| 1 | +# Copyright 2026 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Tests for SVG sanitization to prevent Stored XSS.""" |
| 16 | + |
| 17 | +import pytest |
| 18 | +from google.adk.cli.adk_web_server import _sanitize_svg_content |
| 19 | + |
| 20 | + |
| 21 | +class TestSvgSanitization: |
| 22 | + """Test SVG XSS sanitization.""" |
| 23 | + |
| 24 | + def test_remove_foreignobject(self) -> None: |
| 25 | + """foreignObject elements are removed.""" |
| 26 | + svg = '<svg><foreignObject><img onerror="alert(1)"/></foreignObject></svg>' |
| 27 | + result = _sanitize_svg_content(svg) |
| 28 | + assert '<foreignObject' not in result |
| 29 | + assert 'onerror' not in result |
| 30 | + |
| 31 | + def test_remove_script_tags(self) -> None: |
| 32 | + """Script tags are removed.""" |
| 33 | + svg = '<svg><script>alert(1)</script></svg>' |
| 34 | + result = _sanitize_svg_content(svg) |
| 35 | + assert '<script' not in result |
| 36 | + assert 'alert' not in result |
| 37 | + |
| 38 | + def test_remove_onload_handler(self) -> None: |
| 39 | + """onload event handlers are removed.""" |
| 40 | + svg = '<svg onload="alert(1)"><rect/></svg>' |
| 41 | + result = _sanitize_svg_content(svg) |
| 42 | + assert 'onload' not in result |
| 43 | + |
| 44 | + def test_remove_onclick_handler(self) -> None: |
| 45 | + """onclick event handlers are removed.""" |
| 46 | + svg = '<svg><rect onclick="alert(1)"/></svg>' |
| 47 | + result = _sanitize_svg_content(svg) |
| 48 | + assert 'onclick' not in result |
| 49 | + |
| 50 | + def test_remove_onerror_handler(self) -> None: |
| 51 | + """onerror event handlers are removed.""" |
| 52 | + svg = '<svg><img src=x onerror="alert(1)"/></svg>' |
| 53 | + result = _sanitize_svg_content(svg) |
| 54 | + assert 'onerror' not in result |
| 55 | + |
| 56 | + def test_remove_multiple_event_handlers(self) -> None: |
| 57 | + """Multiple event handlers are removed.""" |
| 58 | + svg = '<svg onload="a()" onclick="b()" onmouseover="c()"><rect/></svg>' |
| 59 | + result = _sanitize_svg_content(svg) |
| 60 | + assert 'onload' not in result |
| 61 | + assert 'onclick' not in result |
| 62 | + assert 'onmouseover' not in result |
| 63 | + |
| 64 | + def test_remove_javascript_urls(self) -> None: |
| 65 | + """javascript: URLs are removed.""" |
| 66 | + svg = '<svg><a href="javascript:alert(1)">Click</a></svg>' |
| 67 | + result = _sanitize_svg_content(svg) |
| 68 | + assert 'javascript:' not in result |
| 69 | + |
| 70 | + def test_preserve_valid_svg_structure(self) -> None: |
| 71 | + """Valid SVG structure is preserved.""" |
| 72 | + svg = '<svg xmlns="http://www.w3.org/2000/svg"><rect x="10" y="10" width="100" height="100"/></svg>' |
| 73 | + result = _sanitize_svg_content(svg) |
| 74 | + assert '<svg' in result |
| 75 | + assert '<rect' in result |
| 76 | + assert 'width="100"' in result |
| 77 | + assert 'height="100"' in result |
| 78 | + |
| 79 | + def test_preserve_svg_attributes(self) -> None: |
| 80 | + """Valid SVG attributes are preserved.""" |
| 81 | + svg = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" width="100" height="100"></svg>' |
| 82 | + result = _sanitize_svg_content(svg) |
| 83 | + assert 'xmlns=' in result |
| 84 | + assert 'viewBox=' in result |
| 85 | + assert 'width="100"' in result |
| 86 | + assert 'height="100"' in result |
| 87 | + |
| 88 | + def test_preserve_svg_elements(self) -> None: |
| 89 | + """Valid SVG elements are preserved.""" |
| 90 | + svg = '<svg><circle cx="50" cy="50" r="40"/><path d="M10 10 L90 90"/></svg>' |
| 91 | + result = _sanitize_svg_content(svg) |
| 92 | + assert '<circle' in result |
| 93 | + assert 'cx="50"' in result |
| 94 | + assert '<path' in result |
| 95 | + |
| 96 | + def test_empty_svg(self) -> None: |
| 97 | + """Empty SVG string returns empty.""" |
| 98 | + svg = '' |
| 99 | + result = _sanitize_svg_content(svg) |
| 100 | + assert result == '' |
| 101 | + |
| 102 | + def test_none_input(self) -> None: |
| 103 | + """None input is handled gracefully.""" |
| 104 | + result = _sanitize_svg_content(None) |
| 105 | + assert result is None |
| 106 | + |
| 107 | + def test_case_insensitive_removal(self) -> None: |
| 108 | + """Event handlers with different cases are removed.""" |
| 109 | + svg = '<svg><rect ONLOAD="alert(1)" OnClick="alert(2)"/></svg>' |
| 110 | + result = _sanitize_svg_content(svg) |
| 111 | + assert 'onload' not in result.lower() or 'ONLOAD' not in result |
| 112 | + assert 'onclick' not in result.lower() or 'OnClick' not in result |
| 113 | + |
| 114 | + def test_complex_xss_payload(self) -> None: |
| 115 | + """Complex XSS payload from issue #5514 is blocked.""" |
| 116 | + svg = '''<svg xmlns="http://www.w3.org/2000/svg" width="300" height="200"> |
| 117 | + <foreignObject width="300" height="200"> |
| 118 | + <body xmlns="http://www.w3.org/1999/xhtml"> |
| 119 | + <img src=x onerror="alert('XSS-ADK-006-foreignObject')"/> |
| 120 | + <h1 style="color:red">XSS via foreignObject</h1> |
| 121 | + </body> |
| 122 | + </foreignObject> |
| 123 | + </svg>''' |
| 124 | + result = _sanitize_svg_content(svg) |
| 125 | + assert '<foreignObject' not in result |
| 126 | + assert 'onerror' not in result |
| 127 | + assert 'alert' not in result |
| 128 | + |
| 129 | + def test_preserves_content_outside_dangerous_elements(self) -> None: |
| 130 | + """Content outside dangerous elements is preserved.""" |
| 131 | + svg = '<svg><text>Safe content</text><foreignObject><script>bad</script></foreignObject></svg>' |
| 132 | + result = _sanitize_svg_content(svg) |
| 133 | + assert '<text>' in result |
| 134 | + assert 'Safe content' in result |
| 135 | + assert '<foreignObject' not in result |
| 136 | + assert '<script' not in result |
| 137 | + |
0 commit comments