- 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 = Locator.parse!(self.portable_data_hash)
- loc.strip_hints!
- self.portable_data_hash = loc.to_s
- rescue ArgumentError => e
- errors.add(:portable_data_hash, "#{e}")
- return false
+ def self.ignored_select_attributes
+ super + ["updated_at", "file_names"]
+ end
+
+ FILE_TOKEN = /^[[:digit:]]+:[[:digit:]]+:/
+ def check_signatures
+ return false if self.manifest_text.nil?
+
+ return true if current_user.andand.is_admin
+
+ # Provided the manifest_text hasn't changed materially since an
+ # earlier validation, it's safe to pass this validation on
+ # subsequent passes without checking any signatures. This is
+ # important because the signatures have probably been stripped off
+ # by the time we get to a second validation pass!
+ if @signatures_checked && @signatures_checked == computed_pdh
+ return true
+ end
+
+ 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 = {
+ api_token: api_token,
+ now: @validation_timestamp.to_i,
+ }
+ self.manifest_text.each_line do |entry|
+ entry.split.each do |tok|
+ if tok == '.' or tok.starts_with? './'
+ # Stream name token.
+ elsif tok =~ FILE_TOKEN
+ # 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