-
Notifications
You must be signed in to change notification settings - Fork 271
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
jarthod
wants to merge
13
commits into
sporkmonger:main
Choose a base branch
from
jarthod:libidn2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
f77bcf4
libidn2 ffi implementation
jarthod dfed402
Merge branch 'main' into libidn2
jarthod 0a6f091
Fix gemspec generation
jarthod a1fb7de
Rename IDNA backend implementations and refactor loading mechanism
jarthod 421cdca
Add Automatic IDNA memory leak detection to CI + keep libidn2 opt-in …
jarthod 84fdb89
Restore exceptions path + various fixes
jarthod a5e87c6
Add strict mode for IDNA conversion
jarthod 3482be0
Update README.md
jarthod c63f9ba
Move libidn1 specific exception handling inside the libidn1.rb file a…
jarthod ccbcb9e
Change IDNA::Error exception to inherit URI::InvalidURIError
jarthod 9eb3910
try with option 2: wrap errors in URI
jarthod 7d75d6c
explicit exception message passing
jarthod f0b98df
more consistent IDNA handling of wrong input
jarthod File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,20 +9,21 @@ Gem::Specification.new do |s| | |
s.metadata = { "changelog_uri" => "https://github.com/sporkmonger/addressable/blob/main/CHANGELOG.md" } if s.respond_to? :metadata= | ||
s.require_paths = ["lib".freeze] | ||
s.authors = ["Bob Aman".freeze] | ||
s.date = "2023-04-09" | ||
s.date = "2023-04-11" | ||
s.description = "Addressable is an alternative implementation to the URI implementation that is\npart of Ruby's standard library. It is flexible, offers heuristic parsing, and\nadditionally provides extensive support for IRIs and URI templates.\n".freeze | ||
s.email = "[email protected]".freeze | ||
s.extra_rdoc_files = ["README.md".freeze] | ||
s.files = ["CHANGELOG.md".freeze, "Gemfile".freeze, "LICENSE.txt".freeze, "README.md".freeze, "Rakefile".freeze, "addressable.gemspec".freeze, "data/unicode.data".freeze, "lib/addressable.rb".freeze, "lib/addressable/idna.rb".freeze, "lib/addressable/idna/native.rb".freeze, "lib/addressable/idna/pure.rb".freeze, "lib/addressable/template.rb".freeze, "lib/addressable/uri.rb".freeze, "lib/addressable/version.rb".freeze, "spec/addressable/idna_spec.rb".freeze, "spec/addressable/net_http_compat_spec.rb".freeze, "spec/addressable/security_spec.rb".freeze, "spec/addressable/template_spec.rb".freeze, "spec/addressable/uri_spec.rb".freeze, "spec/spec_helper.rb".freeze, "tasks/clobber.rake".freeze, "tasks/gem.rake".freeze, "tasks/git.rake".freeze, "tasks/metrics.rake".freeze, "tasks/profile.rake".freeze, "tasks/rspec.rake".freeze, "tasks/yard.rake".freeze] | ||
s.files = ["CHANGELOG.md".freeze, "Gemfile".freeze, "LICENSE.txt".freeze, "README.md".freeze, "Rakefile".freeze, "addressable.gemspec".freeze, "data/unicode.data".freeze, "lib/addressable.rb".freeze, "lib/addressable/idna.rb".freeze, "lib/addressable/idna/native.rb".freeze, "lib/addressable/idna/native2.rb".freeze, "lib/addressable/idna/pure.rb".freeze, "lib/addressable/template.rb".freeze, "lib/addressable/uri.rb".freeze, "lib/addressable/version.rb".freeze, "spec/addressable/idna_spec.rb".freeze, "spec/addressable/net_http_compat_spec.rb".freeze, "spec/addressable/security_spec.rb".freeze, "spec/addressable/template_spec.rb".freeze, "spec/addressable/uri_spec.rb".freeze, "spec/spec_helper.rb".freeze, "tasks/clobber.rake".freeze, "tasks/gem.rake".freeze, "tasks/git.rake".freeze, "tasks/metrics.rake".freeze, "tasks/profile.rake".freeze, "tasks/rspec.rake".freeze, "tasks/yard.rake".freeze] | ||
s.homepage = "https://github.com/sporkmonger/addressable".freeze | ||
s.licenses = ["Apache-2.0".freeze] | ||
s.rdoc_options = ["--main".freeze, "README.md".freeze] | ||
s.required_ruby_version = Gem::Requirement.new(">= 2.2".freeze) | ||
s.rubygems_version = "3.4.10".freeze | ||
s.rubygems_version = "3.4.11".freeze | ||
s.summary = "URI Implementation".freeze | ||
|
||
s.specification_version = 4 | ||
|
||
s.add_runtime_dependency(%q<public_suffix>.freeze, [">= 2.0.2", "< 6.0"]) | ||
s.add_runtime_dependency(%q<ffi>.freeze, [">= 0"]) | ||
s.add_development_dependency(%q<bundler>.freeze, [">= 1.0", "< 3.0"]) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# /usr/bin/env ruby | ||
# frozen_string_literal: true. | ||
|
||
require "benchmark" | ||
require "addressable/idna/libidn2" | ||
require "addressable/idna/libidn1" | ||
require "addressable/idna/pure" | ||
|
||
value = "fiᆵリ宠퐱卄.com" | ||
expected = "xn--fi-w1k207vk59a3qk9w9r.com" | ||
N = 100_000 | ||
|
||
fail "pure ruby does not match" unless expected == Addressable::IDNA::Pure.to_ascii(value) | ||
fail "libidn does not match" unless expected == Addressable::IDNA::Libidn1.to_ascii(value) | ||
fail "addressable does not match" unless expected == Addressable::IDNA::Libidn2.to_ascii(value) | ||
|
||
Benchmark.bmbm do |x| | ||
x.report("pure") { N.times { | ||
Addressable::IDNA::Pure.to_unicode(Addressable::IDNA::Pure.to_ascii(value)) | ||
} } | ||
|
||
x.report("libidn") { N.times { | ||
Addressable::IDNA::Libidn1.to_unicode(Addressable::IDNA::Libidn1.to_ascii(value)) | ||
} } | ||
|
||
x.report("libidn2") { N.times { | ||
Addressable::IDNA::Libidn2.to_unicode(Addressable::IDNA::Libidn2.to_ascii(value)) | ||
} } | ||
end | ||
|
||
# > ruby benchmark/idna.rb | ||
# Rehearsal ------------------------------------------- | ||
# pure 5.914630 0.000000 5.914630 ( 5.915326) | ||
# libidn 0.518971 0.003672 0.522643 ( 0.522676) | ||
# libidn2 0.763936 0.000000 0.763936 ( 0.763983) | ||
# ---------------------------------- total: 7.201209sec | ||
|
||
# user system total real | ||
# pure 6.042877 0.000000 6.042877 ( 6.043252) | ||
# libidn 0.521668 0.000000 0.521668 ( 0.521704) | ||
# libidn2 0.764782 0.000000 0.764782 ( 0.764863) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# frozen_string_literal: true | ||
|
||
#-- | ||
# Copyright (C) Bob Aman | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
#++ | ||
|
||
# libidn1 implementing IDNA2003 | ||
require "idn" | ||
|
||
module Addressable | ||
module IDNA | ||
module Libidn1 | ||
class << self | ||
# @deprecated Use {String#unicode_normalize(:nfkc)} instead | ||
def unicode_normalize_kc(value) | ||
value.to_s.unicode_normalize(:nfkc) | ||
end | ||
|
||
extend Gem::Deprecate | ||
deprecate :unicode_normalize_kc, "String#unicode_normalize(:nfkc)", 2023, 4 | ||
end | ||
|
||
def self.to_ascii(value) | ||
IDN::Idna.toASCII(value, IDN::Idna::ALLOW_UNASSIGNED) | ||
rescue IDN::Idna::IdnaError => e | ||
Addressable::IDNA.strict_mode ? raise(Error.new(e.message)) : value | ||
end | ||
|
||
def self.to_unicode(value) | ||
IDN::Idna.toUnicode(value, IDN::Idna::ALLOW_UNASSIGNED) | ||
rescue IDN::Idna::IdnaError => e | ||
Addressable::IDNA.strict_mode ? raise(Error.new(e.message)) : value | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# frozen_string_literal: true | ||
|
||
#-- | ||
# Copyright (C) Bob Aman | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
#++ | ||
|
||
# libidn2 implementing IDNA2008+TR46 | ||
require "ffi" | ||
|
||
module Addressable | ||
module IDNA | ||
module Libidn2 | ||
extend FFI::Library | ||
|
||
ffi_lib ["idn2", "libidn2.0", "libidn2.so.0"] | ||
|
||
attach_function :idn2_to_ascii_8z, %i[string pointer int], :int | ||
attach_function :idn2_to_unicode_8z8z, %i[string pointer int], :int | ||
attach_function :idn2_strerror, [:int], :string | ||
attach_function :idn2_free, [:pointer], :void | ||
|
||
IDN2_TRANSITIONAL = 4 | ||
IDN2_NONTRANSITIONAL = 8 | ||
|
||
def self.to_ascii(value) | ||
pointer = FFI::MemoryPointer.new(:pointer) | ||
res = idn2_to_ascii_8z(value, pointer, IDN2_NONTRANSITIONAL) | ||
# Fallback to Transitional mode in case of disallowed character | ||
res = idn2_to_ascii_8z(value, pointer, IDN2_TRANSITIONAL) if res == -304 | ||
raise Error.new("libidn2 failed to convert \"#{value}\" to ascii (#{idn2_strerror(res)})") if res != 0 | ||
result = pointer.read_pointer.read_string | ||
idn2_free(pointer.read_pointer) | ||
result | ||
end | ||
|
||
def self.to_unicode(value) | ||
pointer = FFI::MemoryPointer.new(:pointer) | ||
res = idn2_to_unicode_8z8z(value, pointer, IDN2_NONTRANSITIONAL) | ||
raise Error.new("libidn2 failed to convert \"#{value}\" to unicode (#{idn2_strerror(res)})") if res != 0 | ||
result = pointer.read_pointer.read_string | ||
idn2_free(pointer.read_pointer) | ||
result.force_encoding('UTF-8') | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,4 @@ | ||
# frozen_string_literal: true | ||
|
||
#-- | ||
# Copyright (C) Bob Aman | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
#++ | ||
|
||
|
||
require "idn" | ||
|
||
module Addressable | ||
module IDNA | ||
def self.punycode_encode(value) | ||
IDN::Punycode.encode(value.to_s) | ||
end | ||
|
||
def self.punycode_decode(value) | ||
IDN::Punycode.decode(value.to_s) | ||
end | ||
|
||
jarthod marked this conversation as resolved.
Show resolved
Hide resolved
|
||
class << self | ||
# @deprecated Use {String#unicode_normalize(:nfkc)} instead | ||
def unicode_normalize_kc(value) | ||
value.to_s.unicode_normalize(:nfkc) | ||
end | ||
|
||
extend Gem::Deprecate | ||
deprecate :unicode_normalize_kc, "String#unicode_normalize(:nfkc)", 2023, 4 | ||
end | ||
|
||
def self.to_ascii(value) | ||
value.to_s.split('.', -1).map do |segment| | ||
if segment.size > 0 && segment.size < 64 | ||
IDN::Idna.toASCII(segment, IDN::Idna::ALLOW_UNASSIGNED) | ||
elsif segment.size >= 64 | ||
segment | ||
else | ||
'' | ||
end | ||
end.join('.') | ||
end | ||
|
||
def self.to_unicode(value) | ||
value.to_s.split('.', -1).map do |segment| | ||
if segment.size > 0 && segment.size < 64 | ||
IDN::Idna.toUnicode(segment, IDN::Idna::ALLOW_UNASSIGNED) | ||
elsif segment.size >= 64 | ||
segment | ||
else | ||
'' | ||
end | ||
end.join('.') | ||
end | ||
end | ||
end | ||
# Deprecated, for backward compatibility only | ||
require "addressable/idna/libidn1" | ||
Addressable::IDNA.backend = Addressable::IDNA::Libidn1 | ||
warn "NOTE: loading 'addressable/idna/native' is deprecated; use 'addressable/idna/libidn1' instead and set `Addressable::IDNA.backend = Addressable::IDNA::Libidn1` to force libidn1." |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 callAddressable::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 withnil
.If you prefer for the other types like Integer or Array we can also raise a
TypeError
.