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

[FIX] base: avoid fail with wrong mimetype #156209

Closed

Conversation

ryce-odoo
Copy link
Contributor

@ryce-odoo ryce-odoo commented Mar 1, 2024

Uploading a WEBP or SVG file disguised with a proper file extension (JPG, PNG) will cause a traceback because img.image is not populated when there is an empty source, SVG, or WEBP file uploaded as this code should not be reached with these file types.

The reason this occurs is because we check for the file extension when deciding to post process an image, but when we get to initializing the ImageProcess object, we then check the actual file structure to verify the type of file.

This is a workaround for the time being, but should not be a final solution in future versions.

Adding a null check on img.image in the _postprocess_contents method in order to avoid attempting to access the size of this image when it is null.

Raises a user error in order to trigger the catch and exit the code while logging the error and 'Post processing ignored:'.

opw-3672250

I confirm I have signed the CLA and read the PR guidelines at www.odoo.com/submit-pr

@robodoo
Copy link
Contributor

robodoo commented Mar 1, 2024

Pull request status dashboard.

@C3POdoo C3POdoo requested review from a team, xmo-odoo and sleepy-monax and removed request for a team March 1, 2024 21:34
@C3POdoo C3POdoo added the OE the report is linked to a support ticket (opw-...) label Mar 1, 2024
@ryce-odoo ryce-odoo force-pushed the saas-16.4-opw-3672250-ryce branch 2 times, most recently from 1d4634e to 60dcc08 Compare March 4, 2024 17:53
@ryce-odoo ryce-odoo requested a review from JKE-be March 11, 2024 17:34
JKE-be
JKE-be previously requested changes Mar 14, 2024
Copy link
Contributor

@JKE-be JKE-be left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

webp and svg are not resizable, so why would you like to add it in ir.config_parameter 'image_autoresize_extensions' ???
If you don't add it, you will just not enter into the if is_image_resizable and...'s condition...

Your issue here, is that you try to upload a svg that you hide under the extension jpeg to try to bypass some others rules...
But your commit message is wrong, your UserError too. Maybe we should handle this case (raise a Warning if we detect wrong mimetype e.g.) to have a better uX, but this commit don't reflect it.

@ryce-odoo
Copy link
Contributor Author

webp and svg are not resizable, so why would you like to add it in ir.config_parameter 'image_autoresize_extensions' ??? If you don't add it, you will just not enter into the if is_image_resizable and...'s condition...

Your issue here, is that you try to upload a svg that you hide under the extension jpeg to try to bypass some others rules... But your commit message is wrong, your UserError too. Maybe we should handle this case (raise a Warning if we detect wrong mimetype e.g.) to have a better uX, but this commit don't reflect it.

Hey @JKE-be
The issue here is that a customer may not know that they have an SVG or WEBP that had its extension changed. I feel like this is the most efficient way to get around it because raising the error within this try block does not raise any error in the frontend and just exits at the except block saying "Post processing ignored". I feel like this is the best outcome considering some customers may not be tech savvy enough to understand file types.

@JKE-be
Copy link
Contributor

JKE-be commented Mar 19, 2024

so please explain it in the commit msg.

Your problem is not that you really upload a svg file. But that you upload a svg file renamed as .png.
Else you will not enter in this code.

@ryce-odoo
Copy link
Contributor Author

Hey @JKE-be

Sorry for the late reply was out of office. I have updated the commit message. Please let me know if there is anything else I should change.

@JKE-be
Copy link
Contributor

JKE-be commented Mar 27, 2024

