psl-domain-extractor 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 32657f301009e0fc68ce843cb162292c680a3c336e7351619e1cc6838da5a088
4
+ data.tar.gz: 81a7d004e40bd6693ec531250f4f19a6da070d3e219d2508f6e0ce2ec6ead9ed
5
+ SHA512:
6
+ metadata.gz: 56bc8686931f946a280b35f6d9a8fb5f31b18a75bdb8da8275002494ca0c0ca91e22088e9cae520f645e0d110af0bdde4386a32bbb3c2ef31de9353675a2702a
7
+ data.tar.gz: 4e711eaf219bbd5aec534afbd192a8c8c382ddacb43e98cd5f20dec3d2334a0e1c1f97d488f60a8135fc1c58c814f3d1ff7aeec25306f834226a774febb1f730
checksums.yaml.gz.sig ADDED
@@ -0,0 +1,3 @@
1
+ tu� ��c���^�ZY
2
+ �i��is��ѓ���B��,fZ�ݪ,*vX�<�����k����g
3
+ -��XmBc\�f�PN|�.rUl�n����&�<Z6���U���y� hb&��)�j y��9QF�Gjv����f
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ # Changelog
2
+
3
+ ## v1.0.0
4
+
5
+ Initial version.
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2025 Ruben Arakelyan
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,85 @@
1
+ # PSL Domain Extractor
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/psl-domain-extractor.svg?icon=si%3Arubygems)](https://badge.fury.io/rb/psl-domain-extractor)
4
+
5
+ This gem provides methods for extracting domains, subdomains and public suffixes from hostnames using the [Public Suffix List](https://publicsuffix.org).
6
+
7
+ Note this repository contains a submodule in `psl`. To clone the repository, run `git clone --recurse-submodules https://github.com/rubenarakelyan/psl-domain-extractor`.
8
+
9
+ ## Concepts
10
+
11
+ A `hostname` is what you provide to this gem. For example, `www.example.com` is a hostname. You can then extract the `domain`, `subdomain` or `public suffix`.
12
+
13
+ A `label` is one part of a `hostname` separated by a dot. For example, the labels for `www.example.com` are `www`, `example` and `com`.
14
+
15
+ The `domain` is the part of the hostname that is registrable. For example, the domain for `www.example.com` is `example.com` because you could register `example.com` with a domain registrar.
16
+
17
+ The `subdomain` is the part of the hostname that is under the control of the domain’s registrant. For example, the subdomain for `www.example.com` is `www` because as the registrant for `example.com`, you control `www`. Note that a subdomain may contain multiple labels. For example, the subdomain for `foo.bar.example.com` is `foo.bar`, which is made up of two labels, `foo` and `bar`.
18
+
19
+ The `public suffix` is the part of the hostname that is allocated to the domain registry and hence not registrable. For example, the `public suffix` for `www.example.com` is `com`. `com` is allocated to the domain registry (in this case, Verisign) and you cannot register it. The `public suffix` is always one level below the `domain`. For example, `co.uk` and `uk` are both a `public suffix` because you can register `example.co.uk` and `example.uk`, but not `co.uk` or `uk`.
20
+
21
+ ### Important note on multi-tenant domains
22
+
23
+ The actual rules for the public suffix list are more complex than this since they also include wildcards, exceptions, and private multi-tenant domains. This gem takes all those into consideration. For example, Netlify has registered `netlify.app` as a domain name to host apps from multiple customers. `netlify.app` is in the public suffix list. Therefore, even though `netlify.app` is a registrable domain, in this case it is considered the public suffix. A customer’s app at `bar.netlify.app` would be considered the domain (in a sense, this has been registered with Netlify), and for `foo.bar.netlify.app`, the subdomain would be `foo` (not `foo.bar`).
24
+
25
+ ## Getting started
26
+
27
+ Add to your `Gemfile` then run `bundle install`:
28
+
29
+ ```ruby
30
+ gem "psl-domain-extractor"
31
+ ```
32
+
33
+ or install using `gem`:
34
+
35
+ ```bash
36
+ gem install psl-domain-extractor
37
+ ```
38
+
39
+ ## Using with plain Ruby
40
+
41
+ Extract the domain:
42
+
43
+ ```ruby
44
+ PslDomainExtractor.extract_domain(domain)
45
+ ```
46
+
47
+ Extract the subdomain:
48
+
49
+ ```ruby
50
+ PslDomainExtractor.extract_subdomain(domain)
51
+ ```
52
+
53
+ Extract the public suffix:
54
+
55
+ ```ruby
56
+ PslDomainExtractor.extract_public_suffix(domain)
57
+ ```
58
+
59
+ ## Using with a Rails app as the default domain extractor
60
+
61
+ Add the following to your Rails configuration:
62
+
63
+ ```ruby
64
+ config.action_dispatch.domain_extractor = PslDomainExtractor
65
+ ```
66
+
67
+ `request.domain` and `request.subdomain` will then be set using this gem’s logic.
68
+
69
+ Of course if you don’t want to change the default logic for domain extraction, you can always call the gem’s standalone methods as required in your app.
70
+
71
+ ## Note on gem signing and verification
72
+
73
+ This gem is cryptographically signed. To be sure the gem you install hasn’t been tampered with, run:
74
+ ```bash
75
+ gem cert --add <(curl -Ls https://raw.github.com/rubenarakelyan/psl-domain-extractor/main/certs/rubena.pem)
76
+ gem install psl-domain-extractor -P MediumSecurity
77
+ ```
78
+
79
+ The `MediumSecurity` trust profile will verify signed gems, but allow the installation of unsigned dependencies.
80
+
81
+ This is necessary because not all of this gem’s dependencies are signed, so we cannot use `HighSecurity`.
82
+
83
+ ## Licence
84
+
85
+ This gem is licensed under the [MIT licence](LICENSE).
@@ -0,0 +1,64 @@
1
+ module PslDomainExtractor
2
+ class Extractors
3
+ def initialize(rules)
4
+ @rules = rules
5
+ end
6
+
7
+ def extract_domain(domain)
8
+ domain = normalize_domain(domain)
9
+ return if domain.nil?
10
+
11
+ labels = domain.split(".")
12
+ find_match(labels, labels.length, effective_domain: true)
13
+ end
14
+
15
+ def extract_subdomain(domain)
16
+ domain = normalize_domain(domain)
17
+ return if domain.nil?
18
+
19
+ labels = domain.split(".")
20
+ effective_domain = find_match(labels, labels.length, effective_domain: true)
21
+ effective_domain = domain.gsub(/(\.)?#{effective_domain}/, "")
22
+
23
+ effective_domain.empty? ? nil : effective_domain
24
+ end
25
+
26
+ def extract_public_suffix(domain)
27
+ domain = normalize_domain(domain)
28
+ return if domain.nil?
29
+
30
+ labels = domain.split(".")
31
+ find_match(labels, labels.length)
32
+ end
33
+
34
+ private
35
+
36
+ def normalize_domain(domain)
37
+ return if domain.nil? || domain.empty?
38
+
39
+ domain = domain.downcase.gsub(/(^\.)|(\.$)/, "")
40
+ return if domain.include?("..") || domain.empty?
41
+
42
+ domain
43
+ end
44
+
45
+ def find_match(labels, pos, effective_domain: false)
46
+ return if pos < 1
47
+
48
+ suffix_labels = labels[-pos, pos]
49
+ suffix = suffix_labels.join(".")
50
+
51
+ if @rules.include?("!#{suffix}")
52
+ return labels[-(pos - 1), pos + 1]&.join(".") || suffix unless effective_domain
53
+ return suffix if effective_domain
54
+ end
55
+
56
+ match = @rules.include?(suffix) ||
57
+ (pos > 1 && @rules.include?("*.#{suffix_labels[1, pos - 1].join('.')}"))
58
+ return suffix if (match || pos == 1) && !effective_domain
59
+ return labels[-(pos + 1), pos + 1]&.join(".") || suffix if (match || pos == 1) && effective_domain
60
+
61
+ find_match(labels, pos - 1, effective_domain:)
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,17 @@
1
+ module PslDomainExtractor
2
+ class PublicSuffixList
3
+ attr_reader :rules
4
+
5
+ def initialize(file_path)
6
+ @rules = Set.new
7
+ File.open(file_path, "r:utf-8") do |f|
8
+ f.each_line do |line|
9
+ line = line.strip
10
+ next if line.empty? || line.start_with?("//")
11
+
12
+ @rules.add(line)
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module PslDomainExtractor
2
+ VERSION = "1.0.0".freeze
3
+ end
@@ -0,0 +1,37 @@
1
+ require "psl-domain-extractor/extractors"
2
+ require "psl-domain-extractor/public_suffix_list"
3
+
4
+ module PslDomainExtractor
5
+ def self.extract_domain(domain)
6
+ rules = rules_from_psl_file
7
+ Extractors.new(rules).extract_domain(domain)
8
+ end
9
+
10
+ def self.extract_subdomain(domain)
11
+ rules = rules_from_psl_file
12
+ Extractors.new(rules).extract_subdomain(domain)
13
+ end
14
+
15
+ def self.extract_public_suffix(domain)
16
+ rules = rules_from_psl_file
17
+ Extractors.new(rules).extract_public_suffix(domain)
18
+ end
19
+
20
+ # These two methods allow this class to be used with Rails as the domain extractor
21
+ # for `ActionDispatch`. Add the following to your Rails configuration:
22
+ # ```ruby
23
+ # config.action_dispatch.domain_extractor = PslDomainExtractor
24
+ # ```
25
+ def self.domain_from(host, _tld_length = nil)
26
+ extract_domain(host)
27
+ end
28
+
29
+ def self.subdomains_from(host, _tld_length = nil)
30
+ extract_subdomain(host)
31
+ end
32
+
33
+ def self.rules_from_psl_file
34
+ PublicSuffixList.new(File.join(__dir__, "..", "psl", "public_suffix_list.dat")).rules
35
+ end
36
+ private_class_method :rules_from_psl_file
37
+ end