Disallow diaspora* IDs longer than 255 characters

This commit is contained in:
Dennis Schubert 2017-09-13 12:35:08 +02:00
parent b83619cd2c
commit f9b24b15f8
No known key found for this signature in database
GPG key ID: 5A0304BEA7966D7E
2 changed files with 15 additions and 1 deletions

View file

@ -4,6 +4,9 @@ module Validation
#
# A simple rule to validate the base structure of diaspora* IDs.
class DiasporaId
# Maximum length of a full diaspora* ID
DIASPORA_ID_MAX_LENGTH = 255
# The Regex for a valid diaspora* ID
DIASPORA_ID_REGEX = begin
username = "[[:lower:]\\d\\-\\.\\_]+"
@ -29,7 +32,10 @@ module Validation
# Determines if value is a valid diaspora* ID
def valid_value?(value)
value.is_a?(String) && value =~ DIASPORA_ID
return false unless value.is_a?(String)
return false if value.length > DIASPORA_ID_MAX_LENGTH
value =~ DIASPORA_ID
end
# This rule has no params.

View file

@ -99,6 +99,14 @@ describe Validation::Rule::DiasporaId do
expect(validator.errors).to include(:diaspora_id)
end
it "fails if the diaspora* ID is longer than 255 characters" do
validator = Validation::Validator.new(OpenStruct.new(diaspora_id: "#{'a' * 244}@example.com"))
validator.rule(:diaspora_id, :diaspora_id)
expect(validator).not_to be_valid
expect(validator.errors).to include(:diaspora_id)
end
it "fails for nil and empty" do
[nil, ""].each do |val|
validator = Validation::Validator.new(OpenStruct.new(diaspora_id: val))