Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

content_type validation may be broken for application/x-ole-storage #321

Closed
kapso opened this issue Dec 9, 2024 · 4 comments · Fixed by #322
Closed

content_type validation may be broken for application/x-ole-storage #321

kapso opened this issue Dec 9, 2024 · 4 comments · Fixed by #322
Labels
bug Something isn't working

Comments

@kapso
Copy link

kapso commented Dec 9, 2024

For the following model, content_type validation fails for - application/x-ole-storage

So basically a user was able to upload a .doc file with content type = application/x-ole-storage even though this content type is not manifested in CONTENT_TYPES

class Blog < ApplicationRecord

  CONTENT_TYPES = ["application/rtf",
 "application/pdf",
 "application/msword",
 "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
 "application/vnd.openxmlformats-officedocument.wordprocessingml.template",
 "application/vnd.ms-word.document.macroEnabled.12",
 "application/vnd.ms-excel",
 "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
 "application/vnd.openxmlformats-officedocument.spreadsheetml.template",
 "application/vnd.ms-excel.sheet.macroEnabled.12",
 "application/vnd.ms-powerpoint",
 "application/vnd.openxmlformats-officedocument.presentationml.template",
 "application/vnd.openxmlformats-officedocument.presentationml.presentation",
 "application/vnd.openxmlformats-officedocument.presentationml.slideshow",
 "application/vnd.ms-powerpoint.presentation.macroEnabled.12"]

  has_one_attached :document

  validates :document, content_type: { in: CONTENT_TYPES, message: 'is not a PDF or Word or PPT doc' }, attached: true
end
@Mth0158 Mth0158 added the bug Something isn't working label Dec 10, 2024
@Mth0158
Copy link
Collaborator

Mth0158 commented Dec 10, 2024

Hi @kapso,

The issue here lies in the fact that your user might upload a file with an "application/vnd.ms-excel" declared content_type, therefore it passes our validation (we validate against the declared content type).

But then, when storing the blob, Rails perform something a bit more specific to set the corresponding blob content_type value. It uses the file io (first 4kb) + declared content type (Marcel::MimeType.for download_identifiable_chunk, name: filename.to_s, declared_type: content_type here). This might result in a different output content type than the declared one, and I think it's what you are experiencing.

I am considering to do change the way we do things to mimic Rails blob content type assignation, this might be easier for everyone (plus it is a bit more robust than just validating the declared content type). I'll drop a PR in the coming days to solve this.

Mth0158 added a commit that referenced this issue Dec 10, 2024
Mth0158 added a commit that referenced this issue Dec 11, 2024
…on-may-be-broken-for-applicationx-ole-storage

[Validator] Detect attachable content_type like Rails, plus rewind the io after using it (#321)
@Mth0158
Copy link
Collaborator

Mth0158 commented Dec 11, 2024

Will be available in a coming 1.3.5 release this week.

@kapso
Copy link
Author

kapso commented Dec 11, 2024

thanks a lot, for the quick fix

@Mth0158
Copy link
Collaborator

Mth0158 commented Dec 12, 2024

@kapso Released is up, let me know if you still have this issue :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
2 participants