Bump rubocop

This commit is contained in:
Benjamin Neff 2019-10-20 04:38:15 +02:00
parent 0531a02aaa
commit e4f56e06bb
No known key found for this signature in database
GPG key ID: 971464C3F1A90194
29 changed files with 57 additions and 17 deletions

View file

@ -1,3 +1,5 @@
require: rubocop-rails
AllCops:
TargetRubyVersion: 2.5
Exclude:

View file

@ -21,7 +21,8 @@ group :development do
# code style
gem "pronto", "0.10.0", require: false
gem "pronto-rubocop", "0.10.0", require: false
gem "rubocop", "0.57.2", require: false
gem "rubocop", "0.75.1", require: false
gem "rubocop-rails", "2.3.2", require: false
# automatic test runs
gem "guard-rspec", require: false

View file

@ -126,7 +126,6 @@ GEM
parallel (1.18.0)
parser (2.6.5.0)
ast (~> 2.4.0)
powerpack (0.1.2)
pronto (0.10.0)
gitlab (~> 4.0, >= 4.0.0)
httparty (>= 0.13.7)
@ -187,14 +186,16 @@ GEM
rspec-mocks (~> 3.8.0)
rspec-support (~> 3.8.0)
rspec-support (3.8.0)
rubocop (0.57.2)
rubocop (0.75.1)
jaro_winkler (~> 1.5.1)
parallel (~> 1.10)
parser (>= 2.5)
powerpack (~> 0.1)
parser (>= 2.6)
rainbow (>= 2.2.2, < 4.0)
ruby-progressbar (~> 1.7)
unicode-display_width (~> 1.0, >= 1.0.1)
unicode-display_width (>= 1.4.0, < 1.7)
rubocop-rails (2.3.2)
rack (>= 1.1)
rubocop (>= 0.72.0)
ruby-progressbar (1.10.1)
ruby_dep (1.5.0)
rugged (0.28.3.1)
@ -258,7 +259,8 @@ DEPENDENCIES
rspec-collection_matchers (~> 1.1.2)
rspec-json_expectations (~> 2.1)
rspec-rails (~> 3.8.0)
rubocop (= 0.57.2)
rubocop (= 0.75.1)
rubocop-rails (= 2.3.2)
simplecov (= 0.16.1)
simplecov-rcov (= 0.2.3)
spring

View file

@ -258,6 +258,7 @@ module DiasporaFederation
configuration_error "http_timeout: please configure a number" unless @http_timeout.is_a?(Integer)
return unless !@http_verbose.is_a?(TrueClass) && !@http_verbose.is_a?(FalseClass)
configuration_error "http_verbose: please configure a boolean"
end

View file

@ -37,6 +37,7 @@ module DiasporaFederation
def validate_diaspora_id
# Validates if the diaspora* ID matches the diaspora* ID in the webfinger response
return if diaspora_id == clean_diaspora_id(webfinger.acct_uri)
raise DiscoveryError, "diaspora* ID does not match: Wanted #{diaspora_id} but got" \
" #{clean_diaspora_id(webfinger.acct_uri)}"
end
@ -49,6 +50,7 @@ module DiasporaFederation
logger.info "Fetching #{url} for #{diaspora_id}"
response = HttpClient.get(url)
raise "Failed to fetch #{url}: #{response.status}" unless response.success?
response.body
rescue => e # rubocop:disable Style/RescueStandardError
raise DiscoveryError, "Failed to fetch #{url} for #{diaspora_id}: #{e.class}: #{e.message}" unless http_fallback
@ -73,6 +75,7 @@ module DiasporaFederation
def webfinger
return @webfinger if @webfinger
webfinger_url = "https://#{domain}/.well-known/webfinger?resource=#{acct_parameter}"
# This tries the WebFinger URL with https first, then falls back to http if webfinger_http_fallback is enabled.

View file

@ -262,6 +262,7 @@ module DiasporaFederation
doc = Nokogiri::HTML::Document.parse(html_string)
raise InvalidData, "hcard html incomplete" unless html_document_complete?(doc)
doc
end

View file

