+ def check_signatures
+ return false if self.manifest_text.nil?
+
+ return true if current_user.andand.is_admin
+
+ if self.manifest_text_changed?
+ # Check permissions on the collection manifest.
+ # If any signature cannot be verified, raise PermissionDeniedError
+ # which will return 403 Permission denied to the client.
+ api_token = current_api_client_authorization.andand.api_token
+ signing_opts = {
+ key: Rails.configuration.blob_signing_key,
+ api_token: api_token,
+ ttl: Rails.configuration.blob_signing_ttl,
+ }
+ self.manifest_text.lines.each do |entry|
+ entry.split[1..-1].each do |tok|
+ if /^[[:digit:]]+:[[:digit:]]+:/.match tok
+ # This is a filename token, not a blob locator. Note that we
+ # keep checking tokens after this, even though manifest
+ # format dictates that all subsequent tokens will also be
+ # filenames. Safety first!
+ elsif Blob.verify_signature tok, signing_opts
+ # OK.
+ elsif Keep::Locator.parse(tok).andand.signature
+ # Signature provided, but verify_signature did not like it.
+ logger.warn "Invalid signature on locator #{tok}"
+ raise ArvadosModel::PermissionDeniedError
+ elsif Rails.configuration.permit_create_collection_with_unsigned_manifest
+ # No signature provided, but we are running in insecure mode.
+ logger.debug "Missing signature on locator #{tok} ignored"
+ elsif Blob.new(tok).empty?
+ # No signature provided -- but no data to protect, either.
+ else
+ logger.warn "Missing signature on locator #{tok}"
+ raise ArvadosModel::PermissionDeniedError
+ end
+ end
+ end
+ end
+ true
+ end
+
+ def strip_manifest_text
+ if self.manifest_text_changed?
+ # Remove any permission signatures from the manifest.
+ Collection.munge_manifest_locators(self[:manifest_text]) do |loc|
+ loc.without_signature.to_s
+ end
+ end
+ true
+ end
+
+ def set_portable_data_hash
+ if (self.portable_data_hash.nil? or (self.portable_data_hash == "") or (manifest_text_changed? and !portable_data_hash_changed?))
+ self.portable_data_hash = "#{Digest::MD5.hexdigest(manifest_text)}+#{manifest_text.length}"
+ elsif portable_data_hash_changed?
+ begin
+ loc = Keep::Locator.parse!(self.portable_data_hash)
+ loc.strip_hints!
+ if loc.size
+ self.portable_data_hash = loc.to_s
+ else
+ self.portable_data_hash = "#{loc.hash}+#{self.manifest_text.length}"
+ end
+ rescue ArgumentError => e
+ errors.add(:portable_data_hash, "#{e}")
+ return false
+ end
+ end
+ true
+ end
+
+ def ensure_hash_matches_manifest_text
+ if manifest_text_changed? or portable_data_hash_changed?
+ computed_hash = "#{Digest::MD5.hexdigest(manifest_text)}+#{manifest_text.length}"
+ unless computed_hash == portable_data_hash
+ logger.debug "(computed) '#{computed_hash}' != '#{portable_data_hash}' (provided)"
+ errors.add(:portable_data_hash, "does not match hash of manifest_text")
+ return false
+ end
+ end
+ true
+ end
+
+ def check_encoding
+ if manifest_text.encoding.name == 'UTF-8' and manifest_text.valid_encoding?
+ true
+ else
+ errors.add :manifest_text, "must use UTF-8 encoding"
+ false
+ end