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

libidn2 support for IDNA2008+UTS#46 (using ffi) #496

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
4 changes: 2 additions & 2 deletions lib/addressable/idna.rb
Expand Up @@ -32,13 +32,13 @@ class << self

# public interface implemented by all backends
def to_ascii(value)
backend.to_ascii(value)
backend.to_ascii(value) if value.is_a?(String)
rescue Error
strict_mode ? raise : value
end

def to_unicode(value)
backend.to_unicode(value)
backend.to_unicode(value) if value.is_a?(String)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While doing some tests I noticed behavior was not consistent when passing invalid input between backends, so I normalized this with if value.is_a?(String) and added tests in f0b98df. This case shouldn't happen much unless people manually call Addressable::IDNA but in that case we better protect the code a bit.

libidn2 for example was throwing some invalid memory read at address=0x0000000000000000 when called with nil.

If you prefer for the other types like Integer or Array we can also raise a TypeError.

rescue Error
strict_mode ? raise : value
end
Expand Down
14 changes: 14 additions & 0 deletions spec/addressable/idna_spec.rb
Expand Up @@ -152,6 +152,13 @@
"example..host"
)).to eq("example..host")
end

it "handles nil input" do
expect(Addressable::IDNA.to_ascii(nil)).to eq(nil)
expect(Addressable::IDNA.to_ascii(45)).to eq(nil)
expect(Addressable::IDNA.to_ascii([])).to eq(nil)
expect(Addressable::IDNA.to_ascii({})).to eq(nil)
end
end

shared_examples_for "converting from ASCII to unicode" do
Expand Down Expand Up @@ -256,6 +263,13 @@
"example..host"
)).to eq("example..host")
end

it "handles unexpected input as nil" do
expect(Addressable::IDNA.to_unicode(nil)).to eq(nil)
expect(Addressable::IDNA.to_unicode(45)).to eq(nil)
expect(Addressable::IDNA.to_unicode([])).to eq(nil)
expect(Addressable::IDNA.to_unicode({})).to eq(nil)
end
end

describe Addressable::IDNA, "when using the pure-Ruby implementation" do
Expand Down