@ -112,7 +112,7 @@ module DiasporaFederation
to_xrd.to_xml
end
def to_json
def to_json(*_args)
to_xrd.to_json
end

View file

@ -82,7 +82,7 @@ module DiasporaFederation
}.to_xml
end
def to_json
def to_json(*_args)
{
subject: subject,
expires: (expires.strftime(DATETIME_FORMAT) if expires.instance_of?(DateTime)),
@ -148,6 +148,7 @@ module DiasporaFederation
def add_aliases_to(xml)
aliases.each do |a|
next if !a.instance_of?(String) || a.empty?
xml.Alias(a.to_s)
end
end
@ -173,6 +174,7 @@ module DiasporaFederation
doc = Nokogiri::XML(xrd_doc)
raise InvalidDocument, "Not an XRD document" if !doc.root || doc.root.name != "XRD"
doc
end

View file

@ -39,6 +39,7 @@ module DiasporaFederation
# @return [String] diaspora* ID of the old person identity
def old_identity
return @old_identity if author_is_new_id?
author
end
@ -87,6 +88,7 @@ module DiasporaFederation
def sign_with_respective_key
privkey = DiasporaFederation.callbacks.trigger(:fetch_private_key, signer_id)
raise PrivateKeyNotFound, "signer=#{signer_id} obj=#{self}" if privkey.nil?
sign_with_key(privkey).tap do
logger.info "event=sign status=complete signature=signature signer=#{signer_id} obj=#{self}"
end

View file

@ -52,7 +52,7 @@ module DiasporaFederation
end
# never add {RelatedEntity} to json
def to_json
def to_json(*_args)
nil
end
end

View file

@ -90,7 +90,7 @@ module DiasporaFederation
"#{super}#{":#{parent_type}" if respond_to?(:parent_type)}:#{parent_guid}"
end
def to_json
def to_json(*_args)
super.merge!(property_order: signature_order).tap {|json_hash|
missing_properties = json_hash[:property_order] - json_hash[:entity_data].keys
missing_properties.each {|property|
@ -115,6 +115,7 @@ module DiasporaFederation
def sign_with_author
privkey = DiasporaFederation.callbacks.trigger(:fetch_private_key, author)
raise AuthorPrivateKeyNotFound, "author=#{author} obj=#{self}" if privkey.nil?
sign_with_key(privkey).tap do
logger.info "event=sign status=complete signature=author_signature author=#{author} obj=#{self}"
end

View file

@ -30,7 +30,7 @@ module DiasporaFederation
def sender_valid?(sender)
case target_type
when "Comment", "Like", "PollParticipation"
sender == target.author || sender == target.root.author
[target.root.author, target.author].include?(sender)
else
sender == target.author
end

View file

@ -40,7 +40,7 @@ module DiasporaFederation
# Invalid XML characters
# @see https://www.w3.org/TR/REC-xml/#charsets "Extensible Markup Language (XML) 1.0"
INVALID_XML_REGEX = /[^\x09\x0A\x0D\x20-\uD7FF\uE000-\uFFFD\u{10000}-\u{10FFFF}]/
INVALID_XML_REGEX = /[^\x09\x0A\x0D\x20-\uD7FF\uE000-\uFFFD\u{10000}-\u{10FFFF}]/.freeze
# Regex to validate and find entity names
ENTITY_NAME_REGEX = "[a-z]*(?:_[a-z]*)*"
@ -151,6 +151,7 @@ module DiasporaFederation
# @return [Class] entity class
def self.entity_class(entity_name)
raise InvalidEntityName, "'#{entity_name}' is invalid" unless entity_name =~ /\A#{ENTITY_NAME_REGEX}\z/
class_name = entity_name.sub(/\A[a-z]/, &:upcase)
class_name.gsub!(/_([a-z])/) { Regexp.last_match[1].upcase }
@ -171,7 +172,7 @@ module DiasporaFederation
# Renders entity to a hash representation of the entity JSON format
# @return [Hash] Returns a hash that is equal by structure to the entity in JSON format
def to_json
def to_json(*_args)
{
entity_type: self.class.entity_name,
entity_data: json_data
@ -233,12 +234,14 @@ module DiasporaFederation
def nilify(value)
return nil if value.respond_to?(:empty?) && value.empty? && !value.instance_of?(Array)
value
end
def instantiate_nested(name, value)
if value.instance_of?(Array)
return value unless value.first.instance_of?(Hash)
value.map {|hash| self.class.class_props[name].first.new(hash) }
elsif value.instance_of?(Hash)
self.class.class_props[name].new(value)

View file

@ -12,7 +12,7 @@ module DiasporaFederation
(#{Validation::Rule::DiasporaId::DIASPORA_ID_REGEX})/
(#{Entity::ENTITY_NAME_REGEX})/
(#{Validation::Rule::Guid::VALID_CHARS})
}ux
}ux.freeze
# Parses all diaspora:// URLs from the text and fetches the entities from
# the remote server if needed.
@ -27,6 +27,7 @@ module DiasporaFederation
private_class_method def self.fetch_entity(author, type, guid)
class_name = Entity.entity_class(type).to_s.rpartition("::").last
return if DiasporaFederation.callbacks.trigger(:fetch_related_entity, class_name, guid)
Fetcher.fetch_public(author, type, guid)
rescue => e # rubocop:disable Style/RescueStandardError
logger.error "Failed to fetch linked entity #{type}:#{guid}: #{e.class}: #{e.message}"

View file

@ -12,6 +12,7 @@ module DiasporaFederation
def self.fetch_public(author, entity_type, guid)
type = entity_name(entity_type).to_s
raise "Already fetching ..." if fetching[type].include?(guid)
fetch_from_url(author, type, guid)
rescue => e # rubocop:disable Style/RescueStandardError
raise NotFetchable, "Failed to fetch #{entity_type}:#{guid} from #{author}: #{e.class}: #{e.message}"

View file

@ -31,6 +31,7 @@ module DiasporaFederation
# @param [Boolean] legacy use old slap parser
def self.receive_private(data, recipient_private_key, recipient_id, legacy=false)
raise ArgumentError, "no recipient key provided" unless recipient_private_key.instance_of?(OpenSSL::PKey::RSA)
magic_env = if legacy
Salmon::EncryptedSlap.from_xml(data, recipient_private_key)
else

View file

@ -9,6 +9,7 @@ module DiasporaFederation
def validate
raise RecipientRequired if recipient_id.nil?
super
end
end

View file

@ -38,6 +38,7 @@ module DiasporaFederation
text.to_i if text =~ /\A\d+\z/
when :boolean
return true if text =~ /\A(true|t|yes|y|1)\z/i
false if text =~ /\A(false|f|no|n|0)\z/i
else
text
@ -46,6 +47,7 @@ module DiasporaFederation
def assert_parsability_of(entity_class)
return if entity_class == entity_type.entity_name
raise InvalidRootNode, "'#{entity_class}' can't be parsed by #{entity_type.name}"
end

View file

@ -32,12 +32,14 @@ module DiasporaFederation
def parse_element_from_value(type, value)
return if value.nil?
if %i[integer boolean timestamp].include?(type) && !value.is_a?(String)
value
elsif type.instance_of?(Symbol)
parse_string(type, value)
elsif type.instance_of?(Array)
raise DeserializationError, "Expected array for #{type}" unless value.respond_to?(:map)
value.map {|element|
type.first.from_json(element)
}
@ -51,6 +53,7 @@ module DiasporaFederation
prop if json_hash[prop].nil?
}.compact.join(", ")
raise DeserializationError, "Required properties are missing in JSON object: #{missing}" unless missing.empty?
assert_parsability_of(json_hash["entity_type"])
end

View file

@ -20,6 +20,7 @@ module DiasporaFederation
def from_json_sanity_validation(json_hash)
super
return unless json_hash["property_order"].nil?
raise DeserializationError, "Required property is missing in JSON object: property_order"
end
end

View file

@ -79,6 +79,7 @@ module DiasporaFederation
def from_xml_sanity_validation(root_node)
raise ArgumentError, "only Nokogiri::XML::Element allowed" unless root_node.instance_of?(Nokogiri::XML::Element)
assert_parsability_of(root_node.name)
end
end

View file

@ -71,6 +71,7 @@ module DiasporaFederation
if class_prop_aliases.has_key? name
prop_name = class_prop_aliases[name]
raise InvalidData, "only use '#{name}' OR '#{prop_name}'" if data.has_key? prop_name
[prop_name, value]
else
[name, value]
@ -88,7 +89,7 @@ module DiasporaFederation
# @param [String] xml_name name of the property from the received xml
# @return [Hash] the property data
def find_property_for_xml_name(xml_name)
class_props.keys.find {|name| name.to_s == xml_name || xml_names[name].to_s == xml_name }
class_props.keys.find {|name| [name.to_s, xml_names[name].to_s].include?(xml_name) }
end
private
@ -102,6 +103,7 @@ module DiasporaFederation
if type.instance_of?(Symbol)
if opts.has_key? :xml_name
raise InvalidName, "invalid xml_name" unless name_valid?(opts[:xml_name])
opts[:xml_name]
else
name

View file

@ -69,10 +69,12 @@ module DiasporaFederation
# @raise [MissingMagicEnvelope] if the +me:env+ element is missing in the XML
def self.from_xml(slap_xml, privkey)
raise ArgumentError unless slap_xml.instance_of?(String) && privkey.instance_of?(OpenSSL::PKey::RSA)
doc = Nokogiri::XML(slap_xml)
header_elem = doc.at_xpath("d:diaspora/d:encrypted_header", Slap::NS)
raise MissingHeader if header_elem.nil?
header = header_data(header_elem.content, privkey)
sender = header[:author_id]
cipher_params = {key: Base64.decode64(header[:aes_key]), iv: Base64.decode64(header[:iv])}

View file

@ -155,6 +155,7 @@ module DiasporaFederation
# @raise [InvalidEnvelope] if the envelope XML structure is malformed
private_class_method def self.validate_envelope(env)
raise InvalidEnvelope unless env.instance_of?(Nokogiri::XML::Element) && env.name == "env"
validate_element(env, "me:data")
validate_element(env, "me:sig")
end
@ -190,6 +191,7 @@ module DiasporaFederation
private_class_method def self.sender(env)
key_id = env.at_xpath("me:sig")["key_id"]
raise InvalidEnvelope, "no key_id" unless key_id # TODO: move to `envelope_valid?`
Base64.urlsafe_decode64(key_id)
end

View file

@ -35,10 +35,12 @@ module DiasporaFederation
# @raise [MissingMagicEnvelope] if the +me:env+ element is missing from the XML
def self.from_xml(slap_xml)
raise ArgumentError unless slap_xml.instance_of?(String)
doc = Nokogiri::XML(slap_xml)
author_elem = doc.at_xpath("d:diaspora/d:header/d:author_id", Slap::NS)
raise MissingAuthor if author_elem.nil? || author_elem.content.empty?
sender = author_elem.content
MagicEnvelope.unenvelop(magic_env_from_doc(doc), sender)

View file

@ -16,6 +16,7 @@ module DiasporaFederation
def optional_props
return [] unless @obj.class.respond_to?(:optional_props)
@obj.class.optional_props
end
end

View file

@ -24,7 +24,7 @@ module Validation
end
# The Regex for validating a full diaspora* ID
DIASPORA_ID = /\A#{DIASPORA_ID_REGEX}\z/u
DIASPORA_ID = /\A#{DIASPORA_ID_REGEX}\z/u.freeze
# The error key for this rule
# @return [Symbol] error key

View file

@ -33,6 +33,7 @@ module Validation
ids = value.split(";")
return false if params.include?(:maximum) && ids.count > params[:maximum]
return false if params.include?(:minimum) && ids.count < params[:minimum]
ids.each do |id|
return false if DiasporaId::DIASPORA_ID.match(id).nil?
end

View file

@ -36,6 +36,7 @@ class Person
def find_by(opts)
return database[:diaspora_id][opts[:diaspora_id]] if opts[:diaspora_id]
database[:guid][opts[:guid]]
end