Active Storage doesn't have validations yet.
We can restrict the accepted file types in the form:
<div class="field">
<%= f.label :deliverable %>
<%= f.file_field :deliverable, direct_upload: true,
accept: 'application/pdf,
application/zip,application/vnd.openxmlformats-officedocument.wordprocessingml.document' %>
</div>
And add a custom validation in the model:
class Item
has_one_attached :document
validate :correct_document_mime_type
private
def correct_document_mime_type
if document.attached? && !document.content_type.in?(%w(application/msword application/pdf))
errors.add(:document, 'Must be a PDF or a DOC file')
end
end
end
@ConfusedVorlon thank you for the suggestion! I'm a bit confused that secure content type validation is not part of ActiveStorage though. We just had a big Rails app pentested and (apart from a handful of hosting configurations), the ActiveStorage type insecurity was the ONLY thing the security company found.
Edit: actually, it seems it is secure. After create and committing the ActiveStorage::Attachment, it calls the identify() method on the Blob, which determines the Mime type by itself, overwriting the previously supplied one and setting the field "identified" to true in the meta hash of the Blob. It seems we're making some kind of mistake here... Have to dig deeper.