@robodoo override=ci/style (TRY301, to discuss if we keep or not this new rule (ruff mig), but don't rewrite this code now)

Did you check what introduce this regression in saas-16.4 ? What is the difference with saas-16.3 ?

@ryce-odoo
Copy link
Contributor Author

Hey @JKE-be

Yeah it looks like the change that introduced this regression was adding this to the if condition:
"or (source[0:4] == b'RIFF' and source[8:15] == b'WEBPVP8')"

Looks like this change was meant to add compatibility for webp's and with further testing it looks like this issue has always existed with SVG's. However, I don't think anyone would try converting vectors to raster through the file extension like that.

Here's the commit in question:
d1292a9

@bso-odoo
Copy link
Contributor

bso-odoo commented Mar 28, 2024

@ryce-odoo Can you elaborate on the scenario that causes the error (in the commit message) ?
In the opw, it is about an upload in the Documents app. The error also occurs when trying to upload such an image through the website builder inside an image field (e.g. a product image in eCommerce). But other uploads such as an image inside a website page do not cause the error.
Both failing uploads are solved with this PR. 👍

This looks good to me. Maybe we should find a way to make the "incoming" mimetype not trustworthy, and have _compute_mimetype not rely on the value if it was sent from specific routes, but that might not be a change for a stable version.

@ryce-odoo
Copy link
Contributor Author

Hi @bso-odoo
I have updated the commit message. Let me know what you think.

@bso-odoo
Copy link
Contributor

@ryce-odoo Nice, but what I meant was that it did not mention which upload feature you are talking about.
E.g.: state that, for example, the problem occurs when uploading such a file "in the Documents app".

@ryce-odoo
Copy link
Contributor Author

Hey @bso-odoo
The issue on the original ticket was from the product documents page, but this happens anywhere where you upload an image to Odoo. It just depends on how much obfuscation is at play if you get this exact error message telling you where the issue is. For example, if you upload it to the chatter or documents app it will just give you a server error and if you upload it to the attachments, product documents, or website you will get the same traceback that tells you what the problem is. I don't know if it is necessary to clarify when this happens because this is an issue with base.

@ryce-odoo ryce-odoo dismissed JKE-be’s stale review April 3, 2024 17:00

Changes implemented

@ryce-odoo
Copy link
Contributor Author

Hey @bso-odoo @JKE-be

Can either of you guys give this a final check and approve for merge?

@JKE-be
Copy link
Contributor

JKE-be commented Apr 8, 2024

@robodoo delegate=bso-odoo

@JKE-be
Copy link
Contributor

JKE-be commented Apr 8, 2024

@robodoo override=ci/style

@ryce-odoo
Copy link
Contributor Author

ryce-odoo commented Apr 8, 2024

Hey @bso-odoo
I made that change and added it to the pot file.

@ryce-odoo
Copy link
Contributor Author

Hey @bso-odoo
I resolved the scoping issue with _ and included a test. Can you check out the test for me? Should be enough to ensure that the functionality works.

@ryce-odoo ryce-odoo changed the title [FIX] base: Check img.image for null [FIX] base: avoid fail with wrong mimetype Apr 16, 2024
@ryce-odoo
Copy link
Contributor Author

Hey @bso-odoo
I changed the svg to be more legit and changed the commit title. Had to shorten it a bit to fit in the character limit.
Are we good now?

@bso-odoo
Copy link
Contributor

@ryce-odoo Thanks for all the changes.

It seems runbot is not happy 😓 :

  • For the upgrade issue, maybe rebasing your PR on the current saas-16.4 will solve it.
  • For the styling TRY301 issue, I guess you'll have to directly log the error message and return - instead of relying on the catch to do it. Maybe you'll have a better idea that would allow for reusing the catch block's text...

@ryce-odoo ryce-odoo force-pushed the saas-16.4-opw-3672250-ryce branch 5 times, most recently from 9eb3de6 to bc20b34 Compare April 17, 2024 18:31
@ryce-odoo
Copy link
Contributor Author

Hey @bso-odoo
I have rebased and made the change to satisfy the style check. Everything seems to be in order now every check has passed.

Uploading a WEBP or SVG file disguised with a proper file extension (JPG, PNG)
will cause a traceback because img.image is
not populated when there is an empty source, SVG, or WEBP file uploaded
as this code should not be reached with these file types.

The reason this occurs is because we check for the file extension when
deciding to post process an image, but when we get to initializing the
ImageProcess object, we then check the actual file structure to verify
the type of file.

This is a workaround for the time being, but should not be a final
solution in future versions.

Adding a null check on img.image in the _postprocess_contents method
in order to avoid attempting to access the size of this image when it is
null.

Raises a user error in order to trigger the catch and exit the code
while logging the error and 'Post processing ignored:'.

Includes test for this new workflow with no errors.

opw-3672250
@bso-odoo
Copy link
Contributor

@ryce-odoo 😬 Since this is just a console-logged message after all, I de-translated it. Really sorry 🙏
I have kept the renaming of the unused _ because that is prone to generate bugs.

@ryce-odoo
Copy link
Contributor Author

Hey @bso-odoo
Sounds good I was confused whether or not to keep the translation, but in the end even with the old version that didn't pass the style check, the message would only ever be logged in the console.

I think its ready to merge when you are.

@bso-odoo
Copy link
Contributor

@robodoo r+

robodoo pushed a commit that referenced this pull request Apr 23, 2024
Uploading a WEBP or SVG file disguised with a proper file extension (JPG, PNG)
will cause a traceback because img.image is
not populated when there is an empty source, SVG, or WEBP file uploaded
as this code should not be reached with these file types.

The reason this occurs is because we check for the file extension when
deciding to post process an image, but when we get to initializing the
ImageProcess object, we then check the actual file structure to verify
the type of file.

This is a workaround for the time being, but should not be a final
solution in future versions.

Adding a null check on img.image in the _postprocess_contents method
in order to avoid attempting to access the size of this image when it is
null.

Raises a user error in order to trigger the catch and exit the code
while logging the error and 'Post processing ignored:'.

Includes test for this new workflow with no errors.

opw-3672250

closes #156209

Signed-off-by: Benoit Socias (bso) <bso@odoo.com>
@robodoo robodoo closed this Apr 23, 2024
@ryce-odoo
Copy link
Contributor Author

Thanks @bso-odoo !

@fw-bot fw-bot deleted the saas-16.4-opw-3672250-ryce branch May 7, 2024 11:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
OE the report is linked to a support ticket (opw-...)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants