+ # If we don't list this explicitly, the params[:select] code gets
+ # confused by the way we expose signed_manifest_text as
+ # manifest_text in the API response, and never let clients select
+ # the manifest_text column.
+ super.merge('manifest_text' => ['manifest_text'])
+ end
+
+ 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!
+ return true if @signatures_checked and @signatures_checked == compute_pdh
+
+ 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
+ @signatures_checked = compute_pdh
+ end
+
+ def strip_manifest_text
+ if self.manifest_text_changed?
+ # Remove any permission signatures from the manifest.
+ self.class.munge_manifest_locators!(self[:manifest_text]) do |loc|
+ loc.without_signature.to_s
+ end
+ end
+ true
+ end
+
+ def set_portable_data_hash
+ if (portable_data_hash.nil? or
+ portable_data_hash == "" or
+ (manifest_text_changed? and !portable_data_hash_changed?))
+ @need_pdh_validation = false
+ self.portable_data_hash = compute_pdh
+ elsif portable_data_hash_changed?
+ @need_pdh_validation = true
+ 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}+#{portable_manifest_text.bytesize}"
+ end
+ rescue ArgumentError => e
+ errors.add(:portable_data_hash, "#{e}")
+ return false
+ end
+ end
+ true
+ end
+
+ def ensure_hash_matches_manifest_text
+ return true unless manifest_text_changed? or portable_data_hash_changed?
+ # No need verify it if :set_portable_data_hash just computed it!
+ return true if not @need_pdh_validation
+ expect_pdh = compute_pdh
+ if expect_pdh != portable_data_hash
+ errors.add(:portable_data_hash,
+ "does not match computed hash #{expect_pdh}")
+ return false
+ end
+ end
+
+ def check_encoding
+ if manifest_text.encoding.name == 'UTF-8' and manifest_text.valid_encoding?
+ true
+ else
+ begin
+ # If Ruby thinks the encoding is something else, like 7-bit
+ # ASCII, but its stored bytes are equal to the (valid) UTF-8
+ # encoding of the same string, we declare it to be a UTF-8
+ # string.
+ utf8 = manifest_text
+ utf8.force_encoding Encoding::UTF_8
+ if utf8.valid_encoding? and utf8 == manifest_text.encode(Encoding::UTF_8)
+ manifest_text = utf8
+ return true
+ end
+ rescue
+ end
+ errors.add :manifest_text, "must use UTF-8 encoding"
+ false
+ end