href_sanitizer 0.1.1 → 0.1.2
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 +4 -4
- data/lib/href_sanitizer/url_validator.rb +34 -35
- data/lib/href_sanitizer/version.rb +1 -1
- data/lib/href_sanitizer.rb +0 -1
- metadata +1 -2
- data/lib/href_sanitizer/safe_url_validator.rb +0 -5
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 00de4b6bbe17f7fc742280bc420a6459d48db06c3256f84e9738b7bc30862017
|
|
4
|
+
data.tar.gz: c118fdfd859730fe4c440a493764ff3e96d8d155cad88697bee34c8f23579d98
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: d241938c9c7837db70d70eb0fcf08f88451ec16062f30d429ca033c2cd2935180acdc1fc6cdd5dad653650436ecbc70a167812c3f1e196558956489f0c4ad8e4
|
|
7
|
+
data.tar.gz: 40ad36cc158724566bb0a31a68962f61f59547516d151d1eecd034f9dd80d53c53bf86c498c0c24ca459c82106203c1e8a97e2f1905b02edb6a92462e599facd
|
|
@@ -17,50 +17,49 @@ require "href_sanitizer/url_sanitizer"
|
|
|
17
17
|
# Class name is UrlValidator, but we also alias as URLValidator
|
|
18
18
|
# to support apps with `inflect.acronym 'URL'` (which makes Rails
|
|
19
19
|
# resolve `validates :field, url: true` to URLValidator).
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
module HrefSanitizer
|
|
21
|
+
class UrlValidator < ActiveModel::EachValidator
|
|
22
|
+
EMAIL_REGEXP = /\A[^@\s]+@[^@\s]+\z/
|
|
22
23
|
|
|
23
|
-
|
|
24
|
-
|
|
24
|
+
def validate_each(record, attribute, value)
|
|
25
|
+
stripped = value.to_s.strip
|
|
25
26
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
27
|
+
# Accept bare email addresses when accept_email: true
|
|
28
|
+
if options.fetch(:accept_email, false) && stripped.match?(EMAIL_REGEXP)
|
|
29
|
+
return
|
|
30
|
+
end
|
|
30
31
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
32
|
+
uri = parse(stripped)
|
|
33
|
+
unless uri
|
|
34
|
+
record.errors.add(attribute, options.fetch(:message, :url))
|
|
35
|
+
return
|
|
36
|
+
end
|
|
36
37
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
38
|
+
allowed_schemes = options.fetch(:schemes, %w[http https])
|
|
39
|
+
unless uri.scheme&.downcase&.in?(allowed_schemes)
|
|
40
|
+
record.errors.add(attribute, options.fetch(:message, :url))
|
|
41
|
+
return
|
|
42
|
+
end
|
|
42
43
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
44
|
+
unless HrefSanitizer::UrlSanitizer.allowed_uri?(stripped)
|
|
45
|
+
record.errors.add(attribute, options.fetch(:message, :url))
|
|
46
|
+
return
|
|
47
|
+
end
|
|
47
48
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
49
|
+
if options.fetch(:no_local, false)
|
|
50
|
+
unless HrefSanitizer::UrlSanitizer.public_url?(stripped)
|
|
51
|
+
record.errors.add(attribute, options.fetch(:message, :private_ip_url))
|
|
52
|
+
end
|
|
51
53
|
end
|
|
52
54
|
end
|
|
53
|
-
end
|
|
54
55
|
|
|
55
|
-
|
|
56
|
+
private
|
|
56
57
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
58
|
+
def parse(value)
|
|
59
|
+
uri = Addressable::URI.parse(value)
|
|
60
|
+
uri if uri.host.present?
|
|
61
|
+
rescue Addressable::URI::InvalidURIError
|
|
62
|
+
nil
|
|
63
|
+
end
|
|
62
64
|
end
|
|
63
65
|
end
|
|
64
|
-
|
|
65
|
-
# Alias for apps using `inflect.acronym 'URL'`
|
|
66
|
-
URLValidator = UrlValidator
|
data/lib/href_sanitizer.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: href_sanitizer
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- mfo
|
|
@@ -63,7 +63,6 @@ files:
|
|
|
63
63
|
- lib/href_sanitizer.rb
|
|
64
64
|
- lib/href_sanitizer/link_to_patch.rb
|
|
65
65
|
- lib/href_sanitizer/railtie.rb
|
|
66
|
-
- lib/href_sanitizer/safe_url_validator.rb
|
|
67
66
|
- lib/href_sanitizer/url_sanitizer.rb
|
|
68
67
|
- lib/href_sanitizer/url_validator.rb
|
|
69
68
|
- lib/href_sanitizer/version.rb
|