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

ArgumentError (invalid byte sequence in UTF-8) #736 #746

Merged
merged 1 commit into from
Mar 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/apipie/static_dispatcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ def initialize(root)

def match?(path)
# Replace all null bytes
path = ::Rack::Utils.unescape(path || '').gsub(/\x0/, '')
path = ::Rack::Utils.unescape(path || '')
.encode(Encoding::UTF_8, invalid: :replace, replace: '')
.gsub(/\x0/, '')

full_path = path.empty? ? @root : File.join(@root, path)
paths = "#{full_path}#{ext}"
Expand Down
7 changes: 7 additions & 0 deletions spec/lib/file_handler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,12 @@
it { expect(file_handler.match? path).to be_falsy }
it { expect { file_handler.match? path }.to_not raise_error }
end

context 'when the path contans an invalid byte sequence in UTF-8' do
let(:path) { "%B6" }

it { expect(file_handler.match? path).to be_falsy }
it { expect { file_handler.match? path }.to_not raise_error }
end
end
end