Skip to content

Commit 2f35774

Browse files
delphaberclaude
andcommitted
Fix HTML with inline SVG being incorrectly detected as SVG
HTML documents containing inline <svg> elements were misidentified as SVG images. The type parser now checks that <html> does not appear before <svg> in the peeked content, correctly rejecting HTML files. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent a992939 commit 2f35774

3 files changed

Lines changed: 19 additions & 1 deletion

File tree

lib/fastimage/fastimage_parsing/type_parser.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,12 @@ def type
5757
# unknown. We assume the <svg tag cannot be within 10 chars of the end of
5858
# the file, and is within the first 1000 chars.
5959
begin
60-
:svg if (1..100).detect {|n| @stream.peek(10 * n).include?("<svg")}
60+
:svg if (1..100).any? { |n|
61+
peeked = @stream.peek(10 * n)
62+
svg_index = peeked.index("<svg")
63+
html_index = peeked.index(/<html/i)
64+
svg_index && (!html_index || svg_index < html_index)
65+
}
6166
rescue FiberError, FastImage::CannotParseImage
6267
nil
6368
end

test/fixtures/test8.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head><title>Test</title></head>
4+
<body>
5+
<svg viewBox="0 0 100 100"><rect width="50" height="50"/></svg>
6+
</body>
7+
</html>

test/test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,12 @@ def test_should_raise_image_fetch_failure_error_if_net_unreach_exception_happens
196196
end
197197
end
198198

199+
def test_should_raise_unknown_image_type_when_file_is_html_with_inline_svg
200+
assert_raises(FastImage::UnknownImageType) do
201+
FastImage.size(File.join(FixturePath, "test8.html"), :raise_on_failure=>true)
202+
end
203+
end
204+
199205
def test_should_raise_unknown_image_type_when_file_is_non_svg_xml
200206
["test.xml", "test2.xml"].each do |fn|
201207
assert_raises(FastImage::UnknownImageType) do

0 commit comments

Comments
 (0)