simplify diaspora ID regex
hostname doesn't allow underscores, fixes #33
This commit is contained in:
parent
44ee8d81e9
commit
53ba96acca
3 changed files with 17 additions and 12 deletions
|
|
@ -2,23 +2,20 @@ module Validation
|
||||||
module Rule
|
module Rule
|
||||||
# Diaspora ID validation rule
|
# Diaspora ID validation rule
|
||||||
#
|
#
|
||||||
# This rule is based on https://github.com/zombor/Validator/blob/master/lib/validation/rule/email.rb
|
# A simple rule to validate the base structure of diaspora IDs.
|
||||||
# which was adapted from https://github.com/emmanuel/aequitas/blob/master/lib/aequitas/rule/format/email_address.rb
|
|
||||||
class DiasporaId
|
class DiasporaId
|
||||||
# The Regex for a valid diaspora ID
|
# The Regex for a valid diaspora ID
|
||||||
DIASPORA_ID = begin
|
DIASPORA_ID = begin
|
||||||
letter = "a-zA-Z"
|
letter = "a-zA-Z"
|
||||||
digit = "0-9"
|
digit = "0-9"
|
||||||
|
hexadecimal = "[a-fA-F#{digit}]"
|
||||||
username = "[#{letter}#{digit}\\-\\_\\.]+"
|
username = "[#{letter}#{digit}\\-\\_\\.]+"
|
||||||
atext = "[#{letter}#{digit}+\\=\\-\\_]"
|
hostname_part = "[#{letter}#{digit}\\-]"
|
||||||
dot_atom = "#{atext}+([.]#{atext}*)*"
|
hostname = "#{hostname_part}+([.]#{hostname_part}*)*"
|
||||||
no_ws_ctl = '\x01-\x08\x11\x12\x14-\x1f\x7f'
|
ipv4 = "(?:[#{digit}]{1,3}\\.){3}[#{digit}]{1,3}"
|
||||||
text = '[\x01-\x09\x11\x12\x14-\x7f]'
|
ipv6 = "\\[(?:#{hexadecimal}{0,4}:){0,7}#{hexadecimal}{1,4}\\]"
|
||||||
quoted_pair = "(\\x5c#{text})"
|
ip_addr = "(?:#{ipv4}|#{ipv6})"
|
||||||
dtext = "[#{no_ws_ctl}\\x21-\\x5a\\x5e-\\x7e]"
|
domain = "(?:#{hostname}|#{ip_addr})"
|
||||||
dcontent = "(?:#{dtext}|#{quoted_pair})"
|
|
||||||
domain_literal = "\\[#{dcontent}+\\]"
|
|
||||||
domain = "(?:#{dot_atom}|#{domain_literal})"
|
|
||||||
port = "(:[#{digit}]+)?"
|
port = "(:[#{digit}]+)?"
|
||||||
addr_spec = "(#{username}\\@#{domain}#{port})?"
|
addr_spec = "(#{username}\\@#{domain}#{port})?"
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,7 @@ module Validation
|
||||||
ids = value.split(";")
|
ids = value.split(";")
|
||||||
return false unless ids.count <= params[:maximum]
|
return false unless ids.count <= params[:maximum]
|
||||||
ids.each do |id|
|
ids.each do |id|
|
||||||
return false unless DiasporaId.new.valid_value?(id)
|
return false if DiasporaId::DIASPORA_ID.match(id).nil?
|
||||||
end
|
end
|
||||||
true
|
true
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -67,6 +67,14 @@ describe Validation::Rule::DiasporaId do
|
||||||
expect(validator.errors).to include(:diaspora_id)
|
expect(validator.errors).to include(:diaspora_id)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "fails if the diaspora id contains a _ in the domain-name" do
|
||||||
|
validator = Validation::Validator.new(OpenStruct.new(diaspora_id: "some_user@invalid_domain.com"))
|
||||||
|
validator.rule(:diaspora_id, :diaspora_id)
|
||||||
|
|
||||||
|
expect(validator).not_to be_valid
|
||||||
|
expect(validator.errors).to include(:diaspora_id)
|
||||||
|
end
|
||||||
|
|
||||||
it "fails if the diaspora id contains a special-chars in the username" do
|
it "fails if the diaspora id contains a special-chars in the username" do
|
||||||
validator = Validation::Validator.new(OpenStruct.new(diaspora_id: "some_user$^%@example.com"))
|
validator = Validation::Validator.new(OpenStruct.new(diaspora_id: "some_user$^%@example.com"))
|
||||||
validator.rule(:diaspora_id, :diaspora_id)
|
validator.rule(:diaspora_id, :diaspora_id)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue