simplify diaspora ID regex

hostname doesn't allow underscores, fixes #33
This commit is contained in:
Benjamin Neff 2016-04-23 23:43:57 +02:00
parent 44ee8d81e9
commit 53ba96acca
3 changed files with 17 additions and 12 deletions

View file

@ -2,23 +2,20 @@ module Validation
module Rule
# Diaspora ID validation rule
#
# This rule is based on https://github.com/zombor/Validator/blob/master/lib/validation/rule/email.rb
# which was adapted from https://github.com/emmanuel/aequitas/blob/master/lib/aequitas/rule/format/email_address.rb
# A simple rule to validate the base structure of diaspora IDs.
class DiasporaId
# The Regex for a valid diaspora ID
DIASPORA_ID = begin
letter = "a-zA-Z"
digit = "0-9"
hexadecimal = "[a-fA-F#{digit}]"
username = "[#{letter}#{digit}\\-\\_\\.]+"
atext = "[#{letter}#{digit}+\\=\\-\\_]"
dot_atom = "#{atext}+([.]#{atext}*)*"
no_ws_ctl = '\x01-\x08\x11\x12\x14-\x1f\x7f'
text = '[\x01-\x09\x11\x12\x14-\x7f]'
quoted_pair = "(\\x5c#{text})"
dtext = "[#{no_ws_ctl}\\x21-\\x5a\\x5e-\\x7e]"
dcontent = "(?:#{dtext}|#{quoted_pair})"
domain_literal = "\\[#{dcontent}+\\]"
domain = "(?:#{dot_atom}|#{domain_literal})"
hostname_part = "[#{letter}#{digit}\\-]"
hostname = "#{hostname_part}+([.]#{hostname_part}*)*"
ipv4 = "(?:[#{digit}]{1,3}\\.){3}[#{digit}]{1,3}"
ipv6 = "\\[(?:#{hexadecimal}{0,4}:){0,7}#{hexadecimal}{1,4}\\]"
ip_addr = "(?:#{ipv4}|#{ipv6})"
domain = "(?:#{hostname}|#{ip_addr})"
port = "(:[#{digit}]+)?"
addr_spec = "(#{username}\\@#{domain}#{port})?"

View file

@ -28,7 +28,7 @@ module Validation
ids = value.split(";")
return false unless ids.count <= params[:maximum]
ids.each do |id|
return false unless DiasporaId.new.valid_value?(id)
return false if DiasporaId::DIASPORA_ID.match(id).nil?
end
true
end

View file

@ -67,6 +67,14 @@ describe Validation::Rule::DiasporaId do
expect(validator.errors).to include(:diaspora_id)
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
validator = Validation::Validator.new(OpenStruct.new(diaspora_id: "some_user$^%@example.com"))
validator.rule(:diaspora_id, :diaspora_id)