Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added check for content type for remote files #98

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/css_parser/parser.rb
Expand Up @@ -604,6 +604,12 @@ def read_remote_file(uri) # :nodoc:
end
end

unless res.content_type == "text/css"
@redirect_count = nil
raise RemoteFileError.new(uri.to_s) if @options[:io_exceptions]
return '', nil
end

case res['content-encoding']
when 'gzip'
io = Zlib::GzipReader.new(StringIO.new(res.body))
Expand All @@ -614,6 +620,7 @@ def read_remote_file(uri) # :nodoc:
end
end


if charset
if String.method_defined?(:encode)
src.encode!('UTF-8', charset)
Expand Down
Binary file added test/fixtures/LadylikeBB.otf
Binary file not shown.
14 changes: 14 additions & 0 deletions test/test_css_parser_loading.rb
Expand Up @@ -207,4 +207,18 @@ def test_toggling_not_found_exceptions

cp_without_exceptions.load_uri!("#{@uri_base}/no-exist.xyz")
end

def test_invalid_content_type
cp_with_exceptions = Parser.new(:io_exceptions => true)

err = assert_raises RemoteFileError do
cp_with_exceptions.load_uri!("#{@uri_base}/LadylikeBB.otf")
end

assert_includes err.message, "#{@uri_base}/LadylikeBB.otf"

cp_without_exceptions = Parser.new(:io_exceptions => false)

cp_without_exceptions.load_uri!("#{@uri_base}/LadylikeBB.otf")
end
end